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