]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/kr_87.cxx
Patch from Cameron Moore:
[flightgear.git] / src / Cockpit / 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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 #include <Time/FGEventMgr.hxx>
36
37 #include "kr_87.hxx"
38
39 #include <string>
40 SG_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*SG_METER_TO_FEET) - stationElev) / 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() :
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     need_update(true),
77     valid(false),
78     inrange(false),
79     dist(0.0),
80     heading(0.0),
81     goal_needle_deg(0.0),
82     et_flash_time(0.0),
83     ant_mode(0),
84     stby_mode(0),
85     timer_mode(0),
86     count_mode(0),
87     rotation(0),
88     on_off_vol_btn(0.5),
89     adf_btn(true),
90     bfo_btn(false),
91     frq_btn(false),
92     last_frq_btn(false),
93     flt_et_btn(false),
94     last_flt_et_btn(false),
95     set_rst_btn(false),
96     last_set_rst_btn(false),
97     ident_btn(false),
98     freq(0.0),
99     stby_freq(0.0),
100     needle_deg(0.0),
101     flight_timer(0.0),
102     elapsed_timer(0.0),
103     tmp_timer(0.0)
104 {
105     SGPath path( globals->get_fg_root() );
106     SGPath term = path;
107     term.append( "Navaids/range.term" );
108     SGPath low = path;
109     low.append( "Navaids/range.low" );
110     SGPath high = path;
111     high.append( "Navaids/range.high" );
112
113     term_tbl = new SGInterpTable( term.str() );
114     low_tbl = new SGInterpTable( low.str() );
115     high_tbl = new SGInterpTable( high.str() );
116 }
117
118
119 // Destructor
120 FGKR_87::~FGKR_87() {
121     delete term_tbl;
122     delete low_tbl;
123     delete high_tbl;
124 }
125
126
127 void FGKR_87::init () {
128     morse.init();
129
130     update(0);                  // FIXME: use dt
131 }
132
133
134 void FGKR_87::bind () {
135     // internal values
136     fgTie("/radios/kr-87/internal/valid", this, &FGKR_87::get_valid);
137     fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange);
138     fgTie("/radios/kr-87/internal/dist", this, &FGKR_87::get_dist);
139     fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading);
140
141     // modes
142     fgTie("/radios/kr-87/modes/ant", this,
143           &FGKR_87::get_ant_mode);
144     fgTie("/radios/kr-87/modes/stby", this,
145           &FGKR_87::get_stby_mode);
146     fgTie("/radios/kr-87/modes/timer", this,
147           &FGKR_87::get_timer_mode);
148     fgTie("/radios/kr-87/modes/count", this,
149           &FGKR_87::get_count_mode);
150
151     // input and buttons
152     fgTie("/radios/kr-87/inputs/rotation-deg", this,
153           &FGKR_87::get_rotation, &FGKR_87::set_rotation);
154     fgSetArchivable("/radios/kr-87/inputs/rotation-deg");
155     fgTie("/radios/kr-87/inputs/on-off-volume", this,
156           &FGKR_87::get_on_off_vol_btn,
157           &FGKR_87::set_on_off_vol_btn);
158     fgSetArchivable("/radios/kr-87/inputs/on-off-volume");
159     fgTie("/radios/kr-87/inputs/adf-btn", this,
160           &FGKR_87::get_adf_btn,
161           &FGKR_87::set_adf_btn);
162     fgTie("/radios/kr-87/inputs/bfo-btn", this,
163           &FGKR_87::get_bfo_btn,
164           &FGKR_87::set_bfo_btn);
165     fgTie("/radios/kr-87/inputs/frq-btn", this,
166           &FGKR_87::get_frq_btn,
167           &FGKR_87::set_frq_btn);
168     fgTie("/radios/kr-87/inputs/flt-et-btn", this,
169           &FGKR_87::get_flt_et_btn,
170           &FGKR_87::set_flt_et_btn);
171     fgTie("/radios/kr-87/inputs/set-rst-btn", this,
172           &FGKR_87::get_set_rst_btn,
173           &FGKR_87::set_set_rst_btn);
174     fgTie("/radios/kr-87/inputs/ident-btn", this,
175           &FGKR_87::get_ident_btn, &FGKR_87::set_ident_btn);
176
177     // outputs
178     fgTie("/radios/kr-87/outputs/selected-khz", this,
179           &FGKR_87::get_freq, &FGKR_87::set_freq);
180     fgSetArchivable("/radios/kr-87/outputs/selected-khz");
181     fgTie("/radios/kr-87/outputs/standby-khz", this,
182           &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
183     fgSetArchivable("/radios/kr-87/outputs/standby-khz");
184     fgTie("/radios/kr-87/outputs/needle-deg", this,
185           &FGKR_87::get_needle_deg);
186     fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer);
187     fgTie("/radios/kr-87/outputs/elapsed-timer", this,
188           &FGKR_87::get_elapsed_timer,
189           &FGKR_87::set_elapsed_timer);
190
191     // annunciators
192     fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann );
193     fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann );
194     fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann );
195     fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann );
196     fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann );
197     fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann );
198 }
199
200
201 void FGKR_87::unbind () {
202     // internal values
203     fgUntie("/radios/kr-87/internal/valid");
204     fgUntie("/radios/kr-87/internal/inrange");
205     fgUntie("/radios/kr-87/internal/dist");
206     fgUntie("/radios/kr-87/internal/heading");
207
208     // modes
209     fgUntie("/radios/kr-87/modes/ant");
210     fgUntie("/radios/kr-87/modes/stby");
211     fgUntie("/radios/kr-87/modes/timer");
212     fgUntie("/radios/kr-87/modes/count");
213
214     // input and buttons
215     fgUntie("/radios/kr-87/inputs/rotation-deg");
216     fgUntie("/radios/kr-87/inputs/on-off-volume");
217     fgUntie("/radios/kr-87/inputs/adf-btn");
218     fgUntie("/radios/kr-87/inputs/bfo-btn");
219     fgUntie("/radios/kr-87/inputs/frq-btn");
220     fgUntie("/radios/kr-87/inputs/flt-et-btn");
221     fgUntie("/radios/kr-87/inputs/set-rst-btn");
222     fgUntie("/radios/kr-87/inputs/ident-btn");
223
224     // outputs
225     fgUntie("/radios/kr-87/outputs/selected-khz");
226     fgUntie("/radios/kr-87/outputs/standby-khz");
227     fgUntie("/radios/kr-87/outputs/needle-deg");
228     fgUntie("/radios/kr-87/outputs/flight-timer");
229     fgUntie("/radios/kr-87/outputs/elapsed-timer");
230
231     // annunciators
232     fgUntie("/radios/kr-87/annunciators/ant");
233     fgUntie("/radios/kr-87/annunciators/adf");
234     fgUntie("/radios/kr-87/annunciators/bfo");
235     fgUntie("/radios/kr-87/annunciators/frq");
236     fgUntie("/radios/kr-87/annunciators/flt");
237     fgUntie("/radios/kr-87/annunciators/et");
238 }
239
240
241 // Update the various nav values based on position and valid tuned in navs
242 void FGKR_87::update( double dt ) {
243     double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
244     double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
245     double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
246
247     need_update = false;
248
249     Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
250     Point3D station;
251     double az1, az2, s;
252
253     ////////////////////////////////////////////////////////////////////////
254     // Radio
255     ////////////////////////////////////////////////////////////////////////
256
257     if ( on_off_vol_btn >= 0.01 ) {
258         // buttons
259         if ( adf_btn == 0 ) {
260             ant_mode = 1;
261         } else {
262             ant_mode = 0;
263         }
264
265         if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
266             double tmp = freq;
267             freq = stby_freq;
268             stby_freq = tmp;
269         } else if ( frq_btn ) {
270             stby_mode = 0;
271             count_mode = 0;
272         }
273         last_frq_btn = frq_btn;
274
275         if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
276             if ( stby_mode == 0 ) {
277                 timer_mode = 0;
278             } else {
279                 timer_mode = !timer_mode;
280             }
281             stby_mode = 1;
282         }
283         last_flt_et_btn = flt_et_btn;
284
285         if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
286             // button depressed
287            tmp_timer = 0.0;
288         }
289         if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
290             // button depressed and was last iteration too
291             tmp_timer += dt;
292             cout << "tmp_timer = " << tmp_timer << endl;
293             if ( tmp_timer > 2.0 ) {
294                 // button held depressed for 2 seconds
295                 cout << "entering elapsed count down mode" << endl;
296                 timer_mode = 1;
297                 count_mode = 2;
298                 elapsed_timer = 0.0;
299             }    
300         }
301         if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
302             // button released
303             if ( tmp_timer > 2.0 ) {
304                 // button held depressed for 2 seconds, don't adjust
305                 // mode, just exit                
306             } else if ( count_mode == 2 ) {
307                 count_mode = 1;
308             } else {
309                 count_mode = 0;
310                 elapsed_timer = 0.0;
311             }
312         }
313         last_set_rst_btn = set_rst_btn;
314
315         // timers
316         flight_timer += dt;
317
318         if ( set_rst_btn == 0 ) {
319             // only count if set/rst button not depressed
320             if ( count_mode == 0 ) {
321                 elapsed_timer += dt;
322             } else if ( count_mode == 1 ) {
323                 elapsed_timer -= dt;
324                 if ( elapsed_timer < 1.0 ) {
325                     count_mode = 0;
326                     elapsed_timer = 0.0;
327                 }
328             }
329         }
330
331         // annunciators
332         ant_ann = adf_btn;
333         adf_ann = !adf_btn;
334         bfo_ann = bfo_btn;
335         frq_ann = !stby_mode;
336         flt_ann = stby_mode && !timer_mode;
337         if ( count_mode < 2 ) {
338             et_ann = stby_mode && timer_mode;
339         } else {
340             et_flash_time += dt;
341             if ( et_ann && et_flash_time > 0.5 ) {
342                 et_ann = false;
343                 et_flash_time -= 0.5;
344             } else if ( !et_ann && et_flash_time > 0.2 ) {
345                 et_ann = true;
346                 et_flash_time -= 0.2;
347             }
348         }
349
350         if ( valid ) {
351             // staightline distance
352             station = Point3D( x, y, z );
353             dist = aircraft.distance3D( station );
354
355             // wgs84 heading
356             geo_inverse_wgs_84( acft_elev,
357                                 acft_lat * SGD_RADIANS_TO_DEGREES,
358                                 acft_lon * SGD_RADIANS_TO_DEGREES, 
359                                 stn_lat, stn_lon,
360                                 &az1, &az2, &s );
361             heading = az1;
362             // cout << " heading = " << heading
363             //      << " dist = " << dist << endl;
364
365             effective_range = kludgeRange(stn_elev, acft_elev, range);
366             if ( dist < effective_range * SG_NM_TO_METER ) {
367                 inrange = true;
368             } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
369                 inrange = sg_random() < 
370                     ( 2 * effective_range * SG_NM_TO_METER - dist ) /
371                     (effective_range * SG_NM_TO_METER);
372             } else {
373                 inrange = false;
374             }
375
376             if ( inrange ) {
377                 goal_needle_deg = heading
378                     - fgGetDouble("/orientation/heading-deg");
379             }
380         } else {
381             inrange = false;
382         }
383
384         if ( ant_mode ) {
385             goal_needle_deg = 90.0;
386         }
387     } else {
388         // unit turned off
389         goal_needle_deg = 0.0;
390         flight_timer = 0.0;
391         elapsed_timer = 0.0;
392         ant_ann = false;
393         adf_ann = false;
394         bfo_ann = false;
395         frq_ann = false;
396         flt_ann = false;
397         et_ann = false;
398     }
399
400     
401     while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
402     while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
403
404     double diff = goal_needle_deg - needle_deg;
405     while ( diff < -180.0 ) { diff += 360.0; }
406     while ( diff > 180.0 ) { diff -= 360.0; }
407
408     needle_deg += diff * dt * 4;
409     while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
410     while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
411
412     // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
413     //      << endl;
414     // cout << "flt = " << flight_timer << " et = " << elapsed_timer 
415     //      << " needle = " << needle_deg << endl;
416
417 #ifdef ENABLE_AUDIO_SUPPORT
418     if ( valid && inrange ) {
419         // play station ident via audio system if on + ident_btn,
420         // otherwise turn it off
421         if ( on_off_vol_btn >= 0.01 && ident_btn ) {
422             FGSimpleSound *sound;
423             sound = globals->get_soundmgr()->find( "adf-ident" );
424             if ( sound != NULL ) {
425                 sound->set_volume( on_off_vol_btn );
426             } else {
427                 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
428             }
429             if ( last_time <
430                  globals->get_time_params()->get_cur_time() - 30 ) {
431                 last_time = globals->get_time_params()->get_cur_time();
432                 play_count = 0;
433             }
434             if ( play_count < 4 ) {
435                 // play ADF ident
436                 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
437                     globals->get_soundmgr()->play_once( "adf-ident" );
438                     ++play_count;
439                 }
440             }
441         } else {
442             globals->get_soundmgr()->stop( "adf-ident" );
443         }
444     }
445 #endif
446 }
447
448
449 // Update current nav/adf radio stations based on current postition
450 void FGKR_87::search() {
451     double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
452     double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
453     double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
454
455                                 // FIXME: the panel should handle this
456     FGNav nav;
457
458     static string last_ident = "";
459
460     ////////////////////////////////////////////////////////////////////////
461     // ADF.
462     ////////////////////////////////////////////////////////////////////////
463
464     if ( current_navlist->query( acft_lon, acft_lat, acft_elev, freq, &nav ) ) {
465         char sfreq[128];
466         snprintf( sfreq, 10, "%.0f", freq );
467         ident = sfreq;
468         ident += nav.get_ident();
469 //      cout << "adf ident = " << ident << endl;
470         valid = true;
471         if ( last_ident != ident ) {
472             last_ident = ident;
473
474             trans_ident = nav.get_trans_ident();
475             stn_lon = nav.get_lon();
476             stn_lat = nav.get_lat();
477             stn_elev = nav.get_elev();
478             range = nav.get_range();
479             effective_range = kludgeRange(stn_elev, acft_elev, range);
480             x = nav.get_x();
481             y = nav.get_y();
482             z = nav.get_z();
483
484 #ifdef ENABLE_AUDIO_SUPPORT
485             if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
486                 globals->get_soundmgr()->remove( "adf-ident" );
487             }
488             FGSimpleSound *sound;
489             sound = morse.make_ident( trans_ident, LO_FREQUENCY );
490             sound->set_volume( 0.3 );
491             globals->get_soundmgr()->add( sound, "adf-ident" );
492
493             int offset = (int)(sg_random() * 30.0);
494             play_count = offset / 4;
495             last_time = globals->get_time_params()->get_cur_time() -
496                 offset;
497             // cout << "offset = " << offset << " play_count = "
498             //      << play_count << " last_time = "
499             //      << last_time << " current time = "
500             //      << globals->get_time_params()->get_cur_time() << endl;
501 #endif
502
503             // cout << "Found an adf station in range" << endl;
504             // cout << " id = " << nav.get_ident() << endl;
505         }
506     } else {
507         valid = false;
508         ident = "";
509         trans_ident = "";
510 #ifdef ENABLE_AUDIO_SUPPORT
511         globals->get_soundmgr()->remove( "adf-ident" );
512 #endif
513         last_ident = "";
514         // cout << "not picking up adf. :-(" << endl;
515     }
516 }
517
518
519 double FGKR_87::get_stby_freq() const {
520     if ( stby_mode == 0 ) {
521         return stby_freq;
522     } else {
523         if ( timer_mode == 0 ) {
524             return flight_timer;
525         } else {
526             return elapsed_timer;
527         }
528     }
529 }