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