]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/kr_87.cxx
Fixed up a couple loose ends.
[flightgear.git] / src / Cockpit / kr_87.cxx
1 // kr-87.cxx -- class to impliment the King KR 87 Digital ADF
2 //
3 // Written by Curtis Olson, started April 2002.
4 //
5 // Copyright (C) 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/navlist.hxx>
35 #include <Time/FGEventMgr.hxx>
36
37 #include "kr_87.hxx"
38
39 #include <string>
40 SG_USING_STD(string);
41
42 static int play_count = 0;
43 static time_t last_time = 0;
44
45
46 /**
47  * Boy, this is ugly!  Make the VOR range vary by altitude difference.
48  */
49 static double kludgeRange ( double stationElev, double aircraftElev,
50                             double nominalRange)
51 {
52                                 // Assume that the nominal range (usually
53                                 // 50nm) applies at a 5,000 ft difference.
54                                 // Just a wild guess!
55   double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
56   double range = fabs(nominalRange * factor);
57
58                                 // Clamp the range to keep it sane; for
59                                 // now, never less than 50% or more than
60                                 // 500% of nominal range.
61   if (range < nominalRange/2.0) {
62     range = nominalRange/2.0;
63   } else if (range > nominalRange*5.0) {
64     range = nominalRange*5.0;
65   }
66
67   return range;
68 }
69
70
71 // Constructor
72 FGKR_87::FGKR_87() :
73     lon_node(fgGetNode("/position/longitude-deg", true)),
74     lat_node(fgGetNode("/position/latitude-deg", true)),
75     alt_node(fgGetNode("/position/altitude-ft", true)),
76     need_update(true),
77     valid(false),
78     inrange(false),
79     goal_needle_deg(0.0),
80     et_flash_time(0.0),
81     ant_mode(0),
82     stby_mode(0),
83     timer_mode(0),
84     count_mode(0),
85     rotation(0),
86     on_off_vol_btn(0.5),
87     adf_btn(true),
88     bfo_btn(false),
89     frq_btn(false),
90     last_frq_btn(false),
91     flt_et_btn(false),
92     last_flt_et_btn(false),
93     set_rst_btn(false),
94     last_set_rst_btn(false),
95     ident_btn(false),
96     freq(0.0),
97     stby_freq(0.0),
98     needle_deg(0.0),
99     flight_timer(0.0),
100     elapsed_timer(0.0),
101     tmp_timer(0.0)
102 {
103     SGPath path( globals->get_fg_root() );
104     SGPath term = path;
105     term.append( "Navaids/range.term" );
106     SGPath low = path;
107     low.append( "Navaids/range.low" );
108     SGPath high = path;
109     high.append( "Navaids/range.high" );
110
111     term_tbl = new SGInterpTable( term.str() );
112     low_tbl = new SGInterpTable( low.str() );
113     high_tbl = new SGInterpTable( high.str() );
114 }
115
116
117 // Destructor
118 FGKR_87::~FGKR_87() {
119     delete term_tbl;
120     delete low_tbl;
121     delete high_tbl;
122 }
123
124
125 void FGKR_87::init () {
126     morse.init();
127
128     update(0);                  // FIXME: use dt
129 }
130
131
132 void FGKR_87::bind () {
133     // internal values
134     fgTie("/radios/kr-87/internal/valid", this, &FGKR_87::get_valid);
135     fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange);
136     fgTie("/radios/kr-87/internal/dist", this, &FGKR_87::get_dist);
137     fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading);
138
139     // modes
140     fgTie("/radios/kr-87/modes/ant", this,
141           &FGKR_87::get_ant_mode);
142     fgTie("/radios/kr-87/modes/stby", this,
143           &FGKR_87::get_stby_mode);
144     fgTie("/radios/kr-87/modes/timer", this,
145           &FGKR_87::get_timer_mode);
146     fgTie("/radios/kr-87/modes/count", this,
147           &FGKR_87::get_count_mode);
148
149     // input and buttons
150     fgTie("/radios/kr-87/inputs/rotation-deg", this,
151           &FGKR_87::get_rotation, &FGKR_87::set_rotation);
152     fgSetArchivable("/radios/kr-87/inputs/rotation-deg");
153     fgTie("/radios/kr-87/inputs/on-off-volume", this,
154           &FGKR_87::get_on_off_vol_btn,
155           &FGKR_87::set_on_off_vol_btn);
156     fgSetArchivable("/radios/kr-87/inputs/on-off-volume");
157     fgTie("/radios/kr-87/inputs/adf-btn", this,
158           &FGKR_87::get_adf_btn,
159           &FGKR_87::set_adf_btn);
160     fgTie("/radios/kr-87/inputs/bfo-btn", this,
161           &FGKR_87::get_bfo_btn,
162           &FGKR_87::set_bfo_btn);
163     fgTie("/radios/kr-87/inputs/frq-btn", this,
164           &FGKR_87::get_frq_btn,
165           &FGKR_87::set_frq_btn);
166     fgTie("/radios/kr-87/inputs/flt-et-btn", this,
167           &FGKR_87::get_flt_et_btn,
168           &FGKR_87::set_flt_et_btn);
169     fgTie("/radios/kr-87/inputs/set-rst-btn", this,
170           &FGKR_87::get_set_rst_btn,
171           &FGKR_87::set_set_rst_btn);
172     fgTie("/radios/kr-87/inputs/ident-btn", this,
173           &FGKR_87::get_ident_btn, &FGKR_87::set_ident_btn);
174
175     // outputs
176     fgTie("/radios/kr-87/outputs/selected-khz", this,
177           &FGKR_87::get_freq, &FGKR_87::set_freq);
178     fgSetArchivable("/radios/kr-87/outputs/selected-khz");
179     fgTie("/radios/kr-87/outputs/standby-khz", this,
180           &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
181     fgSetArchivable("/radios/kr-87/outputs/standby-khz");
182     fgTie("/radios/kr-87/outputs/needle-deg", this,
183           &FGKR_87::get_needle_deg);
184     fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer);
185     fgTie("/radios/kr-87/outputs/elapsed-timer", this,
186           &FGKR_87::get_elapsed_timer,
187           &FGKR_87::set_elapsed_timer);
188
189     // annunciators
190     fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann );
191     fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann );
192     fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann );
193     fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann );
194     fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann );
195     fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann );
196 }
197
198
199 void FGKR_87::unbind () {
200     // internal values
201     fgUntie("/radios/kr-87/internal/valid");
202     fgUntie("/radios/kr-87/internal/inrange");
203     fgUntie("/radios/kr-87/internal/dist");
204     fgUntie("/radios/kr-87/internal/heading");
205
206     // modes
207     fgUntie("/radios/kr-87/modes/ant");
208     fgUntie("/radios/kr-87/modes/stby");
209     fgUntie("/radios/kr-87/modes/timer");
210     fgUntie("/radios/kr-87/modes/count");
211
212     // input and buttons
213     fgUntie("/radios/kr-87/inputs/rotation-deg");
214     fgUntie("/radios/kr-87/inputs/on-off-volume");
215     fgUntie("/radios/kr-87/inputs/adf-btn");
216     fgUntie("/radios/kr-87/inputs/bfo-btn");
217     fgUntie("/radios/kr-87/inputs/frq-btn");
218     fgUntie("/radios/kr-87/inputs/flt-et-btn");
219     fgUntie("/radios/kr-87/inputs/set-rst-btn");
220     fgUntie("/radios/kr-87/inputs/ident-btn");
221
222     // outputs
223     fgUntie("/radios/kr-87/outputs/selected-khz");
224     fgUntie("/radios/kr-87/outputs/standby-khz");
225     fgUntie("/radios/kr-87/outputs/needle-deg");
226     fgUntie("/radios/kr-87/outputs/flight-timer");
227     fgUntie("/radios/kr-87/outputs/elapsed-timer");
228
229     // annunciators
230     fgUntie("/radios/kr-87/annunciators/ant");
231     fgUntie("/radios/kr-87/annunciators/adf");
232     fgUntie("/radios/kr-87/annunciators/bfo");
233     fgUntie("/radios/kr-87/annunciators/frq");
234     fgUntie("/radios/kr-87/annunciators/flt");
235     fgUntie("/radios/kr-87/annunciators/et");
236 }
237
238
239 // Update the various nav values based on position and valid tuned in navs
240 void FGKR_87::update( double dt ) {
241     double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
242     double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
243     double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
244
245     need_update = false;
246
247     Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
248     Point3D station;
249     double az1, az2, s;
250
251     ////////////////////////////////////////////////////////////////////////
252     // Radio
253     ////////////////////////////////////////////////////////////////////////
254
255     if ( on_off_vol_btn >= 0.01 ) {
256         // buttons
257         if ( adf_btn == 0 ) {
258             ant_mode = 1;
259         } else {
260             ant_mode = 0;
261         }
262
263         if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
264             double tmp = freq;
265             freq = stby_freq;
266             stby_freq = tmp;
267         } else if ( frq_btn ) {
268             stby_mode = 0;
269             count_mode = 0;
270         }
271         last_frq_btn = frq_btn;
272
273         if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
274             if ( stby_mode == 0 ) {
275                 timer_mode = 0;
276             } else {
277                 timer_mode = !timer_mode;
278             }
279             stby_mode = 1;
280         }
281         last_flt_et_btn = flt_et_btn;
282
283         if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
284             // button depressed
285            tmp_timer = 0.0;
286         }
287         if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
288             // button depressed and was last iteration too
289             tmp_timer += dt;
290             cout << "tmp_timer = " << tmp_timer << endl;
291             if ( tmp_timer > 2.0 ) {
292                 // button held depressed for 2 seconds
293                 cout << "entering elapsed count down mode" << endl;
294                 timer_mode = 1;
295                 count_mode = 2;
296                 elapsed_timer = 0.0;
297             }    
298         }
299         if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
300             // button released
301             if ( tmp_timer > 2.0 ) {
302                 // button held depressed for 2 seconds, don't adjust
303                 // mode, just exit                
304             } else if ( count_mode == 2 ) {
305                 count_mode = 1;
306             } else {
307                 count_mode = 0;
308                 elapsed_timer = 0.0;
309             }
310         }
311         last_set_rst_btn = set_rst_btn;
312
313         // timers
314         flight_timer += dt;
315
316         if ( set_rst_btn == 0 ) {
317             // only count if set/rst button not depressed
318             if ( count_mode == 0 ) {
319                 elapsed_timer += dt;
320             } else if ( count_mode == 1 ) {
321                 elapsed_timer -= dt;
322                 if ( elapsed_timer < 1.0 ) {
323                     count_mode = 0;
324                     elapsed_timer = 0.0;
325                 }
326             }
327         }
328
329         // annunciators
330         ant_ann = adf_btn;
331         adf_ann = !adf_btn;
332         bfo_ann = bfo_btn;
333         frq_ann = !stby_mode;
334         flt_ann = stby_mode && !timer_mode;
335         if ( count_mode < 2 ) {
336             et_ann = stby_mode && timer_mode;
337         } else {
338             et_flash_time += dt;
339             if ( et_ann && et_flash_time > 0.5 ) {
340                 et_ann = false;
341                 et_flash_time -= 0.5;
342             } else if ( !et_ann && et_flash_time > 0.2 ) {
343                 et_ann = true;
344                 et_flash_time -= 0.2;
345             }
346         }
347
348         if ( valid ) {
349             // staightline distance
350             station = Point3D( x, y, z );
351             dist = aircraft.distance3D( station );
352
353             // wgs84 heading
354             geo_inverse_wgs_84( acft_elev,
355                                 acft_lat * SGD_RADIANS_TO_DEGREES,
356                                 acft_lon * SGD_RADIANS_TO_DEGREES, 
357                                 stn_lat, stn_lon,
358                                 &az1, &az2, &s );
359             heading = az1;
360             // cout << " heading = " << heading
361             //      << " dist = " << dist << endl;
362
363             effective_range = kludgeRange(stn_elev, acft_elev, range);
364             if ( dist < effective_range * SG_NM_TO_METER ) {
365                 inrange = true;
366             } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
367                 inrange = sg_random() < 
368                     ( 2 * effective_range * SG_NM_TO_METER - dist ) /
369                     (effective_range * SG_NM_TO_METER);
370             } else {
371                 inrange = false;
372             }
373
374             if ( inrange ) {
375                 goal_needle_deg = heading
376                     - fgGetDouble("/orientation/heading-deg");
377             }
378         } else {
379             inrange = false;
380         }
381
382         if ( ant_mode ) {
383             goal_needle_deg = 90.0;
384         }
385     } else {
386         // unit turned off
387         goal_needle_deg = 0.0;
388         flight_timer = 0.0;
389         elapsed_timer = 0.0;
390         ant_ann = false;
391         adf_ann = false;
392         bfo_ann = false;
393         frq_ann = false;
394         flt_ann = false;
395         et_ann = false;
396     }
397
398     
399     while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
400     while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
401
402     double diff = goal_needle_deg - needle_deg;
403     while ( diff < -180.0 ) { diff += 360.0; }
404     while ( diff > 180.0 ) { diff -= 360.0; }
405
406     needle_deg += diff * dt * 4;
407     while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
408     while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
409
410     // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
411     //      << endl;
412     // cout << "flt = " << flight_timer << " et = " << elapsed_timer 
413     //      << " needle = " << needle_deg << endl;
414
415 #ifdef ENABLE_AUDIO_SUPPORT
416     if ( valid && inrange ) {
417         // play station ident via audio system if on + ident_btn,
418         // otherwise turn it off
419         if ( on_off_vol_btn >= 0.01 && ident_btn ) {
420             FGSimpleSound *sound;
421             sound = globals->get_soundmgr()->find( "adf-ident" );
422             if ( sound != NULL ) {
423                 sound->set_volume( on_off_vol_btn );
424             } else {
425                 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
426             }
427             if ( last_time <
428                  globals->get_time_params()->get_cur_time() - 30 ) {
429                 last_time = globals->get_time_params()->get_cur_time();
430                 play_count = 0;
431             }
432             if ( play_count < 4 ) {
433                 // play ADF ident
434                 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
435                     globals->get_soundmgr()->play_once( "adf-ident" );
436                     ++play_count;
437                 }
438             }
439         } else {
440             globals->get_soundmgr()->stop( "adf-ident" );
441         }
442     }
443 #endif
444 }
445
446
447 // Update current nav/adf radio stations based on current postition
448 void FGKR_87::search() {
449     double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
450     double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
451     double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
452
453                                 // FIXME: the panel should handle this
454     FGNav nav;
455
456     static string last_ident = "";
457
458     ////////////////////////////////////////////////////////////////////////
459     // ADF.
460     ////////////////////////////////////////////////////////////////////////
461
462     if ( current_navlist->query( acft_lon, acft_lat, acft_elev, freq, &nav ) ) {
463         char sfreq[128];
464         snprintf( sfreq, 10, "%.0f", freq );
465         ident = sfreq;
466         ident += nav.get_ident();
467 //      cout << "adf ident = " << ident << endl;
468         valid = true;
469         if ( last_ident != ident ) {
470             last_ident = ident;
471
472             trans_ident = nav.get_trans_ident();
473             stn_lon = nav.get_lon();
474             stn_lat = nav.get_lat();
475             stn_elev = nav.get_elev();
476             range = nav.get_range();
477             effective_range = kludgeRange(stn_elev, acft_elev, range);
478             x = nav.get_x();
479             y = nav.get_y();
480             z = nav.get_z();
481
482 #ifdef ENABLE_AUDIO_SUPPORT
483             if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
484                 globals->get_soundmgr()->remove( "adf-ident" );
485             }
486             FGSimpleSound *sound;
487             sound = morse.make_ident( trans_ident, LO_FREQUENCY );
488             sound->set_volume( 0.3 );
489             globals->get_soundmgr()->add( sound, "adf-ident" );
490
491             int offset = (int)(sg_random() * 30.0);
492             play_count = offset / 4;
493             last_time = globals->get_time_params()->get_cur_time() -
494                 offset;
495             // cout << "offset = " << offset << " play_count = "
496             //      << play_count << " last_time = "
497             //      << last_time << " current time = "
498             //      << globals->get_time_params()->get_cur_time() << endl;
499 #endif
500
501             // cout << "Found an adf station in range" << endl;
502             // cout << " id = " << nav.get_ident() << endl;
503         }
504     } else {
505         valid = false;
506         ident = "";
507         trans_ident = "";
508 #ifdef ENABLE_AUDIO_SUPPORT
509         globals->get_soundmgr()->remove( "adf-ident" );
510 #endif
511         last_ident = "";
512         // cout << "not picking up adf. :-(" << endl;
513     }
514 }
515
516
517 double FGKR_87::get_stby_freq() const {
518     if ( stby_mode == 0 ) {
519         return stby_freq;
520     } else {
521         if ( timer_mode == 0 ) {
522             return flight_timer;
523         } else {
524             return elapsed_timer;
525         }
526     }
527 }