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.
29 #include <simgear/sg_inlines.h>
30 #include <simgear/timing/sg_time.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <simgear/misc/sg_path.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/structure/exception.hxx>
35 #include <simgear/math/interpolater.hxx>
36 #include <simgear/misc/strutils.hxx>
38 #include <Navaids/navrecord.hxx>
39 #include <Sound/audioident.hxx>
40 #include <Airports/runways.hxx>
41 #include <Navaids/navlist.hxx>
42 #include <Main/util.hxx>
44 #include "navradio.hxx"
48 // General-purpose sawtooth function. Graph looks like this:
51 // Odd symmetry, inversion symmetry about the origin.
52 // Unit slope at the origin.
53 // Max 1, min -1, period 4.
54 // Two zero-crossings per period, one with + slope, one with - slope.
55 // Useful for false localizer courses.
56 static double sawtooth(double xx)
58 return 4.0 * fabs(xx/4.0 + 0.25 - floor(xx/4.0 + 0.75)) - 1.0;
61 // Calculate a Cartesian unit vector in the
62 // local horizontal plane, i.e. tangent to the
63 // surface of the earth at the local ground zero.
64 // The tangent vector passes through the given <midpoint>
65 // and points forward along the given <heading>.
66 // The <heading> is given in degrees.
67 static SGVec3d tangentVector(const SGGeod& midpoint, const double heading)
69 // The size of the delta is presumably chosen to give
70 // numerical stability. I don't know how the value was chosen.
71 // It probably doesn't matter much. It gets divided out.
72 double delta(100.0); // in meters
74 double az2; // ignored
75 SGGeodesy::direct(midpoint, heading, delta, head, az2);
76 SGGeodesy::direct(midpoint, 180+heading, delta, tail, az2);
77 head.setElevationM(midpoint.getElevationM());
78 tail.setElevationM(midpoint.getElevationM());
79 SGVec3d head_xyz = SGVec3d::fromGeod(head);
80 SGVec3d tail_xyz = SGVec3d::fromGeod(tail);
81 // Awkward formula here, needed because vector-by-scalar
82 // multiplication is defined, but not vector-by-scalar division.
83 return (head_xyz - tail_xyz) * (0.5/delta);
86 // Create a "serviceable" node with a default value of "true"
87 SGPropertyNode_ptr createServiceableProp(SGPropertyNode* aParent,
90 SGPropertyNode_ptr n =
91 aParent->getChild(aName, 0, true)->getChild("serviceable", 0, true);
92 simgear::props::Type typ = n->getType();
93 if ((typ == simgear::props::NONE) || (typ == simgear::props::UNSPECIFIED)) {
94 n->setBoolValue(true);
100 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
108 effective_range(0.0),
113 last_xtrack_error(0.0),
115 _localizerWidth(5.0),
116 _name(node->getStringValue("name", "nav")),
117 _num(node->getIntValue("number", 0)),
118 _time_before_search_sec(-1.0),
119 _gsCart(SGVec3d::zeros()),
120 _gsAxis(SGVec3d::zeros()),
121 _gsVertical(SGVec3d::zeros()),
125 _cdiCrossTrackErrorM(0.0),
126 _gsNeedleDeflection(0.0),
127 _gsNeedleDeflectionNorm(0.0),
130 SGPath path( globals->get_fg_root() );
132 term.append( "Navaids/range.term" );
134 low.append( "Navaids/range.low" );
136 high.append( "Navaids/range.high" );
138 term_tbl = new SGInterpTable( term.str() );
139 low_tbl = new SGInterpTable( low.str() );
140 high_tbl = new SGInterpTable( high.str() );
142 string branch("/instrumentation/" + _name);
143 _radio_node = fgGetNode(branch.c_str(), _num, true);
148 FGNavRadio::~FGNavRadio()
150 if (gps_course_node) {
151 gps_course_node->removeChangeListener(this);
154 if (nav_slaved_to_gps_node) {
155 nav_slaved_to_gps_node->removeChangeListener(this);
169 SGPropertyNode* node = _radio_node.get();
171 fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
174 is_valid_node = node->getChild("data-is-valid", 0, true);
175 power_btn_node = node->getChild("power-btn", 0, true);
176 power_btn_node->setBoolValue( true );
177 vol_btn_node = node->getChild("volume", 0, true);
178 ident_btn_node = node->getChild("ident", 0, true);
179 ident_btn_node->setBoolValue( true );
180 audio_btn_node = node->getChild("audio-btn", 0, true);
181 audio_btn_node->setBoolValue( true );
182 backcourse_node = node->getChild("back-course-btn", 0, true);
183 backcourse_node->setBoolValue( false );
185 nav_serviceable_node = node->getChild("serviceable", 0, true);
186 cdi_serviceable_node = createServiceableProp(node, "cdi");
187 gs_serviceable_node = createServiceableProp(node, "gs");
188 tofrom_serviceable_node = createServiceableProp(node, "to-from");
190 falseCoursesEnabledNode =
191 fgGetNode("/sim/realism/false-radio-courses-enabled");
192 if (!falseCoursesEnabledNode) {
193 falseCoursesEnabledNode =
194 fgGetNode("/sim/realism/false-radio-courses-enabled", true);
195 falseCoursesEnabledNode->setBoolValue(true);
199 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
200 freq_node = subnode->getChild("selected-mhz", 0, true);
201 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
202 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
203 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
204 is_loc_freq_node = subnode->getChild("is-localizer-frequency", 0, true );
207 subnode = node->getChild("radials", 0, true);
208 sel_radial_node = subnode->getChild("selected-deg", 0, true);
209 radial_node = subnode->getChild("actual-deg", 0, true);
210 recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
211 target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
212 target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
215 heading_node = node->getChild("heading-deg", 0, true);
216 time_to_intercept = node->getChild("time-to-intercept-sec", 0, true);
217 to_flag_node = node->getChild("to-flag", 0, true);
218 from_flag_node = node->getChild("from-flag", 0, true);
219 inrange_node = node->getChild("in-range", 0, true);
220 signal_quality_norm_node = node->getChild("signal-quality-norm", 0, true);
221 cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
222 cdi_deflection_norm_node = node->getChild("heading-needle-deflection-norm", 0, true);
223 cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
224 cdi_xtrack_hdg_err_node
225 = node->getChild("crosstrack-heading-error-deg", 0, true);
226 has_gs_node = node->getChild("has-gs", 0, true);
227 loc_node = node->getChild("nav-loc", 0, true);
228 loc_dist_node = node->getChild("nav-distance", 0, true);
229 gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
230 gs_deflection_deg_node = node->getChild("gs-needle-deflection-deg", 0, true);
231 gs_deflection_norm_node = node->getChild("gs-needle-deflection-norm", 0, true);
232 gs_direct_node = node->getChild("gs-direct-deg", 0, true);
233 gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
234 gs_rate_of_climb_fpm_node = node->getChild("gs-rate-of-climb-fpm", 0, true);
235 gs_dist_node = node->getChild("gs-distance", 0, true);
236 gs_inrange_node = node->getChild("gs-in-range", 0, true);
238 nav_id_node = node->getChild("nav-id", 0, true);
239 id_c1_node = node->getChild("nav-id_asc1", 0, true);
240 id_c2_node = node->getChild("nav-id_asc2", 0, true);
241 id_c3_node = node->getChild("nav-id_asc3", 0, true);
242 id_c4_node = node->getChild("nav-id_asc4", 0, true);
244 // gps slaving support
245 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
246 nav_slaved_to_gps_node->addChangeListener(this);
248 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
249 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
250 gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
251 gps_has_gs_node = fgGetNode("/instrumentation/gps/has-gs", true);
252 gps_course_node = fgGetNode("/instrumentation/gps/desired-course-deg", true);
253 gps_course_node->addChangeListener(this);
255 gps_xtrack_error_nm_node = fgGetNode("/instrumentation/gps/wp/wp[1]/course-error-nm", true);
256 _magvarNode = fgGetNode("/environment/magnetic-variation-deg", true);
258 std::ostringstream temp;
259 temp << _name << "-ident-" << _num;
260 if( NULL == _audioIdent )
261 _audioIdent = new VORAudioIdent( temp.str() );
264 // dme-in-range is deprecated,
265 // temporarily create dme-in-range alias for instrumentation/dme[0]/in-range
266 // remove after flightgear 2.6.0
267 node->getNode( "dme-in-range", true )->alias( fgGetNode("/instrumentation/dme[0]/in-range", true ) );
273 _radio_node->tie( "operable", SGRawValueMethods<FGNavRadio,bool>( *this, &FGNavRadio::isOperable ) );
278 FGNavRadio::unbind ()
280 _radio_node->untie("operable");
284 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
285 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
286 double nominalRange )
288 if (nominalRange <= 0.0) {
289 nominalRange = FG_NAV_DEFAULT_RANGE;
292 // extend out actual usable range to be 1.3x the published safe range
293 const double usability_factor = 1.3;
295 // assumptions we model the standard service volume, plus
296 // ... rather than specifying a cylinder, we model a cone that
297 // contains the cylinder. Then we put an upside down cone on top
298 // to model diminishing returns at too-high altitudes.
300 // altitude difference
301 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
302 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
303 // << " station elev = " << stationElev << endl;
305 if ( nominalRange < 25.0 + SG_EPSILON ) {
306 // Standard Terminal Service Volume
307 return term_tbl->interpolate( alt ) * usability_factor;
308 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
309 // Standard Low Altitude Service Volume
310 // table is based on range of 40, scale to actual range
311 return low_tbl->interpolate( alt ) * nominalRange / 40.0
314 // Standard High Altitude Service Volume
315 // table is based on range of 130, scale to actual range
316 return high_tbl->interpolate( alt ) * nominalRange / 130.0
322 // model standard ILS service volumes as per AIM 1-1-9
323 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
324 double offsetDegrees, double distance )
326 // assumptions we model the standard service volume, plus
328 // altitude difference
329 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
330 // double offset = fabs( offsetDegrees );
332 // if ( offset < 10 ) {
333 // return FG_ILS_DEFAULT_RANGE;
334 // } else if ( offset < 35 ) {
335 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
336 // } else if ( offset < 45 ) {
337 // return (45 - offset);
338 // } else if ( offset > 170 ) {
339 // return FG_ILS_DEFAULT_RANGE;
340 // } else if ( offset > 145 ) {
341 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
342 // } else if ( offset > 135 ) {
343 // return (offset - 135);
347 return FG_LOC_DEFAULT_RANGE;
350 // Frequencies with odd 100kHz numbers in the range from 108.00 - 111.95
351 // are LOC/GS (ILS) frequency pairs
352 // (108.00, 108.05, 108.20, 108.25.. =VOR)
353 // (108.10, 108.15, 108.30, 108.35.. =ILS)
354 static inline bool IsLocalizerFrequency( double f )
356 if( f < 108.0 || f >= 112.00 ) return false;
357 return (((SGMiscd::roundToInt(f * 100.0) % 100)/10) % 2) != 0;
361 //////////////////////////////////////////////////////////////////////////
362 // Update the various nav values based on position and valid tuned in navs
363 //////////////////////////////////////////////////////////////////////////
365 FGNavRadio::update(double dt)
371 // Create "formatted" versions of the nav frequencies for
372 // instrument displays.
374 sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
375 fmt_freq_node->setStringValue(tmp);
376 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
377 fmt_alt_freq_node->setStringValue(tmp);
378 is_loc_freq_node->setBoolValue( IsLocalizerFrequency( freq_node->getDoubleValue() ));
380 if (power_btn_node->getBoolValue()
381 && (bus_power_node->getDoubleValue() > 1.0)
382 && nav_serviceable_node->getBoolValue() )
394 void FGNavRadio::clearOutputs()
396 inrange_node->setBoolValue( false );
397 signal_quality_norm_node->setDoubleValue( 0.0 );
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 heading_node->setDoubleValue(0.0);
404 gs_deflection_node->setDoubleValue( 0.0 );
405 gs_deflection_deg_node->setDoubleValue(0.0);
406 gs_deflection_norm_node->setDoubleValue(0.0);
407 gs_direct_node->setDoubleValue(0.0);
408 gs_inrange_node->setBoolValue( false );
409 loc_node->setBoolValue( false );
410 has_gs_node->setBoolValue(false);
412 to_flag_node->setBoolValue( false );
413 from_flag_node->setBoolValue( false );
414 is_valid_node->setBoolValue(false);
415 nav_id_node->setStringValue("");
421 void FGNavRadio::updateReceiver(double dt)
423 SGVec3d aircraft = SGVec3d::fromGeod(globals->get_aircraft_position());
426 // Do a nav station search only once a second to reduce
427 // unnecessary work. (Also, make sure to do this before caching
429 _time_before_search_sec -= dt;
430 if ( _time_before_search_sec < 0 ) {
436 loc_dist = dist(aircraft, _navaid->cart());
437 loc_dist_node->setDoubleValue( loc_dist );
440 if (nav_slaved_to_gps_node->getBoolValue()) {
441 // when slaved to GPS: only allow stuff above: tune NAV station
442 // All other data driven by GPS only.
448 _cdiDeflection = 0.0;
449 _cdiCrossTrackErrorM = 0.0;
450 _toFlag = _fromFlag = false;
451 _gsNeedleDeflection = 0.0;
452 _gsNeedleDeflectionNorm = 0.0;
453 heading_node->setDoubleValue(0.0);
454 inrange_node->setBoolValue(false);
455 signal_quality_norm_node->setDoubleValue(0.0);
456 gs_dist_node->setDoubleValue( 0.0 );
457 gs_inrange_node->setBoolValue(false);
461 double nav_elev = _navaid->get_elev_ft();
463 bool is_loc = loc_node->getBoolValue();
464 double signal_quality_norm = signal_quality_norm_node->getDoubleValue();
467 //////////////////////////////////////////////////////////
468 // compute forward and reverse wgs84 headings to localizer
469 //////////////////////////////////////////////////////////
471 SGGeodesy::inverse(globals->get_aircraft_position(), _navaid->geod(), hdg, az2, s);
472 heading_node->setDoubleValue(hdg);
473 double radial = az2 - twist;
474 double recip = radial + 180.0;
475 SG_NORMALIZE_RANGE(recip, 0.0, 360.0);
476 radial_node->setDoubleValue( radial );
477 recip_radial_node->setDoubleValue( recip );
479 //////////////////////////////////////////////////////////
480 // compute the target/selected radial in "true" heading
481 //////////////////////////////////////////////////////////
483 target_radial = sel_radial_node->getDoubleValue();
486 // VORs need twist (mag-var) added; ILS/LOCs don't but we set twist to 0.0
487 double trtrue = target_radial + twist;
488 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
489 target_radial_true_node->setDoubleValue( trtrue );
491 //////////////////////////////////////////////////////////
492 // adjust reception range for altitude
493 // FIXME: make sure we are using the navdata range now that
494 // it is valid in the data file
495 //////////////////////////////////////////////////////////
497 double offset = radial - target_radial;
498 SG_NORMALIZE_RANGE(offset, -180.0, 180.0);
500 = adjustILSRange( nav_elev, globals->get_aircraft_position().getElevationM(), offset,
501 loc_dist * SG_METER_TO_NM );
504 = adjustNavRange( nav_elev, globals->get_aircraft_position().getElevationM(), _navaid->get_range() );
507 double effective_range_m = effective_range * SG_NM_TO_METER;
509 //////////////////////////////////////////////////////////
510 // compute signal quality
511 // 100% within effective_range
512 // decreases 1/x^2 further out
513 //////////////////////////////////////////////////////////
514 double last_signal_quality_norm = signal_quality_norm;
516 if ( loc_dist < effective_range_m ) {
517 signal_quality_norm = 1.0;
519 double range_exceed_norm = loc_dist/effective_range_m;
520 signal_quality_norm = 1/(range_exceed_norm*range_exceed_norm);
523 signal_quality_norm = fgGetLowPass( last_signal_quality_norm,
524 signal_quality_norm, dt );
526 signal_quality_norm_node->setDoubleValue( signal_quality_norm );
527 bool inrange = signal_quality_norm > 0.2;
528 inrange_node->setBoolValue( inrange );
530 //////////////////////////////////////////////////////////
531 // compute to/from flag status
532 //////////////////////////////////////////////////////////
537 double offset = fabs(radial - target_radial);
538 _toFlag = (offset > 90.0 && offset < 270.0);
540 _fromFlag = !_toFlag;
542 _toFlag = _fromFlag = false;
546 double r = target_radial - radial;
547 SG_NORMALIZE_RANGE(r, -180.0, 180.0);
550 if (falseCoursesEnabledNode->getBoolValue()) {
551 // The factor of 30.0 gives a period of 120 which gives us 3 cycles and six
552 // zeros i.e. six courses: one front course, one back course, and four
553 // false courses. Three of the six are reverse sensing.
554 _cdiDeflection = 30.0 * sawtooth(r / 30.0);
556 // no false courses, but we do need to create a back course
557 if (fabs(r) > 90.0) { // front course
558 _cdiDeflection = r - copysign(180.0, r);
560 _cdiDeflection = r; // back course
563 _cdiDeflection = -_cdiDeflection; // reverse for outbound radial
564 } // of false courses disabled
566 const double VOR_FULL_ARC = 20.0; // VOR is -10 .. 10 degree swing
567 _cdiDeflection *= VOR_FULL_ARC / _localizerWidth; // increased localiser sensitivity
569 if (backcourse_node->getBoolValue()) {
570 _cdiDeflection = -_cdiDeflection;
573 // handle the TO side of the VOR
574 if (fabs(r) > 90.0) {
575 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
578 } // of non-localiser case
580 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
581 _cdiDeflection *= signal_quality_norm;
583 // cross-track error (in meters)
584 _cdiCrossTrackErrorM = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
586 updateGlideSlope(dt, aircraft, signal_quality_norm);
589 void FGNavRadio::updateGlideSlope(double dt, const SGVec3d& aircraft, double signal_quality_norm)
591 bool gsInRange = (_gs && inrange_node->getBoolValue());
596 gsDist = dist(aircraft, _gsCart);
597 gsInRange = (gsDist < (_gs->get_range() * SG_NM_TO_METER));
600 gs_inrange_node->setBoolValue(gsInRange);
601 gs_dist_node->setDoubleValue( gsDist );
605 _gsNeedleDeflection = 0.0;
606 _gsNeedleDeflectionNorm = 0.0;
610 SGVec3d pos = aircraft - _gsCart; // relative vector from gs antenna to aircraft
611 // The positive GS axis points along the runway in the landing direction,
612 // toward the far end, not toward the approach area, so we need a - sign here:
613 double comp_h = -dot(pos, _gsAxis); // component in horiz direction
614 double comp_v = dot(pos, _gsVertical); // component in vertical direction
615 //double comp_b = dot(pos, _gsBaseline); // component in baseline direction
616 //if (comp_b) {} // ... (useful for debugging)
618 // _gsDirect represents the angle of elevation of the aircraft
619 // as seen by the GS transmitter.
620 _gsDirect = atan2(comp_v, comp_h) * SGD_RADIANS_TO_DEGREES;
621 // At this point, if the aircraft is centered on the glide slope,
622 // _gsDirect will be a small positive number, e.g. 3.0 degrees
624 // Aim the branch cut straight down
625 // into the ground below the GS transmitter:
626 if (_gsDirect < -90.0) _gsDirect += 360.0;
628 double deflectionAngle = target_gs - _gsDirect;
630 if (falseCoursesEnabledNode->getBoolValue()) {
631 // Construct false glideslopes. The scale factor of 1.5
632 // in the sawtooth gives a period of 6 degrees.
633 // There will be zeros at 3, 6r, 9, 12r et cetera
634 // where "r" indicates reverse sensing.
635 // This is is consistent with conventional pilot lore
636 // e.g. http://www.allstar.fiu.edu/aerojava/ILS.htm
637 // but inconsistent with
638 // http://www.freepatentsonline.com/3757338.html
640 // It may be that some of each exist.
641 if (deflectionAngle < 0) {
642 deflectionAngle = 1.5 * sawtooth(deflectionAngle / 1.5);
644 // no false GS below the true GS
648 // GS is documented to be 1.4 degrees thick,
649 // i.e. plus or minus 0.7 degrees from the midline:
650 SG_CLAMP_RANGE(deflectionAngle, -0.7, 0.7);
652 // Many older instrument xml frontends depend on
653 // the un-normalized gs-needle-deflection.
654 // Apparently the interface standard is plus or minus 3.5 "volts"
655 // for a full-scale deflection:
656 _gsNeedleDeflection = deflectionAngle * 5.0;
657 _gsNeedleDeflection *= signal_quality_norm;
659 _gsNeedleDeflectionNorm = (deflectionAngle / 0.7) * signal_quality_norm;
661 //////////////////////////////////////////////////////////
662 // Calculate desired rate of climb for intercepting the GS
663 //////////////////////////////////////////////////////////
664 double gs_diff = target_gs - _gsDirect;
665 // convert desired vertical path angle into a climb rate
666 double des_angle = _gsDirect - 10 * gs_diff;
667 /* printf("target_gs=%.1f angle=%.1f gs_diff=%.1f des_angle=%.1f\n",
668 target_gs, _gsDirect, gs_diff, des_angle); */
670 // estimate horizontal speed towards ILS in meters per minute
671 double elapsedDistance = last_x - gsDist;
674 double new_vel = ( elapsedDistance / dt );
675 horiz_vel = 0.99 * horiz_vel + 0.01 * new_vel;
676 /* printf("vel=%.1f (dist=%.1f dt=%.2f)\n", horiz_vel, elapsedDistance, dt);*/
678 gs_rate_of_climb_node
679 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
680 * horiz_vel * SG_METER_TO_FEET );
681 gs_rate_of_climb_fpm_node
682 ->setDoubleValue( gs_rate_of_climb_node->getDoubleValue() * 60 );
685 void FGNavRadio::valueChanged (SGPropertyNode* prop)
687 if (prop == gps_course_node) {
688 if (!nav_slaved_to_gps_node->getBoolValue()) {
692 // GPS desired course has changed, sync up our selected-course
693 double v = prop->getDoubleValue();
694 if (v != sel_radial_node->getDoubleValue()) {
695 sel_radial_node->setDoubleValue(v);
697 } else if (prop == nav_slaved_to_gps_node) {
698 if (prop->getBoolValue()) {
699 // slaved-to-GPS activated, clear obsolete NAV outputs and sync up selected course
701 sel_radial_node->setDoubleValue(gps_course_node->getDoubleValue());
703 // slave-to-GPS enabled/disabled, resync NAV station (update all outputs)
705 _time_before_search_sec = 0;
709 void FGNavRadio::updateGPSSlaved()
711 has_gs_node->setBoolValue(gps_has_gs_node->getBoolValue());
713 _toFlag = gps_to_flag_node->getBoolValue();
714 _fromFlag = gps_from_flag_node->getBoolValue();
716 bool gpsValid = (_toFlag | _fromFlag);
717 inrange_node->setBoolValue(gpsValid);
719 signal_quality_norm_node->setDoubleValue(0.0);
720 _cdiDeflection = 0.0;
721 _cdiCrossTrackErrorM = 0.0;
722 _gsNeedleDeflection = 0.0;
723 _gsNeedleDeflectionNorm = 0.0;
727 // this is unfortunate, but panel instruments use this value to decide
728 // if the navradio output is valid.
729 signal_quality_norm_node->setDoubleValue(1.0);
731 _cdiDeflection = gps_cdi_deflection_node->getDoubleValue();
732 // clmap to some range (+/- 10 degrees) as the regular deflection
733 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
735 _cdiCrossTrackErrorM = gps_xtrack_error_nm_node->getDoubleValue() * SG_NM_TO_METER;
736 _gsNeedleDeflection = 0.0; // FIXME, supply this
738 double trtrue = gps_course_node->getDoubleValue() + _magvarNode->getDoubleValue();
739 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
740 target_radial_true_node->setDoubleValue( trtrue );
743 void FGNavRadio::updateCDI(double dt)
745 bool cdi_serviceable = cdi_serviceable_node->getBoolValue();
746 bool inrange = inrange_node->getBoolValue();
748 if (tofrom_serviceable_node->getBoolValue()) {
749 to_flag_node->setBoolValue(_toFlag);
750 from_flag_node->setBoolValue(_fromFlag);
752 to_flag_node->setBoolValue(false);
753 from_flag_node->setBoolValue(false);
756 if (!cdi_serviceable) {
757 _cdiDeflection = 0.0;
758 _cdiCrossTrackErrorM = 0.0;
761 cdi_deflection_node->setDoubleValue(_cdiDeflection);
762 cdi_deflection_norm_node->setDoubleValue(_cdiDeflection * 0.1);
763 cdi_xtrack_error_node->setDoubleValue(_cdiCrossTrackErrorM);
765 //////////////////////////////////////////////////////////
766 // compute an approximate ground track heading error
767 //////////////////////////////////////////////////////////
768 double hdg_error = 0.0;
769 if ( inrange && cdi_serviceable ) {
770 double vn = fgGetDouble( "/velocities/speed-north-fps" );
771 double ve = fgGetDouble( "/velocities/speed-east-fps" );
772 double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
773 if ( gnd_trk_true < 0.0 ) { gnd_trk_true += 360.0; }
775 SGPropertyNode *true_hdg
776 = fgGetNode("/orientation/heading-deg", true);
777 hdg_error = gnd_trk_true - true_hdg->getDoubleValue();
779 // cout << "ground track = " << gnd_trk_true
780 // << " orientation = " << true_hdg->getDoubleValue() << endl;
782 cdi_xtrack_hdg_err_node->setDoubleValue( hdg_error );
784 //////////////////////////////////////////////////////////
785 // Calculate a suggested target heading to smoothly intercept
787 //////////////////////////////////////////////////////////
789 // Now that we have cross track heading adjustment built in,
790 // we shouldn't need to overdrive the heading angle within 8km
793 // The cdi deflection should be +/-10 for a full range of deflection
794 // so multiplying this by 3 gives us +/- 30 degrees heading
796 double adjustment = _cdiDeflection * 3.0;
797 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
799 // determine the target heading to fly to intercept the
800 // tgt_radial = target radial (true) + cdi offset adjustment -
801 // xtrack heading error adjustment
803 double trtrue = target_radial_true_node->getDoubleValue();
804 if ( loc_node->getBoolValue() && backcourse_node->getBoolValue() ) {
805 // tuned to a localizer and backcourse mode activated
806 trtrue += 180.0; // reverse the target localizer heading
807 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
808 nta_hdg = trtrue - adjustment - hdg_error;
810 nta_hdg = trtrue + adjustment - hdg_error;
813 SG_NORMALIZE_RANGE(nta_hdg, 0.0, 360.0);
814 target_auto_hdg_node->setDoubleValue( nta_hdg );
816 //////////////////////////////////////////////////////////
817 // compute the time to intercept selected radial (based on
818 // current and last cross track errors and dt)
819 //////////////////////////////////////////////////////////
821 if ( inrange && cdi_serviceable ) {
822 double cur_rate = (last_xtrack_error - _cdiCrossTrackErrorM) / dt;
823 xrate_ms = 0.99 * xrate_ms + 0.01 * cur_rate;
824 if ( fabs(xrate_ms) > 0.00001 ) {
825 t = _cdiCrossTrackErrorM / xrate_ms;
830 time_to_intercept->setDoubleValue( t );
832 if (!gs_serviceable_node->getBoolValue() ) {
833 _gsNeedleDeflection = 0.0;
834 _gsNeedleDeflectionNorm = 0.0;
836 gs_deflection_node->setDoubleValue(_gsNeedleDeflection);
837 gs_deflection_deg_node->setDoubleValue(_gsNeedleDeflectionNorm * 0.7);
838 gs_deflection_norm_node->setDoubleValue(_gsNeedleDeflectionNorm);
839 gs_direct_node->setDoubleValue(_gsDirect);
841 last_xtrack_error = _cdiCrossTrackErrorM;
844 void FGNavRadio::updateAudio( double dt )
846 if (!_navaid || !inrange_node->getBoolValue() || !nav_serviceable_node->getBoolValue()) {
847 _audioIdent->setIdent("", 0.0 );
851 // play station ident via audio system if on + ident,
852 // otherwise turn it off
853 if (!power_btn_node->getBoolValue()
854 || !(bus_power_node->getDoubleValue() > 1.0)
855 || !ident_btn_node->getBoolValue()
856 || !audio_btn_node->getBoolValue() ) {
857 _audioIdent->setIdent("", 0.0 );
861 _audioIdent->setIdent( _navaid->get_trans_ident(), vol_btn_node->getFloatValue() );
863 _audioIdent->update( dt );
866 FGNavRecord* FGNavRadio::findPrimaryNavaid(const SGGeod& aPos, double aFreqMHz)
868 FGNavRecord* nav = globals->get_navlist()->findByFreq(aFreqMHz, aPos);
873 return globals->get_loclist()->findByFreq(aFreqMHz, aPos);
876 // Update current nav/adf radio stations based on current position
877 void FGNavRadio::search()
879 // set delay for next search
880 _time_before_search_sec = 1.0;
882 double freq = freq_node->getDoubleValue();
884 // immediate NAV search when frequency has changed (toggle between nav and g/s search otherwise)
885 _nav_search |= (_last_freq != freq);
887 // do we need to search a new NAV station in this iteration?
891 FGNavRecord* nav = findPrimaryNavaid(globals->get_aircraft_position(), freq);
892 if (nav == _navaid) {
893 if (nav && (nav->type() != FGPositioned::VOR))
894 _nav_search = false; // search glideslope on next iteration
895 return; // nav hasn't changed, we're done
897 // remember new navaid station
901 // search glideslope station
902 if ((_navaid.valid()) && (_navaid->type() != FGPositioned::VOR))
904 FGNavRecord* gs = globals->get_gslist()->findByFreq(freq, globals->get_aircraft_position());
905 if ((!_nav_search) && (gs == _gs))
907 _nav_search = true; // search NAV on next iteration
908 return; // g/s hasn't changed, neither has nav - we're done
910 // remember new glideslope station
914 _nav_search = true; // search NAV on next iteration
916 // nav or gs station has changed
920 // Update current nav/adf/glideslope outputs when station has changed
921 void FGNavRadio::updateNav()
923 // update necessary, nav and/or gs has changed
924 FGNavRecord* nav = _navaid;
925 string identBuffer(4, ' ');
927 nav_id_node->setStringValue(nav->get_ident());
928 identBuffer = simgear::strutils::rpad( nav->ident(), 4, ' ' );
930 effective_range = adjustNavRange(nav->get_elev_ft(), globals->get_aircraft_position().getElevationM(), nav->get_range());
931 loc_node->setBoolValue(nav->type() != FGPositioned::VOR);
932 twist = nav->get_multiuse();
934 if (nav->type() == FGPositioned::VOR) {
935 target_radial = sel_radial_node->getDoubleValue();
937 } else { // ILS or LOC
938 _localizerWidth = nav->localizerWidth();
940 effective_range = nav->get_range();
942 target_radial = nav->get_multiuse();
943 SG_NORMALIZE_RANGE(target_radial, 0.0, 360.0);
946 int tmp = (int)(_gs->get_multiuse() / 1000.0);
947 target_gs = (double)tmp / 100.0;
949 double gs_radial = fmod(_gs->get_multiuse(), 1000.0);
950 SG_NORMALIZE_RANGE(gs_radial, 0.0, 360.0);
951 _gsCart = _gs->cart();
953 // GS axis unit tangent vector
954 // (along the runway):
955 _gsAxis = tangentVector(_gs->geod(), gs_radial);
957 // GS baseline unit tangent vector
958 // (transverse to the runway along the ground)
959 _gsBaseline = tangentVector(_gs->geod(), gs_radial + 90.0);
960 _gsVertical = cross(_gsBaseline, _gsAxis);
961 } // of have glideslope
962 } // of found LOC or ILS
964 } else { // found nothing
966 nav_id_node->setStringValue("");
967 loc_node->setBoolValue(false);
968 _audioIdent->setIdent("", 0.0 );
971 has_gs_node->setBoolValue(_gs != NULL);
972 is_valid_node->setBoolValue(nav != NULL);
973 id_c1_node->setIntValue( (int)identBuffer[0] );
974 id_c2_node->setIntValue( (int)identBuffer[1] );
975 id_c3_node->setIntValue( (int)identBuffer[2] );
976 id_c4_node->setIntValue( (int)identBuffer[3] );