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),
72 _time_before_search_sec(0.0)
74 SGPath path( globals->get_fg_root() );
76 term.append( "Navaids/range.term" );
78 low.append( "Navaids/range.low" );
80 high.append( "Navaids/range.high" );
82 term_tbl = new SGInterpTable( term.str() );
83 low_tbl = new SGInterpTable( low.str() );
84 high_tbl = new SGInterpTable( high.str() );
87 for ( i = 0; i < node->nChildren(); ++i ) {
88 SGPropertyNode *child = node->getChild(i);
89 string cname = child->getName();
90 string cval = child->getStringValue();
91 if ( cname == "name" ) {
93 } else if ( cname == "number" ) {
94 num = child->getIntValue();
96 SG_LOG( SG_INSTR, SG_WARN,
97 "Error in nav radio config logic" );
98 if ( name.length() ) {
99 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
108 FGNavRadio::~FGNavRadio()
122 branch = "/instrumentation/" + name;
124 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
127 fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
128 nav_serviceable = node->getChild("serviceable", 0, true);
129 cdi_serviceable = (node->getChild("cdi", 0, true))
130 ->getChild("serviceable", 0, true);
131 gs_serviceable = (node->getChild("gs", 0, true))
132 ->getChild("serviceable");
133 tofrom_serviceable = (node->getChild("to-from", 0, true))
134 ->getChild("serviceable", 0, true);
136 std::ostringstream temp;
137 temp << name << "nav-ident" << num;
138 nav_fx_name = temp.str();
139 temp << name << "dme-ident" << num;
140 dme_fx_name = temp.str();
146 std::ostringstream temp;
149 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
152 fgTie( (branch + "power-btn").c_str(), this,
153 &FGNavRadio::get_power_btn, &FGNavRadio::set_power_btn );
154 fgSetArchivable( (branch + "power-btn").c_str() );
156 fgTie( (branch + "/frequencies/selected-mhz").c_str() , this,
157 &FGNavRadio::get_nav_freq, &FGNavRadio::set_nav_freq );
158 fgSetArchivable( (branch + "/frequencies/selected-mhz").c_str() );
160 fgTie( (branch + "/frequencies/standby-mhz").c_str() , this,
161 &FGNavRadio::get_nav_alt_freq, &FGNavRadio::set_nav_alt_freq);
162 fgSetArchivable( (branch + "/frequencies/standby-mhz").c_str() );
164 fgTie( (branch + "/radials/selected-deg").c_str() , this,
165 &FGNavRadio::get_nav_sel_radial, &FGNavRadio::set_nav_sel_radial );
166 fgSetArchivable((branch + "/radials/selected-deg").c_str() );
168 fgTie( (branch + "/volume").c_str() , this,
169 &FGNavRadio::get_nav_vol_btn, &FGNavRadio::set_nav_vol_btn );
170 fgSetArchivable( (branch + "/volume").c_str() );
172 fgTie( (branch + "/ident").c_str(), this,
173 &FGNavRadio::get_nav_ident_btn, &FGNavRadio::set_nav_ident_btn );
174 fgSetArchivable( (branch + "/ident").c_str() );
177 fgTie( (branch + "/audio-btn").c_str(), this,
178 &FGNavRadio::get_audio_btn, &FGNavRadio::set_audio_btn );
179 fgSetArchivable( (branch + "/audio-btn").c_str() );
181 fgTie( (branch + "/heading-deg").c_str(),
182 this, &FGNavRadio::get_nav_heading );
184 fgTie( (branch + "/radials/actual-deg").c_str(),
185 this, &FGNavRadio::get_nav_radial );
187 fgTie( (branch + "/radials/target-radial-deg").c_str(),
188 this, &FGNavRadio::get_nav_target_radial_true );
190 fgTie( (branch + "/radials/reciprocal-radial-deg").c_str(),
191 this, &FGNavRadio::get_nav_reciprocal_radial );
193 fgTie( (branch + "/radials/target-auto-hdg-deg").c_str(),
194 this, &FGNavRadio::get_nav_target_auto_hdg );
196 fgTie( (branch + "/to-flag").c_str(),
197 this, &FGNavRadio::get_nav_to_flag );
199 fgTie( (branch + "/from-flag").c_str(),
200 this, &FGNavRadio::get_nav_from_flag );
202 fgTie( (branch + "/in-range").c_str(),
203 this, &FGNavRadio::get_nav_inrange );
205 fgTie( (branch + "/heading-needle-deflection").c_str(),
206 this, &FGNavRadio::get_nav_cdi_deflection );
208 fgTie( (branch + "/crosstrack-error-m").c_str(),
209 this, &FGNavRadio::get_nav_cdi_xtrack_error );
211 fgTie( (branch + "/has-gs").c_str(),
212 this, &FGNavRadio::get_nav_has_gs );
214 fgTie( (branch + "/nav-loc").c_str(),
215 this, &FGNavRadio::get_nav_loc );
217 fgTie( (branch + "/gs-rate-of-climb").c_str(),
218 this, &FGNavRadio::get_nav_gs_rate_of_climb );
220 fgTie( (branch + "/gs-needle-deflection").c_str(),
221 this, &FGNavRadio::get_nav_gs_deflection );
223 fgTie( (branch + "/gs-distance").c_str(),
224 this, &FGNavRadio::get_nav_gs_dist_signed );
226 fgTie( (branch + "/nav-distance").c_str(),
227 this, &FGNavRadio::get_nav_loc_dist );
229 fgTie( (branch + "/nav-id").c_str(),
230 this, &FGNavRadio::get_nav_id );
232 // put nav_id characters into seperate properties for instrument displays
233 fgTie( (branch + "/nav-id_asc1").c_str(),
234 this, &FGNavRadio::get_nav_id_c1 );
236 fgTie( (branch + "/nav-id_asc2").c_str(),
237 this, &FGNavRadio::get_nav_id_c2 );
239 fgTie( (branch + "/nav-id_asc3").c_str(),
240 this, &FGNavRadio::get_nav_id_c3 );
242 fgTie( (branch + "/nav-id_asc4").c_str(),
243 this, &FGNavRadio::get_nav_id_c4 );
250 FGNavRadio::unbind ()
252 std::ostringstream temp;
255 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
257 fgUntie( (branch + "/frequencies/selected-mhz").c_str() );
258 fgUntie( (branch + "/frequencies/standby-mhz").c_str() );
259 fgUntie( (branch + "/radials/actual-deg").c_str() );
260 fgUntie( (branch + "/radials/selected-deg").c_str() );
261 fgUntie( (branch + "/ident").c_str() );
262 fgUntie( (branch + "/to-flag").c_str() );
263 fgUntie( (branch + "/from-flag").c_str() );
264 fgUntie( (branch + "/in-range").c_str() );
265 fgUntie( (branch + "/heading-needle-deflection").c_str() );
266 fgUntie( (branch + "/gs-needle-deflection").c_str() );
270 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
271 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
272 double nominalRange )
274 // extend out actual usable range to be 1.3x the published safe range
275 const double usability_factor = 1.3;
277 // assumptions we model the standard service volume, plus
278 // ... rather than specifying a cylinder, we model a cone that
279 // contains the cylinder. Then we put an upside down cone on top
280 // to model diminishing returns at too-high altitudes.
282 // altitude difference
283 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
284 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
285 // << " station elev = " << stationElev << endl;
287 if ( nominalRange < 25.0 + SG_EPSILON ) {
288 // Standard Terminal Service Volume
289 return term_tbl->interpolate( alt ) * usability_factor;
290 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
291 // Standard Low Altitude Service Volume
292 // table is based on range of 40, scale to actual range
293 return low_tbl->interpolate( alt ) * nominalRange / 40.0
296 // Standard High Altitude Service Volume
297 // table is based on range of 130, scale to actual range
298 return high_tbl->interpolate( alt ) * nominalRange / 130.0
304 // model standard ILS service volumes as per AIM 1-1-9
305 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
306 double offsetDegrees, double distance )
308 // assumptions we model the standard service volume, plus
310 // altitude difference
311 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
312 // double offset = fabs( offsetDegrees );
314 // if ( offset < 10 ) {
315 // return FG_ILS_DEFAULT_RANGE;
316 // } else if ( offset < 35 ) {
317 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
318 // } else if ( offset < 45 ) {
319 // return (45 - offset);
320 // } else if ( offset > 170 ) {
321 // return FG_ILS_DEFAULT_RANGE;
322 // } else if ( offset > 145 ) {
323 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
324 // } else if ( offset > 135 ) {
325 // return (offset - 135);
329 return FG_LOC_DEFAULT_RANGE;
333 // Update the various nav values based on position and valid tuned in navs
335 FGNavRadio::update(double dt)
337 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
338 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
339 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
343 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
347 // On timeout, scan again
348 _time_before_search_sec -= dt;
349 if ( _time_before_search_sec < 0 ) {
353 ////////////////////////////////////////////////////////////////////////
355 ////////////////////////////////////////////////////////////////////////
357 // cout << "nav_valid = " << nav_valid
358 // << " power_btn = " << power_btn
359 // << " bus_power = " << bus_power->getDoubleValue()
360 // << " nav_serviceable = " << nav_serviceable->getBoolValue()
363 if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
364 && nav_serviceable->getBoolValue() )
366 station = Point3D( nav_x, nav_y, nav_z );
367 nav_loc_dist = aircraft.distance3D( station );
368 // cout << "station = " << station << " dist = " << nav_loc_dist
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 ) {