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 adf_play_count = 0;
43 static time_t adf_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 25% or more than
60 // 500% of nominal range.
61 if (range < nominalRange/4.0) {
62 range = nominalRange/4.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)),
82 SGPath path( globals->get_fg_root() );
84 term.append( "Navaids/range.term" );
86 low.append( "Navaids/range.low" );
88 high.append( "Navaids/range.high" );
90 term_tbl = new SGInterpTable( term.str() );
91 low_tbl = new SGInterpTable( low.str() );
92 high_tbl = new SGInterpTable( high.str() );
110 update(0); // FIXME: use dt
117 fgTie("/radios/adf/frequencies/selected-khz", this,
118 &FGKR_87::get_adf_freq, &FGKR_87::set_adf_freq);
119 fgSetArchivable("/radios/adf/frequencies/selected-khz");
120 fgTie("/radios/adf/frequencies/standby-khz", this,
121 &FGKR_87::get_adf_alt_freq, &FGKR_87::set_adf_alt_freq);
122 fgSetArchivable("/radios/adf/frequencies/standby-khz");
123 fgTie("/radios/adf/rotation-deg", this,
124 &FGKR_87::get_adf_rotation, &FGKR_87::set_adf_rotation);
125 fgSetArchivable("/radios/adf/rotation-deg");
126 fgTie("/radios/adf/volume", this,
127 &FGKR_87::get_adf_vol_btn,
128 &FGKR_87::set_adf_vol_btn);
129 fgSetArchivable("/radios/adf/volume");
130 fgTie("/radios/adf/ident", this,
131 &FGKR_87::get_adf_ident_btn,
132 &FGKR_87::set_adf_ident_btn);
133 fgSetArchivable("/radios/adf/ident");
136 fgTie("/radios/adf/inrange", this, &FGKR_87::get_adf_inrange);
137 fgTie("/radios/adf/heading", this, &FGKR_87::get_adf_heading);
143 fgUntie("/radios/adf/frequencies/selected-khz");
144 fgUntie("/radios/adf/frequencies/standby-khz");
145 fgUntie("/radios/adf/rotation-deg");
146 fgUntie("/radios/adf/on");
147 fgUntie("/radios/adf/ident");
148 fgUntie("/radios/adf/inrange");
149 fgUntie("/radios/adf/heading");
153 // Update the various nav values based on position and valid tuned in navs
155 FGKR_87::update(double dt)
157 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
158 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
159 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
163 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
167 ////////////////////////////////////////////////////////////////////////
169 ////////////////////////////////////////////////////////////////////////
172 // staightline distance
173 station = Point3D( adf_x, adf_y, adf_z );
174 adf_dist = aircraft.distance3D( station );
177 geo_inverse_wgs_84( elev, lat * SGD_RADIANS_TO_DEGREES,
178 lon * SGD_RADIANS_TO_DEGREES,
182 // cout << " heading = " << adf_heading
183 // << " dist = " << adf_dist << endl;
185 adf_effective_range = kludgeRange(adf_elev, elev, adf_range);
186 if ( adf_dist < adf_effective_range * SG_NM_TO_METER ) {
188 } else if ( adf_dist < 2 * adf_effective_range * SG_NM_TO_METER ) {
189 adf_inrange = sg_random() <
190 ( 2 * adf_effective_range * SG_NM_TO_METER - adf_dist ) /
191 (adf_effective_range * SG_NM_TO_METER);
199 #ifdef ENABLE_AUDIO_SUPPORT
200 if ( adf_valid && adf_inrange ) {
201 // play station ident via audio system if on + ident,
202 // otherwise turn it off
203 if ( adf_vol_btn > 0.1 && adf_ident_btn ) {
204 FGSimpleSound *sound;
205 sound = globals->get_soundmgr()->find( "adf-ident" );
206 if ( sound != NULL ) {
207 sound->set_volume( adf_vol_btn );
209 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
212 globals->get_time_params()->get_cur_time() - 30 ) {
213 adf_last_time = globals->get_time_params()->get_cur_time();
216 if ( adf_play_count < 4 ) {
218 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
219 globals->get_soundmgr()->play_once( "adf-ident" );
224 globals->get_soundmgr()->stop( "adf-ident" );
231 // Update current nav/adf radio stations based on current postition
232 void FGKR_87::search()
234 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
235 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
236 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
238 // FIXME: the panel should handle this
241 static string last_adf_ident = "";
243 ////////////////////////////////////////////////////////////////////////
245 ////////////////////////////////////////////////////////////////////////
247 if ( current_navlist->query( lon, lat, elev, adf_freq, &nav ) ) {
249 snprintf( freq, 10, "%.0f", adf_freq );
251 adf_ident += nav.get_ident();
252 // cout << "adf ident = " << adf_ident << endl;
254 if ( last_adf_ident != adf_ident ) {
255 last_adf_ident = adf_ident;
257 adf_trans_ident = nav.get_trans_ident();
258 adf_lon = nav.get_lon();
259 adf_lat = nav.get_lat();
260 adf_elev = nav.get_elev();
261 adf_range = nav.get_range();
262 adf_effective_range = kludgeRange(adf_elev, elev, adf_range);
267 #ifdef ENABLE_AUDIO_SUPPORT
268 if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
269 globals->get_soundmgr()->remove( "adf-ident" );
271 FGSimpleSound *sound;
272 sound = morse.make_ident( adf_trans_ident, LO_FREQUENCY );
273 sound->set_volume( 0.3 );
274 globals->get_soundmgr()->add( sound, "adf-ident" );
276 int offset = (int)(sg_random() * 30.0);
277 adf_play_count = offset / 4;
278 adf_last_time = globals->get_time_params()->get_cur_time() -
280 // cout << "offset = " << offset << " play_count = "
281 // << adf_play_count << " adf_last_time = "
282 // << adf_last_time << " current time = "
283 // << globals->get_time_params()->get_cur_time() << endl;
286 // cout << "Found an adf station in range" << endl;
287 // cout << " id = " << nav.get_ident() << endl;
292 adf_trans_ident = "";
293 #ifdef ENABLE_AUDIO_SUPPORT
294 globals->get_soundmgr()->remove( "adf-ident" );
297 // cout << "not picking up adf. :-(" << endl;