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