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