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 - curt@flightgear.org
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>
35 #include <Time/FGEventMgr.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*SG_METER_TO_FEET) - stationElev) / 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;
73 lon_node(fgGetNode("/position/longitude-deg", true)),
74 lat_node(fgGetNode("/position/latitude-deg", true)),
75 alt_node(fgGetNode("/position/altitude-ft", true)),
95 last_flt_et_btn(false),
97 last_set_rst_btn(false),
106 SGPath path( globals->get_fg_root() );
108 term.append( "Navaids/range.term" );
110 low.append( "Navaids/range.low" );
112 high.append( "Navaids/range.high" );
114 term_tbl = new SGInterpTable( term.str() );
115 low_tbl = new SGInterpTable( low.str() );
116 high_tbl = new SGInterpTable( high.str() );
121 FGKR_87::~FGKR_87() {
128 void FGKR_87::init () {
131 update(0); // FIXME: use dt
135 void FGKR_87::bind () {
137 fgTie("/radios/kr-87/internal/valid", this, &FGKR_87::get_valid);
138 fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange);
139 fgTie("/radios/kr-87/internal/dist", this, &FGKR_87::get_dist);
140 fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading);
143 fgTie("/radios/kr-87/modes/ant", this,
144 &FGKR_87::get_ant_mode);
145 fgTie("/radios/kr-87/modes/stby", this,
146 &FGKR_87::get_stby_mode);
147 fgTie("/radios/kr-87/modes/timer", this,
148 &FGKR_87::get_timer_mode);
149 fgTie("/radios/kr-87/modes/count", this,
150 &FGKR_87::get_count_mode);
153 fgTie("/radios/kr-87/inputs/rotation-deg", this,
154 &FGKR_87::get_rotation, &FGKR_87::set_rotation);
155 fgSetArchivable("/radios/kr-87/inputs/rotation-deg");
156 fgTie("/radios/kr-87/inputs/power-btn", this,
157 &FGKR_87::get_power_btn,
158 &FGKR_87::set_power_btn);
159 fgSetArchivable("/radios/kr-87/inputs/power-btn");
160 fgTie("/radios/kr-87/inputs/volume", this,
161 &FGKR_87::get_vol_btn,
162 &FGKR_87::set_vol_btn);
163 fgSetArchivable("/radios/kr-87/inputs/volume");
164 fgTie("/radios/kr-87/inputs/adf-btn", this,
165 &FGKR_87::get_adf_btn,
166 &FGKR_87::set_adf_btn);
167 fgTie("/radios/kr-87/inputs/bfo-btn", this,
168 &FGKR_87::get_bfo_btn,
169 &FGKR_87::set_bfo_btn);
170 fgTie("/radios/kr-87/inputs/frq-btn", this,
171 &FGKR_87::get_frq_btn,
172 &FGKR_87::set_frq_btn);
173 fgTie("/radios/kr-87/inputs/flt-et-btn", this,
174 &FGKR_87::get_flt_et_btn,
175 &FGKR_87::set_flt_et_btn);
176 fgTie("/radios/kr-87/inputs/set-rst-btn", this,
177 &FGKR_87::get_set_rst_btn,
178 &FGKR_87::set_set_rst_btn);
179 fgTie("/radios/kr-87/inputs/ident-btn", this,
180 &FGKR_87::get_ident_btn, &FGKR_87::set_ident_btn);
183 fgTie("/radios/kr-87/outputs/selected-khz", this,
184 &FGKR_87::get_freq, &FGKR_87::set_freq);
185 fgSetArchivable("/radios/kr-87/outputs/selected-khz");
186 fgTie("/radios/kr-87/outputs/standby-khz", this,
187 &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
188 fgSetArchivable("/radios/kr-87/outputs/standby-khz");
189 fgTie("/radios/kr-87/outputs/needle-deg", this,
190 &FGKR_87::get_needle_deg);
191 fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer);
192 fgTie("/radios/kr-87/outputs/elapsed-timer", this,
193 &FGKR_87::get_elapsed_timer,
194 &FGKR_87::set_elapsed_timer);
197 fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann );
198 fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann );
199 fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann );
200 fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann );
201 fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann );
202 fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann );
206 void FGKR_87::unbind () {
208 fgUntie("/radios/kr-87/internal/valid");
209 fgUntie("/radios/kr-87/internal/inrange");
210 fgUntie("/radios/kr-87/internal/dist");
211 fgUntie("/radios/kr-87/internal/heading");
214 fgUntie("/radios/kr-87/modes/ant");
215 fgUntie("/radios/kr-87/modes/stby");
216 fgUntie("/radios/kr-87/modes/timer");
217 fgUntie("/radios/kr-87/modes/count");
220 fgUntie("/radios/kr-87/inputs/rotation-deg");
221 fgUntie("/radios/kr-87/inputs/power-btn");
222 fgUntie("/radios/kr-87/inputs/volume");
223 fgUntie("/radios/kr-87/inputs/adf-btn");
224 fgUntie("/radios/kr-87/inputs/bfo-btn");
225 fgUntie("/radios/kr-87/inputs/frq-btn");
226 fgUntie("/radios/kr-87/inputs/flt-et-btn");
227 fgUntie("/radios/kr-87/inputs/set-rst-btn");
228 fgUntie("/radios/kr-87/inputs/ident-btn");
231 fgUntie("/radios/kr-87/outputs/selected-khz");
232 fgUntie("/radios/kr-87/outputs/standby-khz");
233 fgUntie("/radios/kr-87/outputs/needle-deg");
234 fgUntie("/radios/kr-87/outputs/flight-timer");
235 fgUntie("/radios/kr-87/outputs/elapsed-timer");
238 fgUntie("/radios/kr-87/annunciators/ant");
239 fgUntie("/radios/kr-87/annunciators/adf");
240 fgUntie("/radios/kr-87/annunciators/bfo");
241 fgUntie("/radios/kr-87/annunciators/frq");
242 fgUntie("/radios/kr-87/annunciators/flt");
243 fgUntie("/radios/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 ) {
249 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
250 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
251 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
255 Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
259 ////////////////////////////////////////////////////////////////////////
261 ////////////////////////////////////////////////////////////////////////
265 if ( adf_btn == 0 ) {
271 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
275 } else if ( frq_btn ) {
279 last_frq_btn = frq_btn;
281 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
282 if ( stby_mode == 0 ) {
285 timer_mode = !timer_mode;
289 last_flt_et_btn = flt_et_btn;
291 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
295 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
296 // button depressed and was last iteration too
298 cout << "tmp_timer = " << tmp_timer << endl;
299 if ( tmp_timer > 2.0 ) {
300 // button held depressed for 2 seconds
301 cout << "entering elapsed count down mode" << endl;
307 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
309 if ( tmp_timer > 2.0 ) {
310 // button held depressed for 2 seconds, don't adjust
312 } else if ( count_mode == 2 ) {
319 last_set_rst_btn = set_rst_btn;
324 if ( set_rst_btn == 0 ) {
325 // only count if set/rst button not depressed
326 if ( count_mode == 0 ) {
328 } else if ( count_mode == 1 ) {
330 if ( elapsed_timer < 1.0 ) {
341 frq_ann = !stby_mode;
342 flt_ann = stby_mode && !timer_mode;
343 if ( count_mode < 2 ) {
344 et_ann = stby_mode && timer_mode;
347 if ( et_ann && et_flash_time > 0.5 ) {
349 et_flash_time -= 0.5;
350 } else if ( !et_ann && et_flash_time > 0.2 ) {
352 et_flash_time -= 0.2;
357 // staightline distance
358 station = Point3D( x, y, z );
359 dist = aircraft.distance3D( station );
362 geo_inverse_wgs_84( acft_elev,
363 acft_lat * SGD_RADIANS_TO_DEGREES,
364 acft_lon * SGD_RADIANS_TO_DEGREES,
368 // cout << " heading = " << heading
369 // << " dist = " << dist << endl;
371 effective_range = kludgeRange(stn_elev, acft_elev, 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);
383 goal_needle_deg = heading
384 - fgGetDouble("/orientation/heading-deg");
391 goal_needle_deg = 90.0;
395 goal_needle_deg = 0.0;
409 if ( timer_mode == 0 ) {
412 time = elapsed_timer;
414 // cout << time << endl;
415 hours = (int)(time / 3600.0);
416 time -= hours * 3600.00;
417 min = (int)(time / 60.0);
434 char formatted_timer[128];
435 // cout << big << ":" << little << endl;
436 snprintf(formatted_timer, 6, "%02d:%02d", big, little);
437 fgSetString( "/radios/kr-87/outputs/timer-string", formatted_timer );
439 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
440 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
442 double diff = goal_needle_deg - needle_deg;
443 while ( diff < -180.0 ) { diff += 360.0; }
444 while ( diff > 180.0 ) { diff -= 360.0; }
446 needle_deg += diff * dt * 4;
447 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
448 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
450 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
452 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
453 // << " needle = " << needle_deg << endl;
455 #ifdef ENABLE_AUDIO_SUPPORT
456 if ( valid && inrange ) {
457 // play station ident via audio system if on + ident_btn,
458 // otherwise turn it off
459 if ( vol_btn >= 0.01 && ident_btn ) {
460 FGSimpleSound *sound;
461 sound = globals->get_soundmgr()->find( "adf-ident" );
462 if ( sound != NULL ) {
463 sound->set_volume( vol_btn );
465 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
468 globals->get_time_params()->get_cur_time() - 30 ) {
469 last_time = globals->get_time_params()->get_cur_time();
472 if ( play_count < 4 ) {
474 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
475 globals->get_soundmgr()->play_once( "adf-ident" );
480 globals->get_soundmgr()->stop( "adf-ident" );
487 // Update current nav/adf radio stations based on current postition
488 void FGKR_87::search() {
489 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
490 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
491 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
493 // FIXME: the panel should handle this
496 static string last_ident = "";
498 ////////////////////////////////////////////////////////////////////////
500 ////////////////////////////////////////////////////////////////////////
502 if ( current_navlist->query( acft_lon, acft_lat, acft_elev, freq, &nav ) ) {
504 snprintf( sfreq, 10, "%.0f", freq );
506 ident += nav.get_ident();
507 // cout << "adf ident = " << ident << endl;
509 if ( last_ident != ident ) {
512 trans_ident = nav.get_trans_ident();
513 stn_lon = nav.get_lon();
514 stn_lat = nav.get_lat();
515 stn_elev = nav.get_elev();
516 range = nav.get_range();
517 effective_range = kludgeRange(stn_elev, acft_elev, range);
522 #ifdef ENABLE_AUDIO_SUPPORT
523 if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
524 globals->get_soundmgr()->remove( "adf-ident" );
526 FGSimpleSound *sound;
527 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
528 sound->set_volume( 0.3 );
529 globals->get_soundmgr()->add( sound, "adf-ident" );
531 int offset = (int)(sg_random() * 30.0);
532 play_count = offset / 4;
533 last_time = globals->get_time_params()->get_cur_time() -
535 // cout << "offset = " << offset << " play_count = "
536 // << play_count << " last_time = "
537 // << last_time << " current time = "
538 // << globals->get_time_params()->get_cur_time() << endl;
541 // cout << "Found an adf station in range" << endl;
542 // cout << " id = " << nav.get_ident() << endl;
548 #ifdef ENABLE_AUDIO_SUPPORT
549 globals->get_soundmgr()->remove( "adf-ident" );
552 // cout << "not picking up adf. :-(" << endl;
557 double FGKR_87::get_stby_freq() const {
558 if ( stby_mode == 0 ) {
561 if ( timer_mode == 0 ) {
564 return elapsed_timer;