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