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),
115 FGKR_87::~FGKR_87() {
119 void FGKR_87::init () {
120 SGSoundMgr *smgr = globals->get_soundmgr();
121 _sgr = smgr->find("avionics", true);
122 _sgr->tie_to_listener();
127 void FGKR_87::bind () {
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,
134 fgTie("/instrumentation/kr-87/internal/heading", this,
135 &FGKR_87::get_heading);
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);
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);
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);
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 );
210 void FGKR_87::unbind () {
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");
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");
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");
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");
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");
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());
261 // On timeout, scan again
262 _time_before_search_sec -= dt_sec;
263 if ( _time_before_search_sec < 0 ) {
267 ////////////////////////////////////////////////////////////////////////
269 ////////////////////////////////////////////////////////////////////////
271 if ( has_power() && serviceable->getBoolValue() ) {
273 if ( adf_btn == 0 ) {
278 // cout << "ant_mode = " << ant_mode << endl;
280 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
284 } else if ( frq_btn ) {
288 last_frq_btn = frq_btn;
290 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
291 if ( stby_mode == 0 ) {
294 timer_mode = !timer_mode;
298 last_flt_et_btn = flt_et_btn;
300 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
304 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
305 // button depressed and was last iteration too
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;
316 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
318 if ( tmp_timer > 2.0 ) {
319 // button held depressed for 2 seconds, don't adjust
321 } else if ( count_mode == 2 ) {
328 last_set_rst_btn = set_rst_btn;
331 flight_timer += dt_sec;
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 ) {
350 frq_ann = !stby_mode;
351 flt_ann = stby_mode && !timer_mode;
352 if ( count_mode < 2 ) {
353 et_ann = stby_mode && timer_mode;
355 et_flash_time += dt_sec;
356 if ( et_ann && et_flash_time > 0.5 ) {
358 et_flash_time -= 0.5;
359 } else if ( !et_ann && et_flash_time > 0.2 ) {
361 et_flash_time -= 0.2;
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));
372 geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
375 // cout << " heading = " << heading
376 // << " dist = " << dist << endl;
378 effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
379 if ( dist < effective_range * SG_NM_TO_METER ) {
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);
389 // cout << "inrange = " << inrange << endl;
392 goal_needle_deg = heading
393 - fgGetDouble("/orientation/heading-deg");
400 goal_needle_deg = 90.0;
404 goal_needle_deg = 0.0;
418 if ( timer_mode == 0 ) {
421 time = elapsed_timer;
423 // cout << time << endl;
424 hours = (int)(time / 3600.0);
425 time -= hours * 3600.00;
426 min = (int)(time / 60.0);
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",
449 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
450 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
452 double diff = goal_needle_deg - needle_deg;
453 while ( diff < -180.0 ) { diff += 360.0; }
454 while ( diff > 180.0 ) { diff -= 360.0; }
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; }
460 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
462 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
463 // << " needle = " << needle_deg << endl;
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 ) {
473 sound->set_volume( vol_btn );
475 sound->set_volume( vol_btn / 4.0 );
478 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
481 globals->get_time_params()->get_cur_time() - 30 ) {
482 last_time = globals->get_time_params()->get_cur_time();
485 if ( play_count < 4 ) {
487 if ( !_sgr->is_playing("adf-ident") && (vol_btn > 0.05) ) {
488 _sgr->play_once( "adf-ident" );
493 _sgr->stop( "adf-ident" );
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());
504 // FIXME: the panel should handle this
505 static string last_ident = "";
508 _time_before_search_sec = 1.0;
510 ////////////////////////////////////////////////////////////////////////
512 ////////////////////////////////////////////////////////////////////////
515 FGNavRecord *adf = globals->get_navlist()->findByFreq( freq, pos);
518 snprintf( sfreq, 10, "%d", freq );
520 ident += adf->get_ident();
521 // cout << "adf ident = " << ident << endl;
523 if ( last_ident != ident ) {
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);
534 if ( _sgr->exists( "adf-ident" ) ) {
535 _sgr->remove( "adf-ident" );
537 SGSoundSample *sound;
538 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
539 sound->set_volume( 0.3 );
540 _sgr->add( sound, "adf-ident" );
542 int offset = (int)(sg_random() * 30.0);
543 play_count = offset / 4;
544 last_time = globals->get_time_params()->get_cur_time() -
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;
551 // cout << "Found an adf station in range" << endl;
552 // cout << " id = " << nav->get_ident() << endl;
558 _sgr->remove( "adf-ident" );
560 // cout << "not picking up adf. :-(" << endl;
565 int FGKR_87::get_stby_freq() const {
566 if ( stby_mode == 0 ) {
569 if ( timer_mode == 0 ) {
570 return (int)flight_timer;
572 return (int)elapsed_timer;