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>
32 #include <simgear/math/vector.hxx>
34 #include <Aircraft/aircraft.hxx>
35 #include <Navaids/ilslist.hxx>
36 #include <Navaids/navlist.hxx>
37 #include <Time/FGEventMgr.hxx>
46 FGNavCom::FGNavCom() :
47 lon_node(fgGetNode("/position/longitude-deg", true)),
48 lat_node(fgGetNode("/position/latitude-deg", true)),
49 alt_node(fgGetNode("/position/altitude-ft", true)),
66 SGPath path( globals->get_fg_root() );
68 term.append( "Navaids/range.term" );
70 low.append( "Navaids/range.low" );
72 high.append( "Navaids/range.high" );
74 term_tbl = new SGInterpTable( term.str() );
75 low_tbl = new SGInterpTable( low.str() );
76 high_tbl = new SGInterpTable( high.str() );
95 // We assume that index is valid now (it must be set before init()
99 sprintf( propname, "/systems/electrical/outputs/navcom[%d]", index );
100 // default to true in case no electrical system defined.
101 fgSetDouble( propname, 60.0 );
102 bus_power = fgGetNode( propname, true );
104 sprintf( propname, "/instrumentation/comm[%d]/servicable", index );
105 com_servicable = fgGetNode( propname, true );
106 com_servicable->setBoolValue( true );
108 sprintf( propname, "/instrumentation/nav[%d]/servicable", index );
109 nav_servicable = fgGetNode( propname, true );
110 nav_servicable->setBoolValue( true );
112 sprintf( propname, "/instrumentation/vor[%d]/cdi/servicable", index );
113 cdi_servicable = fgGetNode( propname, true );
114 cdi_servicable->setBoolValue( true );
116 sprintf( propname, "/instrumentation/vor[%d]/gs/servicable", index );
117 gs_servicable = fgGetNode( propname, true );
118 gs_servicable->setBoolValue( true );
120 sprintf( propname, "/instrumentation/vor[%d]/to-from/servicable", index );
121 tofrom_servicable = fgGetNode( propname, true );
122 tofrom_servicable->setBoolValue( true );
131 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
132 fgTie( propname, this,
133 &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
134 fgSetArchivable( propname );
136 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
137 fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
138 fgSetArchivable( propname );
140 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
141 fgTie( propname, this,
142 &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
143 fgSetArchivable( propname );
145 sprintf( propname, "/radios/comm[%d]/volume", index );
146 fgTie( propname, this,
147 &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
148 fgSetArchivable( propname );
150 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
151 fgTie( propname, this,
152 &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
153 fgSetArchivable( propname );
155 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
156 fgTie( propname , this,
157 &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
158 fgSetArchivable( propname );
160 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
161 fgTie( propname, this,
162 &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
163 fgSetArchivable( propname );
165 sprintf( propname, "/radios/nav[%d]/volume", index );
166 fgTie( propname, this,
167 &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
168 fgSetArchivable( propname );
170 sprintf( propname, "/radios/nav[%d]/ident", index );
171 fgTie( propname, this,
172 &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
173 fgSetArchivable( propname );
176 sprintf( propname, "/radios/nav[%d]/audio-btn", index );
177 fgTie( propname, this,
178 &FGNavCom::get_audio_btn, &FGNavCom::set_audio_btn );
179 fgSetArchivable( propname );
181 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
182 fgTie( propname, this, &FGNavCom::get_nav_radial );
184 sprintf( propname, "/radios/nav[%d]/to-flag", index );
185 fgTie( propname, this, &FGNavCom::get_nav_to_flag );
187 sprintf( propname, "/radios/nav[%d]/from-flag", index );
188 fgTie( propname, this, &FGNavCom::get_nav_from_flag );
190 sprintf( propname, "/radios/nav[%d]/in-range", index );
191 fgTie( propname, this, &FGNavCom::get_nav_inrange );
193 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
194 fgTie( propname, this, &FGNavCom::get_nav_heading_needle_deflection );
196 sprintf( propname, "/radios/nav[%d]/has-gs", index );
197 fgTie( propname, this, &FGNavCom::get_nav_has_gs );
199 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
200 fgTie( propname, this, &FGNavCom::get_nav_gs_needle_deflection );
211 sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
213 sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
215 sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
218 sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
220 sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
222 sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
224 sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
226 sprintf( propname, "/radios/nav[%d]/ident", index );
228 sprintf( propname, "/radios/nav[%d]/to-flag", index );
230 sprintf( propname, "/radios/nav[%d]/from-flag", index );
232 sprintf( propname, "/radios/nav[%d]/in-range", index );
234 sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
236 sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
241 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
242 double FGNavCom::adjustNavRange( double stationElev, double aircraftElev,
243 double nominalRange )
245 // extend out actual usable range to be 1.3x the published safe range
246 const double usability_factor = 1.3;
248 // assumptions we model the standard service volume, plus
249 // ... rather than specifying a cylinder, we model a cone that
250 // contains the cylinder. Then we put an upside down cone on top
251 // to model diminishing returns at too-high altitudes.
253 // altitude difference
254 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
255 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
256 // << " station elev = " << stationElev << endl;
258 if ( nominalRange < 25.0 + SG_EPSILON ) {
259 // Standard Terminal Service Volume
260 return term_tbl->interpolate( alt ) * usability_factor;
261 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
262 // Standard Low Altitude Service Volume
263 // table is based on range of 40, scale to actual range
264 return low_tbl->interpolate( alt ) * nominalRange / 40.0
267 // Standard High Altitude Service Volume
268 // table is based on range of 130, scale to actual range
269 return high_tbl->interpolate( alt ) * nominalRange / 130.0
275 // model standard ILS service volumes as per AIM 1-1-9
276 double FGNavCom::adjustILSRange( double stationElev, double aircraftElev,
277 double offsetDegrees, double distance )
279 // assumptions we model the standard service volume, plus
281 // altitude difference
282 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
283 // double offset = fabs( offsetDegrees );
285 // if ( offset < 10 ) {
286 // return FG_ILS_DEFAULT_RANGE;
287 // } else if ( offset < 35 ) {
288 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
289 // } else if ( offset < 45 ) {
290 // return (45 - offset);
291 // } else if ( offset > 170 ) {
292 // return FG_ILS_DEFAULT_RANGE;
293 // } else if ( offset > 145 ) {
294 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
295 // } else if ( offset > 135 ) {
296 // return (offset - 135);
300 return FG_ILS_DEFAULT_RANGE;
304 // Update the various nav values based on position and valid tuned in navs
306 FGNavCom::update(double dt)
308 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
309 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
310 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
314 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
318 ////////////////////////////////////////////////////////////////////////
320 ////////////////////////////////////////////////////////////////////////
322 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
323 && nav_servicable->getBoolValue() )
325 station = Point3D( nav_x, nav_y, nav_z );
326 nav_loc_dist = aircraft.distance3D( station );
329 // find closest distance to the gs base line
331 sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
333 sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
334 double dist = sgdClosestPointToLineDistSquared( p, p0,
336 nav_gs_dist = sqrt( dist );
337 // cout << nav_gs_dist;
339 // Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
340 // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
342 // wgs84 heading to glide slope (to determine sign of distance)
343 geo_inverse_wgs_84( elev,
344 lat * SGD_RADIANS_TO_DEGREES,
345 lon * SGD_RADIANS_TO_DEGREES,
346 nav_gslat, nav_gslon,
348 double r = az1 - nav_radial;
349 while ( r > 180.0 ) { r -= 360.0;}
350 while ( r < -180.0 ) { r += 360.0;}
351 if ( r >= -90.0 && r <= 90.0 ) {
352 nav_gs_dist_signed = nav_gs_dist;
354 nav_gs_dist_signed = -nav_gs_dist;
356 /* cout << "Target Radial = " << nav_radial
357 << " Bearing = " << az1
358 << " dist (signed) = " << nav_gs_dist_signed
365 // wgs84 heading to localizer
366 geo_inverse_wgs_84( elev,
367 lat * SGD_RADIANS_TO_DEGREES,
368 lon * SGD_RADIANS_TO_DEGREES,
369 nav_loclat, nav_loclon,
371 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
372 nav_heading = az1 - nav_magvar;
373 // cout << " heading = " << nav_heading
374 // << " dist = " << nav_dist << endl;
377 double offset = nav_heading - nav_radial;
378 while ( offset < -180.0 ) { offset += 360.0; }
379 while ( offset > 180.0 ) { offset -= 360.0; }
380 // cout << "ils offset = " << offset << endl;
381 nav_effective_range = adjustILSRange(nav_elev, elev, offset,
382 nav_loc_dist * SG_METER_TO_NM );
384 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
386 // cout << "nav range = " << nav_effective_range
387 // << " (" << nav_range << ")" << endl;
389 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
391 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
392 nav_inrange = sg_random() <
393 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
394 (nav_effective_range * SG_NM_TO_METER);
400 nav_radial = nav_sel_radial;
404 // cout << "not picking up vor. :-(" << endl;
407 if ( nav_valid && nav_inrange && nav_servicable->getBoolValue() ) {
408 // play station ident via audio system if on + ident,
409 // otherwise turn it off
410 if ( power_btn && (bus_power->getDoubleValue() > 1.0)
411 && nav_ident_btn && audio_btn )
413 SGSimpleSound *sound;
414 sound = globals->get_soundmgr()->find( nav_fx_name );
415 if ( sound != NULL ) {
416 sound->set_volume( nav_vol_btn );
418 SG_LOG( SG_COCKPIT, SG_ALERT,
419 "Can't find nav-vor-ident sound" );
421 sound = globals->get_soundmgr()->find( dme_fx_name );
422 if ( sound != NULL ) {
423 sound->set_volume( nav_vol_btn );
425 SG_LOG( SG_COCKPIT, SG_ALERT,
426 "Can't find nav-dme-ident sound" );
428 // cout << "nav_last_time = " << nav_last_time << " ";
429 // cout << "cur_time = "
430 // << globals->get_time_params()->get_cur_time();
432 globals->get_time_params()->get_cur_time() - 30 ) {
433 nav_last_time = globals->get_time_params()->get_cur_time();
436 // cout << " nav_play_count = " << nav_play_count << endl;
437 // cout << "playing = "
438 // << globals->get_soundmgr()->is_playing(nav_fx_name)
440 if ( nav_play_count < 4 ) {
442 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
443 globals->get_soundmgr()->play_once( nav_fx_name );
446 } else if ( nav_play_count < 5 && nav_has_dme ) {
448 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
449 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
450 globals->get_soundmgr()->play_once( dme_fx_name );
455 globals->get_soundmgr()->stop( nav_fx_name );
456 globals->get_soundmgr()->stop( dme_fx_name );
462 // Update current nav/adf radio stations based on current postition
463 void FGNavCom::search()
465 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
466 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
467 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
472 ////////////////////////////////////////////////////////////////////////
474 ////////////////////////////////////////////////////////////////////////
476 if ( (ils = current_ilslist->findByFreq(nav_freq, lon, lat, elev)) != NULL ) {
477 nav_id = ils->get_locident();
479 if ( last_nav_id != nav_id || last_nav_vor ) {
480 nav_trans_ident = ils->get_trans_ident();
481 last_nav_id = nav_id;
482 last_nav_vor = false;
484 nav_has_dme = ils->get_has_dme();
485 nav_has_gs = ils->get_has_gs();
487 nav_loclon = ils->get_loclon();
488 nav_loclat = ils->get_loclat();
489 nav_gslon = ils->get_gslon();
490 nav_gslat = ils->get_gslat();
491 nav_elev = ils->get_gselev();
493 nav_range = FG_ILS_DEFAULT_RANGE;
494 nav_effective_range = nav_range;
495 nav_target_gs = ils->get_gsangle();
496 nav_radial = ils->get_locheading();
497 while ( nav_radial < 0.0 ) { nav_radial += 360.0; }
498 while ( nav_radial > 360.0 ) { nav_radial -= 360.0; }
499 nav_x = ils->get_x();
500 nav_y = ils->get_y();
501 nav_z = ils->get_z();
502 nav_gs_x = ils->get_gs_x();
503 nav_gs_y = ils->get_gs_y();
504 nav_gs_z = ils->get_gs_z();
506 // derive GS baseline
507 double tlon, tlat, taz;
508 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon, nav_radial + 90,
509 100.0, &tlat, &tlon, &taz );
510 // cout << nav_gslon << "," << nav_gslat << " "
511 // << tlon << "," << tlat << " (" << nav_elev << ")" << endl;
512 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
513 tlat*SGD_DEGREES_TO_RADIANS,
514 nav_elev*SG_FEET_TO_METER) );
515 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z << endl;
516 // cout << p1 << endl;
517 sgdSetVec3( gs_base_vec,
518 p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
519 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
520 // << gs_base_vec[2] << endl;
522 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
523 globals->get_soundmgr()->remove( nav_fx_name );
525 SGSimpleSound *sound;
526 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
527 sound->set_volume( 0.3 );
528 globals->get_soundmgr()->add( sound, nav_fx_name );
530 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
531 globals->get_soundmgr()->remove( dme_fx_name );
533 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
534 sound->set_volume( 0.3 );
535 globals->get_soundmgr()->add( sound, dme_fx_name );
537 int offset = (int)(sg_random() * 30.0);
538 nav_play_count = offset / 4;
539 nav_last_time = globals->get_time_params()->get_cur_time() -
541 // cout << "offset = " << offset << " play_count = "
543 // << " nav_last_time = " << nav_last_time
544 // << " current time = "
545 // << globals->get_time_params()->get_cur_time() << endl;
547 // cout << "Found an ils station in range" << endl;
548 // cout << " id = " << ils->get_locident() << endl;
550 } else if ( (nav = current_navlist->findByFreq(nav_freq, lon, lat, elev)) != NULL ) {
551 nav_id = nav->get_ident();
552 nav_valid = (nav->get_type() == 'V');
553 if ( last_nav_id != nav_id || !last_nav_vor ) {
554 last_nav_id = nav_id;
556 nav_trans_ident = nav->get_trans_ident();
558 nav_has_dme = nav->get_has_dme();
560 nav_loclon = nav->get_lon();
561 nav_loclat = nav->get_lat();
562 nav_elev = nav->get_elev();
563 nav_magvar = nav->get_magvar();
564 nav_range = nav->get_range();
565 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
567 nav_radial = nav_sel_radial;
568 nav_x = nav->get_x();
569 nav_y = nav->get_y();
570 nav_z = nav->get_z();
572 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
573 globals->get_soundmgr()->remove( nav_fx_name );
575 SGSimpleSound *sound;
576 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
577 sound->set_volume( 0.3 );
578 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
579 // cout << "Added nav-vor-ident sound" << endl;
581 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
584 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
585 globals->get_soundmgr()->remove( dme_fx_name );
587 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
588 sound->set_volume( 0.3 );
589 globals->get_soundmgr()->add( sound, dme_fx_name );
591 int offset = (int)(sg_random() * 30.0);
592 nav_play_count = offset / 4;
593 nav_last_time = globals->get_time_params()->get_cur_time() -
595 // cout << "offset = " << offset << " play_count = "
596 // << nav_play_count << " nav_last_time = "
597 // << nav_last_time << " current time = "
598 // << globals->get_time_params()->get_cur_time() << endl;
600 // cout << "Found a vor station in range" << endl;
601 // cout << " id = " << nav->get_ident() << endl;
607 nav_trans_ident = "";
609 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
610 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
612 globals->get_soundmgr()->remove( dme_fx_name );
613 // cout << "not picking up vor1. :-(" << endl;
618 // return the amount of heading needle deflection, returns a value
619 // clamped to the range of ( -10 , 10 )
620 double FGNavCom::get_nav_heading_needle_deflection() const {
624 && nav_servicable->getBoolValue() && cdi_servicable->getBoolValue() )
626 r = nav_heading - nav_radial;
627 // cout << "Radial = " << nav_radial
628 // << " Bearing = " << nav_heading << endl;
630 while ( r > 180.0 ) { r -= 360.0;}
631 while ( r < -180.0 ) { r += 360.0;}
632 if ( fabs(r) > 90.0 )
633 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
635 // According to Robin Peel, the ILS is 4x more sensitive than a vor
636 if ( nav_loc ) { r *= 4.0; }
637 if ( r < -10.0 ) { r = -10.0; }
638 if ( r > 10.0 ) { r = 10.0; }
647 // return the amount of glide slope needle deflection (.i.e. the
648 // number of degrees we are off the glide slope * 5.0
649 double FGNavCom::get_nav_gs_needle_deflection() const {
650 if ( nav_inrange && nav_has_gs
651 && nav_servicable->getBoolValue() && gs_servicable->getBoolValue() )
653 double x = nav_gs_dist;
654 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
656 // cout << "dist = " << x << " height = " << y << endl;
657 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
658 return (nav_target_gs - angle) * 5.0;
666 * Return true if the NAV TO flag should be active.
669 FGNavCom::get_nav_to_flag () const
672 && nav_servicable->getBoolValue()
673 && tofrom_servicable->getBoolValue() )
675 double offset = fabs(nav_heading - nav_radial);
679 return (offset <= 90.0 || offset >= 270.0);
688 * Return true if the NAV FROM flag should be active.
691 FGNavCom::get_nav_from_flag () const
694 && nav_servicable->getBoolValue()
695 && tofrom_servicable->getBoolValue() ) {
696 double offset = fabs(nav_heading - nav_radial);
700 return (offset > 90.0 && offset < 270.0);