1 // kr-87.cxx -- class to impliment the King KR 87 Digital ADF
3 // Written by Curtis Olson, started April 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <stdio.h> // snprintf
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>
35 #include <Aircraft/aircraft.hxx>
36 #include <Navaids/navlist.hxx>
43 static int play_count = 0;
44 static time_t last_time = 0;
48 * Boy, this is ugly! Make the VOR range vary by altitude difference.
50 static double kludgeRange ( double stationElev, double aircraftElev,
53 // Assume that the nominal range (usually
54 // 50nm) applies at a 5,000 ft difference.
56 double factor = (aircraftElev - stationElev)*SG_METER_TO_FEET / 5000.0;
57 double range = fabs(nominalRange * factor);
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;
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)),
99 last_flt_et_btn(false),
101 last_set_rst_btn(false),
108 _time_before_search_sec(0)
114 FGKR_87::~FGKR_87() {
118 void FGKR_87::init () {
123 void FGKR_87::bind () {
125 fgTie("/instrumentation/kr-87/internal/valid", this, &FGKR_87::get_valid);
126 fgTie("/instrumentation/kr-87/internal/inrange", this,
127 &FGKR_87::get_inrange);
128 fgTie("/instrumentation/kr-87/internal/dist", this,
130 fgTie("/instrumentation/kr-87/internal/heading", this,
131 &FGKR_87::get_heading);
134 fgTie("/instrumentation/kr-87/modes/ant", this,
135 &FGKR_87::get_ant_mode);
136 fgTie("/instrumentation/kr-87/modes/stby", this,
137 &FGKR_87::get_stby_mode);
138 fgTie("/instrumentation/kr-87/modes/timer", this,
139 &FGKR_87::get_timer_mode);
140 fgTie("/instrumentation/kr-87/modes/count", this,
141 &FGKR_87::get_count_mode);
144 fgTie("/instrumentation/kr-87/inputs/rotation-deg", this,
145 &FGKR_87::get_rotation, &FGKR_87::set_rotation);
146 fgSetArchivable("/instrumentation/kr-87/inputs/rotation-deg");
147 fgTie("/instrumentation/kr-87/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 fgTie("/instrumentation/kr-87/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 fgTie("/instrumentation/kr-87/inputs/volume", this,
156 &FGKR_87::get_vol_btn,
157 &FGKR_87::set_vol_btn);
158 fgSetArchivable("/instrumentation/kr-87/inputs/volume");
159 fgTie("/instrumentation/kr-87/inputs/adf-btn", this,
160 &FGKR_87::get_adf_btn,
161 &FGKR_87::set_adf_btn);
162 fgTie("/instrumentation/kr-87/inputs/bfo-btn", this,
163 &FGKR_87::get_bfo_btn,
164 &FGKR_87::set_bfo_btn);
165 fgTie("/instrumentation/kr-87/inputs/frq-btn", this,
166 &FGKR_87::get_frq_btn,
167 &FGKR_87::set_frq_btn);
168 fgTie("/instrumentation/kr-87/inputs/flt-et-btn", this,
169 &FGKR_87::get_flt_et_btn,
170 &FGKR_87::set_flt_et_btn);
171 fgTie("/instrumentation/kr-87/inputs/set-rst-btn", this,
172 &FGKR_87::get_set_rst_btn,
173 &FGKR_87::set_set_rst_btn);
176 fgTie("/instrumentation/kr-87/outputs/selected-khz", this,
177 &FGKR_87::get_freq, &FGKR_87::set_freq);
178 fgSetArchivable("/instrumentation/kr-87/outputs/selected-khz");
179 fgTie("/instrumentation/kr-87/outputs/standby-khz", this,
180 &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
181 fgSetArchivable("/instrumentation/kr-87/outputs/standby-khz");
182 fgTie("/instrumentation/kr-87/outputs/needle-deg", this,
183 &FGKR_87::get_needle_deg);
184 fgTie("/instrumentation/kr-87/outputs/flight-timer", this,
185 &FGKR_87::get_flight_timer);
186 fgTie("/instrumentation/kr-87/outputs/elapsed-timer", this,
187 &FGKR_87::get_elapsed_timer,
188 &FGKR_87::set_elapsed_timer);
191 fgTie("/instrumentation/kr-87/annunciators/ant", this,
192 &FGKR_87::get_ant_ann );
193 fgTie("/instrumentation/kr-87/annunciators/adf", this,
194 &FGKR_87::get_adf_ann );
195 fgTie("/instrumentation/kr-87/annunciators/bfo", this,
196 &FGKR_87::get_bfo_ann );
197 fgTie("/instrumentation/kr-87/annunciators/frq", this,
198 &FGKR_87::get_frq_ann );
199 fgTie("/instrumentation/kr-87/annunciators/flt", this,
200 &FGKR_87::get_flt_ann );
201 fgTie("/instrumentation/kr-87/annunciators/et", this,
202 &FGKR_87::get_et_ann );
206 void FGKR_87::unbind () {
208 fgUntie("/instrumentation/kr-87/internal/valid");
209 fgUntie("/instrumentation/kr-87/internal/inrange");
210 fgUntie("/instrumentation/kr-87/internal/dist");
211 fgUntie("/instrumentation/kr-87/internal/heading");
214 fgUntie("/instrumentation/kr-87/modes/ant");
215 fgUntie("/instrumentation/kr-87/modes/stby");
216 fgUntie("/instrumentation/kr-87/modes/timer");
217 fgUntie("/instrumentation/kr-87/modes/count");
220 fgUntie("/instrumentation/kr-87/inputs/rotation-deg");
221 fgUntie("/instrumentation/kr-87/inputs/power-btn");
222 fgUntie("/instrumentation/kr-87/inputs/volume");
223 fgUntie("/instrumentation/kr-87/inputs/adf-btn");
224 fgUntie("/instrumentation/kr-87/inputs/bfo-btn");
225 fgUntie("/instrumentation/kr-87/inputs/frq-btn");
226 fgUntie("/instrumentation/kr-87/inputs/flt-et-btn");
227 fgUntie("/instrumentation/kr-87/inputs/set-rst-btn");
228 fgUntie("/instrumentation/kr-87/inputs/ident-btn");
231 fgUntie("/instrumentation/kr-87/outputs/selected-khz");
232 fgUntie("/instrumentation/kr-87/outputs/standby-khz");
233 fgUntie("/instrumentation/kr-87/outputs/needle-deg");
234 fgUntie("/instrumentation/kr-87/outputs/flight-timer");
235 fgUntie("/instrumentation/kr-87/outputs/elapsed-timer");
238 fgUntie("/instrumentation/kr-87/annunciators/ant");
239 fgUntie("/instrumentation/kr-87/annunciators/adf");
240 fgUntie("/instrumentation/kr-87/annunciators/bfo");
241 fgUntie("/instrumentation/kr-87/annunciators/frq");
242 fgUntie("/instrumentation/kr-87/annunciators/flt");
243 fgUntie("/instrumentation/kr-87/annunciators/et");
247 // Update the various nav values based on position and valid tuned in navs
248 void FGKR_87::update( double dt_sec ) {
249 SGGeod acft = SGGeod::fromDegFt(lon_node->getDoubleValue(),
250 lat_node->getDoubleValue(),
251 alt_node->getDoubleValue());
257 // On timeout, scan again
258 _time_before_search_sec -= dt_sec;
259 if ( _time_before_search_sec < 0 ) {
263 ////////////////////////////////////////////////////////////////////////
265 ////////////////////////////////////////////////////////////////////////
267 if ( has_power() && serviceable->getBoolValue() ) {
269 if ( adf_btn == 0 ) {
274 // cout << "ant_mode = " << ant_mode << endl;
276 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
280 } else if ( frq_btn ) {
284 last_frq_btn = frq_btn;
286 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
287 if ( stby_mode == 0 ) {
290 timer_mode = !timer_mode;
294 last_flt_et_btn = flt_et_btn;
296 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
300 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
301 // button depressed and was last iteration too
303 // cout << "tmp_timer = " << tmp_timer << endl;
304 if ( tmp_timer > 2.0 ) {
305 // button held depressed for 2 seconds
306 // cout << "entering elapsed count down mode" << endl;
312 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
314 if ( tmp_timer > 2.0 ) {
315 // button held depressed for 2 seconds, don't adjust
317 } else if ( count_mode == 2 ) {
324 last_set_rst_btn = set_rst_btn;
327 flight_timer += dt_sec;
329 if ( set_rst_btn == 0 ) {
330 // only count if set/rst button not depressed
331 if ( count_mode == 0 ) {
332 elapsed_timer += dt_sec;
333 } else if ( count_mode == 1 ) {
334 elapsed_timer -= dt_sec;
335 if ( elapsed_timer < 1.0 ) {
346 frq_ann = !stby_mode;
347 flt_ann = stby_mode && !timer_mode;
348 if ( count_mode < 2 ) {
349 et_ann = stby_mode && timer_mode;
351 et_flash_time += dt_sec;
352 if ( et_ann && et_flash_time > 0.5 ) {
354 et_flash_time -= 0.5;
355 } else if ( !et_ann && et_flash_time > 0.2 ) {
357 et_flash_time -= 0.2;
362 // cout << "adf is valid" << endl;
363 // staightline distance
364 // What a hack, dist is a class local variable
365 dist = sqrt(distSqr(SGVec3d::fromGeod(acft), xyz));
368 geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
371 // cout << " heading = " << heading
372 // << " dist = " << dist << endl;
374 effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
375 if ( dist < effective_range * SG_NM_TO_METER ) {
377 } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
378 inrange = sg_random() <
379 ( 2 * effective_range * SG_NM_TO_METER - dist ) /
380 (effective_range * SG_NM_TO_METER);
385 // cout << "inrange = " << inrange << endl;
388 goal_needle_deg = heading
389 - fgGetDouble("/orientation/heading-deg");
396 goal_needle_deg = 90.0;
400 goal_needle_deg = 0.0;
414 if ( timer_mode == 0 ) {
417 time = elapsed_timer;
419 // cout << time << endl;
420 hours = (int)(time / 3600.0);
421 time -= hours * 3600.00;
422 min = (int)(time / 60.0);
439 char formatted_timer[128];
440 // cout << big << ":" << little << endl;
441 snprintf(formatted_timer, 6, "%02d:%02d", big, little);
442 fgSetString( "/instrumentation/kr-87/outputs/timer-string",
445 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
446 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
448 double diff = goal_needle_deg - needle_deg;
449 while ( diff < -180.0 ) { diff += 360.0; }
450 while ( diff > 180.0 ) { diff -= 360.0; }
452 needle_deg += diff * dt_sec * 4;
453 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
454 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
456 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
458 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
459 // << " needle = " << needle_deg << endl;
461 if ( valid && inrange && serviceable->getBoolValue() ) {
462 // play station ident via audio system if on + ant mode,
463 // otherwise turn it off
464 if ( vol_btn >= 0.01 && audio_btn ) {
465 SGSoundSample *sound;
466 sound = globals->get_soundmgr()->find( "adf-ident" );
467 if ( sound != NULL ) {
469 sound->set_volume( vol_btn );
471 sound->set_volume( vol_btn / 4.0 );
474 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
477 globals->get_time_params()->get_cur_time() - 30 ) {
478 last_time = globals->get_time_params()->get_cur_time();
481 if ( play_count < 4 ) {
483 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
484 globals->get_soundmgr()->play_once( "adf-ident" );
489 globals->get_soundmgr()->stop( "adf-ident" );
495 // Update current nav/adf radio stations based on current postition
496 void FGKR_87::search() {
497 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
498 lat_node->getDoubleValue(), alt_node->getDoubleValue());
500 // FIXME: the panel should handle this
501 static string last_ident = "";
504 _time_before_search_sec = 1.0;
506 ////////////////////////////////////////////////////////////////////////
508 ////////////////////////////////////////////////////////////////////////
511 FGNavRecord *adf = globals->get_navlist()->findByFreq( freq, pos);
514 snprintf( sfreq, 10, "%d", freq );
516 ident += adf->get_ident();
517 // cout << "adf ident = " << ident << endl;
519 if ( last_ident != ident ) {
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, pos.getElevationM(), range);
530 if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
531 globals->get_soundmgr()->remove( "adf-ident" );
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" );
538 int offset = (int)(sg_random() * 30.0);
539 play_count = offset / 4;
540 last_time = globals->get_time_params()->get_cur_time() -
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;
547 // cout << "Found an adf station in range" << endl;
548 // cout << " id = " << nav->get_ident() << endl;
554 globals->get_soundmgr()->remove( "adf-ident" );
556 // cout << "not picking up adf. :-(" << endl;
561 int FGKR_87::get_stby_freq() const {
562 if ( stby_mode == 0 ) {
565 if ( timer_mode == 0 ) {
566 return (int)flight_timer;
568 return (int)elapsed_timer;