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)),
64 SGPath path( globals->get_fg_root() );
66 term.append( "Navaids/range.term" );
68 low.append( "Navaids/range.low" );
70 high.append( "Navaids/range.high" );
72 term_tbl = new SGInterpTable( term.str() );
73 low_tbl = new SGInterpTable( low.str() );
74 high_tbl = new SGInterpTable( high.str() );
93 // We assume that index is valid now (it must be set before init()
96 sprintf( propname, "/systems/electrical/outputs/navcom[%d]", index );
97 // default to true in case no electrical system defined.
98 fgSetDouble( propname, 60.0 );
99 bus_power = fgGetNode( propname, true );
108 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
109 fgTie( propname, this,
110 &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
111 fgSetArchivable( propname );
113 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
114 fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
115 fgSetArchivable( propname );
117 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
118 fgTie( propname, this,
119 &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
120 fgSetArchivable( propname );
122 sprintf( propname, "/radios/comm[%d]/volume", index );
123 fgTie( propname, this,
124 &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
125 fgSetArchivable( propname );
127 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
128 fgTie( propname, this,
129 &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
130 fgSetArchivable( propname );
132 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
133 fgTie( propname , this,
134 &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
135 fgSetArchivable( propname );
137 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
138 fgTie( propname, this,
139 &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
140 fgSetArchivable( propname );
142 sprintf( propname, "/radios/nav[%d]/volume", index );
143 fgTie( propname, this,
144 &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
145 fgSetArchivable( propname );
147 sprintf( propname, "/radios/nav[%d]/ident", index );
148 fgTie( propname, this,
149 &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
150 fgSetArchivable( propname );
153 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
154 fgTie( propname, this, &FGNavCom::get_nav_radial );
156 sprintf( propname, "/radios/nav[%d]/to-flag", index );
157 fgTie( propname, this, &FGNavCom::get_nav_to_flag );
159 sprintf( propname, "/radios/nav[%d]/from-flag", index );
160 fgTie( propname, this, &FGNavCom::get_nav_from_flag );
162 sprintf( propname, "/radios/nav[%d]/in-range", index );
163 fgTie( propname, this, &FGNavCom::get_nav_inrange );
165 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
166 fgTie( propname, this, &FGNavCom::get_nav_heading_needle_deflection );
168 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
169 fgTie( propname, this, &FGNavCom::get_nav_gs_needle_deflection );
180 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
182 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
184 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
187 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
189 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
191 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
193 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
195 sprintf( propname, "/radios/nav[%d]/ident", index );
197 sprintf( propname, "/radios/nav[%d]/to-flag", index );
199 sprintf( propname, "/radios/nav[%d]/from-flag", index );
201 sprintf( propname, "/radios/nav[%d]/in-range", index );
203 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
205 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
210 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
211 double FGNavCom::adjustNavRange( double stationElev, double aircraftElev,
212 double nominalRange )
214 // extend out actual usable range to be 1.3x the published safe range
215 const double usability_factor = 1.3;
217 // assumptions we model the standard service volume, plus
218 // ... rather than specifying a cylinder, we model a cone that
219 // contains the cylinder. Then we put an upside down cone on top
220 // to model diminishing returns at too-high altitudes.
222 // altitude difference
223 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
224 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
225 // << " station elev = " << stationElev << endl;
227 if ( nominalRange < 25.0 + SG_EPSILON ) {
228 // Standard Terminal Service Volume
229 return term_tbl->interpolate( alt ) * usability_factor;
230 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
231 // Standard Low Altitude Service Volume
232 // table is based on range of 40, scale to actual range
233 return low_tbl->interpolate( alt ) * nominalRange / 40.0
236 // Standard High Altitude Service Volume
237 // table is based on range of 130, scale to actual range
238 return high_tbl->interpolate( alt ) * nominalRange / 130.0
244 // model standard ILS service volumes as per AIM 1-1-9
245 double FGNavCom::adjustILSRange( double stationElev, double aircraftElev,
246 double offsetDegrees, double distance )
248 // assumptions we model the standard service volume, plus
250 // altitude difference
251 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
252 double offset = fabs( offsetDegrees );
255 return FG_ILS_DEFAULT_RANGE;
256 } else if ( offset < 35 ) {
257 return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
258 } else if ( offset < 45 ) {
259 return (45 - offset);
260 } else if ( offset > 170 ) {
261 return FG_ILS_DEFAULT_RANGE;
262 } else if ( offset > 145 ) {
263 return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
264 } else if ( offset > 135 ) {
265 return (offset - 135);
272 // Update the various nav values based on position and valid tuned in navs
274 FGNavCom::update(double dt)
276 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
277 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
278 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
282 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
286 ////////////////////////////////////////////////////////////////////////
288 ////////////////////////////////////////////////////////////////////////
290 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0) ) {
291 station = Point3D( nav_x, nav_y, nav_z );
292 nav_loc_dist = aircraft.distance3D( station );
295 station = Point3D( nav_gs_x, nav_gs_y, nav_gs_z );
296 nav_gs_dist = aircraft.distance3D( station );
302 geo_inverse_wgs_84( elev, lat * SGD_RADIANS_TO_DEGREES, lon * SGD_RADIANS_TO_DEGREES,
303 nav_loclat, nav_loclon,
305 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
306 nav_heading = az1 - nav_magvar;
307 // cout << " heading = " << nav_heading
308 // << " dist = " << nav_dist << endl;
311 double offset = nav_heading - nav_radial;
312 while ( offset < -180.0 ) { offset += 360.0; }
313 while ( offset > 180.0 ) { offset -= 360.0; }
314 // cout << "ils offset = " << offset << endl;
315 nav_effective_range = adjustILSRange(nav_elev, elev, offset,
316 nav_loc_dist * SG_METER_TO_NM );
318 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
320 // cout << "nav range = " << nav_effective_range
321 // << " (" << nav_range << ")" << endl;
323 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
325 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
326 nav_inrange = sg_random() <
327 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
328 (nav_effective_range * SG_NM_TO_METER);
334 nav_radial = nav_sel_radial;
338 // cout << "not picking up vor. :-(" << endl;
341 #ifdef ENABLE_AUDIO_SUPPORT
342 if ( nav_valid && nav_inrange ) {
343 // play station ident via audio system if on + ident,
344 // otherwise turn it off
345 if ( power_btn && (bus_power->getDoubleValue() > 1.0)
348 FGSimpleSound *sound;
349 sound = globals->get_soundmgr()->find( nav_fx_name );
350 if ( sound != NULL ) {
351 sound->set_volume( nav_vol_btn );
353 SG_LOG( SG_COCKPIT, SG_ALERT,
354 "Can't find nav-vor-ident sound" );
356 sound = globals->get_soundmgr()->find( dme_fx_name );
357 if ( sound != NULL ) {
358 sound->set_volume( nav_vol_btn );
360 SG_LOG( SG_COCKPIT, SG_ALERT,
361 "Can't find nav-dme-ident sound" );
363 // cout << "nav_last_time = " << nav_last_time << " ";
364 // cout << "cur_time = "
365 // << globals->get_time_params()->get_cur_time();
367 globals->get_time_params()->get_cur_time() - 30 ) {
368 nav_last_time = globals->get_time_params()->get_cur_time();
371 // cout << " nav_play_count = " << nav_play_count << endl;
372 // cout << "playing = "
373 // << globals->get_soundmgr()->is_playing(nav_fx_name)
375 if ( nav_play_count < 4 ) {
377 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
378 globals->get_soundmgr()->play_once( nav_fx_name );
381 } else if ( nav_play_count < 5 && nav_has_dme ) {
383 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
384 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
385 globals->get_soundmgr()->play_once( dme_fx_name );
390 globals->get_soundmgr()->stop( nav_fx_name );
391 globals->get_soundmgr()->stop( dme_fx_name );
399 // Update current nav/adf radio stations based on current postition
400 void FGNavCom::search()
402 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
403 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
404 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
409 ////////////////////////////////////////////////////////////////////////
411 ////////////////////////////////////////////////////////////////////////
413 if ( current_ilslist->query( lon, lat, elev, nav_freq, &ils ) ) {
414 nav_id = ils.get_locident();
416 if ( last_nav_id != nav_id || last_nav_vor ) {
417 nav_trans_ident = ils.get_trans_ident();
418 last_nav_id = nav_id;
419 last_nav_vor = false;
421 nav_has_dme = ils.get_has_dme();
422 nav_has_gs = ils.get_has_gs();
424 nav_loclon = ils.get_loclon();
425 nav_loclat = ils.get_loclat();
426 nav_gslon = ils.get_gslon();
427 nav_gslat = ils.get_gslat();
428 nav_elev = ils.get_gselev();
430 nav_range = FG_ILS_DEFAULT_RANGE;
431 nav_effective_range = nav_range;
432 nav_target_gs = ils.get_gsangle();
433 nav_radial = ils.get_locheading();
434 while ( nav_radial < 0.0 ) { nav_radial += 360.0; }
435 while ( nav_radial > 360.0 ) { nav_radial -= 360.0; }
439 nav_gs_x = ils.get_gs_x();
440 nav_gs_y = ils.get_gs_y();
441 nav_gs_z = ils.get_gs_z();
443 #ifdef ENABLE_AUDIO_SUPPORT
444 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
445 globals->get_soundmgr()->remove( nav_fx_name );
447 FGSimpleSound *sound;
448 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
449 sound->set_volume( 0.3 );
450 globals->get_soundmgr()->add( sound, nav_fx_name );
452 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
453 globals->get_soundmgr()->remove( dme_fx_name );
455 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
456 sound->set_volume( 0.3 );
457 globals->get_soundmgr()->add( sound, dme_fx_name );
459 int offset = (int)(sg_random() * 30.0);
460 nav_play_count = offset / 4;
461 nav_last_time = globals->get_time_params()->get_cur_time() -
463 // cout << "offset = " << offset << " play_count = "
465 // << " nav_last_time = " << nav_last_time
466 // << " current time = "
467 // << globals->get_time_params()->get_cur_time() << endl;
470 // cout << "Found an ils station in range" << endl;
471 // cout << " id = " << ils.get_locident() << endl;
473 } else if ( current_navlist->query( lon, lat, elev, nav_freq, &nav ) ) {
474 nav_id = nav.get_ident();
476 if ( last_nav_id != nav_id || !last_nav_vor ) {
477 last_nav_id = nav_id;
479 nav_trans_ident = nav.get_trans_ident();
481 nav_has_dme = nav.get_has_dme();
483 nav_loclon = nav.get_lon();
484 nav_loclat = nav.get_lat();
485 nav_elev = nav.get_elev();
486 nav_magvar = nav.get_magvar();
487 nav_range = nav.get_range();
488 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
490 nav_radial = nav_sel_radial;
495 #ifdef ENABLE_AUDIO_SUPPORT
496 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
497 globals->get_soundmgr()->remove( nav_fx_name );
499 FGSimpleSound *sound;
500 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
501 sound->set_volume( 0.3 );
502 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
503 // cout << "Added nav-vor-ident sound" << endl;
505 cout << "Failed to add v1-vor-ident sound" << endl;
508 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
509 globals->get_soundmgr()->remove( dme_fx_name );
511 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
512 sound->set_volume( 0.3 );
513 globals->get_soundmgr()->add( sound, dme_fx_name );
515 int offset = (int)(sg_random() * 30.0);
516 nav_play_count = offset / 4;
517 nav_last_time = globals->get_time_params()->get_cur_time() -
519 // cout << "offset = " << offset << " play_count = "
520 // << nav_play_count << " nav_last_time = "
521 // << nav_last_time << " current time = "
522 // << globals->get_time_params()->get_cur_time() << endl;
525 // cout << "Found a vor station in range" << endl;
526 // cout << " id = " << nav.get_ident() << endl;
532 nav_trans_ident = "";
534 #ifdef ENABLE_AUDIO_SUPPORT
535 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
536 cout << "Failed to remove nav-vor-ident sound" << endl;
538 globals->get_soundmgr()->remove( dme_fx_name );
540 // cout << "not picking up vor1. :-(" << endl;
545 // return the amount of heading needle deflection, returns a value
546 // clamped to the range of ( -10 , 10 )
547 double FGNavCom::get_nav_heading_needle_deflection() const {
551 r = nav_heading - nav_radial;
552 // cout << "Radial = " << nav_radial
553 // << " Bearing = " << nav_heading << endl;
555 while ( r > 180.0 ) { r -= 360.0;}
556 while ( r < -180.0 ) { r += 360.0;}
557 if ( fabs(r) > 90.0 )
558 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
560 // According to Robin Peel, the ILS is 4x more sensitive than a vor
561 if ( nav_loc ) { r *= 4.0; }
562 if ( r < -10.0 ) { r = -10.0; }
563 if ( r > 10.0 ) { r = 10.0; }
572 // return the amount of glide slope needle deflection (.i.e. the
573 // number of degrees we are off the glide slope * 5.0
574 double FGNavCom::get_nav_gs_needle_deflection() const {
575 if ( nav_inrange && nav_has_gs ) {
576 double x = nav_gs_dist;
577 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
579 double angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
580 return (nav_target_gs - angle) * 5.0;
588 * Return true if the NAV TO flag should be active.
591 FGNavCom::get_nav_to_flag () const
594 double offset = fabs(nav_heading - nav_radial);
598 return (offset <= 90.0 || offset >= 270.0);
606 * Return true if the NAV FROM flag should be active.
609 FGNavCom::get_nav_from_flag () const
612 double offset = fabs(nav_heading - nav_radial);
616 return (offset > 90.0 && offset < 270.0);