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),
102 SGPath path( globals->get_fg_root() );
104 term.append( "Navaids/range.term" );
106 low.append( "Navaids/range.low" );
108 high.append( "Navaids/range.high" );
110 term_tbl = new SGInterpTable( term.str() );
111 low_tbl = new SGInterpTable( low.str() );
112 high_tbl = new SGInterpTable( high.str() );
130 update(0); // FIXME: use dt
137 fgTie("/radios/kr-87/internal/ident", this,
138 &FGKR_87::get_ident_btn,
139 &FGKR_87::set_ident_btn);
140 fgSetArchivable("/radios/kr-87/internal/ident");
141 fgTie("/radios/kr-87/internal/inrange", this, &FGKR_87::get_inrange);
142 fgTie("/radios/kr-87/internal/heading", this, &FGKR_87::get_heading);
145 fgTie("/radios/kr-87/modes/ant", this,
146 &FGKR_87::get_ant_mode);
147 fgTie("/radios/kr-87/modes/stby", this,
148 &FGKR_87::get_stby_mode);
149 fgTie("/radios/kr-87/modes/timer", this,
150 &FGKR_87::get_timer_mode);
151 fgTie("/radios/kr-87/modes/count", this,
152 &FGKR_87::get_count_mode);
155 fgTie("/radios/kr-87/inputs/rotation-deg", this,
156 &FGKR_87::get_rotation, &FGKR_87::set_rotation);
157 fgSetArchivable("/radios/kr-87/inputs/rotation-deg");
158 fgTie("/radios/kr-87/inputs/on-off-volume", this,
159 &FGKR_87::get_on_off_vol_btn,
160 &FGKR_87::set_on_off_vol_btn);
161 fgSetArchivable("/radios/kr-87/inputs/on-off-volume");
162 fgTie("/radios/kr-87/inputs/adf-btn", this,
163 &FGKR_87::get_adf_btn,
164 &FGKR_87::set_adf_btn);
165 fgTie("/radios/kr-87/inputs/bfo-btn", this,
166 &FGKR_87::get_bfo_btn,
167 &FGKR_87::set_bfo_btn);
168 fgTie("/radios/kr-87/inputs/frq-btn", this,
169 &FGKR_87::get_frq_btn,
170 &FGKR_87::set_frq_btn);
171 fgTie("/radios/kr-87/inputs/flt-et-btn", this,
172 &FGKR_87::get_flt_et_btn,
173 &FGKR_87::set_flt_et_btn);
174 fgTie("/radios/kr-87/inputs/set-rst-btn", this,
175 &FGKR_87::get_set_rst_btn,
176 &FGKR_87::set_set_rst_btn);
179 fgTie("/radios/kr-87/outputs/selected-khz", this,
180 &FGKR_87::get_freq, &FGKR_87::set_freq);
181 fgSetArchivable("/radios/kr-87/outputs/selected-khz");
182 fgTie("/radios/kr-87/outputs/standby-khz", this,
183 &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
184 fgSetArchivable("/radios/kr-87/outputs/standby-khz");
185 fgTie("/radios/kr-87/outputs/needle-deg", this,
186 &FGKR_87::get_needle_deg);
187 fgTie("/radios/kr-87/outputs/flight-timer", this, &FGKR_87::get_flight_timer);
188 fgTie("/radios/kr-87/outputs/elapsed-timer", this,
189 &FGKR_87::get_elapsed_timer,
190 &FGKR_87::set_elapsed_timer);
193 fgTie("/radios/kr-87/annunciators/ant", this, &FGKR_87::get_ant_ann );
194 fgTie("/radios/kr-87/annunciators/adf", this, &FGKR_87::get_adf_ann );
195 fgTie("/radios/kr-87/annunciators/bfo", this, &FGKR_87::get_bfo_ann );
196 fgTie("/radios/kr-87/annunciators/frq", this, &FGKR_87::get_frq_ann );
197 fgTie("/radios/kr-87/annunciators/flt", this, &FGKR_87::get_flt_ann );
198 fgTie("/radios/kr-87/annunciators/et", this, &FGKR_87::get_et_ann );
205 fgUntie("/radios/kr-87/internal/ident");
206 fgUntie("/radios/kr-87/internal/inrange");
207 fgUntie("/radios/kr-87/internal/heading");
210 fgUntie("/radios/kr-87/modes/ant");
211 fgUntie("/radios/kr-87/modes/stby");
212 fgUntie("/radios/kr-87/modes/timer");
213 fgUntie("/radios/kr-87/modes/count");
216 fgUntie("/radios/kr-87/inputs/rotation-deg");
217 fgUntie("/radios/kr-87/inputs/on-off-volume");
218 fgUntie("/radios/kr-87/inputs/adf-btn");
219 fgUntie("/radios/kr-87/inputs/bfo-btn");
220 fgUntie("/radios/kr-87/inputs/frq-btn");
221 fgUntie("/radios/kr-87/inputs/flt-et-btn");
222 fgUntie("/radios/kr-87/inputs/set-rst-btn");
225 fgUntie("/radios/kr-87/outputs/selected-khz");
226 fgUntie("/radios/kr-87/outputs/standby-khz");
227 fgUntie("/radios/kr-87/outputs/needle-deg");
228 fgUntie("/radios/kr-87/outputs/flight-timer");
229 fgUntie("/radios/kr-87/outputs/elapsed-timer");
232 fgUntie("/radios/kr-87/annunciators/ant");
233 fgUntie("/radios/kr-87/annunciators/adf");
234 fgUntie("/radios/kr-87/annunciators/bfo");
235 fgUntie("/radios/kr-87/annunciators/frq");
236 fgUntie("/radios/kr-87/annunciators/flt");
237 fgUntie("/radios/kr-87/annunciators/et");
241 // Update the various nav values based on position and valid tuned in navs
243 FGKR_87::update( double dt )
245 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
246 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
247 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
251 Point3D aircraft = sgGeodToCart( Point3D( acft_lon, acft_lat, acft_elev ) );
255 ////////////////////////////////////////////////////////////////////////
257 ////////////////////////////////////////////////////////////////////////
259 if ( on_off_vol_btn >= 0.01 ) {
261 if ( adf_btn == 0 ) {
267 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
271 } else if ( frq_btn ) {
275 last_frq_btn = frq_btn;
277 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
278 if ( stby_mode == 0 ) {
281 timer_mode = !timer_mode;
285 last_flt_et_btn = flt_et_btn;
287 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
291 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
292 // button depressed and was last iteration too
294 cout << "tmp_timer = " << tmp_timer << endl;
295 if ( tmp_timer > 2.0 ) {
296 // button held depressed for 2 seconds
297 cout << "entering elapsed count down mode" << endl;
303 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
305 if ( tmp_timer > 2.0 ) {
306 // button held depressed for 2 seconds, don't adjust
308 } else if ( count_mode == 2 ) {
315 last_set_rst_btn = set_rst_btn;
320 if ( set_rst_btn == 0 ) {
321 // only count if set/rst button not depressed
322 if ( count_mode == 0 ) {
324 } else if ( count_mode == 1 ) {
326 if ( elapsed_timer < 1.0 ) {
337 frq_ann = !stby_mode;
338 flt_ann = stby_mode && !timer_mode;
339 if ( count_mode < 2 ) {
340 et_ann = stby_mode && timer_mode;
343 if ( et_ann && et_flash_time > 0.5 ) {
345 et_flash_time -= 0.5;
346 } else if ( !et_ann && et_flash_time > 0.2 ) {
348 et_flash_time -= 0.2;
353 // staightline distance
354 station = Point3D( x, y, z );
355 dist = aircraft.distance3D( station );
358 geo_inverse_wgs_84( acft_elev,
359 acft_lat * SGD_RADIANS_TO_DEGREES,
360 acft_lon * SGD_RADIANS_TO_DEGREES,
364 // cout << " heading = " << heading
365 // << " dist = " << dist << endl;
367 effective_range = kludgeRange(stn_elev, acft_elev, range);
368 if ( dist < effective_range * SG_NM_TO_METER ) {
370 } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
371 inrange = sg_random() <
372 ( 2 * effective_range * SG_NM_TO_METER - dist ) /
373 (effective_range * SG_NM_TO_METER);
379 goal_needle_deg = heading
380 - fgGetDouble("/orientation/heading-deg");
387 goal_needle_deg = 90.0;
391 goal_needle_deg = 0.0;
403 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
404 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
406 double diff = goal_needle_deg - needle_deg;
407 while ( diff < -180.0 ) { diff += 360.0; }
408 while ( diff > 180.0 ) { diff -= 360.0; }
410 needle_deg += diff * dt * 4;
411 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
412 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
414 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
416 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
417 // << " needle = " << needle_deg << endl;
419 #ifdef ENABLE_AUDIO_SUPPORT
420 if ( valid && inrange ) {
421 // play station ident via audio system if on + ident,
422 // otherwise turn it off
423 if ( on_off_vol_btn >= 0.01 && ident_btn ) {
424 FGSimpleSound *sound;
425 sound = globals->get_soundmgr()->find( "adf-ident" );
426 if ( sound != NULL ) {
427 sound->set_volume( on_off_vol_btn );
429 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
432 globals->get_time_params()->get_cur_time() - 30 ) {
433 last_time = globals->get_time_params()->get_cur_time();
436 if ( play_count < 4 ) {
438 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
439 globals->get_soundmgr()->play_once( "adf-ident" );
444 globals->get_soundmgr()->stop( "adf-ident" );
451 // Update current nav/adf radio stations based on current postition
452 void FGKR_87::search()
454 double acft_lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
455 double acft_lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
456 double acft_elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
458 // FIXME: the panel should handle this
461 static string last_ident = "";
463 ////////////////////////////////////////////////////////////////////////
465 ////////////////////////////////////////////////////////////////////////
467 if ( current_navlist->query( acft_lon, acft_lat, acft_elev, freq, &nav ) ) {
469 snprintf( sfreq, 10, "%.0f", freq );
471 ident += nav.get_ident();
472 // cout << "adf ident = " << ident << endl;
474 if ( last_ident != ident ) {
477 trans_ident = nav.get_trans_ident();
478 stn_lon = nav.get_lon();
479 stn_lat = nav.get_lat();
480 stn_elev = nav.get_elev();
481 range = nav.get_range();
482 effective_range = kludgeRange(stn_elev, acft_elev, range);
487 #ifdef ENABLE_AUDIO_SUPPORT
488 if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
489 globals->get_soundmgr()->remove( "adf-ident" );
491 FGSimpleSound *sound;
492 sound = morse.make_ident( trans_ident, LO_FREQUENCY );
493 sound->set_volume( 0.3 );
494 globals->get_soundmgr()->add( sound, "adf-ident" );
496 int offset = (int)(sg_random() * 30.0);
497 play_count = offset / 4;
498 last_time = globals->get_time_params()->get_cur_time() -
500 // cout << "offset = " << offset << " play_count = "
501 // << play_count << " last_time = "
502 // << last_time << " current time = "
503 // << globals->get_time_params()->get_cur_time() << endl;
506 // cout << "Found an adf station in range" << endl;
507 // cout << " id = " << nav.get_ident() << endl;
513 #ifdef ENABLE_AUDIO_SUPPORT
514 globals->get_soundmgr()->remove( "adf-ident" );
517 // cout << "not picking up adf. :-(" << endl;
522 double FGKR_87::get_stby_freq() const {
523 if ( stby_mode == 0 ) {
526 if ( timer_mode == 0 ) {
529 return elapsed_timer;