]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/kr_87.cxx
8839bbed69ba6b156a9784eaf3be0f0843e0e89c
[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_freq(0.0),
78     adf_alt_freq(0.0),
79     adf_vol_btn(0.0)
80 {
81     SGPath path( globals->get_fg_root() );
82     SGPath term = path;
83     term.append( "Navaids/range.term" );
84     SGPath low = path;
85     low.append( "Navaids/range.low" );
86     SGPath high = path;
87     high.append( "Navaids/range.high" );
88
89     term_tbl = new SGInterpTable( term.str() );
90     low_tbl = new SGInterpTable( low.str() );
91     high_tbl = new SGInterpTable( high.str() );
92 }
93
94
95 // Destructor
96 FGKR_87::~FGKR_87() 
97 {
98     delete term_tbl;
99     delete low_tbl;
100     delete high_tbl;
101 }
102
103
104 void
105 FGKR_87::init ()
106 {
107     morse.init();
108
109     update(0);                  // FIXME: use dt
110 }
111
112 void
113 FGKR_87::bind ()
114 {
115                                 // User inputs
116     fgTie("/radios/adf/frequencies/selected-khz", this,
117           &FGKR_87::get_adf_freq, &FGKR_87::set_adf_freq);
118     fgSetArchivable("/radios/adf/frequencies/selected-khz");
119     fgTie("/radios/adf/frequencies/standby-khz", this,
120           &FGKR_87::get_adf_alt_freq, &FGKR_87::set_adf_alt_freq);
121     fgSetArchivable("/radios/adf/frequencies/standby-khz");
122     fgTie("/radios/adf/rotation-deg", this,
123           &FGKR_87::get_adf_rotation, &FGKR_87::set_adf_rotation);
124     fgSetArchivable("/radios/adf/rotation-deg");
125     fgTie("/radios/adf/volume", this,
126           &FGKR_87::get_adf_vol_btn,
127           &FGKR_87::set_adf_vol_btn);
128     fgSetArchivable("/radios/adf/volume");
129     fgTie("/radios/adf/ident", this,
130           &FGKR_87::get_adf_ident_btn,
131           &FGKR_87::set_adf_ident_btn);
132     fgSetArchivable("/radios/adf/ident");
133
134                                 // calculated values
135     fgTie("/radios/adf/inrange", this, &FGKR_87::get_adf_inrange);
136     fgTie("/radios/adf/heading", this, &FGKR_87::get_adf_heading);
137 }
138
139 void
140 FGKR_87::unbind ()
141 {
142     fgUntie("/radios/adf/frequencies/selected-khz");
143     fgUntie("/radios/adf/frequencies/standby-khz");
144     fgUntie("/radios/adf/rotation-deg");
145     fgUntie("/radios/adf/on");
146     fgUntie("/radios/adf/ident");
147     fgUntie("/radios/adf/inrange");
148     fgUntie("/radios/adf/heading");
149 }
150
151
152 // Update the various nav values based on position and valid tuned in navs
153 void 
154 FGKR_87::update(double dt) 
155 {
156     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
157     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
158     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
159
160     need_update = false;
161
162     Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
163     Point3D station;
164     double az1, az2, s;
165
166     ////////////////////////////////////////////////////////////////////////
167     // ADF
168     ////////////////////////////////////////////////////////////////////////
169
170     if ( adf_valid ) {
171         // staightline distance
172         station = Point3D( adf_x, adf_y, adf_z );
173         adf_dist = aircraft.distance3D( station );
174
175         // wgs84 heading
176         geo_inverse_wgs_84( elev, lat * SGD_RADIANS_TO_DEGREES,
177                             lon * SGD_RADIANS_TO_DEGREES, 
178                             adf_lat, adf_lon,
179                             &az1, &az2, &s );
180         adf_heading = az1;
181         // cout << " heading = " << adf_heading
182         //      << " dist = " << adf_dist << endl;
183
184         adf_effective_range = kludgeRange(adf_elev, elev, adf_range);
185         if ( adf_dist < adf_effective_range * SG_NM_TO_METER ) {
186             adf_inrange = true;
187         } else if ( adf_dist < 2 * adf_effective_range * SG_NM_TO_METER ) {
188             adf_inrange = sg_random() < 
189                 ( 2 * adf_effective_range * SG_NM_TO_METER - adf_dist ) /
190                 (adf_effective_range * SG_NM_TO_METER);
191         } else {
192             adf_inrange = false;
193         }
194     } else {
195         adf_inrange = false;
196     }
197
198 #ifdef ENABLE_AUDIO_SUPPORT
199     if ( adf_valid && adf_inrange ) {
200         // play station ident via audio system if on + ident,
201         // otherwise turn it off
202         if ( adf_vol_btn > 0.1 && adf_ident_btn ) {
203             FGSimpleSound *sound;
204             sound = globals->get_soundmgr()->find( "adf-ident" );
205             if ( sound != NULL ) {
206                 sound->set_volume( adf_vol_btn );
207             } else {
208                 SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
209             }
210             if ( adf_last_time <
211                  globals->get_time_params()->get_cur_time() - 30 ) {
212                 adf_last_time = globals->get_time_params()->get_cur_time();
213                 adf_play_count = 0;
214             }
215             if ( adf_play_count < 4 ) {
216                 // play ADF ident
217                 if ( !globals->get_soundmgr()->is_playing("adf-ident") ) {
218                     globals->get_soundmgr()->play_once( "adf-ident" );
219                     ++adf_play_count;
220                 }
221             }
222         } else {
223             globals->get_soundmgr()->stop( "adf-ident" );
224         }
225     }
226 #endif
227 }
228
229
230 // Update current nav/adf radio stations based on current postition
231 void FGKR_87::search() 
232 {
233     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
234     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
235     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
236
237                                 // FIXME: the panel should handle this
238     FGNav nav;
239
240     static string last_adf_ident = "";
241
242     ////////////////////////////////////////////////////////////////////////
243     // ADF.
244     ////////////////////////////////////////////////////////////////////////
245
246     if ( current_navlist->query( lon, lat, elev, adf_freq, &nav ) ) {
247         char freq[128];
248         snprintf( freq, 10, "%.0f", adf_freq );
249         adf_ident = freq;
250         adf_ident += nav.get_ident();
251         // cout << "adf ident = " << adf_ident << endl;
252         adf_valid = true;
253         if ( last_adf_ident != adf_ident ) {
254             last_adf_ident = adf_ident;
255
256             adf_trans_ident = nav.get_trans_ident();
257             adf_lon = nav.get_lon();
258             adf_lat = nav.get_lat();
259             adf_elev = nav.get_elev();
260             adf_range = nav.get_range();
261             adf_effective_range = kludgeRange(adf_elev, elev, adf_range);
262             adf_x = nav.get_x();
263             adf_y = nav.get_y();
264             adf_z = nav.get_z();
265
266 #ifdef ENABLE_AUDIO_SUPPORT
267             if ( globals->get_soundmgr()->exists( "adf-ident" ) ) {
268                 globals->get_soundmgr()->remove( "adf-ident" );
269             }
270             FGSimpleSound *sound;
271             sound = morse.make_ident( adf_trans_ident, LO_FREQUENCY );
272             sound->set_volume( 0.3 );
273             globals->get_soundmgr()->add( sound, "adf-ident" );
274
275             int offset = (int)(sg_random() * 30.0);
276             adf_play_count = offset / 4;
277             adf_last_time = globals->get_time_params()->get_cur_time() -
278                 offset;
279             // cout << "offset = " << offset << " play_count = "
280             //      << adf_play_count << " adf_last_time = "
281             //      << adf_last_time << " current time = "
282             //      << globals->get_time_params()->get_cur_time() << endl;
283 #endif
284
285             // cout << "Found an adf station in range" << endl;
286             // cout << " id = " << nav.get_ident() << endl;
287         }
288     } else {
289         adf_valid = false;
290         adf_ident = "";
291         adf_trans_ident = "";
292 #ifdef ENABLE_AUDIO_SUPPORT
293         globals->get_soundmgr()->remove( "adf-ident" );
294 #endif
295         last_adf_ident = "";
296         // cout << "not picking up adf. :-(" << endl;
297     }
298 }