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.
27 #include <stdio.h> // snprintf
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>
34 #include <Navaids/navlist.hxx>
38 #include <Sound/morse.hxx>
42 static int play_count = 0;
43 static time_t last_time = 0;
47 * Boy, this is ugly! Make the VOR range vary by altitude difference.
49 static double kludgeRange ( double stationElev, double aircraftElev,
52 // Assume that the nominal range (usually
53 // 50nm) applies at a 5,000 ft difference.
55 double factor = (aircraftElev - stationElev)*SG_METER_TO_FEET / 5000.0;
56 double range = fabs(nominalRange * factor);
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;
72 FGKR_87::FGKR_87( SGPropertyNode *node ) :
73 bus_power(fgGetNode("/systems/electrical/outputs/adf", true)),
74 serviceable(fgGetNode("/instrumentation/adf/serviceable", true)),
95 last_flt_et_btn(false),
97 last_set_rst_btn(false),
104 _time_before_search_sec(0),
111 FGKR_87::~FGKR_87() {
115 void FGKR_87::init () {
116 SGSoundMgr *smgr = globals->get_soundmgr();
117 _sgr = smgr->find("avionics", true);
118 _sgr->tie_to_listener();
122 void FGKR_87::bind () {
124 fgTie("/instrumentation/kr-87/internal/valid", this, &FGKR_87::get_valid);
125 fgTie("/instrumentation/kr-87/internal/inrange", this,
126 &FGKR_87::get_inrange);
127 fgTie("/instrumentation/kr-87/internal/dist", this,
129 fgTie("/instrumentation/kr-87/internal/heading", this,
130 &FGKR_87::get_heading);
133 fgTie("/instrumentation/kr-87/modes/ant", this,
134 &FGKR_87::get_ant_mode);
135 fgTie("/instrumentation/kr-87/modes/stby", this,
136 &FGKR_87::get_stby_mode);
137 fgTie("/instrumentation/kr-87/modes/timer", this,
138 &FGKR_87::get_timer_mode);
139 fgTie("/instrumentation/kr-87/modes/count", this,
140 &FGKR_87::get_count_mode);
143 fgTie("/instrumentation/kr-87/inputs/rotation-deg", this,
144 &FGKR_87::get_rotation, &FGKR_87::set_rotation);
145 fgSetArchivable("/instrumentation/kr-87/inputs/rotation-deg");
146 fgTie("/instrumentation/kr-87/inputs/power-btn", this,
147 &FGKR_87::get_power_btn,
148 &FGKR_87::set_power_btn);
149 fgSetArchivable("/instrumentation/kr-87/inputs/power-btn");
150 fgTie("/instrumentation/kr-87/inputs/audio-btn", this,
151 &FGKR_87::get_audio_btn,
152 &FGKR_87::set_audio_btn);
153 fgSetArchivable("/instrumentation/kr-87/inputs/audio-btn");
154 fgTie("/instrumentation/kr-87/inputs/volume", this,
155 &FGKR_87::get_vol_btn,
156 &FGKR_87::set_vol_btn);
157 fgSetArchivable("/instrumentation/kr-87/inputs/volume");
158 fgTie("/instrumentation/kr-87/inputs/adf-btn", this,
159 &FGKR_87::get_adf_btn,
160 &FGKR_87::set_adf_btn);
161 fgTie("/instrumentation/kr-87/inputs/bfo-btn", this,
162 &FGKR_87::get_bfo_btn,
163 &FGKR_87::set_bfo_btn);
164 fgTie("/instrumentation/kr-87/inputs/frq-btn", this,
165 &FGKR_87::get_frq_btn,
166 &FGKR_87::set_frq_btn);
167 fgTie("/instrumentation/kr-87/inputs/flt-et-btn", this,
168 &FGKR_87::get_flt_et_btn,
169 &FGKR_87::set_flt_et_btn);
170 fgTie("/instrumentation/kr-87/inputs/set-rst-btn", this,
171 &FGKR_87::get_set_rst_btn,
172 &FGKR_87::set_set_rst_btn);
175 fgTie("/instrumentation/kr-87/outputs/selected-khz", this,
176 &FGKR_87::get_freq, &FGKR_87::set_freq);
177 fgSetArchivable("/instrumentation/kr-87/outputs/selected-khz");
178 fgTie("/instrumentation/kr-87/outputs/standby-khz", this,
179 &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
180 fgSetArchivable("/instrumentation/kr-87/outputs/standby-khz");
181 fgTie("/instrumentation/kr-87/outputs/needle-deg", this,
182 &FGKR_87::get_needle_deg);
183 fgTie("/instrumentation/kr-87/outputs/flight-timer", this,
184 &FGKR_87::get_flight_timer);
185 fgTie("/instrumentation/kr-87/outputs/elapsed-timer", this,
186 &FGKR_87::get_elapsed_timer,
187 &FGKR_87::set_elapsed_timer);
190 fgTie("/instrumentation/kr-87/annunciators/ant", this,
191 &FGKR_87::get_ant_ann );
192 fgTie("/instrumentation/kr-87/annunciators/adf", this,
193 &FGKR_87::get_adf_ann );
194 fgTie("/instrumentation/kr-87/annunciators/bfo", this,
195 &FGKR_87::get_bfo_ann );
196 fgTie("/instrumentation/kr-87/annunciators/frq", this,
197 &FGKR_87::get_frq_ann );
198 fgTie("/instrumentation/kr-87/annunciators/flt", this,
199 &FGKR_87::get_flt_ann );
200 fgTie("/instrumentation/kr-87/annunciators/et", this,
201 &FGKR_87::get_et_ann );
205 void FGKR_87::unbind () {
207 fgUntie("/instrumentation/kr-87/internal/valid");
208 fgUntie("/instrumentation/kr-87/internal/inrange");
209 fgUntie("/instrumentation/kr-87/internal/dist");
210 fgUntie("/instrumentation/kr-87/internal/heading");
213 fgUntie("/instrumentation/kr-87/modes/ant");
214 fgUntie("/instrumentation/kr-87/modes/stby");
215 fgUntie("/instrumentation/kr-87/modes/timer");
216 fgUntie("/instrumentation/kr-87/modes/count");
219 fgUntie("/instrumentation/kr-87/inputs/rotation-deg");
220 fgUntie("/instrumentation/kr-87/inputs/power-btn");
221 fgUntie("/instrumentation/kr-87/inputs/volume");
222 fgUntie("/instrumentation/kr-87/inputs/adf-btn");
223 fgUntie("/instrumentation/kr-87/inputs/bfo-btn");
224 fgUntie("/instrumentation/kr-87/inputs/frq-btn");
225 fgUntie("/instrumentation/kr-87/inputs/flt-et-btn");
226 fgUntie("/instrumentation/kr-87/inputs/set-rst-btn");
227 fgUntie("/instrumentation/kr-87/inputs/ident-btn");
230 fgUntie("/instrumentation/kr-87/outputs/selected-khz");
231 fgUntie("/instrumentation/kr-87/outputs/standby-khz");
232 fgUntie("/instrumentation/kr-87/outputs/needle-deg");
233 fgUntie("/instrumentation/kr-87/outputs/flight-timer");
234 fgUntie("/instrumentation/kr-87/outputs/elapsed-timer");
237 fgUntie("/instrumentation/kr-87/annunciators/ant");
238 fgUntie("/instrumentation/kr-87/annunciators/adf");
239 fgUntie("/instrumentation/kr-87/annunciators/bfo");
240 fgUntie("/instrumentation/kr-87/annunciators/frq");
241 fgUntie("/instrumentation/kr-87/annunciators/flt");
242 fgUntie("/instrumentation/kr-87/annunciators/et");
246 // Update the various nav values based on position and valid tuned in navs
247 void FGKR_87::update( double dt_sec ) {
248 SGGeod acft = globals->get_aircraft_position();
254 // On timeout, scan again
255 _time_before_search_sec -= dt_sec;
256 if ( _time_before_search_sec < 0 ) {
260 ////////////////////////////////////////////////////////////////////////
262 ////////////////////////////////////////////////////////////////////////
264 if ( has_power() && serviceable->getBoolValue() ) {
266 if ( adf_btn == 0 ) {
271 // cout << "ant_mode = " << ant_mode << endl;
273 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
277 } else if ( frq_btn ) {
281 last_frq_btn = frq_btn;
283 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
284 if ( stby_mode == 0 ) {
287 timer_mode = !timer_mode;
291 last_flt_et_btn = flt_et_btn;
293 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
297 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
298 // button depressed and was last iteration too
300 // cout << "tmp_timer = " << tmp_timer << endl;
301 if ( tmp_timer > 2.0 ) {
302 // button held depressed for 2 seconds
303 // cout << "entering elapsed count down mode" << endl;
309 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
311 if ( tmp_timer > 2.0 ) {
312 // button held depressed for 2 seconds, don't adjust
314 } else if ( count_mode == 2 ) {
321 last_set_rst_btn = set_rst_btn;
324 flight_timer += dt_sec;
326 if ( set_rst_btn == 0 ) {
327 // only count if set/rst button not depressed
328 if ( count_mode == 0 ) {
329 elapsed_timer += dt_sec;
330 } else if ( count_mode == 1 ) {
331 elapsed_timer -= dt_sec;
332 if ( elapsed_timer < 1.0 ) {
343 frq_ann = !stby_mode;
344 flt_ann = stby_mode && !timer_mode;
345 if ( count_mode < 2 ) {
346 et_ann = stby_mode && timer_mode;
348 et_flash_time += dt_sec;
349 if ( et_ann && et_flash_time > 0.5 ) {
351 et_flash_time -= 0.5;
352 } else if ( !et_ann && et_flash_time > 0.2 ) {
354 et_flash_time -= 0.2;
359 // cout << "adf is valid" << endl;
360 // staightline distance
361 // What a hack, dist is a class local variable
362 dist = sqrt(distSqr(SGVec3d::fromGeod(acft), xyz));
365 geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
368 // cout << " heading = " << heading
369 // << " dist = " << dist << endl;
371 effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
372 if ( dist < effective_range * SG_NM_TO_METER ) {
374 } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
375 inrange = sg_random() <
376 ( 2 * effective_range * SG_NM_TO_METER - dist ) /
377 (effective_range * SG_NM_TO_METER);
382 // cout << "inrange = " << inrange << endl;
385 goal_needle_deg = heading
386 - fgGetDouble("/orientation/heading-deg");
393 goal_needle_deg = 90.0;
397 goal_needle_deg = 0.0;
411 if ( timer_mode == 0 ) {
414 time = elapsed_timer;
416 // cout << time << endl;
417 hours = (int)(time / 3600.0);
418 time -= hours * 3600.00;
419 min = (int)(time / 60.0);
436 char formatted_timer[128];
437 // cout << big << ":" << little << endl;
438 snprintf(formatted_timer, 6, "%02d:%02d", big, little);
439 fgSetString( "/instrumentation/kr-87/outputs/timer-string",
442 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
443 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
445 double diff = goal_needle_deg - needle_deg;
446 while ( diff < -180.0 ) { diff += 360.0; }
447 while ( diff > 180.0 ) { diff -= 360.0; }
449 needle_deg += diff * dt_sec * 4;
450 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
451 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
453 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
455 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
456 // << " needle = " << needle_deg << endl;
458 if ( valid && inrange && serviceable->getBoolValue() ) {
459 // play station ident via audio system if on + ant mode,
460 // otherwise turn it off
461 if ( vol_btn >= 0.01 && audio_btn ) {
462 SGSoundSample *sound;
463 sound = _sgr->find( "adf-ident" );
464 if ( sound != NULL ) {
466 sound->set_volume( vol_btn );
468 sound->set_volume( vol_btn / 4.0 );
471 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
474 globals->get_time_params()->get_cur_time() - 30 ) {
475 last_time = globals->get_time_params()->get_cur_time();
478 if ( play_count < 4 ) {
480 if ( !_sgr->is_playing("adf-ident") && (vol_btn > 0.05) ) {
481 _sgr->play_once( "adf-ident" );
486 _sgr->stop( "adf-ident" );
492 // Update current nav/adf radio stations based on current postition
493 void FGKR_87::search() {
494 SGGeod pos = globals->get_aircraft_position();
496 // FIXME: the panel should handle this
497 static string last_ident = "";
500 _time_before_search_sec = 1.0;
502 ////////////////////////////////////////////////////////////////////////
504 ////////////////////////////////////////////////////////////////////////
507 FGNavRecord *adf = globals->get_navlist()->findByFreq( freq, pos);
510 snprintf( sfreq, 10, "%d", freq );
512 ident += adf->get_ident();
513 // cout << "adf ident = " << ident << endl;
515 if ( last_ident != ident ) {
518 trans_ident = adf->get_trans_ident();
519 stn_lon = adf->get_lon();
520 stn_lat = adf->get_lat();
521 stn_elev = adf->get_elev_ft();
522 range = adf->get_range();
523 effective_range = kludgeRange(stn_elev, pos.getElevationM(), range);
526 if ( _sgr->exists( "adf-ident" ) ) {
527 // stop is required! -- remove alone wouldn't stop immediately
528 _sgr->stop( "adf-ident" );
529 _sgr->remove( "adf-ident" );
531 SGSoundSample *sound;
532 sound = FGMorse::instance()->make_ident( trans_ident, FGMorse::LO_FREQUENCY );
533 sound->set_volume( 0.3 );
534 _sgr->add( sound, "adf-ident" );
536 int offset = (int)(sg_random() * 30.0);
537 play_count = offset / 4;
538 last_time = globals->get_time_params()->get_cur_time() -
540 // cout << "offset = " << offset << " play_count = "
541 // << play_count << " last_time = "
542 // << last_time << " current time = "
543 // << globals->get_time_params()->get_cur_time() << endl;
545 // cout << "Found an adf station in range" << endl;
546 // cout << " id = " << nav->get_ident() << endl;
552 _sgr->remove( "adf-ident" );
554 // cout << "not picking up adf. :-(" << endl;
559 int FGKR_87::get_stby_freq() const {
560 if ( stby_mode == 0 ) {
563 if ( timer_mode == 0 ) {
564 return (int)flight_timer;
566 return (int)elapsed_timer;