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.
30 #include <simgear/sg_inlines.h>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/math/sg_random.h>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/math/interpolater.hxx>
37 #include <simgear/misc/strutils.hxx>
38 #include <simgear/sound/sample_group.hxx>
40 #include <Navaids/navrecord.hxx>
41 #include <Sound/audioident.hxx>
42 #include <Airports/runways.hxx>
43 #include <Navaids/navlist.hxx>
44 #include <Main/util.hxx>
46 #include "navradio.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 Cartesian unit vector in the
64 // local horizontal plane, i.e. tangent to the
65 // surface of the earth at the local ground zero.
66 // The tangent vector passes through the given <midpoint>
67 // and points forward along the given <heading>.
68 // The <heading> is given in degrees.
69 static SGVec3d tangentVector(const SGGeod& midpoint, const double heading)
71 // The size of the delta is presumably chosen to give
72 // numerical stability. I don't know how the value was chosen.
73 // It probably doesn't matter much. It gets divided out.
74 double delta(100.0); // in meters
76 double az2; // ignored
77 SGGeodesy::direct(midpoint, heading, delta, head, az2);
78 SGGeodesy::direct(midpoint, 180+heading, delta, tail, az2);
79 head.setElevationM(midpoint.getElevationM());
80 tail.setElevationM(midpoint.getElevationM());
81 SGVec3d head_xyz = SGVec3d::fromGeod(head);
82 SGVec3d tail_xyz = SGVec3d::fromGeod(tail);
83 // Awkward formula here, needed because vector-by-scalar
84 // multiplication is defined, but not vector-by-scalar division.
85 return (head_xyz - tail_xyz) * (0.5/delta);
88 // Create a "serviceable" node with a default value of "true"
89 SGPropertyNode_ptr createServiceableProp(SGPropertyNode* aParent,
92 SGPropertyNode_ptr n =
93 aParent->getChild(aName, 0, true)->getChild("serviceable", 0, true);
94 simgear::props::Type typ = n->getType();
95 if ((typ == simgear::props::NONE) || (typ == simgear::props::UNSPECIFIED)) {
96 n->setBoolValue(true);
101 static std::auto_ptr<SGInterpTable> static_terminalRangeInterp,
102 static_lowRangeInterp, static_highRangeInterp;
105 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
111 effective_range(0.0),
116 last_xtrack_error(0.0),
118 _localizerWidth(5.0),
119 _name(node->getStringValue("name", "nav")),
120 _num(node->getIntValue("number", 0)),
121 _time_before_search_sec(-1.0),
122 _gsCart(SGVec3d::zeros()),
123 _gsAxis(SGVec3d::zeros()),
124 _gsVertical(SGVec3d::zeros()),
128 _cdiCrossTrackErrorM(0.0),
129 _gsNeedleDeflection(0.0),
130 _gsNeedleDeflectionNorm(0.0),
133 if (!static_terminalRangeInterp.get()) {
134 // one-time interpolator init
135 SGPath path( globals->get_fg_root() );
137 term.append( "Navaids/range.term" );
139 low.append( "Navaids/range.low" );
141 high.append( "Navaids/range.high" );
143 static_terminalRangeInterp.reset(new SGInterpTable(term.str()));
144 static_lowRangeInterp.reset(new SGInterpTable(low.str()));
145 static_highRangeInterp.reset(new SGInterpTable(high.str()));
148 string branch("/instrumentation/" + _name);
149 _radio_node = fgGetNode(branch.c_str(), _num, true);
154 FGNavRadio::~FGNavRadio()
156 if (gps_course_node) {
157 gps_course_node->removeChangeListener(this);
160 if (nav_slaved_to_gps_node) {
161 nav_slaved_to_gps_node->removeChangeListener(this);
171 SGPropertyNode* node = _radio_node.get();
173 fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
176 is_valid_node = node->getChild("data-is-valid", 0, true);
177 power_btn_node = node->getChild("power-btn", 0, true);
178 power_btn_node->setBoolValue( true );
179 vol_btn_node = node->getChild("volume", 0, true);
180 ident_btn_node = node->getChild("ident", 0, true);
181 ident_btn_node->setBoolValue( true );
182 audio_btn_node = node->getChild("audio-btn", 0, true);
183 audio_btn_node->setBoolValue( true );
184 backcourse_node = node->getChild("back-course-btn", 0, true);
185 backcourse_node->setBoolValue( false );
187 nav_serviceable_node = node->getChild("serviceable", 0, true);
188 cdi_serviceable_node = createServiceableProp(node, "cdi");
189 gs_serviceable_node = createServiceableProp(node, "gs");
190 tofrom_serviceable_node = createServiceableProp(node, "to-from");
192 falseCoursesEnabledNode =
193 fgGetNode("/sim/realism/false-radio-courses-enabled");
194 if (!falseCoursesEnabledNode) {
195 falseCoursesEnabledNode =
196 fgGetNode("/sim/realism/false-radio-courses-enabled", true);
197 falseCoursesEnabledNode->setBoolValue(true);
201 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
202 freq_node = subnode->getChild("selected-mhz", 0, true);
203 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
204 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
205 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
206 is_loc_freq_node = subnode->getChild("is-localizer-frequency", 0, true );
209 subnode = node->getChild("radials", 0, true);
210 sel_radial_node = subnode->getChild("selected-deg", 0, true);
211 radial_node = subnode->getChild("actual-deg", 0, true);
212 recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
213 target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
214 target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
217 heading_node = node->getChild("heading-deg", 0, true);
218 time_to_intercept = node->getChild("time-to-intercept-sec", 0, true);
219 to_flag_node = node->getChild("to-flag", 0, true);
220 from_flag_node = node->getChild("from-flag", 0, true);
221 inrange_node = node->getChild("in-range", 0, true);
222 signal_quality_norm_node = node->getChild("signal-quality-norm", 0, true);
223 cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
224 cdi_deflection_norm_node = node->getChild("heading-needle-deflection-norm", 0, true);
225 cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
226 cdi_xtrack_hdg_err_node
227 = node->getChild("crosstrack-heading-error-deg", 0, true);
228 has_gs_node = node->getChild("has-gs", 0, true);
229 loc_node = node->getChild("nav-loc", 0, true);
230 loc_dist_node = node->getChild("nav-distance", 0, true);
231 gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
232 gs_deflection_deg_node = node->getChild("gs-needle-deflection-deg", 0, true);
233 gs_deflection_norm_node = node->getChild("gs-needle-deflection-norm", 0, true);
234 gs_direct_node = node->getChild("gs-direct-deg", 0, true);
235 gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
236 gs_rate_of_climb_fpm_node = node->getChild("gs-rate-of-climb-fpm", 0, true);
237 gs_dist_node = node->getChild("gs-distance", 0, true);
238 gs_inrange_node = node->getChild("gs-in-range", 0, true);
240 nav_id_node = node->getChild("nav-id", 0, true);
241 id_c1_node = node->getChild("nav-id_asc1", 0, true);
242 id_c2_node = node->getChild("nav-id_asc2", 0, true);
243 id_c3_node = node->getChild("nav-id_asc3", 0, true);
244 id_c4_node = node->getChild("nav-id_asc4", 0, true);
246 // gps slaving support
247 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
248 nav_slaved_to_gps_node->addChangeListener(this);
250 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
251 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
252 gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
253 gps_has_gs_node = fgGetNode("/instrumentation/gps/has-gs", true);
254 gps_course_node = fgGetNode("/instrumentation/gps/desired-course-deg", true);
255 gps_course_node->addChangeListener(this);
257 gps_xtrack_error_nm_node = fgGetNode("/instrumentation/gps/wp/wp[1]/course-error-nm", true);
258 _magvarNode = fgGetNode("/environment/magnetic-variation-deg", true);
260 std::ostringstream temp;
261 temp << _name << "-ident-" << _num;
262 if( NULL == _audioIdent )
263 _audioIdent = new VORAudioIdent( temp.str() );
266 // dme-in-range is deprecated,
267 // temporarily create dme-in-range alias for instrumentation/dme[0]/in-range
268 // remove after flightgear 2.6.0
269 node->getNode( "dme-in-range", true )->alias( fgGetNode("/instrumentation/dme[0]/in-range", true ) );
273 FGNavRadio::reinit ()
275 _time_before_search_sec = -1.0;
281 _radio_node->tie( "operable", SGRawValueMethods<FGNavRadio,bool>( *this, &FGNavRadio::isOperable ) );
286 FGNavRadio::unbind ()
288 _radio_node->untie("operable");
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 static_terminalRangeInterp->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 static_lowRangeInterp->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 static_highRangeInterp->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;
358 // Frequencies with odd 100kHz numbers in the range from 108.00 - 111.95
359 // are LOC/GS (ILS) frequency pairs
360 // (108.00, 108.05, 108.20, 108.25.. =VOR)
361 // (108.10, 108.15, 108.30, 108.35.. =ILS)
362 static inline bool IsLocalizerFrequency( double f )
364 if( f < 108.0 || f >= 112.00 ) return false;
365 return (((SGMiscd::roundToInt(f * 100.0) % 100)/10) % 2) != 0;
369 //////////////////////////////////////////////////////////////////////////
370 // Update the various nav values based on position and valid tuned in navs
371 //////////////////////////////////////////////////////////////////////////
373 FGNavRadio::update(double dt)
379 // Create "formatted" versions of the nav frequencies for
380 // instrument displays.
382 sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
383 fmt_freq_node->setStringValue(tmp);
384 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
385 fmt_alt_freq_node->setStringValue(tmp);
386 is_loc_freq_node->setBoolValue( IsLocalizerFrequency( freq_node->getDoubleValue() ));
388 if (power_btn_node->getBoolValue()
389 && (bus_power_node->getDoubleValue() > 1.0)
390 && nav_serviceable_node->getBoolValue() )
402 void FGNavRadio::clearOutputs()
404 inrange_node->setBoolValue( false );
405 signal_quality_norm_node->setDoubleValue( 0.0 );
406 cdi_deflection_node->setDoubleValue( 0.0 );
407 cdi_deflection_norm_node->setDoubleValue( 0.0 );
408 cdi_xtrack_error_node->setDoubleValue( 0.0 );
409 cdi_xtrack_hdg_err_node->setDoubleValue( 0.0 );
410 time_to_intercept->setDoubleValue( 0.0 );
411 heading_node->setDoubleValue(0.0);
412 gs_deflection_node->setDoubleValue( 0.0 );
413 gs_deflection_deg_node->setDoubleValue(0.0);
414 gs_deflection_norm_node->setDoubleValue(0.0);
415 gs_direct_node->setDoubleValue(0.0);
416 gs_inrange_node->setBoolValue( false );
417 loc_node->setBoolValue( false );
418 has_gs_node->setBoolValue(false);
420 to_flag_node->setBoolValue( false );
421 from_flag_node->setBoolValue( false );
422 is_valid_node->setBoolValue(false);
423 nav_id_node->setStringValue("");
429 void FGNavRadio::updateReceiver(double dt)
431 SGVec3d aircraft = SGVec3d::fromGeod(globals->get_aircraft_position());
434 // Do a nav station search only once a second to reduce
435 // unnecessary work. (Also, make sure to do this before caching
437 _time_before_search_sec -= dt;
438 if ( _time_before_search_sec < 0 ) {
444 loc_dist = dist(aircraft, _navaid->cart());
445 loc_dist_node->setDoubleValue( loc_dist );
448 if (nav_slaved_to_gps_node->getBoolValue()) {
449 // when slaved to GPS: only allow stuff above: tune NAV station
450 // All other data driven by GPS only.
456 _cdiDeflection = 0.0;
457 _cdiCrossTrackErrorM = 0.0;
458 _toFlag = _fromFlag = false;
459 _gsNeedleDeflection = 0.0;
460 _gsNeedleDeflectionNorm = 0.0;
461 heading_node->setDoubleValue(0.0);
462 inrange_node->setBoolValue(false);
463 signal_quality_norm_node->setDoubleValue(0.0);
464 gs_dist_node->setDoubleValue( 0.0 );
465 gs_inrange_node->setBoolValue(false);
469 double nav_elev = _navaid->get_elev_ft();
471 bool is_loc = loc_node->getBoolValue();
472 double signal_quality_norm = signal_quality_norm_node->getDoubleValue();
475 //////////////////////////////////////////////////////////
476 // compute forward and reverse wgs84 headings to localizer
477 //////////////////////////////////////////////////////////
479 SGGeodesy::inverse(globals->get_aircraft_position(), _navaid->geod(), hdg, az2, s);
480 heading_node->setDoubleValue(hdg);
481 double radial = az2 - twist;
482 double recip = radial + 180.0;
483 SG_NORMALIZE_RANGE(recip, 0.0, 360.0);
484 radial_node->setDoubleValue( radial );
485 recip_radial_node->setDoubleValue( recip );
487 //////////////////////////////////////////////////////////
488 // compute the target/selected radial in "true" heading
489 //////////////////////////////////////////////////////////
491 target_radial = sel_radial_node->getDoubleValue();
494 // VORs need twist (mag-var) added; ILS/LOCs don't but we set twist to 0.0
495 double trtrue = target_radial + twist;
496 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
497 target_radial_true_node->setDoubleValue( trtrue );
499 //////////////////////////////////////////////////////////
500 // adjust reception range for altitude
501 // FIXME: make sure we are using the navdata range now that
502 // it is valid in the data file
503 //////////////////////////////////////////////////////////
505 double offset = radial - target_radial;
506 SG_NORMALIZE_RANGE(offset, -180.0, 180.0);
508 = adjustILSRange( nav_elev, globals->get_aircraft_position().getElevationM(), offset,
509 loc_dist * SG_METER_TO_NM );
512 = adjustNavRange( nav_elev, globals->get_aircraft_position().getElevationM(), _navaid->get_range() );
515 double effective_range_m = effective_range * SG_NM_TO_METER;
517 //////////////////////////////////////////////////////////
518 // compute signal quality
519 // 100% within effective_range
520 // decreases 1/x^2 further out
521 //////////////////////////////////////////////////////////
522 double last_signal_quality_norm = signal_quality_norm;
524 if ( loc_dist < effective_range_m ) {
525 signal_quality_norm = 1.0;
527 double range_exceed_norm = loc_dist/effective_range_m;
528 signal_quality_norm = 1/(range_exceed_norm*range_exceed_norm);
531 signal_quality_norm = fgGetLowPass( last_signal_quality_norm,
532 signal_quality_norm, dt );
534 signal_quality_norm_node->setDoubleValue( signal_quality_norm );
535 bool inrange = signal_quality_norm > 0.2;
536 inrange_node->setBoolValue( inrange );
538 //////////////////////////////////////////////////////////
539 // compute to/from flag status
540 //////////////////////////////////////////////////////////
545 double offset = fabs(radial - target_radial);
546 _toFlag = (offset > 90.0 && offset < 270.0);
548 _fromFlag = !_toFlag;
550 _toFlag = _fromFlag = false;
554 double r = target_radial - radial;
555 SG_NORMALIZE_RANGE(r, -180.0, 180.0);
558 if (falseCoursesEnabledNode->getBoolValue()) {
559 // The factor of 30.0 gives a period of 120 which gives us 3 cycles and six
560 // zeros i.e. six courses: one front course, one back course, and four
561 // false courses. Three of the six are reverse sensing.
562 _cdiDeflection = 30.0 * sawtooth(r / 30.0);
564 // no false courses, but we do need to create a back course
565 if (fabs(r) > 90.0) { // front course
566 _cdiDeflection = r - copysign(180.0, r);
568 _cdiDeflection = r; // back course
571 _cdiDeflection = -_cdiDeflection; // reverse for outbound radial
572 } // of false courses disabled
574 const double VOR_FULL_ARC = 20.0; // VOR is -10 .. 10 degree swing
575 _cdiDeflection *= VOR_FULL_ARC / _localizerWidth; // increased localizer sensitivity
577 if (backcourse_node->getBoolValue()) {
578 _cdiDeflection = -_cdiDeflection;
581 // handle the TO side of the VOR
582 if (fabs(r) > 90.0) {
583 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
586 } // of non-localizer case
588 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
589 _cdiDeflection *= signal_quality_norm;
591 // cross-track error (in meters)
592 _cdiCrossTrackErrorM = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
594 updateGlideSlope(dt, aircraft, signal_quality_norm);
597 void FGNavRadio::updateGlideSlope(double dt, const SGVec3d& aircraft, double signal_quality_norm)
599 bool gsInRange = (_gs && inrange_node->getBoolValue());
604 gsDist = dist(aircraft, _gsCart);
605 gsInRange = (gsDist < (_gs->get_range() * SG_NM_TO_METER));
608 gs_inrange_node->setBoolValue(gsInRange);
609 gs_dist_node->setDoubleValue( gsDist );
613 _gsNeedleDeflection = 0.0;
614 _gsNeedleDeflectionNorm = 0.0;
618 SGVec3d pos = aircraft - _gsCart; // relative vector from gs antenna to aircraft
619 // The positive GS axis points along the runway in the landing direction,
620 // toward the far end, not toward the approach area, so we need a - sign here:
621 double comp_h = -dot(pos, _gsAxis); // component in horiz direction
622 double comp_v = dot(pos, _gsVertical); // component in vertical direction
623 //double comp_b = dot(pos, _gsBaseline); // component in baseline direction
624 //if (comp_b) {} // ... (useful for debugging)
626 // _gsDirect represents the angle of elevation of the aircraft
627 // as seen by the GS transmitter.
628 _gsDirect = atan2(comp_v, comp_h) * SGD_RADIANS_TO_DEGREES;
629 // At this point, if the aircraft is centered on the glide slope,
630 // _gsDirect will be a small positive number, e.g. 3.0 degrees
632 // Aim the branch cut straight down
633 // into the ground below the GS transmitter:
634 if (_gsDirect < -90.0) _gsDirect += 360.0;
636 double deflectionAngle = target_gs - _gsDirect;
638 if (falseCoursesEnabledNode->getBoolValue()) {
639 // Construct false glideslopes. The scale factor of 1.5
640 // in the sawtooth gives a period of 6 degrees.
641 // There will be zeros at 3, 6r, 9, 12r et cetera
642 // where "r" indicates reverse sensing.
643 // This is is consistent with conventional pilot lore
644 // e.g. http://www.allstar.fiu.edu/aerojava/ILS.htm
645 // but inconsistent with
646 // http://www.freepatentsonline.com/3757338.html
648 // It may be that some of each exist.
649 if (deflectionAngle < 0) {
650 deflectionAngle = 1.5 * sawtooth(deflectionAngle / 1.5);
652 // no false GS below the true GS
656 // GS is documented to be 1.4 degrees thick,
657 // i.e. plus or minus 0.7 degrees from the midline:
658 SG_CLAMP_RANGE(deflectionAngle, -0.7, 0.7);
660 // Many older instrument xml frontends depend on
661 // the un-normalized gs-needle-deflection.
662 // Apparently the interface standard is plus or minus 3.5 "volts"
663 // for a full-scale deflection:
664 _gsNeedleDeflection = deflectionAngle * 5.0;
665 _gsNeedleDeflection *= signal_quality_norm;
667 _gsNeedleDeflectionNorm = (deflectionAngle / 0.7) * signal_quality_norm;
669 //////////////////////////////////////////////////////////
670 // Calculate desired rate of climb for intercepting the GS
671 //////////////////////////////////////////////////////////
672 double gs_diff = target_gs - _gsDirect;
673 // convert desired vertical path angle into a climb rate
674 double des_angle = _gsDirect - 10 * gs_diff;
675 /* printf("target_gs=%.1f angle=%.1f gs_diff=%.1f des_angle=%.1f\n",
676 target_gs, _gsDirect, gs_diff, des_angle); */
678 // estimate horizontal speed towards ILS in meters per minute
679 double elapsedDistance = last_x - gsDist;
682 double new_vel = ( elapsedDistance / dt );
683 horiz_vel = 0.99 * horiz_vel + 0.01 * new_vel;
684 /* printf("vel=%.1f (dist=%.1f dt=%.2f)\n", horiz_vel, elapsedDistance, dt);*/
686 gs_rate_of_climb_node
687 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
688 * horiz_vel * SG_METER_TO_FEET );
689 gs_rate_of_climb_fpm_node
690 ->setDoubleValue( gs_rate_of_climb_node->getDoubleValue() * 60 );
693 void FGNavRadio::valueChanged (SGPropertyNode* prop)
695 if (prop == gps_course_node) {
696 if (!nav_slaved_to_gps_node->getBoolValue()) {
700 // GPS desired course has changed, sync up our selected-course
701 double v = prop->getDoubleValue();
702 if (v != sel_radial_node->getDoubleValue()) {
703 sel_radial_node->setDoubleValue(v);
705 } else if (prop == nav_slaved_to_gps_node) {
706 if (prop->getBoolValue()) {
707 // slaved-to-GPS activated, clear obsolete NAV outputs and sync up selected course
709 sel_radial_node->setDoubleValue(gps_course_node->getDoubleValue());
711 // slave-to-GPS enabled/disabled, resync NAV station (update all outputs)
713 _time_before_search_sec = 0;
717 void FGNavRadio::updateGPSSlaved()
719 has_gs_node->setBoolValue(gps_has_gs_node->getBoolValue());
721 _toFlag = gps_to_flag_node->getBoolValue();
722 _fromFlag = gps_from_flag_node->getBoolValue();
724 bool gpsValid = (_toFlag | _fromFlag);
725 inrange_node->setBoolValue(gpsValid);
727 signal_quality_norm_node->setDoubleValue(0.0);
728 _cdiDeflection = 0.0;
729 _cdiCrossTrackErrorM = 0.0;
730 _gsNeedleDeflection = 0.0;
731 _gsNeedleDeflectionNorm = 0.0;
735 // this is unfortunate, but panel instruments use this value to decide
736 // if the navradio output is valid.
737 signal_quality_norm_node->setDoubleValue(1.0);
739 _cdiDeflection = gps_cdi_deflection_node->getDoubleValue();
740 // clmap to some range (+/- 10 degrees) as the regular deflection
741 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
743 _cdiCrossTrackErrorM = gps_xtrack_error_nm_node->getDoubleValue() * SG_NM_TO_METER;
744 _gsNeedleDeflection = 0.0; // FIXME, supply this
746 double trtrue = gps_course_node->getDoubleValue() + _magvarNode->getDoubleValue();
747 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
748 target_radial_true_node->setDoubleValue( trtrue );
751 void FGNavRadio::updateCDI(double dt)
753 bool cdi_serviceable = cdi_serviceable_node->getBoolValue();
754 bool inrange = inrange_node->getBoolValue();
756 if (tofrom_serviceable_node->getBoolValue()) {
757 to_flag_node->setBoolValue(_toFlag);
758 from_flag_node->setBoolValue(_fromFlag);
760 to_flag_node->setBoolValue(false);
761 from_flag_node->setBoolValue(false);
764 if (!cdi_serviceable) {
765 _cdiDeflection = 0.0;
766 _cdiCrossTrackErrorM = 0.0;
769 cdi_deflection_node->setDoubleValue(_cdiDeflection);
770 cdi_deflection_norm_node->setDoubleValue(_cdiDeflection * 0.1);
771 cdi_xtrack_error_node->setDoubleValue(_cdiCrossTrackErrorM);
773 //////////////////////////////////////////////////////////
774 // compute an approximate ground track heading error
775 //////////////////////////////////////////////////////////
776 double hdg_error = 0.0;
777 if ( inrange && cdi_serviceable ) {
778 double vn = fgGetDouble( "/velocities/speed-north-fps" );
779 double ve = fgGetDouble( "/velocities/speed-east-fps" );
780 double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
781 if ( gnd_trk_true < 0.0 ) { gnd_trk_true += 360.0; }
783 SGPropertyNode *true_hdg
784 = fgGetNode("/orientation/heading-deg", true);
785 hdg_error = gnd_trk_true - true_hdg->getDoubleValue();
787 // cout << "ground track = " << gnd_trk_true
788 // << " orientation = " << true_hdg->getDoubleValue() << endl;
790 cdi_xtrack_hdg_err_node->setDoubleValue( hdg_error );
792 //////////////////////////////////////////////////////////
793 // Calculate a suggested target heading to smoothly intercept
795 //////////////////////////////////////////////////////////
797 // Now that we have cross track heading adjustment built in,
798 // we shouldn't need to overdrive the heading angle within 8km
801 // The cdi deflection should be +/-10 for a full range of deflection
802 // so multiplying this by 3 gives us +/- 30 degrees heading
804 double adjustment = _cdiDeflection * 3.0;
805 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
807 // determine the target heading to fly to intercept the
808 // tgt_radial = target radial (true) + cdi offset adjustment -
809 // xtrack heading error adjustment
811 double trtrue = target_radial_true_node->getDoubleValue();
812 if ( loc_node->getBoolValue() && backcourse_node->getBoolValue() ) {
813 // tuned to a localizer and backcourse mode activated
814 trtrue += 180.0; // reverse the target localizer heading
815 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
816 nta_hdg = trtrue - adjustment - hdg_error;
818 nta_hdg = trtrue + adjustment - hdg_error;
821 SG_NORMALIZE_RANGE(nta_hdg, 0.0, 360.0);
822 target_auto_hdg_node->setDoubleValue( nta_hdg );
824 //////////////////////////////////////////////////////////
825 // compute the time to intercept selected radial (based on
826 // current and last cross track errors and dt)
827 //////////////////////////////////////////////////////////
829 if ( inrange && cdi_serviceable ) {
830 double cur_rate = (last_xtrack_error - _cdiCrossTrackErrorM) / dt;
831 xrate_ms = 0.99 * xrate_ms + 0.01 * cur_rate;
832 if ( fabs(xrate_ms) > 0.00001 ) {
833 t = _cdiCrossTrackErrorM / xrate_ms;
838 time_to_intercept->setDoubleValue( t );
840 if (!gs_serviceable_node->getBoolValue() ) {
841 _gsNeedleDeflection = 0.0;
842 _gsNeedleDeflectionNorm = 0.0;
844 gs_deflection_node->setDoubleValue(_gsNeedleDeflection);
845 gs_deflection_deg_node->setDoubleValue(_gsNeedleDeflectionNorm * 0.7);
846 gs_deflection_norm_node->setDoubleValue(_gsNeedleDeflectionNorm);
847 gs_direct_node->setDoubleValue(_gsDirect);
849 last_xtrack_error = _cdiCrossTrackErrorM;
852 void FGNavRadio::updateAudio( double dt )
854 if (!_navaid || !inrange_node->getBoolValue() || !nav_serviceable_node->getBoolValue()) {
855 _audioIdent->setIdent("", 0.0 );
859 // play station ident via audio system if on + ident,
860 // otherwise turn it off
861 if (!power_btn_node->getBoolValue()
862 || !(bus_power_node->getDoubleValue() > 1.0)
863 || !ident_btn_node->getBoolValue()
864 || !audio_btn_node->getBoolValue() ) {
865 _audioIdent->setIdent("", 0.0 );
869 _audioIdent->setIdent( _navaid->get_trans_ident(), vol_btn_node->getFloatValue() );
871 _audioIdent->update( dt );
874 FGNavRecord* FGNavRadio::findPrimaryNavaid(const SGGeod& aPos, double aFreqMHz)
876 return FGNavList::findByFreq(aFreqMHz, aPos, FGNavList::navFilter());
879 // Update current nav/adf radio stations based on current position
880 void FGNavRadio::search()
882 // set delay for next search
883 _time_before_search_sec = 1.0;
885 double freq = freq_node->getDoubleValue();
887 // immediate NAV search when frequency has changed (toggle between nav and g/s search otherwise)
888 _nav_search |= (_last_freq != freq);
890 // do we need to search a new NAV station in this iteration?
894 FGNavRecord* nav = findPrimaryNavaid(globals->get_aircraft_position(), freq);
895 if (nav == _navaid) {
896 if (nav && (nav->type() != FGPositioned::VOR))
897 _nav_search = false; // search glideslope on next iteration
898 return; // nav hasn't changed, we're done
900 // remember new navaid station
904 // search glideslope station
905 if ((_navaid.valid()) && (_navaid->type() != FGPositioned::VOR))
907 FGNavList::TypeFilter gsFilter(FGPositioned::GS);
908 FGNavRecord* gs = FGNavList::findByFreq(freq, globals->get_aircraft_position(),
910 if ((!_nav_search) && (gs == _gs))
912 _nav_search = true; // search NAV on next iteration
913 return; // g/s hasn't changed, neither has nav - we're done
915 // remember new glideslope station
919 _nav_search = true; // search NAV on next iteration
921 // nav or gs station has changed
925 // Update current nav/adf/glideslope outputs when station has changed
926 void FGNavRadio::updateNav()
928 // update necessary, nav and/or gs has changed
929 FGNavRecord* nav = _navaid;
930 string identBuffer(4, ' ');
932 nav_id_node->setStringValue(nav->get_ident());
933 identBuffer = simgear::strutils::rpad( nav->ident(), 4, ' ' );
935 effective_range = adjustNavRange(nav->get_elev_ft(), globals->get_aircraft_position().getElevationM(), nav->get_range());
936 loc_node->setBoolValue(nav->type() != FGPositioned::VOR);
937 twist = nav->get_multiuse();
939 if (nav->type() == FGPositioned::VOR) {
940 target_radial = sel_radial_node->getDoubleValue();
942 } else { // ILS or LOC
943 _localizerWidth = nav->localizerWidth();
945 effective_range = nav->get_range();
947 target_radial = nav->get_multiuse();
948 SG_NORMALIZE_RANGE(target_radial, 0.0, 360.0);
951 int tmp = (int)(_gs->get_multiuse() / 1000.0);
952 target_gs = (double)tmp / 100.0;
954 double gs_radial = fmod(_gs->get_multiuse(), 1000.0);
955 SG_NORMALIZE_RANGE(gs_radial, 0.0, 360.0);
956 _gsCart = _gs->cart();
958 // GS axis unit tangent vector
959 // (along the runway):
960 _gsAxis = tangentVector(_gs->geod(), gs_radial);
962 // GS baseline unit tangent vector
963 // (transverse to the runway along the ground)
964 _gsBaseline = tangentVector(_gs->geod(), gs_radial + 90.0);
965 _gsVertical = cross(_gsBaseline, _gsAxis);
966 } // of have glideslope
967 } // of found LOC or ILS
969 } else { // found nothing
971 nav_id_node->setStringValue("");
972 loc_node->setBoolValue(false);
973 _audioIdent->setIdent("", 0.0 );
976 has_gs_node->setBoolValue(_gs != NULL);
977 is_valid_node->setBoolValue(nav != NULL);
978 id_c1_node->setIntValue( (int)identBuffer[0] );
979 id_c2_node->setIntValue( (int)identBuffer[1] );
980 id_c3_node->setIntValue( (int)identBuffer[2] );
981 id_c4_node->setIntValue( (int)identBuffer[3] );