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.
27 #include <stdio.h> // snprintf
29 #include <simgear/compiler.h>
30 #include <simgear/math/sg_random.h>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/timing/sg_time.hxx>
34 #include <Navaids/navlist.hxx>
38 #include <Sound/morse.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 - stationElev)*SG_METER_TO_FEET / 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;
72 FGKR_87::FGKR_87( SGPropertyNode *node ) :
73 bus_power(fgGetNode("/systems/electrical/outputs/adf", true)),
74 serviceable(fgGetNode("/instrumentation/adf/serviceable", true)),
95 last_flt_et_btn(false),
97 last_set_rst_btn(false),
104 _time_before_search_sec(0),
111 FGKR_87::~FGKR_87() {
115 void FGKR_87::init () {
116 SGSoundMgr *smgr = globals->get_soundmgr();
117 _sgr = smgr->find("avionics", true);
118 _sgr->tie_to_listener();
122 void FGKR_87::bind () {
123 _tiedProperties.setRoot(fgGetNode("/instrumentation/kr-87", true));
125 _tiedProperties.Tie("internal/valid", this, &FGKR_87::get_valid);
126 _tiedProperties.Tie("internal/inrange", this,
127 &FGKR_87::get_inrange);
128 _tiedProperties.Tie("internal/dist", this,
130 _tiedProperties.Tie("internal/heading", this,
131 &FGKR_87::get_heading);
134 _tiedProperties.Tie("modes/ant", this,
135 &FGKR_87::get_ant_mode);
136 _tiedProperties.Tie("modes/stby", this,
137 &FGKR_87::get_stby_mode);
138 _tiedProperties.Tie("modes/timer", this,
139 &FGKR_87::get_timer_mode);
140 _tiedProperties.Tie("modes/count", this,
141 &FGKR_87::get_count_mode);
144 _tiedProperties.Tie("inputs/rotation-deg", this,
145 &FGKR_87::get_rotation, &FGKR_87::set_rotation);
146 fgSetArchivable("/instrumentation/kr-87/inputs/rotation-deg");
147 _tiedProperties.Tie("inputs/power-btn", this,
148 &FGKR_87::get_power_btn,
149 &FGKR_87::set_power_btn);
150 fgSetArchivable("/instrumentation/kr-87/inputs/power-btn");
151 _tiedProperties.Tie("inputs/audio-btn", this,
152 &FGKR_87::get_audio_btn,
153 &FGKR_87::set_audio_btn);
154 fgSetArchivable("/instrumentation/kr-87/inputs/audio-btn");
155 _tiedProperties.Tie("inputs/volume", this,
156 &FGKR_87::get_vol_btn,
157 &FGKR_87::set_vol_btn);
158 fgSetArchivable("/instrumentation/kr-87/inputs/volume");
159 _tiedProperties.Tie("inputs/adf-btn", this,
160 &FGKR_87::get_adf_btn,
161 &FGKR_87::set_adf_btn);
162 _tiedProperties.Tie("inputs/bfo-btn", this,
163 &FGKR_87::get_bfo_btn,
164 &FGKR_87::set_bfo_btn);
165 _tiedProperties.Tie("inputs/frq-btn", this,
166 &FGKR_87::get_frq_btn,
167 &FGKR_87::set_frq_btn);
168 _tiedProperties.Tie("inputs/flt-et-btn", this,
169 &FGKR_87::get_flt_et_btn,
170 &FGKR_87::set_flt_et_btn);
171 _tiedProperties.Tie("inputs/set-rst-btn", this,
172 &FGKR_87::get_set_rst_btn,
173 &FGKR_87::set_set_rst_btn);
176 _tiedProperties.Tie("outputs/selected-khz", this,
177 &FGKR_87::get_freq, &FGKR_87::set_freq);
178 fgSetArchivable("/instrumentation/kr-87/outputs/selected-khz");
179 _tiedProperties.Tie("outputs/standby-khz", this,
180 &FGKR_87::get_stby_freq, &FGKR_87::set_stby_freq);
181 fgSetArchivable("/instrumentation/kr-87/outputs/standby-khz");
182 _tiedProperties.Tie("outputs/needle-deg", this,
183 &FGKR_87::get_needle_deg);
184 _tiedProperties.Tie("outputs/flight-timer", this,
185 &FGKR_87::get_flight_timer);
186 _tiedProperties.Tie("outputs/elapsed-timer", this,
187 &FGKR_87::get_elapsed_timer,
188 &FGKR_87::set_elapsed_timer);
191 _tiedProperties.Tie("annunciators/ant", this,
192 &FGKR_87::get_ant_ann );
193 _tiedProperties.Tie("annunciators/adf", this,
194 &FGKR_87::get_adf_ann );
195 _tiedProperties.Tie("annunciators/bfo", this,
196 &FGKR_87::get_bfo_ann );
197 _tiedProperties.Tie("annunciators/frq", this,
198 &FGKR_87::get_frq_ann );
199 _tiedProperties.Tie("annunciators/flt", this,
200 &FGKR_87::get_flt_ann );
201 _tiedProperties.Tie("annunciators/et", this,
202 &FGKR_87::get_et_ann );
206 void FGKR_87::unbind () {
207 _tiedProperties.Untie();
211 // Update the various nav values based on position and valid tuned in navs
212 void FGKR_87::update( double dt_sec ) {
213 SGGeod acft = globals->get_aircraft_position();
219 // On timeout, scan again
220 _time_before_search_sec -= dt_sec;
221 if ( _time_before_search_sec < 0 ) {
225 ////////////////////////////////////////////////////////////////////////
227 ////////////////////////////////////////////////////////////////////////
229 if ( has_power() && serviceable->getBoolValue() ) {
231 if ( adf_btn == 0 ) {
236 // cout << "ant_mode = " << ant_mode << endl;
238 if ( frq_btn && frq_btn != last_frq_btn && stby_mode == 0 ) {
242 } else if ( frq_btn ) {
246 last_frq_btn = frq_btn;
248 if ( flt_et_btn && flt_et_btn != last_flt_et_btn ) {
249 if ( stby_mode == 0 ) {
252 timer_mode = !timer_mode;
256 last_flt_et_btn = flt_et_btn;
258 if ( set_rst_btn == 1 && set_rst_btn != last_set_rst_btn ) {
262 if ( set_rst_btn == 1 && set_rst_btn == last_set_rst_btn ) {
263 // button depressed and was last iteration too
265 // cout << "tmp_timer = " << tmp_timer << endl;
266 if ( tmp_timer > 2.0 ) {
267 // button held depressed for 2 seconds
268 // cout << "entering elapsed count down mode" << endl;
274 if ( set_rst_btn == 0 && set_rst_btn != last_set_rst_btn ) {
276 if ( tmp_timer > 2.0 ) {
277 // button held depressed for 2 seconds, don't adjust
279 } else if ( count_mode == 2 ) {
286 last_set_rst_btn = set_rst_btn;
289 flight_timer += dt_sec;
291 if ( set_rst_btn == 0 ) {
292 // only count if set/rst button not depressed
293 if ( count_mode == 0 ) {
294 elapsed_timer += dt_sec;
295 } else if ( count_mode == 1 ) {
296 elapsed_timer -= dt_sec;
297 if ( elapsed_timer < 1.0 ) {
308 frq_ann = !stby_mode;
309 flt_ann = stby_mode && !timer_mode;
310 if ( count_mode < 2 ) {
311 et_ann = stby_mode && timer_mode;
313 et_flash_time += dt_sec;
314 if ( et_ann && et_flash_time > 0.5 ) {
316 et_flash_time -= 0.5;
317 } else if ( !et_ann && et_flash_time > 0.2 ) {
319 et_flash_time -= 0.2;
324 // cout << "adf is valid" << endl;
325 // staightline distance
326 // What a hack, dist is a class local variable
327 dist = sqrt(distSqr(SGVec3d::fromGeod(acft), xyz));
330 geo_inverse_wgs_84( acft, SGGeod::fromDeg(stn_lon, stn_lat),
333 // cout << " heading = " << heading
334 // << " dist = " << dist << endl;
336 effective_range = kludgeRange(stn_elev, acft.getElevationFt(), range);
337 if ( dist < effective_range * SG_NM_TO_METER ) {
339 } else if ( dist < 2 * effective_range * SG_NM_TO_METER ) {
340 inrange = sg_random() <
341 ( 2 * effective_range * SG_NM_TO_METER - dist ) /
342 (effective_range * SG_NM_TO_METER);
347 // cout << "inrange = " << inrange << endl;
350 goal_needle_deg = heading
351 - fgGetDouble("/orientation/heading-deg");
358 goal_needle_deg = 90.0;
362 goal_needle_deg = 0.0;
376 if ( timer_mode == 0 ) {
379 time = elapsed_timer;
381 // cout << time << endl;
382 hours = (int)(time / 3600.0);
383 time -= hours * 3600.00;
384 min = (int)(time / 60.0);
401 char formatted_timer[128];
402 // cout << big << ":" << little << endl;
403 snprintf(formatted_timer, 6, "%02d:%02d", big, little);
404 fgSetString( "/instrumentation/kr-87/outputs/timer-string",
407 while ( goal_needle_deg < 0.0 ) { goal_needle_deg += 360.0; }
408 while ( goal_needle_deg >= 360.0 ) { goal_needle_deg -= 360.0; }
410 double diff = goal_needle_deg - needle_deg;
411 while ( diff < -180.0 ) { diff += 360.0; }
412 while ( diff > 180.0 ) { diff -= 360.0; }
414 needle_deg += diff * dt_sec * 4;
415 while ( needle_deg < 0.0 ) { needle_deg += 360.0; }
416 while ( needle_deg >= 360.0 ) { needle_deg -= 360.0; }
418 // cout << "goal = " << goal_needle_deg << " actual = " << needle_deg
420 // cout << "flt = " << flight_timer << " et = " << elapsed_timer
421 // << " needle = " << needle_deg << endl;
423 if ( valid && inrange && serviceable->getBoolValue() ) {
424 // play station ident via audio system if on + ant mode,
425 // otherwise turn it off
426 if ( vol_btn >= 0.01 && audio_btn ) {
427 SGSoundSample *sound;
428 sound = _sgr->find( "adf-ident" );
429 if ( sound != NULL ) {
431 sound->set_volume( vol_btn );
433 sound->set_volume( vol_btn / 4.0 );
436 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
439 globals->get_time_params()->get_cur_time() - 30 ) {
440 last_time = globals->get_time_params()->get_cur_time();
443 if ( play_count < 4 ) {
445 if ( !_sgr->is_playing("adf-ident") && (vol_btn > 0.05) ) {
446 _sgr->play_once( "adf-ident" );
451 _sgr->stop( "adf-ident" );
457 // Update current nav/adf radio stations based on current postition
458 void FGKR_87::search() {
459 SGGeod pos = globals->get_aircraft_position();
461 // FIXME: the panel should handle this
462 static string last_ident = "";
465 _time_before_search_sec = 1.0;
467 ////////////////////////////////////////////////////////////////////////
469 ////////////////////////////////////////////////////////////////////////
472 FGNavRecord *adf = globals->get_navlist()->findByFreq( freq, pos);
475 snprintf( sfreq, 10, "%d", freq );
477 ident += adf->get_ident();
478 // cout << "adf ident = " << ident << endl;
480 if ( last_ident != ident ) {
483 trans_ident = adf->get_trans_ident();
484 stn_lon = adf->get_lon();
485 stn_lat = adf->get_lat();
486 stn_elev = adf->get_elev_ft();
487 range = adf->get_range();
488 effective_range = kludgeRange(stn_elev, pos.getElevationM(), range);
491 if ( _sgr->exists( "adf-ident" ) ) {
492 // stop is required! -- remove alone wouldn't stop immediately
493 _sgr->stop( "adf-ident" );
494 _sgr->remove( "adf-ident" );
496 SGSoundSample *sound;
497 sound = FGMorse::instance()->make_ident( trans_ident, FGMorse::LO_FREQUENCY );
498 sound->set_volume( 0.3 );
499 _sgr->add( sound, "adf-ident" );
501 int offset = (int)(sg_random() * 30.0);
502 play_count = offset / 4;
503 last_time = globals->get_time_params()->get_cur_time() -
505 // cout << "offset = " << offset << " play_count = "
506 // << play_count << " last_time = "
507 // << last_time << " current time = "
508 // << globals->get_time_params()->get_cur_time() << endl;
510 // cout << "Found an adf station in range" << endl;
511 // cout << " id = " << nav->get_ident() << endl;
517 _sgr->remove( "adf-ident" );
519 // cout << "not picking up adf. :-(" << endl;
524 int FGKR_87::get_stby_freq() const {
525 if ( stby_mode == 0 ) {
528 if ( timer_mode == 0 ) {
529 return (int)flight_timer;
531 return (int)elapsed_timer;