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., 675 Mass Ave, Cambridge, MA 02139, 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 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
248 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
249 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
253 Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
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 station = Point3D( x, y, z );
365 dist = aircraft.distance3D( station );
368 geo_inverse_wgs_84( acft_elev,
369 acft_lat * SGD_RADIANS_TO_DEGREES,
370 acft_lon * SGD_RADIANS_TO_DEGREES,
374 // cout << " heading = " << heading
375 // << " dist = " << dist << endl;
377 effective_range = kludgeRange(stn_elev, acft_elev, range);
378 if ( dist < effective_range * SG_NM_TO_METER ) {
380 } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
381 inrange = sg_random() <
382 ( 2 * effective_range * SG_NM_TO_METER - dist ) /
383 (effective_range * SG_NM_TO_METER);
388 // cout << "inrange = " << inrange << endl;
391 goal_needle_deg = heading
392 - fgGetDouble("/orientation/heading-deg");
399 goal_needle_deg = 90.0;
403 goal_needle_deg = 0.0;
417 if ( timer_mode == 0 ) {
420 time = elapsed_timer;
422 // cout << time << endl;
423 hours = (int)(time / 3600.0);
424 time -= hours * 3600.00;
425 min = (int)(time / 60.0);
442 char formatted_timer[128];
443 // cout << big << ":" << little << endl;
444 snprintf(formatted_timer, 6, "%02d:%02d", big, little);
445 fgSetString( "/instrumentation/kr-87/outputs/timer-string",
448 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
449 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
451 double diff = goal_needle_deg - needle_deg;
452 while ( diff < -180.0 ) { diff += 360.0; }
453 while ( diff > 180.0 ) { diff -= 360.0; }
455 needle_deg += diff * dt_sec * 4;
456 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
457 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
459 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
461 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
462 // << " needle = " << needle_deg << endl;
464 if ( valid && inrange && serviceable->getBoolValue() ) {
465 // play station ident via audio system if on + ant mode,
466 // otherwise turn it off
467 if ( vol_btn >= 0.01 && audio_btn ) {
468 SGSoundSample *sound;
469 sound = globals->get_soundmgr()->find( "adf-ident" );
470 if ( sound != NULL ) {
472 sound->set_volume( vol_btn );
474 sound->set_volume( vol_btn / 4.0 );
477 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
480 globals->get_time_params()->get_cur_time() - 30 ) {
481 last_time = globals->get_time_params()->get_cur_time();
484 if ( play_count < 4 ) {
486 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
487 globals->get_soundmgr()->play_once( "adf-ident" );
492 globals->get_soundmgr()->stop( "adf-ident" );
498 // Update current nav/adf radio stations based on current postition
499 void FGKR_87::search() {
500 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
501 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
502 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
504 // FIXME: the panel should handle this
505 static string last_ident = "";
508 _time_before_search_sec = 1.0;
510 ////////////////////////////////////////////////////////////////////////
512 ////////////////////////////////////////////////////////////////////////
515 = globals->get_navlist()->findByFreq( freq, acft_lon, acft_lat,
519 snprintf( sfreq, 10, "%d", freq );
521 ident += adf->get_ident();
522 // cout << "adf ident = " << ident << endl;
524 if ( last_ident != ident ) {
527 trans_ident = adf->get_trans_ident();
528 stn_lon = adf->get_lon();
529 stn_lat = adf->get_lat();
530 stn_elev = adf->get_elev_ft();
531 range = adf->get_range();
532 effective_range = kludgeRange(stn_elev, acft_elev, range);
537 if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
538 globals->get_soundmgr()->remove( "adf-ident" );
540 SGSoundSample *sound;
541 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
542 sound->set_volume( 0.3 );
543 globals->get_soundmgr()->add( sound, "adf-ident" );
545 int offset = (int)(sg_random() * 30.0);
546 play_count = offset / 4;
547 last_time = globals->get_time_params()->get_cur_time() -
549 // cout << "offset = " << offset << " play_count = "
550 // << play_count << " last_time = "
551 // << last_time << " current time = "
552 // << globals->get_time_params()->get_cur_time() << endl;
554 // cout << "Found an adf station in range" << endl;
555 // cout << " id = " << nav->get_ident() << endl;
561 globals->get_soundmgr()->remove( "adf-ident" );
563 // cout << "not picking up adf. :-(" << endl;
568 int FGKR_87::get_stby_freq() const {
569 if ( stby_mode == 0 ) {
572 if ( timer_mode == 0 ) {
573 return (int)flight_timer;
575 return (int)elapsed_timer;