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>
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/navlist.hxx>
41 static int play_count = 0;
42 static time_t last_time = 0;
46 * Boy, this is ugly! Make the VOR range vary by altitude difference.
48 static double kludgeRange ( double stationElev, double aircraftElev,
51 // Assume that the nominal range (usually
52 // 50nm) applies at a 5,000 ft difference.
54 double factor = (aircraftElev - stationElev)*SG_METER_TO_FEET / 5000.0;
55 double range = fabs(nominalRange * factor);
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;
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)),
97 last_flt_et_btn(false),
99 last_set_rst_btn(false),
106 _time_before_search_sec(0)
112 FGKR_87::~FGKR_87() {
116 void FGKR_87::init () {
121 void FGKR_87::bind () {
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,
128 fgTie("/instrumentation/kr-87/internal/heading", this,
129 &FGKR_87::get_heading);
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);
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);
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);
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 );
204 void FGKR_87::unbind () {
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");
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");
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");
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");
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");
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());
255 // On timeout, scan again
256 _time_before_search_sec -= dt_sec;
257 if ( _time_before_search_sec < 0 ) {
261 ////////////////////////////////////////////////////////////////////////
263 ////////////////////////////////////////////////////////////////////////
265 if ( has_power() && serviceable->getBoolValue() ) {
267 if ( adf_btn == 0 ) {
272 // cout << "ant_mode = " << ant_mode << endl;
274 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
278 } else if ( frq_btn ) {
282 last_frq_btn = frq_btn;
284 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
285 if ( stby_mode == 0 ) {
288 timer_mode = !timer_mode;
292 last_flt_et_btn = flt_et_btn;
294 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
298 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
299 // button depressed and was last iteration too
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;
310 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
312 if ( tmp_timer > 2.0 ) {
313 // button held depressed for 2 seconds, don't adjust
315 } else if ( count_mode == 2 ) {
322 last_set_rst_btn = set_rst_btn;
325 flight_timer += dt_sec;
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 ) {
344 frq_ann = !stby_mode;
345 flt_ann = stby_mode && !timer_mode;
346 if ( count_mode < 2 ) {
347 et_ann = stby_mode && timer_mode;
349 et_flash_time += dt_sec;
350 if ( et_ann && et_flash_time > 0.5 ) {
352 et_flash_time -= 0.5;
353 } else if ( !et_ann && et_flash_time > 0.2 ) {
355 et_flash_time -= 0.2;
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));
366 geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
369 // cout << " heading = " << heading
370 // << " dist = " << dist << endl;
372 effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
373 if ( dist < effective_range * SG_NM_TO_METER ) {
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);
383 // cout << "inrange = " << inrange << endl;
386 goal_needle_deg = heading
387 - fgGetDouble("/orientation/heading-deg");
394 goal_needle_deg = 90.0;
398 goal_needle_deg = 0.0;
412 if ( timer_mode == 0 ) {
415 time = elapsed_timer;
417 // cout << time << endl;
418 hours = (int)(time / 3600.0);
419 time -= hours * 3600.00;
420 min = (int)(time / 60.0);
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",
443 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
444 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
446 double diff = goal_needle_deg - needle_deg;
447 while ( diff < -180.0 ) { diff += 360.0; }
448 while ( diff > 180.0 ) { diff -= 360.0; }
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; }
454 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
456 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
457 // << " needle = " << needle_deg << endl;
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 ) {
467 sound->set_volume( vol_btn );
469 sound->set_volume( vol_btn / 4.0 );
472 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
475 globals->get_time_params()->get_cur_time() - 30 ) {
476 last_time = globals->get_time_params()->get_cur_time();
479 if ( play_count < 4 ) {
481 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
482 globals->get_soundmgr()->play_once( "adf-ident" );
487 globals->get_soundmgr()->stop( "adf-ident" );
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;
499 // FIXME: the panel should handle this
500 static string last_ident = "";
503 _time_before_search_sec = 1.0;
505 ////////////////////////////////////////////////////////////////////////
507 ////////////////////////////////////////////////////////////////////////
510 = globals->get_navlist()->findByFreq( freq, acft_lon, acft_lat,
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, acft_elev, range);
528 xyz = adf->get_cart();
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;