]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/kr_87.cxx
Reintroduce an old bugture: use localizer as dme
[flightgear.git] / src / Instrumentation / 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 - 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <stdio.h>      // snprintf
28
29 #include <simgear/compiler.h>
30 #include <simgear/math/sg_random.h>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/timing/sg_time.hxx>
33
34 #include <Navaids/navlist.hxx>
35
36 #include "kr_87.hxx"
37
38 #include <Sound/morse.hxx>
39 #include <string>
40 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 - stationElev)*SG_METER_TO_FEET / 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( SGPropertyNode *node ) :
73     bus_power(fgGetNode("/systems/electrical/outputs/adf", true)),
74     serviceable(fgGetNode("/instrumentation/adf/serviceable", true)),
75     need_update(true),
76     valid(false),
77     inrange(false),
78     dist(0.0),
79     heading(0.0),
80     goal_needle_deg(0.0),
81     et_flash_time(0.0),
82     ant_mode(0),
83     stby_mode(0),
84     timer_mode(0),
85     count_mode(0),
86     rotation(0),
87     power_btn(true),
88     audio_btn(true),
89     vol_btn(0.5),
90     adf_btn(true),
91     bfo_btn(false),
92     frq_btn(false),
93     last_frq_btn(false),
94     flt_et_btn(false),
95     last_flt_et_btn(false),
96     set_rst_btn(false),
97     last_set_rst_btn(false),
98     freq(0),
99     stby_freq(0),
100     needle_deg(0.0),
101     flight_timer(0.0),
102     elapsed_timer(0.0),
103     tmp_timer(0.0),
104     _time_before_search_sec(0),
105     _sgr(NULL)
106 {
107 }
108
109
110 // Destructor
111 FGKR_87::~FGKR_87() {
112 }
113
114
115 void FGKR_87::init () {
116     SGSoundMgr *smgr = globals->get_soundmgr();
117     _sgr = smgr->find("avionics", true);
118     _sgr->tie_to_listener();
119 }
120
121
122 void FGKR_87::bind () {
123     // internal values
124     fgTie("/instrumentation/kr-87/internal/valid", this, &FGKR_87::get_valid);
125     fgTie("/instrumentation/kr-87/internal/inrange", this,
126           &FGKR_87::get_inrange);
127     fgTie("/instrumentation/kr-87/internal/dist", this,
128           &FGKR_87::get_dist);
129     fgTie("/instrumentation/kr-87/internal/heading", this,
130           &FGKR_87::get_heading);
131
132     // modes
133     fgTie("/instrumentation/kr-87/modes/ant", this,
134           &FGKR_87::get_ant_mode);
135     fgTie("/instrumentation/kr-87/modes/stby", this,
136           &FGKR_87::get_stby_mode);
137     fgTie("/instrumentation/kr-87/modes/timer", this,
138           &FGKR_87::get_timer_mode);
139     fgTie("/instrumentation/kr-87/modes/count", this,
140           &FGKR_87::get_count_mode);
141
142     // input and buttons
143     fgTie("/instrumentation/kr-87/inputs/rotation-deg", this,
144           &FGKR_87::get_rotation, &FGKR_87::set_rotation);
145     fgSetArchivable("/instrumentation/kr-87/inputs/rotation-deg");
146     fgTie("/instrumentation/kr-87/inputs/power-btn", this,
147           &FGKR_87::get_power_btn,
148           &FGKR_87::set_power_btn);
149     fgSetArchivable("/instrumentation/kr-87/inputs/power-btn");
150     fgTie("/instrumentation/kr-87/inputs/audio-btn", this,
151           &FGKR_87::get_audio_btn,
152           &FGKR_87::set_audio_btn);
153     fgSetArchivable("/instrumentation/kr-87/inputs/audio-btn");
154     fgTie("/instrumentation/kr-87/inputs/volume", this,
155           &FGKR_87::get_vol_btn,
156           &FGKR_87::set_vol_btn);
157     fgSetArchivable("/instrumentation/kr-87/inputs/volume");
158     fgTie("/instrumentation/kr-87/inputs/adf-btn", this,
159           &FGKR_87::get_adf_btn,
160           &FGKR_87::set_adf_btn);
161     fgTie("/instrumentation/kr-87/inputs/bfo-btn", this,
162           &FGKR_87::get_bfo_btn,
163           &FGKR_87::set_bfo_btn);
164     fgTie("/instrumentation/kr-87/inputs/frq-btn", this,
165           &FGKR_87::get_frq_btn,
166           &FGKR_87::set_frq_btn);
167     fgTie("/instrumentation/kr-87/inputs/flt-et-btn", this,
168           &FGKR_87::get_flt_et_btn,
169           &FGKR_87::set_flt_et_btn);
170     fgTie("/instrumentation/kr-87/inputs/set-rst-btn", this,
171           &FGKR_87::get_set_rst_btn,
172           &FGKR_87::set_set_rst_btn);
173
174     // outputs
175     fgTie("/instrumentation/kr-87/outputs/selected-khz", this,
176           &FGKR_87::get_freq, &FGKR_87::set_freq);
177     fgSetArchivable("/instrumentation/kr-87/outputs/selected-khz");
178     fgTie("/instrumentation/kr-87/outputs/standby-khz", this,
179           &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
180     fgSetArchivable("/instrumentation/kr-87/outputs/standby-khz");
181     fgTie("/instrumentation/kr-87/outputs/needle-deg", this,
182           &FGKR_87::get_needle_deg);
183     fgTie("/instrumentation/kr-87/outputs/flight-timer", this,
184           &FGKR_87::get_flight_timer);
185     fgTie("/instrumentation/kr-87/outputs/elapsed-timer", this,
186           &FGKR_87::get_elapsed_timer,
187           &FGKR_87::set_elapsed_timer);
188
189     // annunciators
190     fgTie("/instrumentation/kr-87/annunciators/ant", this,
191           &FGKR_87::get_ant_ann );
192     fgTie("/instrumentation/kr-87/annunciators/adf", this,
193           &FGKR_87::get_adf_ann );
194     fgTie("/instrumentation/kr-87/annunciators/bfo", this,
195           &FGKR_87::get_bfo_ann );
196     fgTie("/instrumentation/kr-87/annunciators/frq", this,
197           &FGKR_87::get_frq_ann );
198     fgTie("/instrumentation/kr-87/annunciators/flt", this,
199           &FGKR_87::get_flt_ann );
200     fgTie("/instrumentation/kr-87/annunciators/et", this,
201           &FGKR_87::get_et_ann );
202 }
203
204
205 void FGKR_87::unbind () {
206     // internal values
207     fgUntie("/instrumentation/kr-87/internal/valid");
208     fgUntie("/instrumentation/kr-87/internal/inrange");
209     fgUntie("/instrumentation/kr-87/internal/dist");
210     fgUntie("/instrumentation/kr-87/internal/heading");
211
212     // modes
213     fgUntie("/instrumentation/kr-87/modes/ant");
214     fgUntie("/instrumentation/kr-87/modes/stby");
215     fgUntie("/instrumentation/kr-87/modes/timer");
216     fgUntie("/instrumentation/kr-87/modes/count");
217
218     // input and buttons
219     fgUntie("/instrumentation/kr-87/inputs/rotation-deg");
220     fgUntie("/instrumentation/kr-87/inputs/power-btn");
221     fgUntie("/instrumentation/kr-87/inputs/volume");
222     fgUntie("/instrumentation/kr-87/inputs/adf-btn");
223     fgUntie("/instrumentation/kr-87/inputs/bfo-btn");
224     fgUntie("/instrumentation/kr-87/inputs/frq-btn");
225     fgUntie("/instrumentation/kr-87/inputs/flt-et-btn");
226     fgUntie("/instrumentation/kr-87/inputs/set-rst-btn");
227     fgUntie("/instrumentation/kr-87/inputs/ident-btn");
228
229     // outputs
230     fgUntie("/instrumentation/kr-87/outputs/selected-khz");
231     fgUntie("/instrumentation/kr-87/outputs/standby-khz");
232     fgUntie("/instrumentation/kr-87/outputs/needle-deg");
233     fgUntie("/instrumentation/kr-87/outputs/flight-timer");
234     fgUntie("/instrumentation/kr-87/outputs/elapsed-timer");
235
236     // annunciators
237     fgUntie("/instrumentation/kr-87/annunciators/ant");
238     fgUntie("/instrumentation/kr-87/annunciators/adf");
239     fgUntie("/instrumentation/kr-87/annunciators/bfo");
240     fgUntie("/instrumentation/kr-87/annunciators/frq");
241     fgUntie("/instrumentation/kr-87/annunciators/flt");
242     fgUntie("/instrumentation/kr-87/annunciators/et");
243 }
244
245
246 // Update the various nav values based on position and valid tuned in navs
247 void FGKR_87::update( double dt_sec ) {
248     SGGeod acft = globals->get_aircraft_position();
249
250     need_update = false;
251
252     double az1, az2, s;
253
254     // On timeout, scan again
255     _time_before_search_sec -= dt_sec;
256     if ( _time_before_search_sec < 0 ) {
257         search();
258     }
259
260     ////////////////////////////////////////////////////////////////////////
261     // Radio
262     ////////////////////////////////////////////////////////////////////////
263
264     if ( has_power() && serviceable->getBoolValue() ) {
265         // buttons
266         if ( adf_btn == 0 ) {
267             ant_mode = 1;
268         } else {
269             ant_mode = 0;
270         }
271         // cout << "ant_mode = " << ant_mode << endl;
272
273         if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
274             int tmp = freq;
275             freq = stby_freq;
276             stby_freq = tmp;
277         } else if ( frq_btn ) {
278             stby_mode = 0;
279             count_mode = 0;
280         }
281         last_frq_btn = frq_btn;
282
283         if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
284             if ( stby_mode == 0 ) {
285                 timer_mode = 0;
286             } else {
287                 timer_mode = !timer_mode;
288             }
289             stby_mode = 1;
290         }
291         last_flt_et_btn = flt_et_btn;
292
293         if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
294             // button depressed
295            tmp_timer = 0.0;
296         }
297         if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
298             // button depressed and was last iteration too
299             tmp_timer += dt_sec;
300             // cout << "tmp_timer = " << tmp_timer << endl;
301             if ( tmp_timer > 2.0 ) {
302                 // button held depressed for 2 seconds
303                 // cout << "entering elapsed count down mode" << endl;
304                 timer_mode = 1;
305                 count_mode = 2;
306                 elapsed_timer = 0.0;
307             }    
308         }
309         if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
310             // button released
311             if ( tmp_timer > 2.0 ) {
312                 // button held depressed for 2 seconds, don't adjust
313                 // mode, just exit                
314             } else if ( count_mode == 2 ) {
315                 count_mode = 1;
316             } else {
317                 count_mode = 0;
318                 elapsed_timer = 0.0;
319             }
320         }
321         last_set_rst_btn = set_rst_btn;
322
323         // timers
324         flight_timer += dt_sec;
325
326         if ( set_rst_btn == 0 ) {
327             // only count if set/rst button not depressed
328             if ( count_mode == 0 ) {
329                 elapsed_timer += dt_sec;
330             } else if ( count_mode == 1 ) {
331                 elapsed_timer -= dt_sec;
332                 if ( elapsed_timer < 1.0 ) {
333                     count_mode = 0;
334                     elapsed_timer = 0.0;
335                 }
336             }
337         }
338
339         // annunciators
340         ant_ann = !adf_btn;
341         adf_ann = adf_btn;
342         bfo_ann = bfo_btn;
343         frq_ann = !stby_mode;
344         flt_ann = stby_mode && !timer_mode;
345         if ( count_mode < 2 ) {
346             et_ann = stby_mode && timer_mode;
347         } else {
348             et_flash_time += dt_sec;
349             if ( et_ann && et_flash_time > 0.5 ) {
350                 et_ann = false;
351                 et_flash_time -= 0.5;
352             } else if ( !et_ann && et_flash_time > 0.2 ) {
353                 et_ann = true;
354                 et_flash_time -= 0.2;
355             }
356         }
357
358         if ( valid ) {
359             // cout << "adf is valid" << endl;
360             // staightline distance
361             // What a hack, dist is a class local variable
362             dist = sqrt(distSqr(SGVec3d::fromGeod(acft), xyz));
363
364             // wgs84 heading
365             geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
366                                 &az1, &az2, &s );
367             heading = az1;
368             // cout << " heading = " << heading
369             //      << " dist = " << dist << endl;
370
371             effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
372             if ( dist < effective_range * SG_NM_TO_METER ) {
373                 inrange = true;
374             } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
375                 inrange = sg_random() < 
376                     ( 2 * effective_range * SG_NM_TO_METER - dist ) /
377                     (effective_range * SG_NM_TO_METER);
378             } else {
379                 inrange = false;
380             }
381
382             // cout << "inrange = " << inrange << endl;
383
384             if ( inrange ) {
385                 goal_needle_deg = heading
386                     - fgGetDouble("/orientation/heading-deg");
387             }
388         } else {
389             inrange = false;
390         }
391
392         if ( ant_mode ) {
393             goal_needle_deg = 90.0;
394         }
395     } else {
396         // unit turned off
397         goal_needle_deg = 0.0;
398         flight_timer = 0.0;
399         elapsed_timer = 0.0;
400         ant_ann = false;
401         adf_ann = false;
402         bfo_ann = false;
403         frq_ann = false;
404         flt_ann = false;
405         et_ann = false;
406     }
407
408     // formatted timer
409     double time;
410     int hours, min, sec;
411     if ( timer_mode == 0 ) {
412         time = flight_timer;
413     } else {
414         time = elapsed_timer;
415     }
416     // cout << time << endl;
417     hours = (int)(time / 3600.0);
418     time -= hours * 3600.00;
419     min = (int)(time / 60.0);
420     time -= min * 60.0;
421     sec = (int)time;
422     int big, little;
423     if ( hours > 0 ) {
424         big = hours;
425         if ( big > 99 ) {
426             big = 99;
427         }
428         little = min;
429     } else {
430         big = min;
431         little = sec;
432     }
433     if ( big > 99 ) {
434         big = 99;
435     }
436     char formatted_timer[128];
437     // cout << big << ":" << little << endl;
438     snprintf(formatted_timer, 6, "%02d:%02d", big, little);
439     fgSetString( "/instrumentation/kr-87/outputs/timer-string",
440                  formatted_timer );
441
442     while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
443     while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
444
445     double diff = goal_needle_deg - needle_deg;
446     while ( diff < -180.0 ) { diff += 360.0; }
447     while ( diff > 180.0 ) { diff -= 360.0; }
448
449     needle_deg += diff * dt_sec * 4;
450     while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
451     while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
452
453     // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
454     //      << endl;
455     // cout << "flt = " << flight_timer << " et = " << elapsed_timer 
456     //      << " needle = " << needle_deg << endl;
457
458     if ( valid && inrange && serviceable->getBoolValue() ) {
459         // play station ident via audio system if on + ant mode,
460         // otherwise turn it off
461         if ( vol_btn >= 0.01 && audio_btn ) {
462             SGSoundSample *sound;
463             sound = _sgr->find( "adf-ident" );
464             if ( sound != NULL ) {
465                 if ( !adf_btn ) {
466                     sound->set_volume( vol_btn );
467                 } else {
468                     sound->set_volume( vol_btn / 4.0 );
469                 }
470             } else {
471                 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
472             }
473             if ( last_time <
474                  globals->get_time_params()->get_cur_time() - 30 ) {
475                 last_time = globals->get_time_params()->get_cur_time();
476                 play_count = 0;
477             }
478             if ( play_count < 4 ) {
479                 // play ADF ident
480                 if ( !_sgr->is_playing("adf-ident") && (vol_btn > 0.05) ) {
481                     _sgr->play_once( "adf-ident" );
482                     ++play_count;
483                 }
484             }
485         } else {
486             _sgr->stop( "adf-ident" );
487         }
488     }
489 }
490
491
492 // Update current nav/adf radio stations based on current postition
493 void FGKR_87::search() {
494   SGGeod pos = globals->get_aircraft_position();
495   
496                                 // FIXME: the panel should handle this
497     static string last_ident = "";
498
499     // reset search time
500     _time_before_search_sec = 1.0;
501
502     ////////////////////////////////////////////////////////////////////////
503     // ADF.
504     ////////////////////////////////////////////////////////////////////////
505
506   
507     FGNavRecord *adf = globals->get_navlist()->findByFreq( freq, pos);
508     if ( adf != NULL ) {
509         char sfreq[128];
510         snprintf( sfreq, 10, "%d", freq );
511         ident = sfreq;
512         ident += adf->get_ident();
513         // cout << "adf ident = " << ident << endl;
514         valid = true;
515         if ( last_ident != ident ) {
516             last_ident = ident;
517
518             trans_ident = adf->get_trans_ident();
519             stn_lon = adf->get_lon();
520             stn_lat = adf->get_lat();
521             stn_elev = adf->get_elev_ft();
522             range = adf->get_range();
523             effective_range = kludgeRange(stn_elev, pos.getElevationM(), range);
524             xyz = adf->cart();
525
526             if ( _sgr->exists( "adf-ident" ) ) {
527                 _sgr->remove( "adf-ident" );
528             }
529             SGSoundSample *sound;
530         sound = FGMorse::instance()->make_ident( trans_ident, FGMorse::LO_FREQUENCY );
531             sound->set_volume( 0.3 );
532             _sgr->add( sound, "adf-ident" );
533
534             int offset = (int)(sg_random() * 30.0);
535             play_count = offset / 4;
536             last_time = globals->get_time_params()->get_cur_time() -
537                 offset;
538             // cout << "offset = " << offset << " play_count = "
539             //      << play_count << " last_time = "
540             //      << last_time << " current time = "
541             //      << globals->get_time_params()->get_cur_time() << endl;
542
543             // cout << "Found an adf station in range" << endl;
544             // cout << " id = " << nav->get_ident() << endl;
545         }
546     } else {
547         valid = false;
548         ident = "";
549         trans_ident = "";
550         _sgr->remove( "adf-ident" );
551         last_ident = "";
552         // cout << "not picking up adf. :-(" << endl;
553     }
554 }
555
556
557 int FGKR_87::get_stby_freq() const {
558     if ( stby_mode == 0 ) {
559         return stby_freq;
560     } else {
561         if ( timer_mode == 0 ) {
562             return (int)flight_timer;
563         } else {
564             return (int)elapsed_timer;
565         }
566     }
567 }