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