]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/navcom.cxx
Add a property which specifies if the current nav station has a glideslope.
[flightgear.git] / src / Cockpit / navcom.cxx
1 // navcom.cxx -- class to manage a navcom instance
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000 - 2002  Curtis L. Olson - curt@flightgear.org
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 <stdio.h>      // snprintf
29
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
32
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/ilslist.hxx>
35 #include <Navaids/navlist.hxx>
36 #include <Time/FGEventMgr.hxx>
37
38 #include "navcom.hxx"
39
40 #include <string>
41 SG_USING_STD(string);
42
43
44 // Constructor
45 FGNavCom::FGNavCom() :
46     lon_node(fgGetNode("/position/longitude-deg", true)),
47     lat_node(fgGetNode("/position/latitude-deg", true)),
48     alt_node(fgGetNode("/position/altitude-ft", true)),
49     last_nav_id(""),
50     last_nav_vor(false),
51     nav_play_count(0),
52     nav_last_time(0),
53     need_update(true),
54     power_btn(true),
55     audio_btn(true),
56     comm_freq(0.0),
57     comm_alt_freq(0.0),
58     comm_vol_btn(0.0),
59     nav_freq(0.0),
60     nav_alt_freq(0.0),
61     nav_radial(0.0),
62     nav_vol_btn(0.0),
63     nav_ident_btn(true)
64 {
65     SGPath path( globals->get_fg_root() );
66     SGPath term = path;
67     term.append( "Navaids/range.term" );
68     SGPath low = path;
69     low.append( "Navaids/range.low" );
70     SGPath high = path;
71     high.append( "Navaids/range.high" );
72
73     term_tbl = new SGInterpTable( term.str() );
74     low_tbl = new SGInterpTable( low.str() );
75     high_tbl = new SGInterpTable( high.str() );
76
77 }
78
79
80 // Destructor
81 FGNavCom::~FGNavCom() 
82 {
83     delete term_tbl;
84     delete low_tbl;
85     delete high_tbl;
86 }
87
88
89 void
90 FGNavCom::init ()
91 {
92     morse.init();
93
94     // We assume that index is valid now (it must be set before init()
95     // is called.)
96     char propname[256];
97
98     sprintf( propname, "/systems/electrical/outputs/navcom[%d]", index );
99     // default to true in case no electrical system defined.
100     fgSetDouble( propname, 60.0 );
101     bus_power = fgGetNode( propname, true );
102
103     sprintf( propname, "/instrumentation/comm[%d]/servicable", index );
104     com_servicable = fgGetNode( propname, true );
105     com_servicable->setBoolValue( true );
106
107     sprintf( propname, "/instrumentation/nav[%d]/servicable", index );
108     nav_servicable = fgGetNode( propname, true );
109     nav_servicable->setBoolValue( true );
110
111     sprintf( propname, "/instrumentation/vor[%d]/cdi/servicable", index );
112     cdi_servicable = fgGetNode( propname, true );
113     cdi_servicable->setBoolValue( true );
114
115     sprintf( propname, "/instrumentation/vor[%d]/gs/servicable", index );
116     gs_servicable = fgGetNode( propname, true );
117     gs_servicable->setBoolValue( true );
118
119     sprintf( propname, "/instrumentation/vor[%d]/to-from/servicable", index );
120     tofrom_servicable = fgGetNode( propname, true );
121     tofrom_servicable->setBoolValue( true );
122 }
123
124 void
125 FGNavCom::bind ()
126 {
127     char propname[256];
128
129                                 // User inputs
130     sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
131     fgTie( propname, this,
132            &FGNavCom::get_power_btn, &FGNavCom::set_power_btn );
133     fgSetArchivable( propname );
134
135     sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
136     fgTie( propname, this, &FGNavCom::get_comm_freq, &FGNavCom::set_comm_freq );
137     fgSetArchivable( propname );
138
139     sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
140     fgTie( propname, this,
141            &FGNavCom::get_comm_alt_freq, &FGNavCom::set_comm_alt_freq );
142     fgSetArchivable( propname );
143
144     sprintf( propname, "/radios/comm[%d]/volume", index );
145     fgTie( propname, this,
146            &FGNavCom::get_comm_vol_btn, &FGNavCom::set_comm_vol_btn );
147     fgSetArchivable( propname );
148
149     sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
150     fgTie( propname, this,
151           &FGNavCom::get_nav_freq, &FGNavCom::set_nav_freq );
152     fgSetArchivable( propname );
153
154     sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
155     fgTie( propname , this,
156            &FGNavCom::get_nav_alt_freq, &FGNavCom::set_nav_alt_freq);
157     fgSetArchivable( propname );
158
159     sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
160     fgTie( propname, this,
161            &FGNavCom::get_nav_sel_radial, &FGNavCom::set_nav_sel_radial );
162     fgSetArchivable( propname );
163
164     sprintf( propname, "/radios/nav[%d]/volume", index );
165     fgTie( propname, this,
166            &FGNavCom::get_nav_vol_btn, &FGNavCom::set_nav_vol_btn );
167     fgSetArchivable( propname );
168
169     sprintf( propname, "/radios/nav[%d]/ident", index );
170     fgTie( propname, this,
171            &FGNavCom::get_nav_ident_btn, &FGNavCom::set_nav_ident_btn );
172     fgSetArchivable( propname );
173
174                                 // Radio outputs
175     sprintf( propname, "/radios/nav[%d]/audio-btn", index );
176     fgTie( propname, this,
177            &FGNavCom::get_audio_btn, &FGNavCom::set_audio_btn );
178     fgSetArchivable( propname );
179
180     sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
181     fgTie( propname,  this, &FGNavCom::get_nav_radial );
182
183     sprintf( propname, "/radios/nav[%d]/to-flag", index );
184     fgTie( propname, this, &FGNavCom::get_nav_to_flag );
185
186     sprintf( propname, "/radios/nav[%d]/from-flag", index );
187     fgTie( propname, this, &FGNavCom::get_nav_from_flag );
188
189     sprintf( propname, "/radios/nav[%d]/in-range", index );
190     fgTie( propname, this, &FGNavCom::get_nav_inrange );
191
192     sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
193     fgTie( propname, this, &FGNavCom::get_nav_heading_needle_deflection );
194
195     sprintf( propname, "/radios/nav[%d]/has-gs", index );
196     fgTie( propname, this, &FGNavCom::get_nav_has_gs );
197
198     sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
199     fgTie( propname, this, &FGNavCom::get_nav_gs_needle_deflection );
200
201     // end of binding
202 }
203
204
205 void
206 FGNavCom::unbind ()
207 {
208     char propname[256];
209
210     sprintf( propname, "/radios/comm[%d]/inputs/power-btn", index );
211     fgUntie( propname );
212     sprintf( propname, "/radios/comm[%d]/frequencies/selected-mhz", index );
213     fgUntie( propname );
214     sprintf( propname, "/radios/comm[%d]/frequencies/standby-mhz", index );
215     fgUntie( propname );
216
217     sprintf( propname, "/radios/nav[%d]/frequencies/selected-mhz", index );
218     fgUntie( propname );
219     sprintf( propname, "/radios/nav[%d]/frequencies/standby-mhz", index );
220     fgUntie( propname );
221     sprintf( propname, "/radios/nav[%d]/radials/actual-deg", index );
222     fgUntie( propname );
223     sprintf( propname, "/radios/nav[%d]/radials/selected-deg", index );
224     fgUntie( propname );
225     sprintf( propname, "/radios/nav[%d]/ident", index );
226     fgUntie( propname );
227     sprintf( propname, "/radios/nav[%d]/to-flag", index );
228     fgUntie( propname );
229     sprintf( propname, "/radios/nav[%d]/from-flag", index );
230     fgUntie( propname );
231     sprintf( propname, "/radios/nav[%d]/in-range", index );
232     fgUntie( propname );
233     sprintf( propname, "/radios/nav[%d]/heading-needle-deflection", index );
234     fgUntie( propname );
235     sprintf( propname, "/radios/nav[%d]/gs-needle-deflection", index );
236     fgUntie( propname );
237 }
238
239
240 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
241 double FGNavCom::adjustNavRange( double stationElev, double aircraftElev,
242                                  double nominalRange )
243 {
244     // extend out actual usable range to be 1.3x the published safe range
245     const double usability_factor = 1.3;
246
247     // assumptions we model the standard service volume, plus
248     // ... rather than specifying a cylinder, we model a cone that
249     // contains the cylinder.  Then we put an upside down cone on top
250     // to model diminishing returns at too-high altitudes.
251
252     // altitude difference
253     double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
254     // cout << "aircraft elev = " << aircraftElev * SG_METER_TO_FEET
255     //      << " station elev = " << stationElev << endl;
256
257     if ( nominalRange < 25.0 + SG_EPSILON ) {
258         // Standard Terminal Service Volume
259         return term_tbl->interpolate( alt ) * usability_factor;
260     } else if ( nominalRange < 50.0 + SG_EPSILON ) {
261         // Standard Low Altitude Service Volume
262         // table is based on range of 40, scale to actual range
263         return low_tbl->interpolate( alt ) * nominalRange / 40.0
264             * usability_factor;
265     } else {
266         // Standard High Altitude Service Volume
267         // table is based on range of 130, scale to actual range
268         return high_tbl->interpolate( alt ) * nominalRange / 130.0
269             * usability_factor;
270     }
271 }
272
273
274 // model standard ILS service volumes as per AIM 1-1-9
275 double FGNavCom::adjustILSRange( double stationElev, double aircraftElev,
276                                      double offsetDegrees, double distance )
277 {
278     // assumptions we model the standard service volume, plus
279
280     // altitude difference
281     // double alt = ( aircraftElev * SG_METER_TO_FEET - stationElev );
282 //     double offset = fabs( offsetDegrees );
283
284 //     if ( offset < 10 ) {
285 //      return FG_ILS_DEFAULT_RANGE;
286 //     } else if ( offset < 35 ) {
287 //      return 10 + (35 - offset) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
288 //     } else if ( offset < 45 ) {
289 //      return (45 - offset);
290 //     } else if ( offset > 170 ) {
291 //         return FG_ILS_DEFAULT_RANGE;
292 //     } else if ( offset > 145 ) {
293 //      return 10 + (offset - 145) * (FG_ILS_DEFAULT_RANGE - 10) / 25;
294 //     } else if ( offset > 135 ) {
295 //         return (offset - 135);
296 //     } else {
297 //      return 0;
298 //     }
299     return FG_ILS_DEFAULT_RANGE;
300 }
301
302
303 // Update the various nav values based on position and valid tuned in navs
304 void 
305 FGNavCom::update(double dt) 
306 {
307     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
308     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
309     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
310
311     need_update = false;
312
313     Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
314     Point3D station;
315     double az1, az2, s;
316
317     ////////////////////////////////////////////////////////////////////////
318     // Nav.
319     ////////////////////////////////////////////////////////////////////////
320
321     if ( nav_valid && power_btn && (bus_power->getDoubleValue() > 1.0)
322          && nav_servicable->getBoolValue() )
323     {
324         station = Point3D( nav_x, nav_y, nav_z );
325         nav_loc_dist = aircraft.distance3D( station );
326
327         if ( nav_has_gs ) {
328             station = Point3D( nav_gs_x, nav_gs_y, nav_gs_z );
329             nav_gs_dist = aircraft.distance3D( station );
330             // wgs84 heading to glide slope
331             geo_inverse_wgs_84( elev,
332                                 lat * SGD_RADIANS_TO_DEGREES,
333                                 lon * SGD_RADIANS_TO_DEGREES, 
334                                 nav_gslat, nav_gslon,
335                                 &az1, &az2, &s );
336             double r = az1 - nav_radial;
337             while ( r >  180.0 ) { r -= 360.0;}
338             while ( r < -180.0 ) { r += 360.0;}
339             if ( r >= -90.0 && r <= 90.0 ) {
340                 nav_gs_dist_signed = nav_gs_dist;
341             } else {
342                 nav_gs_dist_signed = -nav_gs_dist;
343             }
344             /* cout << "Target Radial = " << nav_radial 
345                  << "  Bearing = " << az1
346                  << "  dist (signed) = " << nav_gs_dist_signed
347                  << endl; */
348             
349         } else {
350             nav_gs_dist = 0.0;
351         }
352         
353         // wgs84 heading to localizer
354         geo_inverse_wgs_84( elev,
355                             lat * SGD_RADIANS_TO_DEGREES,
356                             lon * SGD_RADIANS_TO_DEGREES, 
357                             nav_loclat, nav_loclon,
358                             &az1, &az2, &s );
359         // cout << "az1 = " << az1 << " magvar = " << nav_magvar << endl;
360         nav_heading = az1 - nav_magvar;
361         // cout << " heading = " << nav_heading
362         //      << " dist = " << nav_dist << endl;
363
364         if ( nav_loc ) {
365             double offset = nav_heading - nav_radial;
366             while ( offset < -180.0 ) { offset += 360.0; }
367             while ( offset > 180.0 ) { offset -= 360.0; }
368             // cout << "ils offset = " << offset << endl;
369             nav_effective_range = adjustILSRange(nav_elev, elev, offset,
370                                                   nav_loc_dist * SG_METER_TO_NM );
371         } else {
372             nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
373         }
374         // cout << "nav range = " << nav_effective_range
375         //      << " (" << nav_range << ")" << endl;
376
377         if ( nav_loc_dist < nav_effective_range * SG_NM_TO_METER ) {
378             nav_inrange = true;
379         } else if ( nav_loc_dist < 2 * nav_effective_range * SG_NM_TO_METER ) {
380             nav_inrange = sg_random() < 
381                 ( 2 * nav_effective_range * SG_NM_TO_METER - nav_loc_dist ) /
382                 (nav_effective_range * SG_NM_TO_METER);
383         } else {
384             nav_inrange = false;
385         }
386
387         if ( !nav_loc ) {
388             nav_radial = nav_sel_radial;
389         }
390     } else {
391         nav_inrange = false;
392         // cout << "not picking up vor. :-(" << endl;
393     }
394
395     if ( nav_valid && nav_inrange && nav_servicable->getBoolValue() ) {
396         // play station ident via audio system if on + ident,
397         // otherwise turn it off
398         if ( power_btn && (bus_power->getDoubleValue() > 1.0)
399              && nav_ident_btn && audio_btn )
400         {
401             FGSimpleSound *sound;
402             sound = globals->get_soundmgr()->find( nav_fx_name );
403             if ( sound != NULL ) {
404                 sound->set_volume( nav_vol_btn );
405             } else {
406                 SG_LOG( SG_COCKPIT, SG_ALERT,
407                         "Can't find nav-vor-ident sound" );
408             }
409             sound = globals->get_soundmgr()->find( dme_fx_name );
410             if ( sound != NULL ) {
411                 sound->set_volume( nav_vol_btn );
412             } else {
413                 SG_LOG( SG_COCKPIT, SG_ALERT,
414                         "Can't find nav-dme-ident sound" );
415             }
416             // cout << "nav_last_time = " << nav_last_time << " ";
417             // cout << "cur_time = "
418             //      << globals->get_time_params()->get_cur_time();
419             if ( nav_last_time <
420                  globals->get_time_params()->get_cur_time() - 30 ) {
421                 nav_last_time = globals->get_time_params()->get_cur_time();
422                 nav_play_count = 0;
423             }
424             // cout << " nav_play_count = " << nav_play_count << endl;
425             // cout << "playing = "
426             //      << globals->get_soundmgr()->is_playing(nav_fx_name)
427             //      << endl;
428             if ( nav_play_count < 4 ) {
429                 // play VOR ident
430                 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) ) {
431                     globals->get_soundmgr()->play_once( nav_fx_name );
432                     ++nav_play_count;
433                 }
434             } else if ( nav_play_count < 5 && nav_has_dme ) {
435                 // play DME ident
436                 if ( !globals->get_soundmgr()->is_playing(nav_fx_name) &&
437                      !globals->get_soundmgr()->is_playing(dme_fx_name) ) {
438                     globals->get_soundmgr()->play_once( dme_fx_name );
439                     ++nav_play_count;
440                 }
441             }
442         } else {
443             globals->get_soundmgr()->stop( nav_fx_name );
444             globals->get_soundmgr()->stop( dme_fx_name );
445         }
446     }
447 }
448
449
450 // Update current nav/adf radio stations based on current postition
451 void FGNavCom::search() 
452 {
453     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
454     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
455     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
456
457     FGILS *ils;
458     FGNav *nav;
459
460     ////////////////////////////////////////////////////////////////////////
461     // Nav.
462     ////////////////////////////////////////////////////////////////////////
463
464     if ( (ils = current_ilslist->findByFreq(nav_freq, lon, lat, elev)) != NULL ) {
465         nav_id = ils->get_locident();
466         nav_valid = true;
467         if ( last_nav_id != nav_id || last_nav_vor ) {
468             nav_trans_ident = ils->get_trans_ident();
469             last_nav_id = nav_id;
470             last_nav_vor = false;
471             nav_loc = true;
472             nav_has_dme = ils->get_has_dme();
473             nav_has_gs = ils->get_has_gs();
474
475             nav_loclon = ils->get_loclon();
476             nav_loclat = ils->get_loclat();
477             nav_gslon = ils->get_gslon();
478             nav_gslat = ils->get_gslat();
479             nav_elev = ils->get_gselev();
480             nav_magvar = 0;
481             nav_range = FG_ILS_DEFAULT_RANGE;
482             nav_effective_range = nav_range;
483             nav_target_gs = ils->get_gsangle();
484             nav_radial = ils->get_locheading();
485             while ( nav_radial <   0.0 ) { nav_radial += 360.0; }
486             while ( nav_radial > 360.0 ) { nav_radial -= 360.0; }
487             nav_x = ils->get_x();
488             nav_y = ils->get_y();
489             nav_z = ils->get_z();
490             nav_gs_x = ils->get_gs_x();
491             nav_gs_y = ils->get_gs_y();
492             nav_gs_z = ils->get_gs_z();
493
494             if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
495                 globals->get_soundmgr()->remove( nav_fx_name );
496             }
497             FGSimpleSound *sound;
498             sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
499             sound->set_volume( 0.3 );
500             globals->get_soundmgr()->add( sound, nav_fx_name );
501
502             if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
503                 globals->get_soundmgr()->remove( dme_fx_name );
504             }
505             sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
506             sound->set_volume( 0.3 );
507             globals->get_soundmgr()->add( sound, dme_fx_name );
508
509             int offset = (int)(sg_random() * 30.0);
510             nav_play_count = offset / 4;
511             nav_last_time = globals->get_time_params()->get_cur_time() -
512                 offset;
513             // cout << "offset = " << offset << " play_count = "
514             //      << nav_play_count
515             //      << " nav_last_time = " << nav_last_time
516             //      << " current time = "
517             //      << globals->get_time_params()->get_cur_time() << endl;
518
519             // cout << "Found an ils station in range" << endl;
520             // cout << " id = " << ils->get_locident() << endl;
521         }
522     } else if ( (nav = current_navlist->findByFreq(nav_freq, lon, lat, elev)) != NULL ) {
523         nav_id = nav->get_ident();
524         nav_valid = (nav->get_type() == 'V');
525         if ( last_nav_id != nav_id || !last_nav_vor ) {
526             last_nav_id = nav_id;
527             last_nav_vor = true;
528             nav_trans_ident = nav->get_trans_ident();
529             nav_loc = false;
530             nav_has_dme = nav->get_has_dme();
531             nav_has_gs = false;
532             nav_loclon = nav->get_lon();
533             nav_loclat = nav->get_lat();
534             nav_elev = nav->get_elev();
535             nav_magvar = nav->get_magvar();
536             nav_range = nav->get_range();
537             nav_effective_range = adjustNavRange(nav_elev, elev, nav_range);
538             nav_target_gs = 0.0;
539             nav_radial = nav_sel_radial;
540             nav_x = nav->get_x();
541             nav_y = nav->get_y();
542             nav_z = nav->get_z();
543
544             if ( globals->get_soundmgr()->exists( nav_fx_name ) ) {
545                 globals->get_soundmgr()->remove( nav_fx_name );
546             }
547             FGSimpleSound *sound;
548             sound = morse.make_ident( nav_trans_ident, LO_FREQUENCY );
549             sound->set_volume( 0.3 );
550             if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) {
551                 // cout << "Added nav-vor-ident sound" << endl;
552             } else {
553                 SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound");
554             }
555
556             if ( globals->get_soundmgr()->exists( dme_fx_name ) ) {
557                 globals->get_soundmgr()->remove( dme_fx_name );
558             }
559             sound = morse.make_ident( nav_trans_ident, HI_FREQUENCY );
560             sound->set_volume( 0.3 );
561             globals->get_soundmgr()->add( sound, dme_fx_name );
562
563             int offset = (int)(sg_random() * 30.0);
564             nav_play_count = offset / 4;
565             nav_last_time = globals->get_time_params()->get_cur_time() -
566                 offset;
567             // cout << "offset = " << offset << " play_count = "
568             //      << nav_play_count << " nav_last_time = "
569             //      << nav_last_time << " current time = "
570             //      << globals->get_time_params()->get_cur_time() << endl;
571
572             // cout << "Found a vor station in range" << endl;
573             // cout << " id = " << nav->get_ident() << endl;
574         }
575     } else {
576         nav_valid = false;
577         nav_id = "";
578         nav_radial = 0;
579         nav_trans_ident = "";
580         last_nav_id = "";
581         if ( ! globals->get_soundmgr()->remove( nav_fx_name ) ) {
582             SG_LOG(SG_COCKPIT, SG_WARN, "Failed to remove nav-vor-ident sound");
583         }
584         globals->get_soundmgr()->remove( dme_fx_name );
585         // cout << "not picking up vor1. :-(" << endl;
586     }
587 }
588
589
590 // return the amount of heading needle deflection, returns a value
591 // clamped to the range of ( -10 , 10 )
592 double FGNavCom::get_nav_heading_needle_deflection() const {
593     double r;
594
595     if ( nav_inrange
596          && nav_servicable->getBoolValue() && cdi_servicable->getBoolValue() )
597     {
598         r = nav_heading - nav_radial;
599         // cout << "Radial = " << nav_radial 
600         //      << "  Bearing = " << nav_heading << endl;
601     
602         while ( r >  180.0 ) { r -= 360.0;}
603         while ( r < -180.0 ) { r += 360.0;}
604         if ( fabs(r) > 90.0 )
605             r = ( r<0.0 ? -r-180.0 : -r+180.0 );
606
607         // According to Robin Peel, the ILS is 4x more sensitive than a vor
608         if ( nav_loc ) { r *= 4.0; }
609         if ( r < -10.0 ) { r = -10.0; }
610         if ( r >  10.0 ) { r = 10.0; }
611     } else {
612         r = 0.0;
613     }
614
615     return r;
616 }
617
618
619 // return the amount of glide slope needle deflection (.i.e. the
620 // number of degrees we are off the glide slope * 5.0
621 double FGNavCom::get_nav_gs_needle_deflection() const {
622     if ( nav_inrange && nav_has_gs
623          && nav_servicable->getBoolValue() && gs_servicable->getBoolValue() )
624     {
625         double x = nav_gs_dist;
626         double y = (fgGetDouble("/position/altitude-ft") - nav_elev)
627             * SG_FEET_TO_METER;
628         double angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
629         return (nav_target_gs - angle) * 5.0;
630     } else {
631         return 0.0;
632     }
633 }
634
635
636 /**
637  * Return true if the NAV TO flag should be active.
638  */
639 bool 
640 FGNavCom::get_nav_to_flag () const
641 {
642     if ( nav_inrange
643          && nav_servicable->getBoolValue()
644          && tofrom_servicable->getBoolValue() )
645     {
646         double offset = fabs(nav_heading - nav_radial);
647         if (nav_loc) {
648             return true;
649         } else {
650             return (offset <= 90.0 || offset >= 270.0);
651         }
652     } else {
653         return false;
654     }
655 }
656
657
658 /**
659  * Return true if the NAV FROM flag should be active.
660  */
661 bool
662 FGNavCom::get_nav_from_flag () const
663 {
664     if ( nav_inrange
665          && nav_servicable->getBoolValue()
666          && tofrom_servicable->getBoolValue() ) {
667         double offset = fabs(nav_heading - nav_radial);
668         if (nav_loc) {
669             return false;
670         } else {
671           return (offset > 90.0 && offset < 270.0);
672         }
673     } else {
674         return false;
675     }
676 }