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)),
92 last_flt_et_btn(false),
94 last_set_rst_btn(false),
103 SGPath path( globals->get_fg_root() );
105 term.append( "Navaids/range.term" );
107 low.append( "Navaids/range.low" );
109 high.append( "Navaids/range.high" );
111 term_tbl = new SGInterpTable( term.str() );
112 low_tbl = new SGInterpTable( low.str() );
113 high_tbl = new SGInterpTable( high.str() );
118 FGKR_87::~FGKR_87() {
125 void FGKR_87::init () {
128 update(0); // FIXME: use dt
132 void FGKR_87::bind () {
134 fgTie("/radios/kr-87/internal/valid", this, &FGKR_87::get_valid);
135 fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange);
136 fgTie("/radios/kr-87/internal/dist", this, &FGKR_87::get_dist);
137 fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading);
140 fgTie("/radios/kr-87/modes/ant", this,
141 &FGKR_87::get_ant_mode);
142 fgTie("/radios/kr-87/modes/stby", this,
143 &FGKR_87::get_stby_mode);
144 fgTie("/radios/kr-87/modes/timer", this,
145 &FGKR_87::get_timer_mode);
146 fgTie("/radios/kr-87/modes/count", this,
147 &FGKR_87::get_count_mode);
150 fgTie("/radios/kr-87/inputs/rotation-deg", this,
151 &FGKR_87::get_rotation, &FGKR_87::set_rotation);
152 fgSetArchivable("/radios/kr-87/inputs/rotation-deg");
153 fgTie("/radios/kr-87/inputs/on-off-volume", this,
154 &FGKR_87::get_on_off_vol_btn,
155 &FGKR_87::set_on_off_vol_btn);
156 fgSetArchivable("/radios/kr-87/inputs/on-off-volume");
157 fgTie("/radios/kr-87/inputs/adf-btn", this,
158 &FGKR_87::get_adf_btn,
159 &FGKR_87::set_adf_btn);
160 fgTie("/radios/kr-87/inputs/bfo-btn", this,
161 &FGKR_87::get_bfo_btn,
162 &FGKR_87::set_bfo_btn);
163 fgTie("/radios/kr-87/inputs/frq-btn", this,
164 &FGKR_87::get_frq_btn,
165 &FGKR_87::set_frq_btn);
166 fgTie("/radios/kr-87/inputs/flt-et-btn", this,
167 &FGKR_87::get_flt_et_btn,
168 &FGKR_87::set_flt_et_btn);
169 fgTie("/radios/kr-87/inputs/set-rst-btn", this,
170 &FGKR_87::get_set_rst_btn,
171 &FGKR_87::set_set_rst_btn);
172 fgTie("/radios/kr-87/inputs/ident-btn", this,
173 &FGKR_87::get_ident_btn, &FGKR_87::set_ident_btn);
176 fgTie("/radios/kr-87/outputs/selected-khz", this,
177 &FGKR_87::get_freq, &FGKR_87::set_freq);
178 fgSetArchivable("/radios/kr-87/outputs/selected-khz");
179 fgTie("/radios/kr-87/outputs/standby-khz", this,
180 &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
181 fgSetArchivable("/radios/kr-87/outputs/standby-khz");
182 fgTie("/radios/kr-87/outputs/needle-deg", this,
183 &FGKR_87::get_needle_deg);
184 fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer);
185 fgTie("/radios/kr-87/outputs/elapsed-timer", this,
186 &FGKR_87::get_elapsed_timer,
187 &FGKR_87::set_elapsed_timer);
190 fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann );
191 fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann );
192 fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann );
193 fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann );
194 fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann );
195 fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann );
199 void FGKR_87::unbind () {
201 fgUntie("/radios/kr-87/internal/valid");
202 fgUntie("/radios/kr-87/internal/inrange");
203 fgUntie("/radios/kr-87/internal/dist");
204 fgUntie("/radios/kr-87/internal/heading");
207 fgUntie("/radios/kr-87/modes/ant");
208 fgUntie("/radios/kr-87/modes/stby");
209 fgUntie("/radios/kr-87/modes/timer");
210 fgUntie("/radios/kr-87/modes/count");
213 fgUntie("/radios/kr-87/inputs/rotation-deg");
214 fgUntie("/radios/kr-87/inputs/on-off-volume");
215 fgUntie("/radios/kr-87/inputs/adf-btn");
216 fgUntie("/radios/kr-87/inputs/bfo-btn");
217 fgUntie("/radios/kr-87/inputs/frq-btn");
218 fgUntie("/radios/kr-87/inputs/flt-et-btn");
219 fgUntie("/radios/kr-87/inputs/set-rst-btn");
220 fgUntie("/radios/kr-87/inputs/ident-btn");
223 fgUntie("/radios/kr-87/outputs/selected-khz");
224 fgUntie("/radios/kr-87/outputs/standby-khz");
225 fgUntie("/radios/kr-87/outputs/needle-deg");
226 fgUntie("/radios/kr-87/outputs/flight-timer");
227 fgUntie("/radios/kr-87/outputs/elapsed-timer");
230 fgUntie("/radios/kr-87/annunciators/ant");
231 fgUntie("/radios/kr-87/annunciators/adf");
232 fgUntie("/radios/kr-87/annunciators/bfo");
233 fgUntie("/radios/kr-87/annunciators/frq");
234 fgUntie("/radios/kr-87/annunciators/flt");
235 fgUntie("/radios/kr-87/annunciators/et");
239 // Update the various nav values based on position and valid tuned in navs
240 void FGKR_87::update( double dt ) {
241 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
242 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
243 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
247 Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
251 ////////////////////////////////////////////////////////////////////////
253 ////////////////////////////////////////////////////////////////////////
255 if ( on_off_vol_btn >= 0.01 ) {
257 if ( adf_btn == 0 ) {
263 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
267 } else if ( frq_btn ) {
271 last_frq_btn = frq_btn;
273 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
274 if ( stby_mode == 0 ) {
277 timer_mode = !timer_mode;
281 last_flt_et_btn = flt_et_btn;
283 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
287 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
288 // button depressed and was last iteration too
290 cout << "tmp_timer = " << tmp_timer << endl;
291 if ( tmp_timer > 2.0 ) {
292 // button held depressed for 2 seconds
293 cout << "entering elapsed count down mode" << endl;
299 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
301 if ( tmp_timer > 2.0 ) {
302 // button held depressed for 2 seconds, don't adjust
304 } else if ( count_mode == 2 ) {
311 last_set_rst_btn = set_rst_btn;
316 if ( set_rst_btn == 0 ) {
317 // only count if set/rst button not depressed
318 if ( count_mode == 0 ) {
320 } else if ( count_mode == 1 ) {
322 if ( elapsed_timer < 1.0 ) {
333 frq_ann = !stby_mode;
334 flt_ann = stby_mode && !timer_mode;
335 if ( count_mode < 2 ) {
336 et_ann = stby_mode && timer_mode;
339 if ( et_ann && et_flash_time > 0.5 ) {
341 et_flash_time -= 0.5;
342 } else if ( !et_ann && et_flash_time > 0.2 ) {
344 et_flash_time -= 0.2;
349 // staightline distance
350 station = Point3D( x, y, z );
351 dist = aircraft.distance3D( station );
354 geo_inverse_wgs_84( acft_elev,
355 acft_lat * SGD_RADIANS_TO_DEGREES,
356 acft_lon * SGD_RADIANS_TO_DEGREES,
360 // cout << " heading = " << heading
361 // << " dist = " << dist << endl;
363 effective_range = kludgeRange(stn_elev, acft_elev, range);
364 if ( dist < effective_range * SG_NM_TO_METER ) {
366 } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
367 inrange = sg_random() <
368 ( 2 * effective_range * SG_NM_TO_METER - dist ) /
369 (effective_range * SG_NM_TO_METER);
375 goal_needle_deg = heading
376 - fgGetDouble("/orientation/heading-deg");
383 goal_needle_deg = 90.0;
387 goal_needle_deg = 0.0;
399 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
400 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
402 double diff = goal_needle_deg - needle_deg;
403 while ( diff < -180.0 ) { diff += 360.0; }
404 while ( diff > 180.0 ) { diff -= 360.0; }
406 needle_deg += diff * dt * 4;
407 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
408 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
410 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
412 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
413 // << " needle = " << needle_deg << endl;
415 #ifdef ENABLE_AUDIO_SUPPORT
416 if ( valid && inrange ) {
417 // play station ident via audio system if on + ident_btn,
418 // otherwise turn it off
419 if ( on_off_vol_btn >= 0.01 && ident_btn ) {
420 FGSimpleSound *sound;
421 sound = globals->get_soundmgr()->find( "adf-ident" );
422 if ( sound != NULL ) {
423 sound->set_volume( on_off_vol_btn );
425 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
428 globals->get_time_params()->get_cur_time() - 30 ) {
429 last_time = globals->get_time_params()->get_cur_time();
432 if ( play_count < 4 ) {
434 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
435 globals->get_soundmgr()->play_once( "adf-ident" );
440 globals->get_soundmgr()->stop( "adf-ident" );
447 // Update current nav/adf radio stations based on current postition
448 void FGKR_87::search() {
449 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
450 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
451 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
453 // FIXME: the panel should handle this
456 static string last_ident = "";
458 ////////////////////////////////////////////////////////////////////////
460 ////////////////////////////////////////////////////////////////////////
462 if ( current_navlist->query( acft_lon, acft_lat, acft_elev, freq, &nav ) ) {
464 snprintf( sfreq, 10, "%.0f", freq );
466 ident += nav.get_ident();
467 // cout << "adf ident = " << ident << endl;
469 if ( last_ident != ident ) {
472 trans_ident = nav.get_trans_ident();
473 stn_lon = nav.get_lon();
474 stn_lat = nav.get_lat();
475 stn_elev = nav.get_elev();
476 range = nav.get_range();
477 effective_range = kludgeRange(stn_elev, acft_elev, range);
482 #ifdef ENABLE_AUDIO_SUPPORT
483 if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
484 globals->get_soundmgr()->remove( "adf-ident" );
486 FGSimpleSound *sound;
487 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
488 sound->set_volume( 0.3 );
489 globals->get_soundmgr()->add( sound, "adf-ident" );
491 int offset = (int)(sg_random() * 30.0);
492 play_count = offset / 4;
493 last_time = globals->get_time_params()->get_cur_time() -
495 // cout << "offset = " << offset << " play_count = "
496 // << play_count << " last_time = "
497 // << last_time << " current time = "
498 // << globals->get_time_params()->get_cur_time() << endl;
501 // cout << "Found an adf station in range" << endl;
502 // cout << " id = " << nav.get_ident() << endl;
508 #ifdef ENABLE_AUDIO_SUPPORT
509 globals->get_soundmgr()->remove( "adf-ident" );
512 // cout << "not picking up adf. :-(" << endl;
517 double FGKR_87::get_stby_freq() const {
518 if ( stby_mode == 0 ) {
521 if ( timer_mode == 0 ) {
524 return elapsed_timer;