1 // navcom.cxx -- class to manage a navcom instance
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 - 2002 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <stdio.h> // snprintf
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/ilslist.hxx>
35 #include <Navaids/navlist.hxx>
36 #include <Time/FGEventMgr.hxx>
45 FGNavCom::FGNavCom() :
46 lon_node(fgGetNode("/position/longitude-deg", true)),
47 lat_node(fgGetNode("/position/latitude-deg", true)),
48 alt_node(fgGetNode("/position/altitude-ft", true)),
65 SGPath path( globals->get_fg_root() );
67 term.append( "Navaids/range.term" );
69 low.append( "Navaids/range.low" );
71 high.append( "Navaids/range.high" );
73 term_tbl = new SGInterpTable( term.str() );
74 low_tbl = new SGInterpTable( low.str() );
75 high_tbl = new SGInterpTable( high.str() );
94 // We assume that index is valid now (it must be set before init()
98 sprintf( propname, "/systems/electrical/outputs/navcom[%d]", index );
99 // default to true in case no electrical system defined.
100 fgSetDouble( propname, 60.0 );
101 bus_power = fgGetNode( propname, true );
103 sprintf( propname, "/instrumentation/comm[%d]/servicable", index );
104 com_servicable = fgGetNode( propname, true );
105 com_servicable->setBoolValue( true );
107 sprintf( propname, "/instrumentation/nav[%d]/servicable", index );
108 nav_servicable = fgGetNode( propname, true );
109 nav_servicable->setBoolValue( true );
111 sprintf( propname, "/instrumentation/vor[%d]/cdi/servicable", index );
112 cdi_servicable = fgGetNode( propname, true );
113 cdi_servicable->setBoolValue( true );
115 sprintf( propname, "/instrumentation/vor[%d]/gs/servicable", index );
116 gs_servicable = fgGetNode( propname, true );
117 gs_servicable->setBoolValue( true );
119 sprintf( propname, "/instrumentation/vor[%d]/to-from/servicable", index );
120 tofrom_servicable = fgGetNode( propname, true );
121 tofrom_servicable->setBoolValue( true );
130 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
131 fgTie( propname, this,
132 &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
133 fgSetArchivable( propname );
135 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
136 fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
137 fgSetArchivable( propname );
139 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
140 fgTie( propname, this,
141 &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
142 fgSetArchivable( propname );
144 sprintf( propname, "/radios/comm[%d]/volume", index );
145 fgTie( propname, this,
146 &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
147 fgSetArchivable( propname );
149 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
150 fgTie( propname, this,
151 &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
152 fgSetArchivable( propname );
154 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
155 fgTie( propname , this,
156 &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
157 fgSetArchivable( propname );
159 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
160 fgTie( propname, this,
161 &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
162 fgSetArchivable( propname );
164 sprintf( propname, "/radios/nav[%d]/volume", index );
165 fgTie( propname, this,
166 &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
167 fgSetArchivable( propname );
169 sprintf( propname, "/radios/nav[%d]/ident", index );
170 fgTie( propname, this,
171 &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
172 fgSetArchivable( propname );
175 sprintf( propname, "/radios/nav[%d]/audio-btn", index );
176 fgTie( propname, this,
177 &FGNavCom::get_audio_btn, &FGNavCom::set_audio_btn );
178 fgSetArchivable( propname );
180 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
181 fgTie( propname, this, &FGNavCom::get_nav_radial );
183 sprintf( propname, "/radios/nav[%d]/to-flag", index );
184 fgTie( propname, this, &FGNavCom::get_nav_to_flag );
186 sprintf( propname, "/radios/nav[%d]/from-flag", index );
187 fgTie( propname, this, &FGNavCom::get_nav_from_flag );
189 sprintf( propname, "/radios/nav[%d]/in-range", index );
190 fgTie( propname, this, &FGNavCom::get_nav_inrange );
192 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
193 fgTie( propname, this, &FGNavCom::get_nav_heading_needle_deflection );
195 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
196 fgTie( propname, this, &FGNavCom::get_nav_gs_needle_deflection );
207 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
209 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
211 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
214 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
216 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
218 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
220 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
222 sprintf( propname, "/radios/nav[%d]/ident", index );
224 sprintf( propname, "/radios/nav[%d]/to-flag", index );
226 sprintf( propname, "/radios/nav[%d]/from-flag", index );
228 sprintf( propname, "/radios/nav[%d]/in-range", index );
230 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
232 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
237 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
238 double FGNavCom::adjustNavRange( double stationElev, double aircraftElev,
239 double nominalRange )
241 // extend out actual usable range to be 1.3x the published safe range
242 const double usability_factor = 1.3;
244 // assumptions we model the standard service volume, plus
245 // ... rather than specifying a cylinder, we model a cone that
246 // contains the cylinder. Then we put an upside down cone on top
247 // to model diminishing returns at too-high altitudes.
249 // altitude difference
250 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
251 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
252 // << " station elev = " << stationElev << endl;
254 if ( nominalRange < 25.0 + SG_EPSILON ) {
255 // Standard Terminal Service Volume
256 return term_tbl->interpolate( alt ) * usability_factor;
257 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
258 // Standard Low Altitude Service Volume
259 // table is based on range of 40, scale to actual range
260 return low_tbl->interpolate( alt ) * nominalRange / 40.0
263 // Standard High Altitude Service Volume
264 // table is based on range of 130, scale to actual range
265 return high_tbl->interpolate( alt ) * nominalRange / 130.0
271 // model standard ILS service volumes as per AIM 1-1-9
272 double FGNavCom::adjustILSRange( double stationElev, double aircraftElev,
273 double offsetDegrees, double distance )
275 // assumptions we model the standard service volume, plus
277 // altitude difference
278 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
279 // double offset = fabs( offsetDegrees );
281 // if ( offset < 10 ) {
282 // return FG_ILS_DEFAULT_RANGE;
283 // } else if ( offset < 35 ) {
284 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
285 // } else if ( offset < 45 ) {
286 // return (45 - offset);
287 // } else if ( offset > 170 ) {
288 // return FG_ILS_DEFAULT_RANGE;
289 // } else if ( offset > 145 ) {
290 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
291 // } else if ( offset > 135 ) {
292 // return (offset - 135);
296 return FG_ILS_DEFAULT_RANGE;
300 // Update the various nav values based on position and valid tuned in navs
302 FGNavCom::update(double dt)
304 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
305 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
306 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
310 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
314 ////////////////////////////////////////////////////////////////////////
316 ////////////////////////////////////////////////////////////////////////
318 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
319 && nav_servicable->getBoolValue() )
321 station = Point3D( nav_x, nav_y, nav_z );
322 nav_loc_dist = aircraft.distance3D( station );
325 station = Point3D( nav_gs_x, nav_gs_y, nav_gs_z );
326 nav_gs_dist = aircraft.distance3D( station );
327 // wgs84 heading to glide slope
328 geo_inverse_wgs_84( elev,
329 lat * SGD_RADIANS_TO_DEGREES,
330 lon * SGD_RADIANS_TO_DEGREES,
331 nav_gslat, nav_gslon,
333 double r = az1 - nav_radial;
334 while ( r > 180.0 ) { r -= 360.0;}
335 while ( r < -180.0 ) { r += 360.0;}
336 if ( r >= -90.0 && r <= 90.0 ) {
337 nav_gs_dist_signed = nav_gs_dist;
339 nav_gs_dist_signed = -nav_gs_dist;
341 /* cout << "Target Radial = " << nav_radial
342 << " Bearing = " << az1
343 << " dist (signed) = " << nav_gs_dist_signed
350 // wgs84 heading to localizer
351 geo_inverse_wgs_84( elev,
352 lat * SGD_RADIANS_TO_DEGREES,
353 lon * SGD_RADIANS_TO_DEGREES,
354 nav_loclat, nav_loclon,
356 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
357 nav_heading = az1 - nav_magvar;
358 // cout << " heading = " << nav_heading
359 // << " dist = " << nav_dist << endl;
362 double offset = nav_heading - nav_radial;
363 while ( offset < -180.0 ) { offset += 360.0; }
364 while ( offset > 180.0 ) { offset -= 360.0; }
365 // cout << "ils offset = " << offset << endl;
366 nav_effective_range = adjustILSRange(nav_elev, elev, offset,
367 nav_loc_dist * SG_METER_TO_NM );
369 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
371 // cout << "nav range = " << nav_effective_range
372 // << " (" << nav_range << ")" << endl;
374 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
376 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
377 nav_inrange = sg_random() <
378 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
379 (nav_effective_range * SG_NM_TO_METER);
385 nav_radial = nav_sel_radial;
389 // cout << "not picking up vor. :-(" << endl;
392 if ( nav_valid && nav_inrange && nav_servicable->getBoolValue() ) {
393 // play station ident via audio system if on + ident,
394 // otherwise turn it off
395 if ( power_btn && (bus_power->getDoubleValue() > 1.0)
396 && nav_ident_btn && audio_btn )
398 FGSimpleSound *sound;
399 sound = globals->get_soundmgr()->find( nav_fx_name );
400 if ( sound != NULL ) {
401 sound->set_volume( nav_vol_btn );
403 SG_LOG( SG_COCKPIT, SG_ALERT,
404 "Can't find nav-vor-ident sound" );
406 sound = globals->get_soundmgr()->find( dme_fx_name );
407 if ( sound != NULL ) {
408 sound->set_volume( nav_vol_btn );
410 SG_LOG( SG_COCKPIT, SG_ALERT,
411 "Can't find nav-dme-ident sound" );
413 // cout << "nav_last_time = " << nav_last_time << " ";
414 // cout << "cur_time = "
415 // << globals->get_time_params()->get_cur_time();
417 globals->get_time_params()->get_cur_time() - 30 ) {
418 nav_last_time = globals->get_time_params()->get_cur_time();
421 // cout << " nav_play_count = " << nav_play_count << endl;
422 // cout << "playing = "
423 // << globals->get_soundmgr()->is_playing(nav_fx_name)
425 if ( nav_play_count < 4 ) {
427 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
428 globals->get_soundmgr()->play_once( nav_fx_name );
431 } else if ( nav_play_count < 5 && nav_has_dme ) {
433 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
434 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
435 globals->get_soundmgr()->play_once( dme_fx_name );
440 globals->get_soundmgr()->stop( nav_fx_name );
441 globals->get_soundmgr()->stop( dme_fx_name );
447 // Update current nav/adf radio stations based on current postition
448 void FGNavCom::search()
450 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
451 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
452 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
457 ////////////////////////////////////////////////////////////////////////
459 ////////////////////////////////////////////////////////////////////////
461 if ( (ils = current_ilslist->findByFreq(nav_freq, lon, lat, elev)) != NULL ) {
462 nav_id = ils->get_locident();
464 if ( last_nav_id != nav_id || last_nav_vor ) {
465 nav_trans_ident = ils->get_trans_ident();
466 last_nav_id = nav_id;
467 last_nav_vor = false;
469 nav_has_dme = ils->get_has_dme();
470 nav_has_gs = ils->get_has_gs();
472 nav_loclon = ils->get_loclon();
473 nav_loclat = ils->get_loclat();
474 nav_gslon = ils->get_gslon();
475 nav_gslat = ils->get_gslat();
476 nav_elev = ils->get_gselev();
478 nav_range = FG_ILS_DEFAULT_RANGE;
479 nav_effective_range = nav_range;
480 nav_target_gs = ils->get_gsangle();
481 nav_radial = ils->get_locheading();
482 while ( nav_radial < 0.0 ) { nav_radial += 360.0; }
483 while ( nav_radial > 360.0 ) { nav_radial -= 360.0; }
484 nav_x = ils->get_x();
485 nav_y = ils->get_y();
486 nav_z = ils->get_z();
487 nav_gs_x = ils->get_gs_x();
488 nav_gs_y = ils->get_gs_y();
489 nav_gs_z = ils->get_gs_z();
491 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
492 globals->get_soundmgr()->remove( nav_fx_name );
494 FGSimpleSound *sound;
495 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
496 sound->set_volume( 0.3 );
497 globals->get_soundmgr()->add( sound, nav_fx_name );
499 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
500 globals->get_soundmgr()->remove( dme_fx_name );
502 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
503 sound->set_volume( 0.3 );
504 globals->get_soundmgr()->add( sound, dme_fx_name );
506 int offset = (int)(sg_random() * 30.0);
507 nav_play_count = offset / 4;
508 nav_last_time = globals->get_time_params()->get_cur_time() -
510 // cout << "offset = " << offset << " play_count = "
512 // << " nav_last_time = " << nav_last_time
513 // << " current time = "
514 // << globals->get_time_params()->get_cur_time() << endl;
516 // cout << "Found an ils station in range" << endl;
517 // cout << " id = " << ils->get_locident() << endl;
519 } else if ( (nav = current_navlist->findByFreq(nav_freq, lon, lat, elev)) != NULL ) {
520 nav_id = nav->get_ident();
521 nav_valid = (nav->get_type() == 'V');
522 if ( last_nav_id != nav_id || !last_nav_vor ) {
523 last_nav_id = nav_id;
525 nav_trans_ident = nav->get_trans_ident();
527 nav_has_dme = nav->get_has_dme();
529 nav_loclon = nav->get_lon();
530 nav_loclat = nav->get_lat();
531 nav_elev = nav->get_elev();
532 nav_magvar = nav->get_magvar();
533 nav_range = nav->get_range();
534 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
536 nav_radial = nav_sel_radial;
537 nav_x = nav->get_x();
538 nav_y = nav->get_y();
539 nav_z = nav->get_z();
541 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
542 globals->get_soundmgr()->remove( nav_fx_name );
544 FGSimpleSound *sound;
545 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
546 sound->set_volume( 0.3 );
547 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
548 // cout << "Added nav-vor-ident sound" << endl;
550 cout << "Failed to add v1-vor-ident sound" << endl;
553 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
554 globals->get_soundmgr()->remove( dme_fx_name );
556 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
557 sound->set_volume( 0.3 );
558 globals->get_soundmgr()->add( sound, dme_fx_name );
560 int offset = (int)(sg_random() * 30.0);
561 nav_play_count = offset / 4;
562 nav_last_time = globals->get_time_params()->get_cur_time() -
564 // cout << "offset = " << offset << " play_count = "
565 // << nav_play_count << " nav_last_time = "
566 // << nav_last_time << " current time = "
567 // << globals->get_time_params()->get_cur_time() << endl;
569 // cout << "Found a vor station in range" << endl;
570 // cout << " id = " << nav->get_ident() << endl;
576 nav_trans_ident = "";
578 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
579 cout << "Failed to remove nav-vor-ident sound" << endl;
581 globals->get_soundmgr()->remove( dme_fx_name );
582 // cout << "not picking up vor1. :-(" << endl;
587 // return the amount of heading needle deflection, returns a value
588 // clamped to the range of ( -10 , 10 )
589 double FGNavCom::get_nav_heading_needle_deflection() const {
593 && nav_servicable->getBoolValue() && cdi_servicable->getBoolValue() )
595 r = nav_heading - nav_radial;
596 // cout << "Radial = " << nav_radial
597 // << " Bearing = " << nav_heading << endl;
599 while ( r > 180.0 ) { r -= 360.0;}
600 while ( r < -180.0 ) { r += 360.0;}
601 if ( fabs(r) > 90.0 )
602 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
604 // According to Robin Peel, the ILS is 4x more sensitive than a vor
605 if ( nav_loc ) { r *= 4.0; }
606 if ( r < -10.0 ) { r = -10.0; }
607 if ( r > 10.0 ) { r = 10.0; }
616 // return the amount of glide slope needle deflection (.i.e. the
617 // number of degrees we are off the glide slope * 5.0
618 double FGNavCom::get_nav_gs_needle_deflection() const {
619 if ( nav_inrange && nav_has_gs
620 && nav_servicable->getBoolValue() && gs_servicable->getBoolValue() )
622 double x = nav_gs_dist;
623 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
625 double angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
626 return (nav_target_gs - angle) * 5.0;
634 * Return true if the NAV TO flag should be active.
637 FGNavCom::get_nav_to_flag () const
640 && nav_servicable->getBoolValue()
641 && tofrom_servicable->getBoolValue() )
643 double offset = fabs(nav_heading - nav_radial);
647 return (offset <= 90.0 || offset >= 270.0);
656 * Return true if the NAV FROM flag should be active.
659 FGNavCom::get_nav_from_flag () const
662 && nav_servicable->getBoolValue()
663 && tofrom_servicable->getBoolValue() ) {
664 double offset = fabs(nav_heading - nav_radial);
668 return (offset > 90.0 && offset < 270.0);