]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/navradio.cxx
b5219bb1d7390d7ab1a35c3305d3fc8f040ce0eb
[flightgear.git] / src / Instrumentation / navradio.cxx
1 // navradio.cxx -- class to manage a nav radio instance
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000 - 2002  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31
32 #include <simgear/compiler.h>
33 #include <simgear/sg_inlines.h>
34 #include <simgear/math/sg_random.h>
35 #include <simgear/math/vector.hxx>
36
37 #include <Aircraft/aircraft.hxx>
38 #include <Navaids/navlist.hxx>
39
40 #include "navradio.hxx"
41
42 #include <string>
43 SG_USING_STD(string);
44
45
46 // Constructor
47 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
48     lon_node(fgGetNode("/position/longitude-deg", true)),
49     lat_node(fgGetNode("/position/latitude-deg", true)),
50     alt_node(fgGetNode("/position/altitude-ft", true)),
51     power_btn_node(NULL),
52     freq_node(NULL),
53     alt_freq_node(NULL),
54     fmt_freq_node(NULL),
55     fmt_alt_freq_node(NULL),
56     sel_radial_node(NULL),
57     vol_btn_node(NULL),
58     ident_btn_node(NULL),
59     audio_btn_node(NULL),
60     heading_node(NULL),
61     radial_node(NULL),
62     recip_radial_node(NULL),
63     target_radial_true_node(NULL),
64     target_auto_hdg_node(NULL),
65     to_flag_node(NULL),
66     from_flag_node(NULL),
67     inrange_node(NULL),
68     cdi_deflection_node(NULL),
69     cdi_xtrack_error_node(NULL),
70     has_gs_node(NULL),
71     loc_node(NULL),
72     loc_dist_node(NULL),
73     gs_deflection_node(NULL),
74     gs_rate_of_climb_node(NULL),
75     gs_dist_node(NULL),
76     id_node(NULL),
77     id_c1_node(NULL),
78     id_c2_node(NULL),
79     id_c3_node(NULL),
80     id_c4_node(NULL),
81     last_id(""),
82     last_nav_vor(false),
83     nav_play_count(0),
84     nav_last_time(0),
85     nav_target_radial(0.0),
86     horiz_vel(0.0),
87     last_x(0.0),
88     name("nav"),
89     num(0),
90     _time_before_search_sec(-1.0)
91 {
92     SGPath path( globals->get_fg_root() );
93     SGPath term = path;
94     term.append( "Navaids/range.term" );
95     SGPath low = path;
96     low.append( "Navaids/range.low" );
97     SGPath high = path;
98     high.append( "Navaids/range.high" );
99
100     term_tbl = new SGInterpTable( term.str() );
101     low_tbl = new SGInterpTable( low.str() );
102     high_tbl = new SGInterpTable( high.str() );
103
104     int i;
105     for ( i = 0; i < node->nChildren(); ++i ) {
106         SGPropertyNode *child = node->getChild(i);
107         string cname = child->getName();
108         string cval = child->getStringValue();
109         if ( cname == "name" ) {
110             name = cval;
111         } else if ( cname == "number" ) {
112             num = child->getIntValue();
113         } else {
114             SG_LOG( SG_INSTR, SG_WARN, 
115                     "Error in nav radio config logic" );
116             if ( name.length() ) {
117                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
118             }
119         }
120     }
121
122 }
123
124
125 // Destructor
126 FGNavRadio::~FGNavRadio() 
127 {
128     delete term_tbl;
129     delete low_tbl;
130     delete high_tbl;
131 }
132
133
134 void
135 FGNavRadio::init ()
136 {
137     morse.init();
138
139     string branch;
140     branch = "/instrumentation/" + name;
141
142     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
143
144     bus_power_node = 
145         fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
146
147     // inputs
148     power_btn_node = node->getChild("power-btn", 0, true);
149     power_btn_node->setBoolValue( true );
150     vol_btn_node = node->getChild("volume", 0, true);
151     ident_btn_node = node->getChild("ident", 0, true);
152     ident_btn_node->setBoolValue( true );
153     audio_btn_node = node->getChild("audio-btn", 0, true);
154     audio_btn_node->setBoolValue( true );
155
156     // frequencies
157     SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
158     freq_node = subnode->getChild("selected-mhz", 0, true);
159     alt_freq_node = subnode->getChild("standby-mhz", 0, true);
160     fmt_freq_node = subnode->getChild("selected-mhz-fmt", 0, true);
161     fmt_alt_freq_node = subnode->getChild("standby-mhz-fmt", 0, true);
162
163     // radials
164     subnode = node->getChild("radials", 0, true);
165     sel_radial_node = subnode->getChild("selected-deg", 0, true);
166     radial_node = subnode->getChild("actual-deg", 0, true);
167     recip_radial_node = subnode->getChild("reciprocal-radial-deg", 0, true);
168     target_radial_true_node = subnode->getChild("target-radial-deg", 0, true);
169     target_auto_hdg_node = subnode->getChild("target-auto-hdg-deg", 0, true);
170
171     // outputs
172     heading_node = node->getChild("heading-deg", 0, true);
173     to_flag_node = node->getChild("to-flag", 0, true);
174     from_flag_node = node->getChild("from-flag", 0, true);
175     inrange_node = node->getChild("in-range", 0, true);
176     cdi_deflection_node = node->getChild("heading-needle-deflection", 0, true);
177     cdi_xtrack_error_node = node->getChild("crosstrack-error-m", 0, true);
178     has_gs_node = node->getChild("has-gs", 0, true);
179     loc_node = node->getChild("nav-loc", 0, true);
180     loc_dist_node = node->getChild("nav-distance", 0, true);
181     gs_deflection_node = node->getChild("gs-needle-deflection", 0, true);
182     gs_rate_of_climb_node = node->getChild("gs-rate-of-climb", 0, true);
183     gs_dist_node = node->getChild("gs-distance", 0, true);
184     id_node = node->getChild("nav-id", 0, true);
185     id_c1_node = node->getChild("nav-id_asc1", 0, true);
186     id_c2_node = node->getChild("nav-id_asc2", 0, true);
187     id_c3_node = node->getChild("nav-id_asc3", 0, true);
188     id_c4_node = node->getChild("nav-id_asc4", 0, true);
189
190     nav_serviceable_node = node->getChild("serviceable", 0, true);
191     cdi_serviceable_node = (node->getChild("cdi", 0, true))
192         ->getChild("serviceable", 0, true);
193     gs_serviceable_node = (node->getChild("gs", 0, true))
194         ->getChild("serviceable");
195     tofrom_serviceable_node = (node->getChild("to-from", 0, true))
196         ->getChild("serviceable", 0, true);
197     nav_slaved_to_gps_node = node->getChild("slaved-to-gps", 0, true);
198
199     gps_cdi_deflection_node = fgGetNode("/instrumentation/gps/cdi-deflection", true);
200     gps_to_flag_node = fgGetNode("/instrumentation/gps/to-flag", true);
201     gps_from_flag_node = fgGetNode("/instrumentation/gps/from-flag", true);
202     
203     std::ostringstream temp;
204     temp << name << "nav-ident" << num;
205     nav_fx_name = temp.str();
206     temp << name << "dme-ident" << num;
207     dme_fx_name = temp.str();
208 }
209
210 void
211 FGNavRadio::bind ()
212 {
213     std::ostringstream temp;
214     string branch;
215     temp << num;
216     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
217 }
218
219
220 void
221 FGNavRadio::unbind ()
222 {
223     std::ostringstream temp;
224     string branch;
225     temp << num;
226     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
227 }
228
229
230 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
231 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
232                                  double nominalRange )
233 {
234     // extend out actual usable range to be 1.3x the published safe range
235     const double usability_factor = 1.3;
236
237     // assumptions we model the standard service volume, plus
238     // ... rather than specifying a cylinder, we model a cone that
239     // contains the cylinder.  Then we put an upside down cone on top
240     // to model diminishing returns at too-high altitudes.
241
242     // altitude difference
243     double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
244     // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
245     //      << " station elev = " << stationElev << endl;
246
247     if ( nominalRange < 25.0 + SG_EPSILON ) {
248         // Standard Terminal Service Volume
249         return term_tbl->interpolate( alt ) * usability_factor;
250     } else if ( nominalRange < 50.0 + SG_EPSILON ) {
251         // Standard Low Altitude Service Volume
252         // table is based on range of 40, scale to actual range
253         return low_tbl->interpolate( alt ) * nominalRange / 40.0
254             * usability_factor;
255     } else {
256         // Standard High Altitude Service Volume
257         // table is based on range of 130, scale to actual range
258         return high_tbl->interpolate( alt ) * nominalRange / 130.0
259             * usability_factor;
260     }
261 }
262
263
264 // model standard ILS service volumes as per AIM 1-1-9
265 double FGNavRadio::adjustILSRange( double stationElev, double aircraftElev,
266                                  double offsetDegrees, double distance )
267 {
268     // assumptions we model the standard service volume, plus
269
270     // altitude difference
271     // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
272 //     double offset = fabs( offsetDegrees );
273
274 //     if ( offset < 10 ) {
275 //      return FG_ILS_DEFAULT_RANGE;
276 //     } else if ( offset < 35 ) {
277 //      return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
278 //     } else if ( offset < 45 ) {
279 //      return (45 - offset);
280 //     } else if ( offset > 170 ) {
281 //         return FG_ILS_DEFAULT_RANGE;
282 //     } else if ( offset > 145 ) {
283 //      return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
284 //     } else if ( offset > 135 ) {
285 //         return (offset - 135);
286 //     } else {
287 //      return 0;
288 //     }
289     return FG_LOC_DEFAULT_RANGE;
290 }
291
292
293 // Update the various nav values based on position and valid tuned in navs
294 void 
295 FGNavRadio::update(double dt) 
296 {
297     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
298     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
299     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
300
301     // SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
302
303     Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
304     Point3D station;
305     double az1, az2, s;
306
307     // Create "formatted" versions of the nav frequencies for
308     // consistant display output.
309     char tmp[16];
310     sprintf( tmp, "%.2f", freq_node->getDoubleValue() );
311     fmt_freq_node->setStringValue(tmp);
312     sprintf( tmp, "%.2f", alt_freq_node->getDoubleValue() );
313     fmt_alt_freq_node->setStringValue(tmp);
314
315     // On timeout, scan again
316     _time_before_search_sec -= dt;
317     if ( _time_before_search_sec < 0 ) {
318         search();
319     }
320
321     ////////////////////////////////////////////////////////////////////////
322     // Nav.
323     ////////////////////////////////////////////////////////////////////////
324
325     // cout << "nav_valid = " << nav_valid
326     //      << " power_btn = " << power_btn_node->getBoolValue()
327     //      << " bus_power = " << bus_power_node->getDoubleValue()
328     //      << " nav_serviceable = " << nav_serviceable->getBoolValue()
329     //      << endl;
330
331     if ( nav_valid && power_btn_node->getBoolValue()
332          && (bus_power_node->getDoubleValue() > 1.0)
333          && nav_serviceable_node->getBoolValue() )
334     {
335         station = Point3D( nav_x, nav_y, nav_z );
336         loc_dist_node->setDoubleValue( aircraft.distance3D( station ) );
337         // cout << "station = " << station << " dist = " 
338         //      << loc_dist_node->getDoubleValue() << endl;
339
340         if ( has_gs_node->getBoolValue() ) {
341             // find closest distance to the gs base line
342             sgdVec3 p;
343             sgdSetVec3( p, aircraft.x(), aircraft.y(), aircraft.z() );
344             sgdVec3 p0;
345             sgdSetVec3( p0, nav_gs_x, nav_gs_y, nav_gs_z );
346             double dist = sgdClosestPointToLineDistSquared( p, p0,
347                                                             gs_base_vec );
348             gs_dist_node->setDoubleValue( sqrt( dist ) );
349             // cout << "nav_gs_dist = " << gs_dist_node->getDoubleValue()
350             //      << endl;
351
352             Point3D tmp( nav_gs_x, nav_gs_y, nav_gs_z );
353             // cout << " (" << aircraft.distance3D( tmp ) << ")" << endl;
354
355             // wgs84 heading to glide slope (to determine sign of distance)
356             geo_inverse_wgs_84( elev,
357                                 lat * SGD_RADIANS_TO_DEGREES,
358                                 lon * SGD_RADIANS_TO_DEGREES, 
359                                 nav_gslat, nav_gslon,
360                                 &az1, &az2, &s );
361             double r = az1 - nav_target_radial;
362             while ( r >  180.0 ) { r -= 360.0;}
363             while ( r < -180.0 ) { r += 360.0;}
364             if ( r >= -90.0 && r <= 90.0 ) {
365                 nav_gs_dist_signed = gs_dist_node->getDoubleValue();
366             } else {
367                 nav_gs_dist_signed = -gs_dist_node->getDoubleValue();
368             }
369             /* cout << "Target Radial = " << nav_target_radial 
370                  << "  Bearing = " << az1
371                  << "  dist (signed) = " << nav_gs_dist_signed
372                  << endl; */
373             
374         } else {
375             gs_dist_node->setDoubleValue( 0.0 );
376         }
377         
378         // wgs84 heading to localizer
379         double hdg;
380         geo_inverse_wgs_84( elev,
381                             lat * SGD_RADIANS_TO_DEGREES,
382                             lon * SGD_RADIANS_TO_DEGREES, 
383                             nav_loclat, nav_loclon,
384                             &hdg, &az2, &s );
385         // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
386         heading_node->setDoubleValue( hdg );
387         double radial = az2 - nav_twist;
388         double recip = radial + 180.0;
389         if ( recip >= 360.0 ) { recip -= 360.0; }
390         radial_node->setDoubleValue( radial );
391         recip_radial_node->setDoubleValue( recip );
392         // cout << " heading = " << heading_node->getDoubleValue()
393         //      << " dist = " << nav_dist << endl;
394
395         if ( loc_node->getBoolValue() ) {
396             double offset = radial - nav_target_radial;
397             while ( offset < -180.0 ) { offset += 360.0; }
398             while ( offset > 180.0 ) { offset -= 360.0; }
399             // cout << "ils offset = " << offset << endl;
400             nav_effective_range
401                 = adjustILSRange( nav_elev, elev, offset,
402                                   loc_dist_node->getDoubleValue()
403                                   * SG_METER_TO_NM );
404         } else {
405             nav_effective_range = adjustNavRange( nav_elev, elev, nav_range );
406         }
407         // cout << "nav range = " << nav_effective_range
408         //      << " (" << nav_range << ")" << endl;
409
410         if ( loc_dist_node->getDoubleValue()
411              < nav_effective_range * SG_NM_TO_METER )
412         {
413             inrange_node->setBoolValue( true );
414         } else if ( loc_dist_node->getDoubleValue()
415                     < 2 * nav_effective_range * SG_NM_TO_METER )
416         {
417             inrange_node->setBoolValue( sg_random() < 
418                 ( 2 * nav_effective_range * SG_NM_TO_METER
419                   - loc_dist_node->getDoubleValue() ) /
420                 (nav_effective_range * SG_NM_TO_METER) );
421         } else {
422             inrange_node->setBoolValue( false );
423         }
424
425         if ( !loc_node->getBoolValue() ) {
426             nav_target_radial = sel_radial_node->getDoubleValue();
427         }
428
429         // Calculate some values for the nav/ils hold autopilot
430
431         double cur_radial = recip;
432         if ( loc_node->getBoolValue() ) {
433             // ILS localizers radials are already "true" in our
434             // database
435         } else {
436             cur_radial += nav_twist;
437         }
438         if ( from_flag_node->getBoolValue() ) {
439             cur_radial += 180.0;
440             while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
441         }
442         
443         // AUTOPILOT/FLIGHT-DIRECTOR HELPERS
444
445         // determine the target radial in "true" heading
446         double trtrue = 0.0;
447         if ( loc_node->getBoolValue() ) {
448             // ILS localizers radials are already "true" in our
449             // database
450             trtrue = nav_target_radial;
451         } else {
452             // VOR radials need to have that vor's offset added in
453             trtrue = nav_target_radial + nav_twist;
454         }
455
456         while ( trtrue < 0.0 ) { trtrue += 360.0; }
457         while ( trtrue > 360.0 ) { trtrue -= 360.0; }
458         target_radial_true_node->setDoubleValue( trtrue );
459
460         // determine the heading adjustment needed.
461         // over 8km scale by 3.0 
462         //    (3 is chosen because max deflection is 10
463         //    and 30 is clamped angle to radial)
464         // under 8km scale by 10.0
465         //    because the overstated error helps drive it to the radial in a 
466         //    moderate cross wind.
467         double adjustment = 0.0;
468         if (loc_dist_node->getDoubleValue() > 8000) {
469             adjustment = cdi_deflection_node->getDoubleValue() * 3.0;
470         } else {
471             adjustment = cdi_deflection_node->getDoubleValue() * 10.0;
472         }
473         SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
474         
475         // determine the target heading to fly to intercept the
476         // tgt_radial
477         double nta_hdg = trtrue + adjustment; 
478         while ( nta_hdg <   0.0 ) { nta_hdg += 360.0; }
479         while ( nta_hdg > 360.0 ) { nta_hdg -= 360.0; }
480         target_auto_hdg_node->setDoubleValue( nta_hdg );
481
482         // cross track error
483         // ????
484
485         // Calculate desired rate of climb for intercepting the GS
486         double x = gs_dist_node->getDoubleValue();
487         double y = (alt_node->getDoubleValue() - nav_elev)
488             * SG_FEET_TO_METER;
489         double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
490
491         double target_angle = nav_target_gs;
492         double gs_diff = target_angle - current_angle;
493
494         // convert desired vertical path angle into a climb rate
495         double des_angle = current_angle - 10 * gs_diff;
496
497         // estimate horizontal speed towards ILS in meters per minute
498         double dist = last_x - x;
499         last_x = x;
500         if ( dt > 0.0 ) {
501             // avoid nan
502             double new_vel = ( dist / dt );
503  
504             horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
505             // double horiz_vel = cur_fdm_state->get_V_ground_speed()
506             //    * SG_FEET_TO_METER * 60.0;
507             // double horiz_vel = airspeed_node->getFloatValue()
508             //    * SG_FEET_TO_METER * 60.0;
509
510             gs_rate_of_climb_node
511                 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
512                                   * horiz_vel * SG_METER_TO_FEET );
513         }
514     } else {
515         inrange_node->setBoolValue( false );
516         // cout << "not picking up vor. :-(" << endl;
517     }
518
519     // compute to/from flag status
520     double value = false;
521     double offset = fabs(radial_node->getDoubleValue() - nav_target_radial);
522     if ( nav_slaved_to_gps_node->getBoolValue() ) {
523         value = gps_to_flag_node->getBoolValue();
524     } else if ( inrange_node->getBoolValue()
525                 && nav_serviceable_node->getBoolValue()
526                 && tofrom_serviceable_node->getBoolValue() )
527     {
528         if ( loc_node->getBoolValue() ) {
529             value = true;
530         } else {
531             value = !(offset <= 90.0 || offset >= 270.0);
532         }
533     }
534     to_flag_node->setBoolValue( value );
535
536     value = false;
537     if ( nav_slaved_to_gps_node->getBoolValue() ) {
538         value = gps_from_flag_node->getBoolValue();
539     } else if ( inrange_node->getBoolValue()
540                 && nav_serviceable_node->getBoolValue()
541                 && tofrom_serviceable_node->getBoolValue() )
542     {
543         if ( loc_node->getBoolValue() ) {
544             value = false;
545         } else {
546             value = !(offset > 90.0 && offset < 270.0);
547         }
548     }
549     from_flag_node->setBoolValue( value );
550
551     // compute the deflection of the CDI needle, clamped to the range
552     // of ( -10 , 10 )
553     double r = 0.0;
554     if ( nav_slaved_to_gps_node->getBoolValue() ) {
555         r = gps_cdi_deflection_node->getDoubleValue();
556         // We want +- 5 dots deflection for the gps, so clamp to -12.5/12.5
557         if ( r < -12.5 ) { r = -12.5; }
558         if ( r >  12.5 ) { r =  12.5; }
559     } else if ( inrange_node->getBoolValue()
560                 && nav_serviceable_node->getBoolValue() 
561                 && cdi_serviceable_node->getBoolValue() )
562     {
563         r = radial_node->getDoubleValue() - nav_target_radial;
564         // cout << "Target radial = " << nav_target_radial 
565         //      << "  Actual radial = " << radial_node->getDoubleValue()
566         //      << endl;
567     
568         while ( r >  180.0 ) { r -= 360.0;}
569         while ( r < -180.0 ) { r += 360.0;}
570         if ( fabs(r) > 90.0 ) {
571             r = ( r<0.0 ? -r-180.0 : -r+180.0 );
572         }
573
574         // According to Robin Peel, the ILS is 4x more sensitive than a vor
575         r = -r;                 // reverse, since radial is outbound
576         if ( loc_node->getBoolValue() ) { r *= 4.0; }
577         if ( r < -10.0 ) { r = -10.0; }
578         if ( r >  10.0 ) { r =  10.0; }
579     } else {
580         r = 0.0;
581     }
582     cdi_deflection_node->setDoubleValue( r );
583
584     // compute the amount of cross track distance error in meters
585     double m = 0.0;
586     if ( inrange_node->getBoolValue()
587          && nav_serviceable_node->getBoolValue()
588          && cdi_serviceable_node->getBoolValue() )
589     {
590         r = radial_node->getDoubleValue() - nav_target_radial;
591         // cout << "Target radial = " << nav_target_radial 
592         //     << "  Actual radial = " << radial_node->getDoubleValue()
593         //     << "  r = " << r << endl;
594     
595         while ( r >  180.0 ) { r -= 360.0;}
596         while ( r < -180.0 ) { r += 360.0;}
597         if ( fabs(r) > 90.0 ) {
598             r = ( r<0.0 ? -r-180.0 : -r+180.0 );
599         }
600
601         r = -r;                 // reverse, since radial is outbound
602
603         m = loc_dist_node->getDoubleValue() * sin(r * SGD_DEGREES_TO_RADIANS);
604
605     } else {
606         m = 0.0;
607     }
608     cdi_xtrack_error_node->setDoubleValue( m );
609
610     // compute the amount of glide slope needle deflection (.i.e. the
611     // number of degrees we are off the glide slope * 5.0
612     r = 0.0;
613     if ( nav_slaved_to_gps_node->getBoolValue() ) {
614         // FIXME, what should be set here?
615     } else if ( inrange_node->getBoolValue() && has_gs_node->getBoolValue()
616          && nav_serviceable_node->getBoolValue()
617          && gs_serviceable_node->getBoolValue() )
618     {
619         double x = gs_dist_node->getDoubleValue();
620         double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
621             * SG_FEET_TO_METER;
622         // cout << "dist = " << x << " height = " << y << endl;
623         double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
624         r = (nav_target_gs - angle) * 5.0;
625     }
626     gs_deflection_node->setDoubleValue( r );
627
628     // audio effects
629     if ( nav_valid
630          && inrange_node->getBoolValue()
631          && nav_serviceable_node->getBoolValue() )
632     {
633         // play station ident via audio system if on + ident,
634         // otherwise turn it off
635         if ( power_btn_node->getBoolValue()
636              && (bus_power_node->getDoubleValue() > 1.0)
637              && ident_btn_node->getBoolValue()
638              && audio_btn_node->getBoolValue() )
639         {
640             SGSoundSample *sound;
641             sound = globals->get_soundmgr()->find( nav_fx_name );
642             double vol = vol_btn_node->getDoubleValue();
643             if ( vol < 0.0 ) { vol = 0.0; }
644             if ( vol > 1.0 ) { vol = 1.0; }
645             if ( sound != NULL ) {
646                 sound->set_volume( vol );
647             } else {
648                 SG_LOG( SG_COCKPIT, SG_ALERT,
649                         "Can't find nav-vor-ident sound" );
650             }
651             sound = globals->get_soundmgr()->find( dme_fx_name );
652             if ( sound != NULL ) {
653                 sound->set_volume( vol );
654             } else {
655                 SG_LOG( SG_COCKPIT, SG_ALERT,
656                         "Can't find nav-dme-ident sound" );
657             }
658             // cout << "nav_last_time = " << nav_last_time << " ";
659             // cout << "cur_time = "
660             //      << globals->get_time_params()->get_cur_time();
661             if ( nav_last_time <
662                  globals->get_time_params()->get_cur_time() - 30 ) {
663                 nav_last_time = globals->get_time_params()->get_cur_time();
664                 nav_play_count = 0;
665             }
666             // cout << " nav_play_count = " << nav_play_count << endl;
667             // cout << "playing = "
668             //      << globals->get_soundmgr()->is_playing(nav_fx_name)
669             //      << endl;
670             if ( nav_play_count < 4 ) {
671                 // play VOR ident
672                 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
673                     globals->get_soundmgr()->play_once( nav_fx_name );
674                     ++nav_play_count;
675                 }
676             } else if ( nav_play_count < 5 && nav_has_dme ) {
677                 // play DME ident
678                 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
679                      !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
680                     globals->get_soundmgr()->play_once( dme_fx_name );
681                     ++nav_play_count;
682                 }
683             }
684         } else {
685             globals->get_soundmgr()->stop( nav_fx_name );
686             globals->get_soundmgr()->stop( dme_fx_name );
687         }
688     }
689 }
690
691
692 // Update current nav/adf radio stations based on current postition
693 void FGNavRadio::search() 
694 {
695
696     // reset search time
697     _time_before_search_sec = 1.0;
698
699     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
700     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
701     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
702
703     FGNavRecord *nav = NULL;
704     FGNavRecord *loc = NULL;
705     FGNavRecord *dme = NULL;
706     FGNavRecord *gs = NULL;
707
708     ////////////////////////////////////////////////////////////////////////
709     // Nav.
710     ////////////////////////////////////////////////////////////////////////
711
712     double freq = freq_node->getDoubleValue();
713     nav = globals->get_navlist()->findByFreq(freq, lon, lat, elev);
714     dme = globals->get_dmelist()->findByFreq(freq, lon, lat, elev);
715     if ( nav == NULL ) {
716         loc = globals->get_loclist()->findByFreq(freq, lon, lat, elev);
717         gs = globals->get_gslist()->findByFreq(freq, lon, lat, elev);
718     }
719         
720     if ( loc != NULL ) {
721         id_node->setStringValue( loc->get_ident() );
722         // cout << "localizer = " << id_node->getStringValue() << endl;
723         nav_valid = true;
724         if ( last_id != id_node->getStringValue() || last_nav_vor ) {
725             nav_trans_ident = loc->get_trans_ident();
726             nav_target_radial = loc->get_multiuse();
727             while ( nav_target_radial <   0.0 ) { nav_target_radial += 360.0; }
728             while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
729             nav_loclon = loc->get_lon();
730             nav_loclat = loc->get_lat();
731             nav_x = loc->get_x();
732             nav_y = loc->get_y();
733             nav_z = loc->get_z();
734             last_id = id_node->getStringValue();
735             last_nav_vor = false;
736             loc_node->setBoolValue( true );
737             nav_has_dme = (dme != NULL);
738             has_gs_node->setBoolValue( gs != NULL );
739             if ( has_gs_node->getBoolValue() ) {
740                 nav_gslon = gs->get_lon();
741                 nav_gslat = gs->get_lat();
742                 nav_elev = gs->get_elev_ft();
743                 int tmp = (int)(gs->get_multiuse() / 1000.0);
744                 nav_target_gs = (double)tmp / 100.0;
745                 nav_gs_x = gs->get_x();
746                 nav_gs_y = gs->get_y();
747                 nav_gs_z = gs->get_z();
748
749                 // derive GS baseline (perpendicular to the runay
750                 // along the ground)
751                 double tlon, tlat, taz;
752                 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
753                                     nav_target_radial + 90,  
754                                     100.0, &tlat, &tlon, &taz );
755                 // cout << "nav_target_radial = " << nav_target_radial << endl;
756                 // cout << "nav_loc = " << loc_node->getBoolValue() << endl;
757                 // cout << nav_gslon << "," << nav_gslat << "  "
758                 //      << tlon << "," << tlat << "  (" << nav_elev << ")"
759                 //      << endl;
760                 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
761                                                    tlat*SGD_DEGREES_TO_RADIANS,
762                                                    nav_elev*SG_FEET_TO_METER)
763                                            );
764                 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
765                 //      << endl;
766                 // cout << p1 << endl;
767                 sgdSetVec3( gs_base_vec,
768                             p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
769                 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
770                 //      << gs_base_vec[2] << endl;
771             } else {
772                 nav_elev = loc->get_elev_ft();
773             }
774             nav_twist = 0;
775             nav_range = FG_LOC_DEFAULT_RANGE;
776             nav_effective_range = nav_range;
777
778             if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
779                 globals->get_soundmgr()->remove( nav_fx_name );
780             }
781             SGSoundSample *sound;
782             sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
783             sound->set_volume( 0.3 );
784             globals->get_soundmgr()->add( sound, nav_fx_name );
785
786             if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
787                 globals->get_soundmgr()->remove( dme_fx_name );
788             }
789             sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
790             sound->set_volume( 0.3 );
791             globals->get_soundmgr()->add( sound, dme_fx_name );
792
793             int offset = (int)(sg_random() * 30.0);
794             nav_play_count = offset / 4;
795             nav_last_time = globals->get_time_params()->get_cur_time() -
796                 offset;
797             // cout << "offset = " << offset << " play_count = "
798             //      << nav_play_count
799             //      << " nav_last_time = " << nav_last_time
800             //      << " current time = "
801             //      << globals->get_time_params()->get_cur_time() << endl;
802
803             // cout << "Found an loc station in range" << endl;
804             // cout << " id = " << loc->get_locident() << endl;
805         }
806     } else if ( nav != NULL ) {
807         id_node->setStringValue( nav->get_ident() );
808         // cout << "nav = " << id_node->getStringValue() << endl;
809         nav_valid = true;
810         if ( last_id != id_node->getStringValue() || !last_nav_vor ) {
811             last_id = id_node->getStringValue();
812             last_nav_vor = true;
813             nav_trans_ident = nav->get_trans_ident();
814             loc_node->setBoolValue( false );
815             nav_has_dme = (dme != NULL);
816             has_gs_node->setBoolValue( false );
817             nav_loclon = nav->get_lon();
818             nav_loclat = nav->get_lat();
819             nav_elev = nav->get_elev_ft();
820             nav_twist = nav->get_multiuse();
821             nav_range = nav->get_range();
822             nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
823             nav_target_gs = 0.0;
824             nav_target_radial = sel_radial_node->getDoubleValue();
825             nav_x = nav->get_x();
826             nav_y = nav->get_y();
827             nav_z = nav->get_z();
828
829             if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
830                 globals->get_soundmgr()->remove( nav_fx_name );
831             }
832             SGSoundSample *sound;
833             sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
834             sound->set_volume( 0.3 );
835             if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
836                 // cout << "Added nav-vor-ident sound" << endl;
837             } else {
838                 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
839             }
840
841             if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
842                 globals->get_soundmgr()->remove( dme_fx_name );
843             }
844             sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
845             sound->set_volume( 0.3 );
846             globals->get_soundmgr()->add( sound, dme_fx_name );
847
848             int offset = (int)(sg_random() * 30.0);
849             nav_play_count = offset / 4;
850             nav_last_time = globals->get_time_params()->get_cur_time() -
851                 offset;
852             // cout << "offset = " << offset << " play_count = "
853             //      << nav_play_count << " nav_last_time = "
854             //      << nav_last_time << " current time = "
855             //      << globals->get_time_params()->get_cur_time() << endl;
856
857             // cout << "Found a vor station in range" << endl;
858             // cout << " id = " << nav->get_ident() << endl;
859         }
860     } else {
861         nav_valid = false;
862         id_node->setStringValue( "" );
863         nav_target_radial = 0;
864         nav_trans_ident = "";
865         last_id = "";
866         if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
867             SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
868         }
869         globals->get_soundmgr()->remove( dme_fx_name );
870         // cout << "not picking up vor1. :-(" << endl;
871     }
872
873     char tmpid[5];
874     strncpy( tmpid, id_node->getStringValue(), 5 );
875     id_c1_node->setIntValue( (int)tmpid[0] );
876     id_c2_node->setIntValue( (int)tmpid[1] );
877     id_c3_node->setIntValue( (int)tmpid[2] );
878     id_c4_node->setIntValue( (int)tmpid[3] );
879 }