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