1 // navradio.cxx -- class to manage a nav radio instance
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 - 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
32 #include <simgear/compiler.h>
33 #include <simgear/sg_inlines.h>
34 #include <simgear/math/sg_random.h>
35 #include <simgear/math/vector.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Navaids/navlist.hxx>
40 #include "navradio.hxx"
47 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
48 lon_node(fgGetNode("/position/longitude-deg", true)),
49 lat_node(fgGetNode("/position/latitude-deg", true)),
50 alt_node(fgGetNode("/position/altitude-ft", true)),
62 nav_target_radial(0.0),
63 nav_target_radial_true(0.0),
64 nav_target_auto_hdg(0.0),
65 nav_gs_rate_of_climb(0.0),
73 SGPath path( globals->get_fg_root() );
75 term.append( "Navaids/range.term" );
77 low.append( "Navaids/range.low" );
79 high.append( "Navaids/range.high" );
81 term_tbl = new SGInterpTable( term.str() );
82 low_tbl = new SGInterpTable( low.str() );
83 high_tbl = new SGInterpTable( high.str() );
86 for ( i = 0; i < node->nChildren(); ++i ) {
87 SGPropertyNode *child = node->getChild(i);
88 string cname = child->getName();
89 string cval = child->getStringValue();
90 if ( cname == "name" ) {
92 } else if ( cname == "number" ) {
93 num = child->getIntValue();
95 SG_LOG( SG_INSTR, SG_WARN,
96 "Error in nav radio config logic" );
97 if ( name.length() ) {
98 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
107 FGNavRadio::~FGNavRadio()
121 branch = "/instrumentation/" + name;
123 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
126 fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
127 nav_serviceable = node->getChild("serviceable", 0, true);
128 cdi_serviceable = (node->getChild("cdi", 0, true))
129 ->getChild("serviceable", 0, true);
130 gs_serviceable = (node->getChild("gs", 0, true))
131 ->getChild("serviceable");
132 tofrom_serviceable = (node->getChild("to-from", 0, true))
133 ->getChild("serviceable", 0, true);
135 std::ostringstream temp;
136 temp << name << "nav-ident" << num;
137 nav_fx_name = temp.str();
138 temp << name << "dme-ident" << num;
139 dme_fx_name = temp.str();
145 std::ostringstream temp;
148 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
151 fgTie( (branch + "power-btn").c_str(), this,
152 &FGNavRadio::get_power_btn, &FGNavRadio::set_power_btn );
153 fgSetArchivable( (branch + "power-btn").c_str() );
155 fgTie( (branch + "/frequencies/selected-mhz").c_str() , this,
156 &FGNavRadio::get_nav_freq, &FGNavRadio::set_nav_freq );
157 fgSetArchivable( (branch + "/frequencies/selected-mhz").c_str() );
159 fgTie( (branch + "/frequencies/standby-mhz").c_str() , this,
160 &FGNavRadio::get_nav_alt_freq, &FGNavRadio::set_nav_alt_freq);
161 fgSetArchivable( (branch + "/frequencies/standby-mhz").c_str() );
163 fgTie( (branch + "/radials/selected-deg").c_str() , this,
164 &FGNavRadio::get_nav_sel_radial, &FGNavRadio::set_nav_sel_radial );
165 fgSetArchivable((branch + "/radials/selected-deg").c_str() );
167 fgTie( (branch + "/volume").c_str() , this,
168 &FGNavRadio::get_nav_vol_btn, &FGNavRadio::set_nav_vol_btn );
169 fgSetArchivable( (branch + "/volume").c_str() );
171 fgTie( (branch + "/ident").c_str(), this,
172 &FGNavRadio::get_nav_ident_btn, &FGNavRadio::set_nav_ident_btn );
173 fgSetArchivable( (branch + "/ident").c_str() );
176 fgTie( (branch + "/audio-btn").c_str(), this,
177 &FGNavRadio::get_audio_btn, &FGNavRadio::set_audio_btn );
178 fgSetArchivable( (branch + "/audio-btn").c_str() );
180 fgTie( (branch + "/heading-deg").c_str(),
181 this, &FGNavRadio::get_nav_heading );
183 fgTie( (branch + "/radials/actual-deg").c_str(),
184 this, &FGNavRadio::get_nav_radial );
186 fgTie( (branch + "/radials/target-radial-deg").c_str(),
187 this, &FGNavRadio::get_nav_target_radial_true );
189 fgTie( (branch + "/radials/reciprocal-radial-deg").c_str(),
190 this, &FGNavRadio::get_nav_reciprocal_radial );
192 fgTie( (branch + "/radials/target-radial2-deg").c_str(),
193 this, &FGNavRadio::get_nav_target_radial );
195 fgTie( (branch + "/radials/target-auto-hdg-deg").c_str(),
196 this, &FGNavRadio::get_nav_target_auto_hdg );
198 fgTie( (branch + "/to-flag").c_str(),
199 this, &FGNavRadio::get_nav_to_flag );
201 fgTie( (branch + "/from-flag").c_str(),
202 this, &FGNavRadio::get_nav_from_flag );
204 fgTie( (branch + "/in-range").c_str(),
205 this, &FGNavRadio::get_nav_inrange );
207 fgTie( (branch + "/heading-needle-deflection").c_str(),
208 this, &FGNavRadio::get_nav_cdi_deflection );
210 fgTie( (branch + "/crosstrack-error-m").c_str(),
211 this, &FGNavRadio::get_nav_cdi_xtrack_error );
213 fgTie( (branch + "/has-gs").c_str(),
214 this, &FGNavRadio::get_nav_has_gs );
216 fgTie( (branch + "/nav-loc").c_str(),
217 this, &FGNavRadio::get_nav_loc );
219 fgTie( (branch + "/gs-rate-of-climb").c_str(),
220 this, &FGNavRadio::get_nav_gs_rate_of_climb );
222 fgTie( (branch + "/gs-needle-deflection").c_str(),
223 this, &FGNavRadio::get_nav_gs_deflection );
225 fgTie( (branch + "/gs-distance").c_str(),
226 this, &FGNavRadio::get_nav_gs_dist_signed );
228 fgTie( (branch + "/nav-distance").c_str(),
229 this, &FGNavRadio::get_nav_loc_dist );
231 fgTie( (branch + "/nav-id").c_str(),
232 this, &FGNavRadio::get_nav_id );
234 // put nav_id characters into seperate properties for instrument displays
235 fgTie( (branch + "/nav-id_asc1").c_str(),
236 this, &FGNavRadio::get_nav_id_c1 );
238 fgTie( (branch + "/nav-id_asc2").c_str(),
239 this, &FGNavRadio::get_nav_id_c2 );
241 fgTie( (branch + "/nav-id_asc3").c_str(),
242 this, &FGNavRadio::get_nav_id_c3 );
244 fgTie( (branch + "/nav-id_asc4").c_str(),
245 this, &FGNavRadio::get_nav_id_c4 );
252 FGNavRadio::unbind ()
254 std::ostringstream temp;
257 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
259 fgUntie( (branch + "/frequencies/selected-mhz").c_str() );
260 fgUntie( (branch + "/frequencies/standby-mhz").c_str() );
261 fgUntie( (branch + "/radials/actual-deg").c_str() );
262 fgUntie( (branch + "/radials/selected-deg").c_str() );
263 fgUntie( (branch + "/ident").c_str() );
264 fgUntie( (branch + "/to-flag").c_str() );
265 fgUntie( (branch + "/from-flag").c_str() );
266 fgUntie( (branch + "/in-range").c_str() );
267 fgUntie( (branch + "/heading-needle-deflection").c_str() );
268 fgUntie( (branch + "/gs-needle-deflection").c_str() );
272 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
273 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
274 double nominalRange )
276 // extend out actual usable range to be 1.3x the published safe range
277 const double usability_factor = 1.3;
279 // assumptions we model the standard service volume, plus
280 // ... rather than specifying a cylinder, we model a cone that
281 // contains the cylinder. Then we put an upside down cone on top
282 // to model diminishing returns at too-high altitudes.
284 // altitude difference
285 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
286 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
287 // << " station elev = " << stationElev << endl;
289 if ( nominalRange < 25.0 + SG_EPSILON ) {
290 // Standard Terminal Service Volume
291 return term_tbl->interpolate( alt ) * usability_factor;
292 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
293 // Standard Low Altitude Service Volume
294 // table is based on range of 40, scale to actual range
295 return low_tbl->interpolate( alt ) * nominalRange / 40.0
298 // Standard High Altitude Service Volume
299 // table is based on range of 130, scale to actual range
300 return high_tbl->interpolate( alt ) * nominalRange / 130.0
306 // model standard ILS service volumes as per AIM 1-1-9
307 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
308 double offsetDegrees, double distance )
310 // assumptions we model the standard service volume, plus
312 // altitude difference
313 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
314 // double offset = fabs( offsetDegrees );
316 // if ( offset < 10 ) {
317 // return FG_ILS_DEFAULT_RANGE;
318 // } else if ( offset < 35 ) {
319 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
320 // } else if ( offset < 45 ) {
321 // return (45 - offset);
322 // } else if ( offset > 170 ) {
323 // return FG_ILS_DEFAULT_RANGE;
324 // } else if ( offset > 145 ) {
325 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
326 // } else if ( offset > 135 ) {
327 // return (offset - 135);
331 return FG_LOC_DEFAULT_RANGE;
335 // Update the various nav values based on position and valid tuned in navs
337 FGNavRadio::update(double dt)
339 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
340 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
341 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
345 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
349 // On timeout, scan again
350 _time_before_search_sec -= dt;
351 if ( _time_before_search_sec < 0 ) {
355 ////////////////////////////////////////////////////////////////////////
357 ////////////////////////////////////////////////////////////////////////
359 // cout << "nav_valid = " << nav_valid
360 // << " power_btn = " << power_btn
361 // << " bus_power = " << bus_power->getDoubleValue()
362 // << " nav_serviceable = " << nav_serviceable->getBoolValue()
365 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
366 && nav_serviceable->getBoolValue() )
368 station = Point3D( nav_x, nav_y, nav_z );
369 nav_loc_dist = aircraft.distance3D( station );
372 // find closest distance to the gs base line
374 sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
376 sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
377 double dist = sgdClosestPointToLineDistSquared( p, p0,
379 nav_gs_dist = sqrt( dist );
380 // cout << "nav_gs_dist = " << nav_gs_dist << endl;
382 Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
383 // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
385 // wgs84 heading to glide slope (to determine sign of distance)
386 geo_inverse_wgs_84( elev,
387 lat * SGD_RADIANS_TO_DEGREES,
388 lon * SGD_RADIANS_TO_DEGREES,
389 nav_gslat, nav_gslon,
391 double r = az1 - nav_target_radial;
392 while ( r > 180.0 ) { r -= 360.0;}
393 while ( r < -180.0 ) { r += 360.0;}
394 if ( r >= -90.0 && r <= 90.0 ) {
395 nav_gs_dist_signed = nav_gs_dist;
397 nav_gs_dist_signed = -nav_gs_dist;
399 /* cout << "Target Radial = " << nav_target_radial
400 << " Bearing = " << az1
401 << " dist (signed) = " << nav_gs_dist_signed
408 // wgs84 heading to localizer
409 geo_inverse_wgs_84( elev,
410 lat * SGD_RADIANS_TO_DEGREES,
411 lon * SGD_RADIANS_TO_DEGREES,
412 nav_loclat, nav_loclon,
413 &nav_heading, &az2, &s );
414 // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
415 nav_radial = az2 - nav_twist;
416 // cout << " heading = " << nav_heading
417 // << " dist = " << nav_dist << endl;
420 double offset = nav_radial - nav_target_radial;
421 while ( offset < -180.0 ) { offset += 360.0; }
422 while ( offset > 180.0 ) { offset -= 360.0; }
423 // cout << "ils offset = " << offset << endl;
425 = adjustILSRange( nav_elev, elev, offset,
426 nav_loc_dist * SG_METER_TO_NM );
428 nav_effective_range = adjustNavRange( nav_elev, elev, nav_range );
430 // cout << "nav range = " << nav_effective_range
431 // << " (" << nav_range << ")" << endl;
433 if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
435 } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
436 nav_inrange = sg_random() <
437 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
438 (nav_effective_range * SG_NM_TO_METER);
444 nav_target_radial = nav_sel_radial;
447 // Calculate some values for the nav/ils hold autopilot
449 double cur_radial = get_nav_reciprocal_radial();
451 // ILS localizers radials are already "true" in our
454 cur_radial += nav_twist;
456 if ( get_nav_from_flag() ) {
458 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
463 // determine the target radial in "true" heading
464 nav_target_radial_true = nav_target_radial;
466 // ILS localizers radials are already "true" in our
469 // VOR radials need to have that vor's offset added in
470 nav_target_radial_true += nav_twist;
473 while ( nav_target_radial_true < 0.0 ) {
474 nav_target_radial_true += 360.0;
476 while ( nav_target_radial_true > 360.0 ) {
477 nav_target_radial_true -= 360.0;
480 // determine the heading adjustment needed.
481 // over 8km scale by 3.0
482 // (3 is chosen because max deflection is 10
483 // and 30 is clamped angle to radial)
484 // under 8km scale by 10.0
485 // because the overstated error helps drive it to the radial in a
486 // moderate cross wind.
487 double adjustment = 0.0;
488 if (nav_loc_dist > 8000) {
489 adjustment = get_nav_cdi_deflection() * 3.0;
491 adjustment = get_nav_cdi_deflection() * 10.0;
493 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
495 // determine the target heading to fly to intercept the
497 nav_target_auto_hdg = nav_target_radial_true + adjustment;
498 while ( nav_target_auto_hdg < 0.0 ) { nav_target_auto_hdg += 360.0; }
499 while ( nav_target_auto_hdg > 360.0 ) { nav_target_auto_hdg -= 360.0; }
504 // Calculate desired rate of climb for intercepting the GS
505 double x = nav_gs_dist;
506 double y = (alt_node->getDoubleValue() - nav_elev)
508 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
510 double target_angle = nav_target_gs;
511 double gs_diff = target_angle - current_angle;
513 // convert desired vertical path angle into a climb rate
514 double des_angle = current_angle - 10 * gs_diff;
516 // estimate horizontal speed towards ILS in meters per minute
517 double dist = last_x - x;
521 double new_vel = ( dist / dt );
523 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
524 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
525 // * SG_FEET_TO_METER * 60.0;
526 // double horiz_vel = airspeed_node->getFloatValue()
527 // * SG_FEET_TO_METER * 60.0;
529 nav_gs_rate_of_climb = -sin( des_angle * SGD_DEGREES_TO_RADIANS )
530 * horiz_vel * SG_METER_TO_FEET;
534 // cout << "not picking up vor. :-(" << endl;
537 if ( nav_valid && nav_inrange && nav_serviceable->getBoolValue() ) {
538 // play station ident via audio system if on + ident,
539 // otherwise turn it off
540 if ( power_btn && (bus_power->getDoubleValue() > 1.0)
541 && nav_ident_btn && audio_btn )
543 SGSoundSample *sound;
544 sound = globals->get_soundmgr()->find( nav_fx_name );
545 if ( sound != NULL ) {
546 sound->set_volume( nav_vol_btn );
548 SG_LOG( SG_COCKPIT, SG_ALERT,
549 "Can't find nav-vor-ident sound" );
551 sound = globals->get_soundmgr()->find( dme_fx_name );
552 if ( sound != NULL ) {
553 sound->set_volume( nav_vol_btn );
555 SG_LOG( SG_COCKPIT, SG_ALERT,
556 "Can't find nav-dme-ident sound" );
558 // cout << "nav_last_time = " << nav_last_time << " ";
559 // cout << "cur_time = "
560 // << globals->get_time_params()->get_cur_time();
562 globals->get_time_params()->get_cur_time() - 30 ) {
563 nav_last_time = globals->get_time_params()->get_cur_time();
566 // cout << " nav_play_count = " << nav_play_count << endl;
567 // cout << "playing = "
568 // << globals->get_soundmgr()->is_playing(nav_fx_name)
570 if ( nav_play_count < 4 ) {
572 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
573 globals->get_soundmgr()->play_once( nav_fx_name );
576 } else if ( nav_play_count < 5 && nav_has_dme ) {
578 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
579 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
580 globals->get_soundmgr()->play_once( dme_fx_name );
585 globals->get_soundmgr()->stop( nav_fx_name );
586 globals->get_soundmgr()->stop( dme_fx_name );
592 // Update current nav/adf radio stations based on current postition
593 void FGNavRadio::search()
597 _time_before_search_sec = 1.0;
599 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
600 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
601 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
603 FGNavRecord *nav = NULL;
604 FGNavRecord *loc = NULL;
605 FGNavRecord *dme = NULL;
606 FGNavRecord *gs = NULL;
608 ////////////////////////////////////////////////////////////////////////
610 ////////////////////////////////////////////////////////////////////////
612 nav = globals->get_navlist()->findByFreq(nav_freq, lon, lat, elev);
613 dme = globals->get_dmelist()->findByFreq(nav_freq, lon, lat, elev);
615 loc = globals->get_loclist()->findByFreq(nav_freq, lon, lat, elev);
616 gs = globals->get_gslist()->findByFreq(nav_freq, lon, lat, elev);
620 nav_id = loc->get_ident();
621 // cout << "localizer = " << nav_id << endl;
623 if ( last_nav_id != nav_id || last_nav_vor ) {
624 nav_trans_ident = loc->get_trans_ident();
625 nav_target_radial = loc->get_multiuse();
626 while ( nav_target_radial < 0.0 ) { nav_target_radial += 360.0; }
627 while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
628 nav_loclon = loc->get_lon();
629 nav_loclat = loc->get_lat();
630 nav_x = loc->get_x();
631 nav_y = loc->get_y();
632 nav_z = loc->get_z();
633 last_nav_id = nav_id;
634 last_nav_vor = false;
636 nav_has_dme = (dme != NULL);
637 nav_has_gs = (gs != NULL);
639 nav_gslon = gs->get_lon();
640 nav_gslat = gs->get_lat();
641 nav_elev = gs->get_elev_ft();
642 int tmp = (int)(gs->get_multiuse() / 1000.0);
643 nav_target_gs = (double)tmp / 100.0;
644 nav_gs_x = gs->get_x();
645 nav_gs_y = gs->get_y();
646 nav_gs_z = gs->get_z();
648 // derive GS baseline (perpendicular to the runay
650 double tlon, tlat, taz;
651 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
652 nav_target_radial + 90,
653 100.0, &tlat, &tlon, &taz );
654 // cout << "nav_target_radial = " << nav_target_radial << endl;
655 // cout << "nav_loc = " << nav_loc << endl;
656 // cout << nav_gslon << "," << nav_gslat << " "
657 // << tlon << "," << tlat << " (" << nav_elev << ")"
659 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
660 tlat*SGD_DEGREES_TO_RADIANS,
661 nav_elev*SG_FEET_TO_METER)
663 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
665 // cout << p1 << endl;
666 sgdSetVec3( gs_base_vec,
667 p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
668 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
669 // << gs_base_vec[2] << endl;
671 nav_elev = loc->get_elev_ft();
674 nav_range = FG_LOC_DEFAULT_RANGE;
675 nav_effective_range = nav_range;
677 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
678 globals->get_soundmgr()->remove( nav_fx_name );
680 SGSoundSample *sound;
681 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
682 sound->set_volume( 0.3 );
683 globals->get_soundmgr()->add( sound, nav_fx_name );
685 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
686 globals->get_soundmgr()->remove( dme_fx_name );
688 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
689 sound->set_volume( 0.3 );
690 globals->get_soundmgr()->add( sound, dme_fx_name );
692 int offset = (int)(sg_random() * 30.0);
693 nav_play_count = offset / 4;
694 nav_last_time = globals->get_time_params()->get_cur_time() -
696 // cout << "offset = " << offset << " play_count = "
698 // << " nav_last_time = " << nav_last_time
699 // << " current time = "
700 // << globals->get_time_params()->get_cur_time() << endl;
702 // cout << "Found an loc station in range" << endl;
703 // cout << " id = " << loc->get_locident() << endl;
705 } else if ( nav != NULL ) {
706 nav_id = nav->get_ident();
707 // cout << "nav = " << nav_id << endl;
709 if ( last_nav_id != nav_id || !last_nav_vor ) {
710 last_nav_id = nav_id;
712 nav_trans_ident = nav->get_trans_ident();
714 nav_has_dme = (dme != NULL);
716 nav_loclon = nav->get_lon();
717 nav_loclat = nav->get_lat();
718 nav_elev = nav->get_elev_ft();
719 nav_twist = nav->get_multiuse();
720 nav_range = nav->get_range();
721 nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
723 nav_target_radial = nav_sel_radial;
724 nav_x = nav->get_x();
725 nav_y = nav->get_y();
726 nav_z = nav->get_z();
728 if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
729 globals->get_soundmgr()->remove( nav_fx_name );
731 SGSoundSample *sound;
732 sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
733 sound->set_volume( 0.3 );
734 if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
735 // cout << "Added nav-vor-ident sound" << endl;
737 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
740 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
741 globals->get_soundmgr()->remove( dme_fx_name );
743 sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
744 sound->set_volume( 0.3 );
745 globals->get_soundmgr()->add( sound, dme_fx_name );
747 int offset = (int)(sg_random() * 30.0);
748 nav_play_count = offset / 4;
749 nav_last_time = globals->get_time_params()->get_cur_time() -
751 // cout << "offset = " << offset << " play_count = "
752 // << nav_play_count << " nav_last_time = "
753 // << nav_last_time << " current time = "
754 // << globals->get_time_params()->get_cur_time() << endl;
756 // cout << "Found a vor station in range" << endl;
757 // cout << " id = " << nav->get_ident() << endl;
762 nav_target_radial = 0;
763 nav_trans_ident = "";
765 if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
766 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
768 globals->get_soundmgr()->remove( dme_fx_name );
769 // cout << "not picking up vor1. :-(" << endl;
774 // return the amount of heading needle deflection, returns a value
775 // clamped to the range of ( -10 , 10 )
776 double FGNavRadio::get_nav_cdi_deflection() const {
780 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
782 r = nav_radial - nav_target_radial;
783 // cout << "Target radial = " << nav_target_radial
784 // << " Actual radial = " << nav_radial << endl;
786 while ( r > 180.0 ) { r -= 360.0;}
787 while ( r < -180.0 ) { r += 360.0;}
788 if ( fabs(r) > 90.0 )
789 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
791 // According to Robin Peel, the ILS is 4x more sensitive than a vor
792 r = -r; // reverse, since radial is outbound
793 if ( nav_loc ) { r *= 4.0; }
794 if ( r < -10.0 ) { r = -10.0; }
795 if ( r > 10.0 ) { r = 10.0; }
803 // return the amount of cross track distance error, returns a meters
804 double FGNavRadio::get_nav_cdi_xtrack_error() const {
808 && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
810 r = nav_radial - nav_target_radial;
811 // cout << "Target radial = " << nav_target_radial
812 // << " Actual radial = " << nav_radial
813 // << " r = " << r << endl;
815 while ( r > 180.0 ) { r -= 360.0;}
816 while ( r < -180.0 ) { r += 360.0;}
817 if ( fabs(r) > 90.0 )
818 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
820 r = -r; // reverse, since radial is outbound
822 m = nav_loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
831 // return the amount of glide slope needle deflection (.i.e. the
832 // number of degrees we are off the glide slope * 5.0
833 double FGNavRadio::get_nav_gs_deflection() const {
834 if ( nav_inrange && nav_has_gs
835 && nav_serviceable->getBoolValue() && gs_serviceable->getBoolValue() )
837 double x = nav_gs_dist;
838 double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
840 // cout << "dist = " << x << " height = " << y << endl;
841 double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
842 return (nav_target_gs - angle) * 5.0;
850 * Return true if the NAV TO flag should be active.
853 FGNavRadio::get_nav_to_flag () const
856 && nav_serviceable->getBoolValue()
857 && tofrom_serviceable->getBoolValue() )
859 double offset = fabs(nav_radial - nav_target_radial);
863 return !(offset <= 90.0 || offset >= 270.0);
872 * Return true if the NAV FROM flag should be active.
875 FGNavRadio::get_nav_from_flag () const
878 && nav_serviceable->getBoolValue()
879 && tofrom_serviceable->getBoolValue() ) {
880 double offset = fabs(nav_radial - nav_target_radial);
884 return !(offset > 90.0 && offset < 270.0);
893 * Return the true heading to station
896 FGNavRadio::get_nav_heading () const
903 * Return the current radial.
906 FGNavRadio::get_nav_radial () const
912 FGNavRadio::get_nav_reciprocal_radial () const
914 double recip = nav_radial + 180;
915 if ( recip >= 360 ) {