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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "navradio.hxx"
32 #include <simgear/sg_inlines.h>
33 #include <simgear/timing/sg_time.hxx>
34 #include <simgear/math/vector.hxx>
35 #include <simgear/math/sg_random.h>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/math/interpolater.hxx>
41 #include <Navaids/navrecord.hxx>
43 #include <Airports/runways.hxx>
44 #include <Navaids/navlist.hxx>
45 #include <Main/util.hxx>
50 // General-purpose sawtooth function. Graph looks like this:
53 // Odd symmetry, inversion symmetry about the origin.
54 // Unit slope at the origin.
55 // Max 1, min -1, period 4.
56 // Two zero-crossings per period, one with + slope, one with - slope.
57 // Useful for false localizer courses.
58 static double sawtooth(double xx)
60 return 4.0 * fabs(xx/4.0 + 0.25 - floor(xx/4.0 + 0.75)) - 1.0;
63 // Calculate a unit vector in the horizontal tangent plane
64 // starting at the given "tail" of the vector and going off
65 // with the given heading.
66 static SGVec3d tangentVector(const SGGeod& tail, const SGVec3d& tail_xyz,
69 // The fudge factor here is presumably intended to improve
70 // numerical stability. I don't know if it is necessary.
71 // It gets divided out later.
74 double az2; // ignored
75 SGGeodesy::direct(tail, heading, fudge, head, az2);
76 head.setElevationM(tail.getElevationM());
77 SGVec3d head_xyz = SGVec3d::fromGeod(head);
78 return (head_xyz - tail_xyz) * (1.0/fudge);
81 // Create a "serviceable" node with a default value of "true"
82 SGPropertyNode_ptr createServiceableProp(SGPropertyNode* aParent, const char* aName)
84 SGPropertyNode_ptr n = (aParent->getChild(aName, 0, true)->getChild("serviceable", 0, true));
85 simgear::props::Type typ = n->getType();
86 if ((typ == simgear::props::NONE) || (typ == simgear::props::UNSPECIFIED)) {
87 n->setBoolValue(true);
93 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
94 lon_node(fgGetNode("/position/longitude-deg", true)),
95 lat_node(fgGetNode("/position/latitude-deg", true)),
96 alt_node(fgGetNode("/position/altitude-ft", true)),
101 sel_radial_node(NULL),
103 ident_btn_node(NULL),
104 audio_btn_node(NULL),
105 backcourse_node(NULL),
106 nav_serviceable_node(NULL),
107 cdi_serviceable_node(NULL),
108 gs_serviceable_node(NULL),
109 tofrom_serviceable_node(NULL),
110 dme_serviceable_node(NULL),
112 fmt_alt_freq_node(NULL),
115 recip_radial_node(NULL),
116 target_radial_true_node(NULL),
117 target_auto_hdg_node(NULL),
118 time_to_intercept(NULL),
120 from_flag_node(NULL),
122 signal_quality_norm_node(NULL),
123 cdi_deflection_node(NULL),
124 cdi_deflection_norm_node(NULL),
125 cdi_xtrack_error_node(NULL),
126 cdi_xtrack_hdg_err_node(NULL),
130 gs_deflection_node(NULL),
131 gs_deflection_deg_node(NULL),
132 gs_deflection_norm_node(NULL),
133 gs_rate_of_climb_node(NULL),
135 gs_inrange_node(NULL),
141 nav_slaved_to_gps_node(NULL),
142 gps_cdi_deflection_node(NULL),
143 gps_to_flag_node(NULL),
144 gps_from_flag_node(NULL),
145 gps_has_gs_node(NULL),
152 last_xtrack_error(0.0),
153 _localizerWidth(5.0),
154 _name(node->getStringValue("name", "nav")),
155 _num(node->getIntValue("number", 0)),
156 _time_before_search_sec(-1.0),
157 _falseCoursesEnabled(true),
160 SGPath path( globals->get_fg_root() );
162 term.append( "Navaids/range.term" );
164 low.append( "Navaids/range.low" );
166 high.append( "Navaids/range.high" );
168 term_tbl = new SGInterpTable( term.str() );
169 low_tbl = new SGInterpTable( low.str() );
170 high_tbl = new SGInterpTable( high.str() );
175 FGNavRadio::~FGNavRadio()
186 SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
187 _sgr = smgr->find("avionics", true);
192 branch = "/instrumentation/" + _name;
194 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
197 fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
200 is_valid_node = node->getChild("data-is-valid", 0, true);
201 power_btn_node = node->getChild("power-btn", 0, true);
202 power_btn_node->setBoolValue( true );
203 vol_btn_node = node->getChild("volume", 0, true);
204 ident_btn_node = node->getChild("ident", 0, true);
205 ident_btn_node->setBoolValue( true );
206 audio_btn_node = node->getChild("audio-btn", 0, true);
207 audio_btn_node->setBoolValue( true );
208 backcourse_node = node->getChild("back-course-btn", 0, true);
209 backcourse_node->setBoolValue( false );
211 nav_serviceable_node = node->getChild("serviceable", 0, true);
212 cdi_serviceable_node = createServiceableProp(node, "cdi");
213 gs_serviceable_node = createServiceableProp(node, "gs");
214 tofrom_serviceable_node = createServiceableProp(node, "to-from");
215 dme_serviceable_node = createServiceableProp(node, "dme");
217 globals->get_props()->tie("sim/realism/false-radio-courses-enabled",
218 SGRawValuePointer<bool>(&_falseCoursesEnabled));
221 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
222 freq_node = subnode->getChild("selected-mhz", 0, true);
223 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
224 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
225 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
228 subnode = node->getChild("radials", 0, true);
229 sel_radial_node = subnode->getChild("selected-deg", 0, true);
230 radial_node = subnode->getChild("actual-deg", 0, true);
231 recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
232 target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
233 target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
236 heading_node = node->getChild("heading-deg", 0, true);
237 time_to_intercept = node->getChild("time-to-intercept-sec", 0, true);
238 to_flag_node = node->getChild("to-flag", 0, true);
239 from_flag_node = node->getChild("from-flag", 0, true);
240 inrange_node = node->getChild("in-range", 0, true);
241 signal_quality_norm_node = node->getChild("signal-quality-norm", 0, true);
242 cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
243 cdi_deflection_norm_node = node->getChild("heading-needle-deflection-norm", 0, true);
244 cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
245 cdi_xtrack_hdg_err_node
246 = node->getChild("crosstrack-heading-error-deg", 0, true);
247 has_gs_node = node->getChild("has-gs", 0, true);
248 loc_node = node->getChild("nav-loc", 0, true);
249 loc_dist_node = node->getChild("nav-distance", 0, true);
250 gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
251 gs_deflection_deg_node = node->getChild("gs-needle-deflection-deg", 0, true);
252 gs_deflection_norm_node = node->getChild("gs-needle-deflection-norm", 0, true);
253 gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
254 gs_dist_node = node->getChild("gs-distance", 0, true);
255 gs_inrange_node = node->getChild("gs-in-range", 0, true);
257 nav_id_node = node->getChild("nav-id", 0, true);
258 id_c1_node = node->getChild("nav-id_asc1", 0, true);
259 id_c2_node = node->getChild("nav-id_asc2", 0, true);
260 id_c3_node = node->getChild("nav-id_asc3", 0, true);
261 id_c4_node = node->getChild("nav-id_asc4", 0, true);
263 node->tie("dme-in-range", SGRawValuePointer<bool>(&_dmeInRange));
265 // gps slaving support
266 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
267 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
268 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
269 gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
270 gps_has_gs_node = fgGetNode("/instrumentation/gps/has-gs", true);
272 std::ostringstream temp;
273 temp << _name << "nav-ident" << _num;
274 nav_fx_name = temp.str();
275 temp << _name << "dme-ident" << _num;
276 dme_fx_name = temp.str();
287 FGNavRadio::unbind ()
292 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
293 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
294 double nominalRange )
296 if (nominalRange <= 0.0) {
297 nominalRange = FG_NAV_DEFAULT_RANGE;
300 // extend out actual usable range to be 1.3x the published safe range
301 const double usability_factor = 1.3;
303 // assumptions we model the standard service volume, plus
304 // ... rather than specifying a cylinder, we model a cone that
305 // contains the cylinder. Then we put an upside down cone on top
306 // to model diminishing returns at too-high altitudes.
308 // altitude difference
309 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
310 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
311 // << " station elev = " << stationElev << endl;
313 if ( nominalRange < 25.0 + SG_EPSILON ) {
314 // Standard Terminal Service Volume
315 return term_tbl->interpolate( alt ) * usability_factor;
316 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
317 // Standard Low Altitude Service Volume
318 // table is based on range of 40, scale to actual range
319 return low_tbl->interpolate( alt ) * nominalRange / 40.0
322 // Standard High Altitude Service Volume
323 // table is based on range of 130, scale to actual range
324 return high_tbl->interpolate( alt ) * nominalRange / 130.0
330 // model standard ILS service volumes as per AIM 1-1-9
331 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
332 double offsetDegrees, double distance )
334 // assumptions we model the standard service volume, plus
336 // altitude difference
337 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
338 // double offset = fabs( offsetDegrees );
340 // if ( offset < 10 ) {
341 // return FG_ILS_DEFAULT_RANGE;
342 // } else if ( offset < 35 ) {
343 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
344 // } else if ( offset < 45 ) {
345 // return (45 - offset);
346 // } else if ( offset > 170 ) {
347 // return FG_ILS_DEFAULT_RANGE;
348 // } else if ( offset > 145 ) {
349 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
350 // } else if ( offset > 135 ) {
351 // return (offset - 135);
355 return FG_LOC_DEFAULT_RANGE;
359 //////////////////////////////////////////////////////////////////////////
360 // Update the various nav values based on position and valid tuned in navs
361 //////////////////////////////////////////////////////////////////////////
363 FGNavRadio::update(double dt)
369 // Create "formatted" versions of the nav frequencies for
370 // instrument displays.
372 sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
373 fmt_freq_node->setStringValue(tmp);
374 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
375 fmt_alt_freq_node->setStringValue(tmp);
377 if (power_btn_node->getBoolValue()
378 && (bus_power_node->getDoubleValue() > 1.0)
379 && nav_serviceable_node->getBoolValue() )
381 if (nav_slaved_to_gps_node->getBoolValue()) {
395 void FGNavRadio::clearOutputs()
397 inrange_node->setBoolValue( false );
398 cdi_deflection_node->setDoubleValue( 0.0 );
399 cdi_deflection_norm_node->setDoubleValue( 0.0 );
400 cdi_xtrack_error_node->setDoubleValue( 0.0 );
401 cdi_xtrack_hdg_err_node->setDoubleValue( 0.0 );
402 time_to_intercept->setDoubleValue( 0.0 );
403 gs_deflection_node->setDoubleValue( 0.0 );
404 gs_deflection_deg_node->setDoubleValue(0.0);
405 gs_deflection_norm_node->setDoubleValue(0.0);
406 gs_inrange_node->setBoolValue( false );
408 to_flag_node->setBoolValue( false );
409 from_flag_node->setBoolValue( false );
414 void FGNavRadio::updateReceiver(double dt)
416 // Do a nav station search only once a second to reduce
417 // unnecessary work. (Also, make sure to do this before caching
419 _time_before_search_sec -= dt;
420 if ( _time_before_search_sec < 0 ) {
425 _cdiDeflection = 0.0;
426 _cdiCrossTrackErrorM = 0.0;
427 _toFlag = _fromFlag = false;
428 _gsNeedleDeflection = 0.0;
429 _gsNeedleDeflectionNorm = 0.0;
430 inrange_node->setBoolValue(false);
434 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
435 lat_node->getDoubleValue(),
436 alt_node->getDoubleValue());
438 double nav_elev = _navaid->get_elev_ft();
439 SGVec3d aircraft = SGVec3d::fromGeod(pos);
440 double loc_dist = dist(aircraft, _navaid->cart());
441 loc_dist_node->setDoubleValue( loc_dist );
442 bool is_loc = loc_node->getBoolValue();
443 double signal_quality_norm = signal_quality_norm_node->getDoubleValue();
446 //////////////////////////////////////////////////////////
447 // compute forward and reverse wgs84 headings to localizer
448 //////////////////////////////////////////////////////////
450 SGGeodesy::inverse(pos, _navaid->geod(), hdg, az2, s);
451 heading_node->setDoubleValue(hdg);
452 double radial = az2 - twist;
453 double recip = radial + 180.0;
454 SG_NORMALIZE_RANGE(recip, 0.0, 360.0);
455 radial_node->setDoubleValue( radial );
456 recip_radial_node->setDoubleValue( recip );
458 //////////////////////////////////////////////////////////
459 // compute the target/selected radial in "true" heading
460 //////////////////////////////////////////////////////////
462 target_radial = sel_radial_node->getDoubleValue();
465 // VORs need twist (mag-var) added; ILS/LOCs don't but we set twist to 0.0
466 double trtrue = target_radial + twist;
467 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
468 target_radial_true_node->setDoubleValue( trtrue );
470 //////////////////////////////////////////////////////////
471 // adjust reception range for altitude
472 // FIXME: make sure we are using the navdata range now that
473 // it is valid in the data file
474 //////////////////////////////////////////////////////////
476 double offset = radial - target_radial;
477 SG_NORMALIZE_RANGE(offset, -180.0, 180.0);
479 = adjustILSRange( nav_elev, pos.getElevationM(), offset,
480 loc_dist * SG_METER_TO_NM );
483 = adjustNavRange( nav_elev, pos.getElevationM(), _navaid->get_range() );
486 double effective_range_m = effective_range * SG_NM_TO_METER;
488 //////////////////////////////////////////////////////////
489 // compute signal quality
490 // 100% within effective_range
491 // decreases 1/x^2 further out
492 //////////////////////////////////////////////////////////
493 double last_signal_quality_norm = signal_quality_norm;
495 if ( loc_dist < effective_range_m ) {
496 signal_quality_norm = 1.0;
498 double range_exceed_norm = loc_dist/effective_range_m;
499 signal_quality_norm = 1/(range_exceed_norm*range_exceed_norm);
502 signal_quality_norm = fgGetLowPass( last_signal_quality_norm,
503 signal_quality_norm, dt );
505 signal_quality_norm_node->setDoubleValue( signal_quality_norm );
506 bool inrange = signal_quality_norm > 0.2;
507 inrange_node->setBoolValue( inrange );
509 //////////////////////////////////////////////////////////
510 // compute to/from flag status
511 //////////////////////////////////////////////////////////
516 double offset = fabs(radial - target_radial);
517 _toFlag = (offset > 90.0 && offset < 270.0);
519 _fromFlag = !_toFlag;
521 _toFlag = _fromFlag = false;
525 double r = target_radial - radial;
526 SG_NORMALIZE_RANGE(r, -180.0, 180.0);
529 if (_falseCoursesEnabled) {
530 // The factor of 30.0 gives a period of 120 which gives us 3 cycles and six
531 // zeros i.e. six courses: one front course, one back course, and four
532 // false courses. Three of the six are reverse sensing.
533 _cdiDeflection = 30.0 * sawtooth(r / 30.0);
535 // no false courses, but we do need to create a back course
536 if (fabs(r) > 90.0) { // front course
537 _cdiDeflection = r - copysign(180.0, r);
539 _cdiDeflection = r; // back course
542 _cdiDeflection = -_cdiDeflection; // reverse for outbound radial
543 } // of false courses disabled
545 const double VOR_FULL_ARC = 20.0; // VOR is -10 .. 10 degree swing
546 _cdiDeflection *= VOR_FULL_ARC / _localizerWidth; // increased localiser sensitivity
548 if (backcourse_node->getBoolValue()) {
549 _cdiDeflection = -_cdiDeflection;
552 // handle the TO side of the VOR
553 if (fabs(r) > 90.0) {
554 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
557 } // of non-localiser case
559 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
560 _cdiDeflection *= signal_quality_norm;
562 // cross-track error (in metres)
563 _cdiCrossTrackErrorM = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
565 updateGlideSlope(dt, aircraft, signal_quality_norm);
568 last_loc_dist = loc_dist;
571 void FGNavRadio::updateGlideSlope(double dt, const SGVec3d& aircraft, double signal_quality_norm)
573 _gsNeedleDeflection = 0.0;
574 if (!_gs || !inrange_node->getBoolValue()) {
575 gs_dist_node->setDoubleValue( 0.0 );
576 gs_inrange_node->setBoolValue(false);
580 double gsDist = dist(aircraft, _gsCart);
581 gs_dist_node->setDoubleValue(gsDist);
582 bool gsInRange = (gsDist < (_gs->get_range() * SG_NM_TO_METER));
583 gs_inrange_node->setBoolValue(gsInRange);
589 SGVec3d pos = aircraft - _gsCart; // relative vector from gs antenna to aircraft
590 // The positive GS axis points along the runway in the landing direction,
591 // toward the far end, not toward the approach area, so we need a - sign here:
592 double dot_h = -dot(pos, _gsAxis);
593 double dot_v = dot(pos, _gsVertical);
594 double angle = atan2(dot_v, dot_h) * SGD_RADIANS_TO_DEGREES;
595 double deflectionAngle = target_gs - angle;
597 if (_falseCoursesEnabled) {
598 // Construct false glideslopes. The scale factor of 1.5
599 // in the sawtooth gives a period of 6 degrees.
600 // There will be zeros at 3, 6r, 9, 12r et cetera
601 // where "r" indicates reverse sensing.
602 // This is is consistent with conventional pilot lore
603 // e.g. http://www.allstar.fiu.edu/aerojava/ILS.htm
604 // but inconsistent with
605 // http://www.freepatentsonline.com/3757338.html
607 // It may be that some of each exist.
608 if (deflectionAngle < 0) {
609 deflectionAngle = 1.5 * sawtooth(deflectionAngle / 1.5);
611 // no false GS below the true GS
615 _gsNeedleDeflection = deflectionAngle * 5.0;
616 _gsNeedleDeflection *= signal_quality_norm;
618 SG_CLAMP_RANGE(deflectionAngle, -0.7, 0.7);
619 _gsNeedleDeflectionNorm = (deflectionAngle / 0.7) * signal_quality_norm;
621 //////////////////////////////////////////////////////////
622 // Calculate desired rate of climb for intercepting the GS
623 //////////////////////////////////////////////////////////
624 double gs_diff = target_gs - angle;
625 // convert desired vertical path angle into a climb rate
626 double des_angle = angle - 10 * gs_diff;
628 // estimate horizontal speed towards ILS in meters per minute
629 double elapsedDistance = last_x - gsDist;
632 double new_vel = ( elapsedDistance / dt );
633 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
635 gs_rate_of_climb_node
636 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
637 * horiz_vel * SG_METER_TO_FEET );
640 void FGNavRadio::updateDME(const SGVec3d& aircraft)
642 if (!_dme || !dme_serviceable_node->getBoolValue()) {
647 double dme_distance = dist(aircraft, _dme->cart());
648 _dmeInRange = (dme_distance < _dme->get_range() * SG_NM_TO_METER);
651 void FGNavRadio::updateGPSSlaved()
653 has_gs_node->setBoolValue(gps_has_gs_node->getBoolValue());
655 _toFlag = gps_to_flag_node->getBoolValue();
656 _fromFlag = gps_from_flag_node->getBoolValue();
658 inrange_node->setBoolValue(_toFlag | _fromFlag);
660 _cdiDeflection = gps_cdi_deflection_node->getDoubleValue();
661 // clmap to some range (+/- 10 degrees) as the regular deflection
662 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
664 _cdiCrossTrackErrorM = 0.0; // FIXME, supply this
665 _gsNeedleDeflection = 0.0; // FIXME, supply this
668 void FGNavRadio::updateCDI(double dt)
670 bool cdi_serviceable = cdi_serviceable_node->getBoolValue();
671 bool inrange = inrange_node->getBoolValue();
673 if (tofrom_serviceable_node->getBoolValue()) {
674 to_flag_node->setBoolValue(_toFlag);
675 from_flag_node->setBoolValue(_fromFlag);
677 to_flag_node->setBoolValue(false);
678 from_flag_node->setBoolValue(false);
681 if (!cdi_serviceable) {
682 _cdiDeflection = 0.0;
683 _cdiCrossTrackErrorM = 0.0;
686 cdi_deflection_node->setDoubleValue(_cdiDeflection);
687 cdi_deflection_norm_node->setDoubleValue(_cdiDeflection * 0.1);
688 cdi_xtrack_error_node->setDoubleValue(_cdiCrossTrackErrorM);
690 //////////////////////////////////////////////////////////
691 // compute an approximate ground track heading error
692 //////////////////////////////////////////////////////////
693 double hdg_error = 0.0;
694 if ( inrange && cdi_serviceable ) {
695 double vn = fgGetDouble( "/velocities/speed-north-fps" );
696 double ve = fgGetDouble( "/velocities/speed-east-fps" );
697 double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
698 if ( gnd_trk_true < 0.0 ) { gnd_trk_true += 360.0; }
700 SGPropertyNode *true_hdg
701 = fgGetNode("/orientation/heading-deg", true);
702 hdg_error = gnd_trk_true - true_hdg->getDoubleValue();
704 // cout << "ground track = " << gnd_trk_true
705 // << " orientation = " << true_hdg->getDoubleValue() << endl;
707 cdi_xtrack_hdg_err_node->setDoubleValue( hdg_error );
709 //////////////////////////////////////////////////////////
710 // Calculate a suggested target heading to smoothly intercept
712 //////////////////////////////////////////////////////////
714 // Now that we have cross track heading adjustment built in,
715 // we shouldn't need to overdrive the heading angle within 8km
718 // The cdi deflection should be +/-10 for a full range of deflection
719 // so multiplying this by 3 gives us +/- 30 degrees heading
721 double adjustment = _cdiDeflection * 3.0;
722 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
724 // determine the target heading to fly to intercept the
725 // tgt_radial = target radial (true) + cdi offset adjustmest -
726 // xtrack heading error adjustment
728 double trtrue = target_radial_true_node->getDoubleValue();
729 if ( loc_node->getBoolValue() && backcourse_node->getBoolValue() ) {
730 // tuned to a localizer and backcourse mode activated
731 trtrue += 180.0; // reverse the target localizer heading
732 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
733 nta_hdg = trtrue - adjustment - hdg_error;
735 nta_hdg = trtrue + adjustment - hdg_error;
738 SG_NORMALIZE_RANGE(nta_hdg, 0.0, 360.0);
739 target_auto_hdg_node->setDoubleValue( nta_hdg );
741 //////////////////////////////////////////////////////////
742 // compute the time to intercept selected radial (based on
743 // current and last cross track errors and dt
744 //////////////////////////////////////////////////////////
746 if ( inrange && cdi_serviceable ) {
747 double xrate_ms = (last_xtrack_error - _cdiCrossTrackErrorM) / dt;
748 if ( fabs(xrate_ms) > 0.00001 ) {
749 t = _cdiCrossTrackErrorM / xrate_ms;
754 time_to_intercept->setDoubleValue( t );
756 if (!gs_serviceable_node->getBoolValue() ) {
757 _gsNeedleDeflection = 0.0;
758 _gsNeedleDeflectionNorm = 0.0;
760 gs_deflection_node->setDoubleValue(_gsNeedleDeflection);
761 gs_deflection_deg_node->setDoubleValue(_gsNeedleDeflectionNorm * 0.7);
762 gs_deflection_norm_node->setDoubleValue(_gsNeedleDeflectionNorm);
764 last_xtrack_error = _cdiCrossTrackErrorM;
767 void FGNavRadio::updateAudio()
769 if (!_navaid || !inrange_node->getBoolValue() || !nav_serviceable_node->getBoolValue()) {
773 // play station ident via audio system if on + ident,
774 // otherwise turn it off
775 if (!power_btn_node->getBoolValue()
776 || !(bus_power_node->getDoubleValue() > 1.0)
777 || !ident_btn_node->getBoolValue()
778 || !audio_btn_node->getBoolValue() ) {
779 _sgr->stop( nav_fx_name );
780 _sgr->stop( dme_fx_name );
784 SGSoundSample *sound = _sgr->find( nav_fx_name );
785 double vol = vol_btn_node->getFloatValue();
786 SG_CLAMP_RANGE(vol, 0.0, 1.0);
788 if ( sound != NULL ) {
789 sound->set_volume( vol );
791 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find nav-vor-ident sound" );
794 sound = _sgr->find( dme_fx_name );
795 if ( sound != NULL ) {
796 sound->set_volume( vol );
798 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find nav-dme-ident sound" );
801 const int NUM_IDENT_SLOTS = 5;
802 const time_t SLOT_LENGTH = 5; // seconds
804 // There are N slots numbered 0 through (NUM_IDENT_SLOTS-1) inclusive.
805 // Each slot is 5 seconds long.
806 // Slots 0 is for DME
807 // the rest are for azimuth.
808 time_t now = globals->get_time_params()->get_cur_time();
809 if ((now >= last_time) && (now < last_time + SLOT_LENGTH)) {
810 return; // wait longer
814 play_count = ++play_count % NUM_IDENT_SLOTS;
816 // Previous ident is out of time; if still playing, cut it off:
817 _sgr->stop( nav_fx_name );
818 _sgr->stop( dme_fx_name );
819 if (play_count == 0) { // the DME slot
820 if (_dmeInRange && dme_serviceable_node->getBoolValue()) {
822 if (vol > 0.05) _sgr->play_once( dme_fx_name );
825 if (inrange_node->getBoolValue() && nav_serviceable_node->getBoolValue()) {
826 if (vol > 0.05) _sgr->play_once(nav_fx_name);
831 FGNavRecord* FGNavRadio::findPrimaryNavaid(const SGGeod& aPos, double aFreqMHz)
833 FGNavRecord* nav = globals->get_navlist()->findByFreq(aFreqMHz, aPos);
838 return globals->get_loclist()->findByFreq(aFreqMHz, aPos);
841 // Update current nav/adf radio stations based on current postition
842 void FGNavRadio::search()
844 _time_before_search_sec = 1.0;
845 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
846 lat_node->getDoubleValue(), alt_node->getDoubleValue());
847 double freq = freq_node->getDoubleValue();
849 FGNavRecord* nav = findPrimaryNavaid(pos, freq);
850 if (nav == _navaid) {
851 return; // found the same as last search, we're done
855 char identBuffer[5] = " ";
857 _dme = globals->get_dmelist()->findByFreq(freq, pos);
859 nav_id_node->setStringValue(nav->get_ident());
860 strncpy(identBuffer, nav->ident().c_str(), 5);
862 effective_range = adjustNavRange(nav->get_elev_ft(), pos.getElevationM(), nav->get_range());
863 loc_node->setBoolValue(nav->type() != FGPositioned::VOR);
864 twist = nav->get_multiuse();
866 if (nav->type() == FGPositioned::VOR) {
867 target_radial = sel_radial_node->getDoubleValue();
869 has_gs_node->setBoolValue(false);
870 } else { // ILS or LOC
871 _gs = globals->get_gslist()->findByFreq(freq, pos);
872 has_gs_node->setBoolValue(_gs != NULL);
873 _localizerWidth = localizerWidth(nav);
875 effective_range = nav->get_range();
877 target_radial = nav->get_multiuse();
878 SG_NORMALIZE_RANGE(target_radial, 0.0, 360.0);
881 int tmp = (int)(_gs->get_multiuse() / 1000.0);
882 target_gs = (double)tmp / 100.0;
884 // until penaltyForNav goes away, we cannot assume we always pick
885 // paired LOC/GS trasmsitters. As we pass over a runway threshold, we
886 // often end up picking the 'wrong' LOC, but the correct GS. To avoid
887 // breaking the basis computation, ensure we use the GS radial and not
888 // the (potentially reversed) LOC radial.
889 double gs_radial = fmod(_gs->get_multiuse(), 1000.0);
890 SG_NORMALIZE_RANGE(gs_radial, 0.0, 360.0);
892 // GS axis unit tangent vector
893 // (along the runway)
894 _gsCart = _gs->cart();
895 _gsAxis = tangentVector(_gs->geod(), _gsCart, gs_radial);
897 // GS baseline unit tangent vector
898 // (perpendicular to the runay along the ground)
899 SGVec3d baseline = tangentVector(_gs->geod(), _gsCart, gs_radial + 90.0);
900 _gsVertical = cross(baseline, _gsAxis);
901 } // of have glideslope
902 } // of found LOC or ILS
905 } else { // found nothing
908 nav_id_node->setStringValue("");
910 _sgr->remove( nav_fx_name );
911 _sgr->remove( dme_fx_name );
914 is_valid_node->setBoolValue(nav != NULL);
915 id_c1_node->setIntValue( (int)identBuffer[0] );
916 id_c2_node->setIntValue( (int)identBuffer[1] );
917 id_c3_node->setIntValue( (int)identBuffer[2] );
918 id_c4_node->setIntValue( (int)identBuffer[3] );
921 double FGNavRadio::localizerWidth(FGNavRecord* aLOC)
923 FGRunway* rwy = aLOC->runway();
926 SGVec3d thresholdCart(SGVec3d::fromGeod(rwy->threshold()));
927 double axisLength = dist(aLOC->cart(), thresholdCart);
928 double landingLength = dist(thresholdCart, SGVec3d::fromGeod(rwy->end()));
930 // Reference: http://dcaa.slv.dk:8000/icaodocs/
931 // ICAO standard width at threshold is 210 m = 689 feet = approx 700 feet.
932 // ICAO 3.1.1 half course = DDM = 0.0775
933 // ICAO 3.1.3.7.1 Sensitivity 0.00145 DDM/m at threshold
934 // implies peg-to-peg of 214 m ... we will stick with 210.
935 // ICAO 3.1.3.7.1 "Course sector angle shall not exceed 6 degrees."
937 // Very short runway: less than 1200 m (4000 ft) landing length:
938 if (landingLength < 1200.0) {
939 // ICAO fudges localizer sensitivity for very short runways.
940 // This produces a non-monotonic sensitivity-versus length relation.
941 axisLength += 1050.0;
944 // Example: very short: San Diego KMYF (Montgomery Field) ILS RWY 28R
945 // Example: short: Tom's River KMJX (Robert J. Miller) ILS RWY 6
946 // Example: very long: Denver KDEN (Denver) ILS RWY 16R
947 double raw_width = 210.0 / axisLength * SGD_RADIANS_TO_DEGREES;
948 return raw_width < 6.0? raw_width : 6.0;
951 void FGNavRadio::audioNavidChanged()
953 if (_sgr->exists(nav_fx_name)) {
954 _sgr->remove(nav_fx_name);
958 string trans_ident(_navaid->get_trans_ident());
959 SGSoundSample* sound = morse.make_ident(trans_ident, LO_FREQUENCY);
960 sound->set_volume( 0.3 );
961 if (!_sgr->add( sound, nav_fx_name )) {
962 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
965 if ( _sgr->exists( dme_fx_name ) ) {
966 _sgr->remove( dme_fx_name );
969 sound = morse.make_ident( trans_ident, HI_FREQUENCY );
970 sound->set_volume( 0.3 );
971 _sgr->add( sound, dme_fx_name );
973 int offset = (int)(sg_random() * 30.0);
974 play_count = offset / 4;
975 last_time = globals->get_time_params()->get_cur_time() - offset;
976 } catch (sg_io_exception& e) {
977 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());