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);
82 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
83 lon_node(fgGetNode("/position/longitude-deg", true)),
84 lat_node(fgGetNode("/position/latitude-deg", true)),
85 alt_node(fgGetNode("/position/altitude-ft", true)),
90 sel_radial_node(NULL),
94 backcourse_node(NULL),
95 nav_serviceable_node(NULL),
96 cdi_serviceable_node(NULL),
97 gs_serviceable_node(NULL),
98 tofrom_serviceable_node(NULL),
100 fmt_alt_freq_node(NULL),
103 recip_radial_node(NULL),
104 target_radial_true_node(NULL),
105 target_auto_hdg_node(NULL),
106 time_to_intercept(NULL),
108 from_flag_node(NULL),
110 signal_quality_norm_node(NULL),
111 cdi_deflection_node(NULL),
112 cdi_deflection_norm_node(NULL),
113 cdi_xtrack_error_node(NULL),
114 cdi_xtrack_hdg_err_node(NULL),
118 gs_deflection_node(NULL),
119 gs_deflection_deg_node(NULL),
120 gs_deflection_norm_node(NULL),
121 gs_rate_of_climb_node(NULL),
128 nav_slaved_to_gps_node(NULL),
129 gps_cdi_deflection_node(NULL),
130 gps_to_flag_node(NULL),
131 gps_from_flag_node(NULL),
132 gps_has_gs_node(NULL),
133 gps_xtrack_error_nm_node(NULL),
140 last_xtrack_error(0.0),
141 _localizerWidth(5.0),
142 _name(node->getStringValue("name", "nav")),
143 _num(node->getIntValue("number", 0)),
144 _time_before_search_sec(-1.0)
146 SGPath path( globals->get_fg_root() );
148 term.append( "Navaids/range.term" );
150 low.append( "Navaids/range.low" );
152 high.append( "Navaids/range.high" );
154 term_tbl = new SGInterpTable( term.str() );
155 low_tbl = new SGInterpTable( low.str() );
156 high_tbl = new SGInterpTable( high.str() );
161 FGNavRadio::~FGNavRadio()
175 branch = "/instrumentation/" + _name;
177 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
180 fgGetNode(("/systems/electrical/outputs/" + _name).c_str(), true);
183 is_valid_node = node->getChild("data-is-valid", 0, true);
184 power_btn_node = node->getChild("power-btn", 0, true);
185 power_btn_node->setBoolValue( true );
186 vol_btn_node = node->getChild("volume", 0, true);
187 ident_btn_node = node->getChild("ident", 0, true);
188 ident_btn_node->setBoolValue( true );
189 audio_btn_node = node->getChild("audio-btn", 0, true);
190 audio_btn_node->setBoolValue( true );
191 backcourse_node = node->getChild("back-course-btn", 0, true);
192 backcourse_node->setBoolValue( false );
193 nav_serviceable_node = node->getChild("serviceable", 0, true);
194 cdi_serviceable_node = (node->getChild("cdi", 0, true))
195 ->getChild("serviceable", 0, true);
196 gs_serviceable_node = (node->getChild("gs", 0, true))
197 ->getChild("serviceable");
198 tofrom_serviceable_node = (node->getChild("to-from", 0, true))
199 ->getChild("serviceable", 0, true);
202 SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
203 freq_node = subnode->getChild("selected-mhz", 0, true);
204 alt_freq_node = subnode->getChild("standby-mhz", 0, true);
205 fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
206 fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 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_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
235 gs_dist_node = node->getChild("gs-distance", 0, true);
236 nav_id_node = node->getChild("nav-id", 0, true);
237 id_c1_node = node->getChild("nav-id_asc1", 0, true);
238 id_c2_node = node->getChild("nav-id_asc2", 0, true);
239 id_c3_node = node->getChild("nav-id_asc3", 0, true);
240 id_c4_node = node->getChild("nav-id_asc4", 0, true);
242 // gps slaving support
243 nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
244 gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
245 gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
246 gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
247 gps_has_gs_node = fgGetNode("/instrumentation/gps/has-gs", true);
248 gps_course_node = fgGetNode("/instrumentation/gps/selected-course-deg", true);
249 gps_xtrack_error_nm_node = fgGetNode("/instrumentation/gps/wp/wp[1]/course-error-nm", true);
251 std::ostringstream temp;
252 temp << _name << "nav-ident" << _num;
253 nav_fx_name = temp.str();
254 temp << _name << "dme-ident" << _num;
255 dme_fx_name = temp.str();
261 std::ostringstream temp;
264 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
269 FGNavRadio::unbind ()
271 std::ostringstream temp;
274 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
278 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
279 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
280 double nominalRange )
282 // extend out actual usable range to be 1.3x the published safe range
283 const double usability_factor = 1.3;
285 // assumptions we model the standard service volume, plus
286 // ... rather than specifying a cylinder, we model a cone that
287 // contains the cylinder. Then we put an upside down cone on top
288 // to model diminishing returns at too-high altitudes.
290 // altitude difference
291 double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
292 // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
293 // << " station elev = " << stationElev << endl;
295 if ( nominalRange < 25.0 + SG_EPSILON ) {
296 // Standard Terminal Service Volume
297 return term_tbl->interpolate( alt ) * usability_factor;
298 } else if ( nominalRange < 50.0 + SG_EPSILON ) {
299 // Standard Low Altitude Service Volume
300 // table is based on range of 40, scale to actual range
301 return low_tbl->interpolate( alt ) * nominalRange / 40.0
304 // Standard High Altitude Service Volume
305 // table is based on range of 130, scale to actual range
306 return high_tbl->interpolate( alt ) * nominalRange / 130.0
312 // model standard ILS service volumes as per AIM 1-1-9
313 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
314 double offsetDegrees, double distance )
316 // assumptions we model the standard service volume, plus
318 // altitude difference
319 // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
320 // double offset = fabs( offsetDegrees );
322 // if ( offset < 10 ) {
323 // return FG_ILS_DEFAULT_RANGE;
324 // } else if ( offset < 35 ) {
325 // return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
326 // } else if ( offset < 45 ) {
327 // return (45 - offset);
328 // } else if ( offset > 170 ) {
329 // return FG_ILS_DEFAULT_RANGE;
330 // } else if ( offset > 145 ) {
331 // return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
332 // } else if ( offset > 135 ) {
333 // return (offset - 135);
337 return FG_LOC_DEFAULT_RANGE;
341 //////////////////////////////////////////////////////////////////////////
342 // Update the various nav values based on position and valid tuned in navs
343 //////////////////////////////////////////////////////////////////////////
345 FGNavRadio::update(double dt)
351 // Create "formatted" versions of the nav frequencies for
352 // instrument displays.
354 sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
355 fmt_freq_node->setStringValue(tmp);
356 sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
357 fmt_alt_freq_node->setStringValue(tmp);
359 if (power_btn_node->getBoolValue()
360 && (bus_power_node->getDoubleValue() > 1.0)
361 && nav_serviceable_node->getBoolValue() )
363 if (nav_slaved_to_gps_node->getBoolValue()) {
377 void FGNavRadio::clearOutputs()
379 inrange_node->setBoolValue( false );
380 cdi_deflection_node->setDoubleValue( 0.0 );
381 cdi_deflection_norm_node->setDoubleValue( 0.0 );
382 cdi_xtrack_error_node->setDoubleValue( 0.0 );
383 cdi_xtrack_hdg_err_node->setDoubleValue( 0.0 );
384 time_to_intercept->setDoubleValue( 0.0 );
385 gs_deflection_node->setDoubleValue( 0.0 );
386 gs_deflection_deg_node->setDoubleValue(0.0);
387 gs_deflection_norm_node->setDoubleValue(0.0);
389 to_flag_node->setBoolValue( false );
390 from_flag_node->setBoolValue( false );
393 void FGNavRadio::updateReceiver(double dt)
395 // Do a nav station search only once a second to reduce
396 // unnecessary work. (Also, make sure to do this before caching
398 _time_before_search_sec -= dt;
399 if ( _time_before_search_sec < 0 ) {
404 _cdiDeflection = 0.0;
405 _cdiCrossTrackErrorM = 0.0;
406 _toFlag = _fromFlag = false;
407 _gsNeedleDeflection = 0.0;
408 _gsNeedleDeflectionNorm = 0.0;
409 inrange_node->setBoolValue(false);
413 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
414 lat_node->getDoubleValue(),
415 alt_node->getDoubleValue());
417 double nav_elev = _navaid->get_elev_ft();
418 SGVec3d aircraft = SGVec3d::fromGeod(pos);
419 double loc_dist = dist(aircraft, _navaid->cart());
420 loc_dist_node->setDoubleValue( loc_dist );
421 bool is_loc = loc_node->getBoolValue();
422 double signal_quality_norm = signal_quality_norm_node->getDoubleValue();
425 //////////////////////////////////////////////////////////
426 // compute forward and reverse wgs84 headings to localizer
427 //////////////////////////////////////////////////////////
429 SGGeodesy::inverse(pos, _navaid->geod(), hdg, az2, s);
430 heading_node->setDoubleValue(hdg);
431 double radial = az2 - twist;
432 double recip = radial + 180.0;
433 SG_NORMALIZE_RANGE(recip, 0.0, 360.0);
434 radial_node->setDoubleValue( radial );
435 recip_radial_node->setDoubleValue( recip );
437 //////////////////////////////////////////////////////////
438 // compute the target/selected radial in "true" heading
439 //////////////////////////////////////////////////////////
441 target_radial = sel_radial_node->getDoubleValue();
444 // VORs need twist (mag-var) added; ILS/LOCs don't but we set twist to 0.0
445 double trtrue = target_radial + twist;
446 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
447 target_radial_true_node->setDoubleValue( trtrue );
449 //////////////////////////////////////////////////////////
450 // adjust reception range for altitude
451 // FIXME: make sure we are using the navdata range now that
452 // it is valid in the data file
453 //////////////////////////////////////////////////////////
455 double offset = radial - target_radial;
456 SG_NORMALIZE_RANGE(offset, -180.0, 180.0);
458 = adjustILSRange( nav_elev, pos.getElevationM(), offset,
459 loc_dist * SG_METER_TO_NM );
462 = adjustNavRange( nav_elev, pos.getElevationM(), _navaid->get_range() );
465 double effective_range_m = effective_range * SG_NM_TO_METER;
467 //////////////////////////////////////////////////////////
468 // compute signal quality
469 // 100% within effective_range
470 // decreases 1/x^2 further out
471 //////////////////////////////////////////////////////////
472 double last_signal_quality_norm = signal_quality_norm;
474 if ( loc_dist < effective_range_m ) {
475 signal_quality_norm = 1.0;
477 double range_exceed_norm = loc_dist/effective_range_m;
478 signal_quality_norm = 1/(range_exceed_norm*range_exceed_norm);
481 signal_quality_norm = fgGetLowPass( last_signal_quality_norm,
482 signal_quality_norm, dt );
484 signal_quality_norm_node->setDoubleValue( signal_quality_norm );
485 bool inrange = signal_quality_norm > 0.2;
486 inrange_node->setBoolValue( inrange );
488 //////////////////////////////////////////////////////////
489 // compute to/from flag status
490 //////////////////////////////////////////////////////////
495 double offset = fabs(radial - target_radial);
496 _toFlag = (offset > 90.0 && offset < 270.0);
498 _fromFlag = !_toFlag;
500 _toFlag = _fromFlag = false;
504 double r = target_radial - radial;
505 SG_NORMALIZE_RANGE(r, -180.0, 180.0);
508 // The factor of 30.0 gives a period of 120 which gives us 3 cycles and six
509 // zeros i.e. six courses: one front course, one back course, and four
510 // false courses. Three of the six are reverse sensing.
511 _cdiDeflection = 30.0 * sawtooth(r / 30.0);
512 const double VOR_FULL_ARC = 20.0; // VOR is -10 .. 10 degree swing
513 _cdiDeflection *= VOR_FULL_ARC / _localizerWidth; // increased localiser sensitivity
515 // handle the TO side of the VOR
516 if (fabs(r) > 90.0) {
517 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
520 } // of non-localiser case
522 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
523 _cdiDeflection *= signal_quality_norm;
525 // cross-track error (in metres)
526 _cdiCrossTrackErrorM = loc_dist * sin(r * SGD_DEGREES_TO_RADIANS);
528 updateGlideSlope(dt, aircraft, signal_quality_norm);
530 last_loc_dist = loc_dist;
533 void FGNavRadio::updateGlideSlope(double dt, const SGVec3d& aircraft, double signal_quality_norm)
535 _gsNeedleDeflection = 0.0;
536 if (!_gs || !inrange_node->getBoolValue()) {
537 gs_dist_node->setDoubleValue( 0.0 );
541 double gsDist = dist(aircraft, _gsCart);
542 gs_dist_node->setDoubleValue(gsDist);
543 if (gsDist > (_gs->get_range() * SG_NM_TO_METER)) {
547 SGVec3d pos = aircraft - _gsCart; // relative vector from gs antenna to aircraft
548 // The positive GS axis points along the runway in the landing direction,
549 // toward the far end, not toward the approach area, so we need a - sign here:
550 double dot_h = -dot(pos, _gsAxis);
551 double dot_v = dot(pos, _gsVertical);
552 double angle = atan2(dot_v, dot_h) * SGD_RADIANS_TO_DEGREES;
553 double deflectionAngle = target_gs - angle;
555 // Construct false glideslopes. The scale factor of 1.5
556 // in the sawtooth gives a period of 6 degrees.
557 // There will be zeros at 3, 6r, 9, 12r et cetera
558 // where "r" indicates reverse sensing.
559 // This is is consistent with conventional pilot lore
560 // e.g. http://www.allstar.fiu.edu/aerojava/ILS.htm
561 // but inconsistent with
562 // http://www.freepatentsonline.com/3757338.html
564 // It may be that some of each exist.
565 if (deflectionAngle < 0) {
566 deflectionAngle = 1.5 * sawtooth(deflectionAngle / 1.5);
568 // no false GS below the true GS
571 _gsNeedleDeflection = deflectionAngle * 5.0;
572 _gsNeedleDeflection *= signal_quality_norm;
574 SG_CLAMP_RANGE(deflectionAngle, -0.7, 0.7);
575 _gsNeedleDeflectionNorm = (deflectionAngle / 0.7) * signal_quality_norm;
577 //////////////////////////////////////////////////////////
578 // Calculate desired rate of climb for intercepting the GS
579 //////////////////////////////////////////////////////////
580 double gs_diff = target_gs - angle;
581 // convert desired vertical path angle into a climb rate
582 double des_angle = angle - 10 * gs_diff;
584 // estimate horizontal speed towards ILS in meters per minute
585 double elapsedDistance = last_x - gsDist;
588 double new_vel = ( elapsedDistance / dt );
589 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
591 gs_rate_of_climb_node
592 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
593 * horiz_vel * SG_METER_TO_FEET );
596 void FGNavRadio::updateGPSSlaved()
598 has_gs_node->setBoolValue(gps_has_gs_node->getBoolValue());
600 _toFlag = gps_to_flag_node->getBoolValue();
601 _fromFlag = gps_from_flag_node->getBoolValue();
603 bool gpsValid = (_toFlag | _fromFlag);
604 inrange_node->setBoolValue(gpsValid);
606 signal_quality_norm_node->setDoubleValue(0.0);
607 _cdiDeflection = 0.0;
608 _cdiCrossTrackErrorM = 0.0;
609 _gsNeedleDeflection = 0.0;
613 // this is unfortunate, but panel instruments use this value to decide
614 // if the navradio output is valid.
615 signal_quality_norm_node->setDoubleValue(1.0);
617 _cdiDeflection = gps_cdi_deflection_node->getDoubleValue();
618 // clmap to some range (+/- 10 degrees) as the regular deflection
619 SG_CLAMP_RANGE(_cdiDeflection, -10.0, 10.0 );
621 _cdiCrossTrackErrorM = gps_xtrack_error_nm_node->getDoubleValue() * SG_NM_TO_METER;
622 _gsNeedleDeflection = 0.0; // FIXME, supply this
624 //sel_radial_node->setDoubleValue(gps_course_node->getDoubleValue());
627 void FGNavRadio::updateCDI(double dt)
629 bool cdi_serviceable = cdi_serviceable_node->getBoolValue();
630 bool inrange = inrange_node->getBoolValue();
632 if (tofrom_serviceable_node->getBoolValue()) {
633 to_flag_node->setBoolValue(_toFlag);
634 from_flag_node->setBoolValue(_fromFlag);
636 to_flag_node->setBoolValue(false);
637 from_flag_node->setBoolValue(false);
640 if (!cdi_serviceable) {
641 _cdiDeflection = 0.0;
642 _cdiCrossTrackErrorM = 0.0;
645 cdi_deflection_node->setDoubleValue(_cdiDeflection);
646 cdi_deflection_norm_node->setDoubleValue(_cdiDeflection * 0.1);
647 cdi_xtrack_error_node->setDoubleValue(_cdiCrossTrackErrorM);
649 //////////////////////////////////////////////////////////
650 // compute an approximate ground track heading error
651 //////////////////////////////////////////////////////////
652 double hdg_error = 0.0;
653 if ( inrange && cdi_serviceable ) {
654 double vn = fgGetDouble( "/velocities/speed-north-fps" );
655 double ve = fgGetDouble( "/velocities/speed-east-fps" );
656 double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
657 if ( gnd_trk_true < 0.0 ) { gnd_trk_true += 360.0; }
659 SGPropertyNode *true_hdg
660 = fgGetNode("/orientation/heading-deg", true);
661 hdg_error = gnd_trk_true - true_hdg->getDoubleValue();
663 // cout << "ground track = " << gnd_trk_true
664 // << " orientation = " << true_hdg->getDoubleValue() << endl;
666 cdi_xtrack_hdg_err_node->setDoubleValue( hdg_error );
668 //////////////////////////////////////////////////////////
669 // Calculate a suggested target heading to smoothly intercept
671 //////////////////////////////////////////////////////////
673 // Now that we have cross track heading adjustment built in,
674 // we shouldn't need to overdrive the heading angle within 8km
677 // The cdi deflection should be +/-10 for a full range of deflection
678 // so multiplying this by 3 gives us +/- 30 degrees heading
680 double adjustment = _cdiDeflection * 3.0;
681 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
683 // determine the target heading to fly to intercept the
684 // tgt_radial = target radial (true) + cdi offset adjustmest -
685 // xtrack heading error adjustment
687 double trtrue = target_radial_true_node->getDoubleValue();
688 if ( loc_node->getBoolValue() && backcourse_node->getBoolValue() ) {
689 // tuned to a localizer and backcourse mode activated
690 trtrue += 180.0; // reverse the target localizer heading
691 SG_NORMALIZE_RANGE(trtrue, 0.0, 360.0);
692 nta_hdg = trtrue - adjustment - hdg_error;
694 nta_hdg = trtrue + adjustment - hdg_error;
697 SG_NORMALIZE_RANGE(nta_hdg, 0.0, 360.0);
698 target_auto_hdg_node->setDoubleValue( nta_hdg );
700 //////////////////////////////////////////////////////////
701 // compute the time to intercept selected radial (based on
702 // current and last cross track errors and dt
703 //////////////////////////////////////////////////////////
705 if ( inrange && cdi_serviceable ) {
706 double xrate_ms = (last_xtrack_error - _cdiCrossTrackErrorM) / dt;
707 if ( fabs(xrate_ms) > 0.00001 ) {
708 t = _cdiCrossTrackErrorM / xrate_ms;
713 time_to_intercept->setDoubleValue( t );
715 if (!gs_serviceable_node->getBoolValue() ) {
716 _gsNeedleDeflection = 0.0;
717 _gsNeedleDeflectionNorm = 0.0;
719 gs_deflection_node->setDoubleValue(_gsNeedleDeflection);
720 gs_deflection_deg_node->setDoubleValue(_gsNeedleDeflectionNorm * 0.7);
721 gs_deflection_norm_node->setDoubleValue(_gsNeedleDeflectionNorm);
723 last_xtrack_error = _cdiCrossTrackErrorM;
726 void FGNavRadio::updateAudio()
728 if (!_navaid || !inrange_node->getBoolValue() || !nav_serviceable_node->getBoolValue()) {
732 // play station ident via audio system if on + ident,
733 // otherwise turn it off
734 if (!power_btn_node->getBoolValue()
735 || !(bus_power_node->getDoubleValue() > 1.0)
736 || !ident_btn_node->getBoolValue()
737 || !audio_btn_node->getBoolValue() ) {
738 globals->get_soundmgr()->stop( nav_fx_name );
739 globals->get_soundmgr()->stop( dme_fx_name );
743 SGSoundSample *sound = globals->get_soundmgr()->find( nav_fx_name );
744 double vol = vol_btn_node->getDoubleValue();
745 SG_CLAMP_RANGE(vol, 0.0, 1.0);
747 if ( sound != NULL ) {
748 sound->set_volume( vol );
750 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find nav-vor-ident sound" );
753 sound = globals->get_soundmgr()->find( dme_fx_name );
754 if ( sound != NULL ) {
755 sound->set_volume( vol );
757 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find nav-dme-ident sound" );
760 if ( last_time < globals->get_time_params()->get_cur_time() - 30 ) {
761 last_time = globals->get_time_params()->get_cur_time();
765 if ( play_count < 4 ) {
767 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
768 globals->get_soundmgr()->play_once( nav_fx_name );
771 } else if ( play_count < 5 && has_dme) {
773 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
774 !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
775 globals->get_soundmgr()->play_once( dme_fx_name );
781 FGNavRecord* FGNavRadio::findPrimaryNavaid(const SGGeod& aPos, double aFreqMHz)
783 FGNavRecord* nav = globals->get_navlist()->findByFreq(aFreqMHz, aPos);
788 return globals->get_loclist()->findByFreq(aFreqMHz, aPos);
791 // Update current nav/adf radio stations based on current postition
792 void FGNavRadio::search()
794 _time_before_search_sec = 1.0;
795 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
796 lat_node->getDoubleValue(), alt_node->getDoubleValue());
797 double freq = freq_node->getDoubleValue();
799 FGNavRecord* nav = findPrimaryNavaid(pos, freq);
800 if (nav == _navaid) {
801 return; // found the same as last search, we're done
805 char identBuffer[5] = " ";
807 FGNavRecord* dme = globals->get_dmelist()->findByFreq(freq, pos);
808 has_dme = (dme != NULL);
810 nav_id_node->setStringValue(nav->get_ident());
811 strncpy(identBuffer, nav->ident().c_str(), 5);
813 effective_range = adjustNavRange(nav->get_elev_ft(), pos.getElevationM(), nav->get_range());
814 loc_node->setBoolValue(nav->type() != FGPositioned::VOR);
815 twist = nav->get_multiuse();
817 if (nav->type() == FGPositioned::VOR) {
818 target_radial = sel_radial_node->getDoubleValue();
820 } else { // ILS or LOC
821 _gs = globals->get_gslist()->findByFreq(freq, pos);
822 _localizerWidth = localizerWidth(nav);
823 has_gs_node->setBoolValue(_gs != NULL);
825 effective_range = nav->get_range();
827 target_radial = nav->get_multiuse();
828 SG_NORMALIZE_RANGE(target_radial, 0.0, 360.0);
831 int tmp = (int)(_gs->get_multiuse() / 1000.0);
832 target_gs = (double)tmp / 100.0;
834 // GS axis unit tangent vector
835 // (along the runway)
836 _gsCart = _gs->cart();
837 _gsAxis = tangentVector(_gs->geod(), _gsCart, target_radial);
839 // GS baseline unit tangent vector
840 // (perpendicular to the runay along the ground)
841 SGVec3d baseline = tangentVector(_gs->geod(), _gsCart, target_radial + 90.0);
842 _gsVertical = cross(baseline, _gsAxis);
843 } // of have glideslope
844 } // of found LOC or ILS
847 } else { // found nothing
849 nav_id_node->setStringValue("");
851 globals->get_soundmgr()->remove( nav_fx_name );
852 globals->get_soundmgr()->remove( dme_fx_name );
855 is_valid_node->setBoolValue(nav != NULL);
856 id_c1_node->setIntValue( (int)identBuffer[0] );
857 id_c2_node->setIntValue( (int)identBuffer[1] );
858 id_c3_node->setIntValue( (int)identBuffer[2] );
859 id_c4_node->setIntValue( (int)identBuffer[3] );
862 double FGNavRadio::localizerWidth(FGNavRecord* aLOC)
864 FGRunway* rwy = aLOC->runway();
867 SGVec3d thresholdCart(SGVec3d::fromGeod(rwy->threshold()));
868 double axisLength = dist(aLOC->cart(), thresholdCart);
869 double landingLength = dist(thresholdCart, SGVec3d::fromGeod(rwy->end()));
871 // Reference: http://dcaa.slv.dk:8000/icaodocs/
872 // ICAO standard width at threshold is 210 m = 689 feet = approx 700 feet.
873 // ICAO 3.1.1 half course = DDM = 0.0775
874 // ICAO 3.1.3.7.1 Sensitivity 0.00145 DDM/m at threshold
875 // implies peg-to-peg of 214 m ... we will stick with 210.
876 // ICAO 3.1.3.7.1 "Course sector angle shall not exceed 6 degrees."
878 // Very short runway: less than 1200 m (4000 ft) landing length:
879 if (landingLength < 1200.0) {
880 // ICAO fudges localizer sensitivity for very short runways.
881 // This produces a non-monotonic sensitivity-versus length relation.
882 axisLength += 1050.0;
885 // Example: very short: San Diego KMYF (Montgomery Field) ILS RWY 28R
886 // Example: short: Tom's River KMJX (Robert J. Miller) ILS RWY 6
887 // Example: very long: Denver KDEN (Denver) ILS RWY 16R
888 double raw_width = 210.0 / axisLength * SGD_RADIANS_TO_DEGREES;
889 return raw_width < 6.0? raw_width : 6.0;
892 void FGNavRadio::audioNavidChanged()
894 if ( globals->get_soundmgr()->exists(nav_fx_name)) {
895 globals->get_soundmgr()->remove(nav_fx_name);
899 string trans_ident(_navaid->get_trans_ident());
900 SGSoundSample* sound = morse.make_ident(trans_ident, LO_FREQUENCY);
901 sound->set_volume( 0.3 );
902 if (!globals->get_soundmgr()->add( sound, nav_fx_name )) {
903 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
906 if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
907 globals->get_soundmgr()->remove( dme_fx_name );
910 sound = morse.make_ident( trans_ident, HI_FREQUENCY );
911 sound->set_volume( 0.3 );
912 globals->get_soundmgr()->add( sound, dme_fx_name );
914 int offset = (int)(sg_random() * 30.0);
915 play_count = offset / 4;
916 last_time = globals->get_time_params()->get_cur_time() - offset;
917 } catch (sg_io_exception& e) {
918 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());