]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/navradio.cxx
384af92b9b72bbdb32dcb0fa8f24ae92a81fc684
[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(NULL),
52     nav_freq(NULL),
53     nav_alt_freq(NULL),
54     fmt_freq(NULL),
55     fmt_alt_freq(NULL),
56     nav_sel_radial(NULL),
57     nav_vol_btn(NULL),
58     nav_ident_btn(NULL),
59     audio_btn(NULL),
60     nav_heading(NULL),
61     nav_radial(NULL),
62     reciprocal_radial(NULL),
63     nav_target_radial_true(NULL),
64     nav_target_auto_hdg(NULL),
65     nav_to_flag(NULL),
66     nav_from_flag(NULL),
67     nav_inrange(NULL),
68     nav_cdi_deflection(NULL),
69     nav_cdi_xtrack_error(NULL),
70     nav_has_gs(NULL),
71     nav_loc(NULL),
72     nav_loc_dist(NULL),
73     nav_gs_deflection(NULL),
74     nav_gs_rate_of_climb(NULL),
75     nav_gs_dist(NULL),
76     nav_id(NULL),
77     nav_id_c1(NULL),
78     nav_id_c2(NULL),
79     nav_id_c3(NULL),
80     nav_id_c4(NULL),
81     last_nav_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 = 
145         fgGetNode(("/systems/electrical/outputs/" + name).c_str(), true);
146
147     // inputs
148     power_btn = node->getChild("power-btn", 0, true);
149     power_btn->setBoolValue( true );
150     nav_vol_btn = node->getChild("volume", 0, true);
151     nav_ident_btn = node->getChild("ident", 0, true);
152     nav_ident_btn->setBoolValue( true );
153     audio_btn = node->getChild("audio-btn", 0, true);
154     audio_btn->setBoolValue( true );
155
156     // frequencies
157     SGPropertyNode *subnode = node->getChild("frequencies", 0, true);
158     nav_freq = subnode->getChild("selected-mhz", 0, true);
159     nav_alt_freq = subnode->getChild("standby-mhz", 0, true);
160     fmt_freq = subnode->getChild("selected-mhz-fmt", 0, true);
161     fmt_alt_freq = subnode->getChild("standby-mhz-fmt", 0, true);
162
163     // radials
164     subnode = node->getChild("radials", 0, true);
165     nav_sel_radial = subnode->getChild("selected-deg", 0, true);
166     nav_radial = subnode->getChild("actual-deg", 0, true);
167     reciprocal_radial = subnode->getChild("reciprocal-radial-deg", 0, true);
168     nav_target_radial_true = subnode->getChild("target-radial-deg", 0, true);
169     nav_target_auto_hdg = subnode->getChild("target-auto-hdg-deg", 0, true);
170
171     // outputs
172     nav_heading = node->getChild("heading-deg", 0, true);
173     nav_to_flag = node->getChild("to-flag", 0, true);
174     nav_from_flag = node->getChild("from-flag", 0, true);
175     nav_inrange = node->getChild("in-range", 0, true);
176     nav_cdi_deflection = node->getChild("heading-needle-deflection", 0, true);
177     nav_cdi_xtrack_error = node->getChild("crosstrack-error-m", 0, true);
178     nav_has_gs = node->getChild("has-gs", 0, true);
179     nav_loc = node->getChild("nav-loc", 0, true);
180     nav_loc_dist = node->getChild("nav-distance", 0, true);
181     nav_gs_deflection = node->getChild("gs-needle-deflection", 0, true);
182     nav_gs_rate_of_climb = node->getChild("gs-rate-of-climb", 0, true);
183     nav_gs_dist = node->getChild("gs-distance", 0, true);
184     nav_id = node->getChild("nav-id", 0, true);
185     nav_id_c1 = node->getChild("nav-id_asc1", 0, true);
186     nav_id_c2 = node->getChild("nav-id_asc2", 0, true);
187     nav_id_c3 = node->getChild("nav-id_asc3", 0, true);
188     nav_id_c4 = node->getChild("nav-id_asc4", 0, true);
189
190     nav_serviceable = node->getChild("serviceable", 0, true);
191     cdi_serviceable = (node->getChild("cdi", 0, true))
192         ->getChild("serviceable", 0, true);
193     gs_serviceable = (node->getChild("gs", 0, true))
194         ->getChild("serviceable");
195     tofrom_serviceable = (node->getChild("to-from", 0, true))
196         ->getChild("serviceable", 0, true);
197     nav_slaved_to_gps = node->getChild("slaved-to-gps", 0, true);
198
199     gps_cdi_deflection = fgGetNode("/instrumentation/gps/cdi-deflection", true);
200     gps_to_flag = fgGetNode("/instrumentation/gps/to-flag", true);
201     gps_from_flag = 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", nav_freq->getDoubleValue() );
311     fmt_freq->setStringValue(tmp);
312     sprintf( tmp, "%.2f", nav_alt_freq->getDoubleValue() );
313     fmt_alt_freq->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->getBoolValue()
327     //      << " bus_power = " << bus_power->getDoubleValue()
328     //      << " nav_serviceable = " << nav_serviceable->getBoolValue()
329     //      << endl;
330
331     if ( nav_valid && power_btn->getBoolValue()
332          && (bus_power->getDoubleValue() > 1.0)
333          && nav_serviceable->getBoolValue() )
334     {
335         station = Point3D( nav_x, nav_y, nav_z );
336         nav_loc_dist->setDoubleValue( aircraft.distance3D( station ) );
337         // cout << "station = " << station << " dist = " 
338         //      << nav_loc_dist->getDoubleValue() << endl;
339
340         if ( nav_has_gs->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             nav_gs_dist->setDoubleValue( sqrt( dist ) );
349             // cout << "nav_gs_dist = " << nav_gs_dist->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 = nav_gs_dist->getDoubleValue();
366             } else {
367                 nav_gs_dist_signed = -nav_gs_dist->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             nav_gs_dist->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         nav_heading->setDoubleValue( hdg );
387         double radial = az2 - nav_twist;
388         double recip = radial + 180.0;
389         if ( recip >= 360.0 ) { recip -= 360.0; }
390         nav_radial->setDoubleValue( radial );
391         reciprocal_radial->setDoubleValue( recip );
392         // cout << " heading = " << nav_heading
393         //      << " dist = " << nav_dist << endl;
394
395         if ( nav_loc->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                                   nav_loc_dist->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 ( nav_loc_dist->getDoubleValue()
411              < nav_effective_range * SG_NM_TO_METER )
412         {
413             nav_inrange->setBoolValue( true );
414         } else if ( nav_loc_dist->getDoubleValue()
415                     < 2 * nav_effective_range * SG_NM_TO_METER )
416         {
417             nav_inrange->setBoolValue( sg_random() < 
418                 ( 2 * nav_effective_range * SG_NM_TO_METER
419                   - nav_loc_dist->getDoubleValue() ) /
420                 (nav_effective_range * SG_NM_TO_METER) );
421         } else {
422             nav_inrange->setBoolValue( false );
423         }
424
425         if ( !nav_loc->getBoolValue() ) {
426             nav_target_radial = nav_sel_radial->getDoubleValue();
427         }
428
429         // Calculate some values for the nav/ils hold autopilot
430
431         double cur_radial = recip;
432         if ( nav_loc->getBoolValue() ) {
433             // ILS localizers radials are already "true" in our
434             // database
435         } else {
436             cur_radial += nav_twist;
437         }
438         if ( nav_from_flag->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 ( nav_loc->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         nav_target_radial_true->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 (nav_loc_dist->getDoubleValue() > 8000) {
469             adjustment = nav_cdi_deflection->getDoubleValue() * 3.0;
470         } else {
471             adjustment = nav_cdi_deflection->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         nav_target_auto_hdg->setDoubleValue( nta_hdg );
481
482         // cross track error
483         // ????
484
485         // Calculate desired rate of climb for intercepting the GS
486         double x = nav_gs_dist->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             nav_gs_rate_of_climb
511                 ->setDoubleValue( -sin( des_angle * SGD_DEGREES_TO_RADIANS )
512                                   * horiz_vel * SG_METER_TO_FEET );
513         }
514     } else {
515         nav_inrange->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(nav_radial->getDoubleValue() - nav_target_radial);
522     if ( nav_slaved_to_gps->getBoolValue() ) {
523         value = gps_to_flag->getBoolValue();
524     } else if ( nav_inrange->getBoolValue()
525                 && nav_serviceable->getBoolValue()
526                 && tofrom_serviceable->getBoolValue() )
527     {
528         if ( nav_loc->getBoolValue() ) {
529             value = true;
530         } else {
531             value = !(offset <= 90.0 || offset >= 270.0);
532         }
533     }
534     nav_to_flag->setBoolValue( value );
535
536     value = false;
537     if ( nav_slaved_to_gps->getBoolValue() ) {
538         value = gps_from_flag->getBoolValue();
539     } else if ( nav_inrange->getBoolValue()
540                 && nav_serviceable->getBoolValue()
541                 && tofrom_serviceable->getBoolValue() )
542     {
543         if ( nav_loc->getBoolValue() ) {
544             value = false;
545         } else {
546             value = !(offset > 90.0 && offset < 270.0);
547         }
548     }
549     nav_from_flag->setBoolValue( value );
550
551     // compute the deflection of the CDI needle, clamped to the range
552     // of ( -10 , 10 )
553     double r;
554     if ( nav_slaved_to_gps->getBoolValue() ) {
555         r = gps_cdi_deflection->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 ( nav_inrange->getBoolValue() && nav_serviceable->getBoolValue() 
560                 && cdi_serviceable->getBoolValue()
561                 && !nav_slaved_to_gps->getBoolValue() )
562     {
563         r = nav_radial->getDoubleValue() - nav_target_radial;
564         // cout << "Target radial = " << nav_target_radial 
565         //      << "  Actual radial = " << nav_radial->getDoubleValue() << endl;
566     
567         while ( r >  180.0 ) { r -= 360.0;}
568         while ( r < -180.0 ) { r += 360.0;}
569         if ( fabs(r) > 90.0 ) {
570             r = ( r<0.0 ? -r-180.0 : -r+180.0 );
571         }
572
573         // According to Robin Peel, the ILS is 4x more sensitive than a vor
574         r = -r;                 // reverse, since radial is outbound
575         if ( nav_loc->getBoolValue() ) { r *= 4.0; }
576         if ( r < -10.0 ) { r = -10.0; }
577         if ( r >  10.0 ) { r =  10.0; }
578     } else {
579         r = 0.0;
580     }
581     nav_cdi_deflection->setDoubleValue( r );
582
583     // compute the amount of cross track distance error in meters
584     double m;
585     if ( nav_inrange->getBoolValue()
586          && nav_serviceable->getBoolValue() && cdi_serviceable->getBoolValue() )
587     {
588         r = nav_radial->getDoubleValue() - nav_target_radial;
589         // cout << "Target radial = " << nav_target_radial 
590         //     << "  Actual radial = " << nav_radial->getDoubleValue()
591         //     << "  r = " << r << endl;
592     
593         while ( r >  180.0 ) { r -= 360.0;}
594         while ( r < -180.0 ) { r += 360.0;}
595         if ( fabs(r) > 90.0 ) {
596             r = ( r<0.0 ? -r-180.0 : -r+180.0 );
597         }
598
599         r = -r;                 // reverse, since radial is outbound
600
601         m = nav_loc_dist->getDoubleValue() * sin(r * SGD_DEGREES_TO_RADIANS);
602
603     } else {
604         m = 0.0;
605     }
606     nav_cdi_xtrack_error->setDoubleValue( m );
607
608     // compute the amount of glide slope needle deflection (.i.e. the
609     // number of degrees we are off the glide slope * 5.0
610     if ( nav_inrange->getBoolValue() && nav_has_gs->getBoolValue()
611          && nav_serviceable->getBoolValue()
612          && gs_serviceable->getBoolValue()
613          && !nav_slaved_to_gps->getBoolValue() )
614     {
615         double x = nav_gs_dist->getDoubleValue();
616         double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
617             * SG_FEET_TO_METER;
618         // cout << "dist = " << x << " height = " << y << endl;
619         double angle = asin( y / x ) * SGD_RADIANS_TO_DEGREES;
620         r = (nav_target_gs - angle) * 5.0;
621     } else {
622         r = 0.0;
623     }
624     nav_gs_deflection->setDoubleValue( r );
625
626     // audio effects
627     if ( nav_valid
628          && nav_inrange->getBoolValue()
629          && nav_serviceable->getBoolValue() )
630     {
631         // play station ident via audio system if on + ident,
632         // otherwise turn it off
633         if ( power_btn->getBoolValue() && (bus_power->getDoubleValue() > 1.0)
634              && nav_ident_btn->getBoolValue() && audio_btn->getBoolValue() )
635         {
636             SGSoundSample *sound;
637             sound = globals->get_soundmgr()->find( nav_fx_name );
638             double vol = nav_vol_btn->getDoubleValue();
639             if ( vol < 0.0 ) { vol = 0.0; }
640             if ( vol > 1.0 ) { vol = 1.0; }
641             if ( sound != NULL ) {
642                 sound->set_volume( vol );
643             } else {
644                 SG_LOG( SG_COCKPIT, SG_ALERT,
645                         "Can't find nav-vor-ident sound" );
646             }
647             sound = globals->get_soundmgr()->find( dme_fx_name );
648             if ( sound != NULL ) {
649                 sound->set_volume( vol );
650             } else {
651                 SG_LOG( SG_COCKPIT, SG_ALERT,
652                         "Can't find nav-dme-ident sound" );
653             }
654             // cout << "nav_last_time = " << nav_last_time << " ";
655             // cout << "cur_time = "
656             //      << globals->get_time_params()->get_cur_time();
657             if ( nav_last_time <
658                  globals->get_time_params()->get_cur_time() - 30 ) {
659                 nav_last_time = globals->get_time_params()->get_cur_time();
660                 nav_play_count = 0;
661             }
662             // cout << " nav_play_count = " << nav_play_count << endl;
663             // cout << "playing = "
664             //      << globals->get_soundmgr()->is_playing(nav_fx_name)
665             //      << endl;
666             if ( nav_play_count < 4 ) {
667                 // play VOR ident
668                 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
669                     globals->get_soundmgr()->play_once( nav_fx_name );
670                     ++nav_play_count;
671                 }
672             } else if ( nav_play_count < 5 && nav_has_dme ) {
673                 // play DME ident
674                 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
675                      !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
676                     globals->get_soundmgr()->play_once( dme_fx_name );
677                     ++nav_play_count;
678                 }
679             }
680         } else {
681             globals->get_soundmgr()->stop( nav_fx_name );
682             globals->get_soundmgr()->stop( dme_fx_name );
683         }
684     }
685 }
686
687
688 // Update current nav/adf radio stations based on current postition
689 void FGNavRadio::search() 
690 {
691
692     // reset search time
693     _time_before_search_sec = 1.0;
694
695     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
696     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
697     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
698
699     FGNavRecord *nav = NULL;
700     FGNavRecord *loc = NULL;
701     FGNavRecord *dme = NULL;
702     FGNavRecord *gs = NULL;
703
704     ////////////////////////////////////////////////////////////////////////
705     // Nav.
706     ////////////////////////////////////////////////////////////////////////
707
708     double freq = nav_freq->getDoubleValue();
709     nav = globals->get_navlist()->findByFreq(freq, lon, lat, elev);
710     dme = globals->get_dmelist()->findByFreq(freq, lon, lat, elev);
711     if ( nav == NULL ) {
712         loc = globals->get_loclist()->findByFreq(freq, lon, lat, elev);
713         gs = globals->get_gslist()->findByFreq(freq, lon, lat, elev);
714     }
715         
716     if ( loc != NULL ) {
717         nav_id->setStringValue( loc->get_ident() );
718         // cout << "localizer = " << nav_id->getStringValue() << endl;
719         nav_valid = true;
720         if ( last_nav_id != nav_id->getStringValue() || last_nav_vor ) {
721             nav_trans_ident = loc->get_trans_ident();
722             nav_target_radial = loc->get_multiuse();
723             while ( nav_target_radial <   0.0 ) { nav_target_radial += 360.0; }
724             while ( nav_target_radial > 360.0 ) { nav_target_radial -= 360.0; }
725             nav_loclon = loc->get_lon();
726             nav_loclat = loc->get_lat();
727             nav_x = loc->get_x();
728             nav_y = loc->get_y();
729             nav_z = loc->get_z();
730             last_nav_id = nav_id->getStringValue();
731             last_nav_vor = false;
732             nav_loc->setBoolValue( true );
733             nav_has_dme = (dme != NULL);
734             nav_has_gs->setBoolValue( gs != NULL );
735             if ( nav_has_gs->getBoolValue() ) {
736                 nav_gslon = gs->get_lon();
737                 nav_gslat = gs->get_lat();
738                 nav_elev = gs->get_elev_ft();
739                 int tmp = (int)(gs->get_multiuse() / 1000.0);
740                 nav_target_gs = (double)tmp / 100.0;
741                 nav_gs_x = gs->get_x();
742                 nav_gs_y = gs->get_y();
743                 nav_gs_z = gs->get_z();
744
745                 // derive GS baseline (perpendicular to the runay
746                 // along the ground)
747                 double tlon, tlat, taz;
748                 geo_direct_wgs_84 ( 0.0, nav_gslat, nav_gslon,
749                                     nav_target_radial + 90,  
750                                     100.0, &tlat, &tlon, &taz );
751                 // cout << "nav_target_radial = " << nav_target_radial << endl;
752                 // cout << "nav_loc = " << nav_loc->getBoolValue() << endl;
753                 // cout << nav_gslon << "," << nav_gslat << "  "
754                 //      << tlon << "," << tlat << "  (" << nav_elev << ")"
755                 //      << endl;
756                 Point3D p1 = sgGeodToCart( Point3D(tlon*SGD_DEGREES_TO_RADIANS,
757                                                    tlat*SGD_DEGREES_TO_RADIANS,
758                                                    nav_elev*SG_FEET_TO_METER)
759                                            );
760                 // cout << nav_gs_x << "," << nav_gs_y << "," << nav_gs_z
761                 //      << endl;
762                 // cout << p1 << endl;
763                 sgdSetVec3( gs_base_vec,
764                             p1.x()-nav_gs_x, p1.y()-nav_gs_y, p1.z()-nav_gs_z );
765                 // cout << gs_base_vec[0] << "," << gs_base_vec[1] << ","
766                 //      << gs_base_vec[2] << endl;
767             } else {
768                 nav_elev = loc->get_elev_ft();
769             }
770             nav_twist = 0;
771             nav_range = FG_LOC_DEFAULT_RANGE;
772             nav_effective_range = nav_range;
773
774             if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
775                 globals->get_soundmgr()->remove( nav_fx_name );
776             }
777             SGSoundSample *sound;
778             sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
779             sound->set_volume( 0.3 );
780             globals->get_soundmgr()->add( sound, nav_fx_name );
781
782             if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
783                 globals->get_soundmgr()->remove( dme_fx_name );
784             }
785             sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
786             sound->set_volume( 0.3 );
787             globals->get_soundmgr()->add( sound, dme_fx_name );
788
789             int offset = (int)(sg_random() * 30.0);
790             nav_play_count = offset / 4;
791             nav_last_time = globals->get_time_params()->get_cur_time() -
792                 offset;
793             // cout << "offset = " << offset << " play_count = "
794             //      << nav_play_count
795             //      << " nav_last_time = " << nav_last_time
796             //      << " current time = "
797             //      << globals->get_time_params()->get_cur_time() << endl;
798
799             // cout << "Found an loc station in range" << endl;
800             // cout << " id = " << loc->get_locident() << endl;
801         }
802     } else if ( nav != NULL ) {
803         nav_id->setStringValue( nav->get_ident() );
804         // cout << "nav = " << nav_id->getStringValue() << endl;
805         nav_valid = true;
806         if ( last_nav_id != nav_id->getStringValue() || !last_nav_vor ) {
807             last_nav_id = nav_id->getStringValue();
808             last_nav_vor = true;
809             nav_trans_ident = nav->get_trans_ident();
810             nav_loc->setBoolValue( false );
811             nav_has_dme = (dme != NULL);
812             nav_has_gs->setBoolValue( false );
813             nav_loclon = nav->get_lon();
814             nav_loclat = nav->get_lat();
815             nav_elev = nav->get_elev_ft();
816             nav_twist = nav->get_multiuse();
817             nav_range = nav->get_range();
818             nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
819             nav_target_gs = 0.0;
820             nav_target_radial = nav_sel_radial->getDoubleValue();
821             nav_x = nav->get_x();
822             nav_y = nav->get_y();
823             nav_z = nav->get_z();
824
825             if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
826                 globals->get_soundmgr()->remove( nav_fx_name );
827             }
828             SGSoundSample *sound;
829             sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
830             sound->set_volume( 0.3 );
831             if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
832                 // cout << "Added nav-vor-ident sound" << endl;
833             } else {
834                 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
835             }
836
837             if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
838                 globals->get_soundmgr()->remove( dme_fx_name );
839             }
840             sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
841             sound->set_volume( 0.3 );
842             globals->get_soundmgr()->add( sound, dme_fx_name );
843
844             int offset = (int)(sg_random() * 30.0);
845             nav_play_count = offset / 4;
846             nav_last_time = globals->get_time_params()->get_cur_time() -
847                 offset;
848             // cout << "offset = " << offset << " play_count = "
849             //      << nav_play_count << " nav_last_time = "
850             //      << nav_last_time << " current time = "
851             //      << globals->get_time_params()->get_cur_time() << endl;
852
853             // cout << "Found a vor station in range" << endl;
854             // cout << " id = " << nav->get_ident() << endl;
855         }
856     } else {
857         nav_valid = false;
858         nav_id->setStringValue( "" );
859         nav_target_radial = 0;
860         nav_trans_ident = "";
861         last_nav_id = "";
862         if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
863             SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
864         }
865         globals->get_soundmgr()->remove( dme_fx_name );
866         // cout << "not picking up vor1. :-(" << endl;
867     }
868
869     char tmpid[5];
870     strncpy( tmpid, nav_id->getStringValue(), 5 );
871     nav_id_c1->setIntValue( (int)tmpid[0] );
872     nav_id_c2->setIntValue( (int)tmpid[1] );
873     nav_id_c3->setIntValue( (int)tmpid[2] );
874     nav_id_c4->setIntValue( (int)tmpid[3] );
875 }