]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/kr_87.cxx
6d9a8a22f15d6ded3f803ecbe260875bad39779b
[flightgear.git] / src / Cockpit / kr_87.cxx
1 // kr-87.cxx -- class to impliment the King KR 87 Digital ADF
2 //
3 // Written by Curtis Olson, started April 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>      // snprintf
29
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
32
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/navlist.hxx>
35 #include <Time/FGEventMgr.hxx>
36
37 #include "kr_87.hxx"
38
39 #include <string>
40 SG_USING_STD(string);
41
42 static int adf_play_count = 0;
43 static time_t adf_last_time = 0;
44
45
46 /**
47  * Boy, this is ugly!  Make the VOR range vary by altitude difference.
48  */
49 static double kludgeRange ( double stationElev, double aircraftElev,
50                             double nominalRange)
51 {
52                                 // Assume that the nominal range (usually
53                                 // 50nm) applies at a 5,000 ft difference.
54                                 // Just a wild guess!
55   double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
56   double range = fabs(nominalRange * factor);
57
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;
65   }
66
67   return range;
68 }
69
70
71 // Constructor
72 FGKR_87::FGKR_87() :
73     lon_node(fgGetNode("/position/longitude-deg", true)),
74     lat_node(fgGetNode("/position/latitude-deg", true)),
75     alt_node(fgGetNode("/position/altitude-ft", true)),
76     need_update(true),
77     adf_valid(false),
78     adf_freq(0.0),
79     adf_alt_freq(0.0),
80     adf_vol_btn(0.0)
81 {
82     SGPath path( globals->get_fg_root() );
83     SGPath term = path;
84     term.append( "Navaids/range.term" );
85     SGPath low = path;
86     low.append( "Navaids/range.low" );
87     SGPath high = path;
88     high.append( "Navaids/range.high" );
89
90     term_tbl = new SGInterpTable( term.str() );
91     low_tbl = new SGInterpTable( low.str() );
92     high_tbl = new SGInterpTable( high.str() );
93 }
94
95
96 // Destructor
97 FGKR_87::~FGKR_87() 
98 {
99     delete term_tbl;
100     delete low_tbl;
101     delete high_tbl;
102 }
103
104
105 void
106 FGKR_87::init ()
107 {
108     morse.init();
109
110     update(0);                  // FIXME: use dt
111 }
112
113 void
114 FGKR_87::bind ()
115 {
116                                 // User inputs
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");
134
135                                 // calculated values
136     fgTie("/radios/adf/inrange", this, &FGKR_87::get_adf_inrange);
137     fgTie("/radios/adf/heading", this, &FGKR_87::get_adf_heading);
138 }
139
140 void
141 FGKR_87::unbind ()
142 {
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");
150 }
151
152
153 // Update the various nav values based on position and valid tuned in navs
154 void 
155 FGKR_87::update(double dt) 
156 {
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;
160
161     need_update = false;
162
163     Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
164     Point3D station;
165     double az1, az2, s;
166
167     ////////////////////////////////////////////////////////////////////////
168     // ADF
169     ////////////////////////////////////////////////////////////////////////
170
171     if ( adf_valid ) {
172         // staightline distance
173         station = Point3D( adf_x, adf_y, adf_z );
174         adf_dist = aircraft.distance3D( station );
175
176         // wgs84 heading
177         geo_inverse_wgs_84( elev, lat * SGD_RADIANS_TO_DEGREES,
178                             lon * SGD_RADIANS_TO_DEGREES, 
179                             adf_lat, adf_lon,
180                             &az1, &az2, &s );
181         adf_heading = az1;
182         // cout << " heading = " << adf_heading
183         //      << " dist = " << adf_dist << endl;
184
185         adf_effective_range = kludgeRange(adf_elev, elev, adf_range);
186         if ( adf_dist < adf_effective_range * SG_NM_TO_METER ) {
187             adf_inrange = true;
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);
192         } else {
193             adf_inrange = false;
194         }
195     } else {
196         adf_inrange = false;
197     }
198
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 );
208             } else {
209                 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
210             }
211             if ( adf_last_time <
212                  globals->get_time_params()->get_cur_time() - 30 ) {
213                 adf_last_time = globals->get_time_params()->get_cur_time();
214                 adf_play_count = 0;
215             }
216             if ( adf_play_count < 4 ) {
217                 // play ADF ident
218                 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
219                     globals->get_soundmgr()->play_once( "adf-ident" );
220                     ++adf_play_count;
221                 }
222             }
223         } else {
224             globals->get_soundmgr()->stop( "adf-ident" );
225         }
226     }
227 #endif
228 }
229
230
231 // Update current nav/adf radio stations based on current postition
232 void FGKR_87::search() 
233 {
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;
237
238                                 // FIXME: the panel should handle this
239     FGNav nav;
240
241     static string last_adf_ident = "";
242
243     ////////////////////////////////////////////////////////////////////////
244     // ADF.
245     ////////////////////////////////////////////////////////////////////////
246
247     if ( current_navlist->query( lon, lat, elev, adf_freq, &nav ) ) {
248         char freq[128];
249         snprintf( freq, 10, "%.0f", adf_freq );
250         adf_ident = freq;
251         adf_ident += nav.get_ident();
252         // cout << "adf ident = " << adf_ident << endl;
253         adf_valid = true;
254         if ( last_adf_ident != adf_ident ) {
255             last_adf_ident = adf_ident;
256
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);
263             adf_x = nav.get_x();
264             adf_y = nav.get_y();
265             adf_z = nav.get_z();
266
267 #ifdef ENABLE_AUDIO_SUPPORT
268             if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
269                 globals->get_soundmgr()->remove( "adf-ident" );
270             }
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" );
275
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() -
279                 offset;
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;
284 #endif
285
286             // cout << "Found an adf station in range" << endl;
287             // cout << " id = " << nav.get_ident() << endl;
288         }
289     } else {
290         adf_valid = false;
291         adf_ident = "";
292         adf_trans_ident = "";
293 #ifdef ENABLE_AUDIO_SUPPORT
294         globals->get_soundmgr()->remove( "adf-ident" );
295 #endif
296         last_adf_ident = "";
297         // cout << "not picking up adf. :-(" << endl;
298     }
299 }