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/mkrbeacons.hxx>
36 #include <Navaids/navlist.hxx>
37 #include <Time/FGEventMgr.hxx>
46 * Boy, this is ugly! Make the VOR range vary by altitude difference.
48 static double kludgeRange ( double stationElev, double aircraftElev,
51 // Assume that the nominal range (usually 50nm) applies at a 5,000
52 // ft difference. Just a wild guess!
53 double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
54 double range = fabs(nominalRange * factor);
56 // Clamp the range to keep it sane; for now, never less than 25%
57 // or more than 500% of nominal range.
58 if (range < nominalRange/4.0) {
59 range = nominalRange/4.0;
60 } else if (range > nominalRange*5.0) {
61 range = nominalRange*5.0;
69 FGNavCom::FGNavCom() :
70 lon_node(fgGetNode("/position/longitude-deg", true)),
71 lat_node(fgGetNode("/position/latitude-deg", true)),
72 alt_node(fgGetNode("/position/altitude-ft", true)),
88 SGPath path( globals->get_fg_root() );
90 term.append( "Navaids/range.term" );
92 low.append( "Navaids/range.low" );
94 high.append( "Navaids/range.high" );
96 term_tbl = new SGInterpTable( term.str() );
97 low_tbl = new SGInterpTable( low.str() );
98 high_tbl = new SGInterpTable( high.str() );
104 FGNavCom::~FGNavCom()
120 update(0); // FIXME: use dt
129 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
130 fgTie( propname, this,
131 &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
132 fgSetArchivable( propname );
134 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
135 fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
136 fgSetArchivable( propname );
138 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
139 fgTie( propname, this,
140 &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
141 fgSetArchivable( propname );
143 sprintf( propname, "/radios/comm[%d]/volume", index );
144 fgTie( propname, this,
145 &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
146 fgSetArchivable( propname );
148 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
149 fgTie( propname, this,
150 &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
151 fgSetArchivable( propname );
153 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
154 fgTie( propname , this,
155 &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
156 fgSetArchivable( propname );
158 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
159 fgTie( propname, this,
160 &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
161 fgSetArchivable( propname );
163 sprintf( propname, "/radios/nav[%d]/volume", index );
164 fgTie( propname, this,
165 &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
166 fgSetArchivable( propname );
168 sprintf( propname, "/radios/nav[%d]/ident", index );
169 fgTie( propname, this,
170 &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
171 fgSetArchivable( propname );
174 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
175 fgTie( propname, this, &FGNavCom::get_nav_radial );
177 sprintf( propname, "/radios/nav[%d]/to-flag", index );
178 fgTie( propname, this, &FGNavCom::get_nav_to_flag );
180 sprintf( propname, "/radios/nav[%d]/from-flag", index );
181 fgTie( propname, this, &FGNavCom::get_nav_from_flag );
183 sprintf( propname, "/radios/nav[%d]/in-range", index );
184 fgTie( propname, this, &FGNavCom::get_nav_inrange );
186 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
187 fgTie( propname, this, &FGNavCom::get_nav_heading_needle_deflection );
189 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
190 fgTie( propname, this, &FGNavCom::get_nav_gs_needle_deflection );
199 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
201 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
203 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
206 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
208 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
210 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
212 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
214 sprintf( propname, "/radios/nav[%d]/ident", index );
216 sprintf( propname, "/radios/nav[%d]/to-flag", index );
218 sprintf( propname, "/radios/nav[%d]/from-flag", index );
220 sprintf( propname, "/radios/nav[%d]/in-range", index );
222 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
224 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
229 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
230 double FGNavCom::adjustNavRange( double stationElev, double aircraftElev,
231 double nominalRange )
233 // extend out actual usable range to be 1.3x the published safe range
234 const double usability_factor = 1.3;
236 // assumptions we model the standard service volume, plus
237 // ... rather than specifying a cylinder, we model a cone that
238 // contains the cylinder. Then we put an upside down cone on top
239 // to model diminishing returns at too-high altitudes.
241 // altitude difference
242 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
243 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
244 // << " station elev = " << stationElev << endl;
246 if ( nominalRange < 25.0 + SG_EPSILON ) {
247 // Standard Terminal Service Volume
248 return term_tbl->interpolate( alt ) * usability_factor;
249 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
250 // Standard Low Altitude Service Volume
251 // table is based on range of 40, scale to actual range
252 return low_tbl->interpolate( alt ) * nominalRange / 40.0
255 // Standard High Altitude Service Volume
256 // table is based on range of 130, scale to actual range
257 return high_tbl->interpolate( alt ) * nominalRange / 130.0
263 // model standard ILS service volumes as per AIM 1-1-9
264 double FGNavCom::adjustILSRange( double stationElev, double aircraftElev,
265 double offsetDegrees, double distance )
267 // assumptions we model the standard service volume, plus
269 // altitude difference
270 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
271 double offset = fabs( offsetDegrees );
274 return FG_ILS_DEFAULT_RANGE;
275 } else if ( offset < 35 ) {
276 return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
277 } else if ( offset < 45 ) {
278 return (45 - offset);
279 } else if ( offset > 170 ) {
280 return FG_ILS_DEFAULT_RANGE;
281 } else if ( offset > 145 ) {
282 return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
283 } else if ( offset > 135 ) {
284 return (offset - 135);
291 // Update the various nav values based on position and valid tuned in navs
293 FGNavCom::update(double dt)
295 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
296 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
297 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
301 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
305 ////////////////////////////////////////////////////////////////////////
307 ////////////////////////////////////////////////////////////////////////
309 if ( nav_valid && power_btn ) {
310 station = Point3D( nav_x, nav_y, nav_z );
311 nav_loc_dist = aircraft.distance3D( station );
314 station = Point3D( nav_gs_x, nav_gs_y, nav_gs_z );
315 nav_gs_dist = aircraft.distance3D( station );
321 geo_inverse_wgs_84( elev, lat * SGD_RADIANS_TO_DEGREES, lon * SGD_RADIANS_TO_DEGREES,
322 nav_loclat, nav_loclon,
324 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
325 nav_heading = az1 - nav_magvar;
326 // cout << " heading = " << nav_heading
327 // << " dist = " << nav_dist << endl;
330 double offset = nav_heading - nav_radial;
331 while ( offset < -180.0 ) { offset += 360.0; }
332 while ( offset > 180.0 ) { offset -= 360.0; }
333 // cout << "ils offset = " << offset << endl;
334 nav_effective_range = adjustILSRange(nav_elev, elev, offset,
335 nav_loc_dist * SG_METER_TO_NM );
337 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
339 // cout << "nav range = " << nav_effective_range
340 // << " (" << nav_range << ")" << endl;
342 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
344 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
345 nav_inrange = sg_random() <
346 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
347 (nav_effective_range * SG_NM_TO_METER);
353 nav_radial = nav_sel_radial;
357 // cout << "not picking up vor. :-(" << endl;
360 #ifdef ENABLE_AUDIO_SUPPORT
361 if ( nav_valid && nav_inrange ) {
362 // play station ident via audio system if on + ident,
363 // otherwise turn it off
364 if ( power_btn && nav_ident_btn ) {
365 FGSimpleSound *sound;
366 sound = globals->get_soundmgr()->find( nav_fx_name );
367 if ( sound != NULL ) {
368 sound->set_volume( nav_vol_btn );
370 SG_LOG( SG_COCKPIT, SG_ALERT,
371 "Can't find nav-vor-ident sound" );
373 sound = globals->get_soundmgr()->find( dme_fx_name );
374 if ( sound != NULL ) {
375 sound->set_volume( nav_vol_btn );
377 SG_LOG( SG_COCKPIT, SG_ALERT,
378 "Can't find nav-dme-ident sound" );
380 // cout << "nav_last_time = " << nav_last_time << " ";
381 // cout << "cur_time = "
382 // << globals->get_time_params()->get_cur_time();
384 globals->get_time_params()->get_cur_time() - 30 ) {
385 nav_last_time = globals->get_time_params()->get_cur_time();
388 // cout << " nav_play_count = " << nav_play_count << endl;
389 // cout << "playing = "
390 // << globals->get_soundmgr()->is_playing(nav_fx_name)
392 if ( nav_play_count < 4 ) {
394 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
395 globals->get_soundmgr()->play_once( nav_fx_name );
398 } else if ( nav_play_count < 5 && nav_has_dme ) {
400 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
401 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
402 globals->get_soundmgr()->play_once( dme_fx_name );
407 globals->get_soundmgr()->stop( nav_fx_name );
408 globals->get_soundmgr()->stop( dme_fx_name );
416 // Update current nav/adf radio stations based on current postition
417 void FGNavCom::search()
419 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
420 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
421 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
426 ////////////////////////////////////////////////////////////////////////
428 ////////////////////////////////////////////////////////////////////////
430 if ( current_ilslist->query( lon, lat, elev, nav_freq, &ils ) ) {
431 nav_id = ils.get_locident();
433 if ( last_nav_id != nav_id || last_nav_vor ) {
434 nav_trans_ident = ils.get_trans_ident();
435 last_nav_id = nav_id;
436 last_nav_vor = false;
438 nav_has_dme = ils.get_has_dme();
439 nav_has_gs = ils.get_has_gs();
441 nav_loclon = ils.get_loclon();
442 nav_loclat = ils.get_loclat();
443 nav_gslon = ils.get_gslon();
444 nav_gslat = ils.get_gslat();
445 nav_elev = ils.get_gselev();
447 nav_range = FG_ILS_DEFAULT_RANGE;
448 nav_effective_range = nav_range;
449 nav_target_gs = ils.get_gsangle();
450 nav_radial = ils.get_locheading();
451 while ( nav_radial < 0.0 ) { nav_radial += 360.0; }
452 while ( nav_radial > 360.0 ) { nav_radial -= 360.0; }
456 nav_gs_x = ils.get_gs_x();
457 nav_gs_y = ils.get_gs_y();
458 nav_gs_z = ils.get_gs_z();
460 #ifdef ENABLE_AUDIO_SUPPORT
461 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
462 globals->get_soundmgr()->remove( nav_fx_name );
464 FGSimpleSound *sound;
465 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
466 sound->set_volume( 0.3 );
467 globals->get_soundmgr()->add( sound, nav_fx_name );
469 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
470 globals->get_soundmgr()->remove( dme_fx_name );
472 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
473 sound->set_volume( 0.3 );
474 globals->get_soundmgr()->add( sound, dme_fx_name );
476 int offset = (int)(sg_random() * 30.0);
477 nav_play_count = offset / 4;
478 nav_last_time = globals->get_time_params()->get_cur_time() -
480 // cout << "offset = " << offset << " play_count = "
482 // << " nav_last_time = " << nav_last_time
483 // << " current time = "
484 // << globals->get_time_params()->get_cur_time() << endl;
487 // cout << "Found an ils station in range" << endl;
488 // cout << " id = " << ils.get_locident() << endl;
490 } else if ( current_navlist->query( lon, lat, elev, nav_freq, &nav ) ) {
491 nav_id = nav.get_ident();
493 if ( last_nav_id != nav_id || !last_nav_vor ) {
494 last_nav_id = nav_id;
496 nav_trans_ident = nav.get_trans_ident();
498 nav_has_dme = nav.get_has_dme();
500 nav_loclon = nav.get_lon();
501 nav_loclat = nav.get_lat();
502 nav_elev = nav.get_elev();
503 nav_magvar = nav.get_magvar();
504 nav_range = nav.get_range();
505 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
507 nav_radial = nav_sel_radial;
512 #ifdef ENABLE_AUDIO_SUPPORT
513 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
514 globals->get_soundmgr()->remove( nav_fx_name );
516 FGSimpleSound *sound;
517 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
518 sound->set_volume( 0.3 );
519 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
520 // cout << "Added nav-vor-ident sound" << endl;
522 cout << "Failed to add v1-vor-ident sound" << endl;
525 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
526 globals->get_soundmgr()->remove( dme_fx_name );
528 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
529 sound->set_volume( 0.3 );
530 globals->get_soundmgr()->add( sound, dme_fx_name );
532 int offset = (int)(sg_random() * 30.0);
533 nav_play_count = offset / 4;
534 nav_last_time = globals->get_time_params()->get_cur_time() -
536 // cout << "offset = " << offset << " play_count = "
537 // << nav_play_count << " nav_last_time = "
538 // << nav_last_time << " current time = "
539 // << globals->get_time_params()->get_cur_time() << endl;
542 // cout << "Found a vor station in range" << endl;
543 // cout << " id = " << nav.get_ident() << endl;
549 nav_trans_ident = "";
551 #ifdef ENABLE_AUDIO_SUPPORT
552 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
553 cout << "Failed to remove nav-vor-ident sound" << endl;
555 globals->get_soundmgr()->remove( dme_fx_name );
557 // cout << "not picking up vor1. :-(" << endl;
562 // return the amount of heading needle deflection, returns a value
563 // clamped to the range of ( -10 , 10 )
564 double FGNavCom::get_nav_heading_needle_deflection() const {
568 r = nav_heading - nav_radial;
569 // cout << "Radial = " << nav_radial
570 // << " Bearing = " << nav_heading << endl;
572 while ( r > 180.0 ) { r -= 360.0;}
573 while ( r < -180.0 ) { r += 360.0;}
574 if ( fabs(r) > 90.0 ) {
575 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
581 // According to Robin Peel, the ILS is 4x more sensitive than a vor
582 if ( nav_loc ) { r *= 4.0; }
583 if ( r < -10.0 ) { r = -10.0; }
584 if ( r > 10.0 ) { r = 10.0; }
593 // return the amount of glide slope needle deflection (.i.e. the
594 // number of degrees we are off the glide slope * 5.0
595 double FGNavCom::get_nav_gs_needle_deflection() const {
596 if ( nav_inrange && nav_has_gs ) {
597 double x = nav_gs_dist;
598 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
600 double angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
601 return (nav_target_gs - angle) * 5.0;
609 * Return true if the NAV TO flag should be active.
612 FGNavCom::get_nav_to_flag () const
615 double offset = fabs(nav_heading - nav_radial);
619 return (offset <= 90.0 || offset >= 270.0);
627 * Return true if the NAV FROM flag should be active.
630 FGNavCom::get_nav_from_flag () const
633 double offset = fabs(nav_heading - nav_radial);
637 return (offset > 90.0 && offset < 270.0);