]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/adf.cxx
Fix a dumb bug in NavDisplay text-enable.
[flightgear.git] / src / Instrumentation / adf.cxx
1 // adf.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <simgear/compiler.h>
11 #include <simgear/math/sg_geodesy.hxx>
12 #include <simgear/math/sg_random.h>
13 #include <simgear/timing/sg_time.hxx>
14
15 #include <Main/fg_props.hxx>
16 #include <Main/util.hxx>
17 #include <Navaids/navlist.hxx>
18
19 #include "adf.hxx"
20 #include <Sound/morse.hxx>
21
22
23 #include <iostream>
24 #include <string>
25 #include <sstream>
26
27
28 // Use a bigger number to be more responsive, or a smaller number
29 // to be more sluggish.
30 #define RESPONSIVENESS 0.5
31
32
33 /**
34  * Fiddle with the reception range a bit.
35  *
36  * TODO: better reception at night (??).
37  */
38 static double
39 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
40               double max_range_nm)
41 {
42     double delta_elevation_ft =
43         aircraft_altitude_ft - transmitter_elevation_ft;
44     double range_nm = max_range_nm;
45
46                                 // kludge slightly better reception at
47                                 // altitude
48     if (delta_elevation_ft < 0)
49         delta_elevation_ft = 200;
50     if (delta_elevation_ft <= 1000)
51         range_nm *= sqrt(delta_elevation_ft / 1000);
52     else if (delta_elevation_ft >= 5000)
53         range_nm *= sqrt(delta_elevation_ft / 5000);
54     if (range_nm >= max_range_nm * 3)
55         range_nm = max_range_nm * 3;
56
57     double rand = sg_random();
58     return range_nm + (range_nm * rand * rand);
59 }
60
61
62 ADF::ADF (SGPropertyNode *node )
63     :
64     _name(node->getStringValue("name", "adf")),
65     _num(node->getIntValue("number", 0)),
66     _time_before_search_sec(0),
67     _last_frequency_khz(-1),
68     _transmitter_valid(false),
69     _transmitter_pos(SGGeod::fromDeg(0, 0)),
70     _transmitter_cart(0, 0, 0),
71     _transmitter_range_nm(0),
72     _ident_count(0),
73     _last_ident_time(0),
74     _last_volume(-1),
75     _sgr(0)
76 {
77 }
78
79 ADF::~ADF ()
80 {
81 }
82
83 void
84 ADF::init ()
85 {
86     string branch;
87     branch = "/instrumentation/" + _name;
88
89     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
90     _longitude_node = fgGetNode("/position/longitude-deg", true);
91     _latitude_node = fgGetNode("/position/latitude-deg", true);
92     _altitude_node = fgGetNode("/position/altitude-ft", true);
93     _heading_node = fgGetNode("/orientation/heading-deg", true);
94     _serviceable_node = node->getChild("serviceable", 0, true);
95     _error_node = node->getChild("error-deg", 0, true);
96     _electrical_node = fgGetNode("/systems/electrical/outputs/adf", true);
97     branch = branch + "/frequencies";
98     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
99     _frequency_node = fnode->getChild("selected-khz", 0, true);
100     _mode_node = node->getChild("mode", 0, true);
101     _volume_node = node->getChild("volume-norm", 0, true);
102     _in_range_node = node->getChild("in-range", 0, true);
103     _bearing_node = node->getChild("indicated-bearing-deg", 0, true);
104     _ident_node = node->getChild("ident", 0, true);
105     _ident_audible_node = node->getChild("ident-audible", 0, true);
106     _power_btn_node = node->getChild("power-btn", 0, true);
107
108     if (_power_btn_node->getType() == simgear::props::NONE) 
109       _power_btn_node->setBoolValue(true); // front end didn't implement a power button
110
111     SGSoundMgr *smgr = globals->get_soundmgr();
112     _sgr = smgr->find("avionics", true);
113     _sgr->tie_to_listener();
114
115     std::ostringstream temp;
116     temp << _name << _num;
117     _adf_ident = temp.str();
118 }
119
120 void
121 ADF::update (double delta_time_sec)
122 {
123                                 // If it's off, don't waste any time.
124     if (_electrical_node->getDoubleValue() < 8.0
125             || !_serviceable_node->getBoolValue()
126             || !_power_btn_node->getBoolValue()     ) {
127         _in_range_node->setBoolValue(false);
128         _ident_node->setStringValue("");
129         return;
130     }
131
132     string mode = _mode_node->getStringValue();
133     if (mode == "ant" || mode == "test") set_bearing(delta_time_sec, 90);
134     if (mode != "bfo" && mode != "adf") {
135         _in_range_node->setBoolValue(false);
136         _ident_node->setStringValue("");
137         return;
138     }
139                                 // Get the frequency
140     int frequency_khz = _frequency_node->getIntValue();
141     if (frequency_khz != _last_frequency_khz) {
142         _time_before_search_sec = 0;
143         _last_frequency_khz = frequency_khz;
144     }
145
146                                 // Get the aircraft position
147     double longitude_deg = _longitude_node->getDoubleValue();
148     double latitude_deg = _latitude_node->getDoubleValue();
149     double altitude_m = _altitude_node->getDoubleValue();
150
151     double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
152     double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
153
154                                 // On timeout, scan again
155     _time_before_search_sec -= delta_time_sec;
156     if (_time_before_search_sec < 0)
157         search(frequency_khz, longitude_rad, latitude_rad, altitude_m);
158
159     if (!_transmitter_valid) {
160         _in_range_node->setBoolValue(false);
161         _ident_node->setStringValue("");
162         return;
163     }
164
165                                 // Calculate the bearing to the transmitter
166     SGGeod geod = SGGeod::fromRadM(longitude_rad, latitude_rad, altitude_m);
167     SGVec3d location = SGVec3d::fromGeod(geod);
168     
169     double distance_nm = dist(_transmitter_cart, location) * SG_METER_TO_NM;
170     double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
171                                    altitude_m * SG_METER_TO_FEET,
172                                    _transmitter_range_nm);
173
174     if (distance_nm <= range_nm) {
175
176         double bearing, az2, s;
177         double heading = _heading_node->getDoubleValue();
178
179         geo_inverse_wgs_84(geod, _transmitter_pos,
180                            &bearing, &az2, &s);
181         _in_range_node->setBoolValue(true);
182
183         bearing -= heading;
184         if (bearing < 0)
185             bearing += 360;
186         set_bearing(delta_time_sec, bearing);
187
188         // adf ident sound
189         float volume;
190         if ( _ident_audible_node->getBoolValue() )
191             volume = _volume_node->getFloatValue();
192         else
193             volume = 0.0;
194
195         if ( volume != _last_volume ) {
196             _last_volume = volume;
197
198             SGSoundSample *sound;
199             sound = _sgr->find( _adf_ident );
200             if ( sound != NULL )
201                 sound->set_volume( volume );
202             else
203                 SG_LOG( SG_INSTR, SG_ALERT, "Can't find adf-ident sound" );
204         }
205
206         time_t cur_time = globals->get_time_params()->get_cur_time();
207         if ( _last_ident_time < cur_time - 30 ) {
208             _last_ident_time = cur_time;
209             _ident_count = 0;
210         }
211
212         if ( _ident_count < 4 ) {
213             if ( !_sgr->is_playing(_adf_ident) && (volume > 0.05) ) {
214                 _sgr->play_once( _adf_ident );
215                 ++_ident_count;
216             }
217         }
218     } else {
219         _in_range_node->setBoolValue(false);
220         _ident_node->setStringValue("");
221         _sgr->stop( _adf_ident );
222     }
223 }
224
225 void
226 ADF::search (double frequency_khz, double longitude_rad,
227              double latitude_rad, double altitude_m)
228 {
229     string ident = "";
230                                 // reset search time
231     _time_before_search_sec = 1.0;
232
233                                 // try the ILS list first
234     FGNavRecord *nav = globals->get_navlist()->findByFreq(frequency_khz,
235       SGGeod::fromRadM(longitude_rad, latitude_rad, altitude_m));
236
237     _transmitter_valid = (nav != NULL);
238     if ( _transmitter_valid ) {
239         ident = nav->get_trans_ident();
240         if ( ident != _last_ident ) {
241             _transmitter_pos = nav->geod();
242             _transmitter_cart = nav->cart();
243             _transmitter_range_nm = nav->get_range();
244         }
245     }
246
247     if ( _last_ident != ident ) {
248         _last_ident = ident;
249         _ident_node->setStringValue(ident.c_str());
250
251         if ( _sgr->exists( _adf_ident ) ) {
252             // stop is required! -- remove alone wouldn't stop immediately
253             _sgr->stop( _adf_ident );
254             _sgr->remove( _adf_ident );
255         }
256
257         SGSoundSample *sound;
258         sound = FGMorse::instance()->make_ident( ident, FGMorse::LO_FREQUENCY );
259         sound->set_volume(_last_volume = 0);
260         _sgr->add( sound, _adf_ident );
261
262         int offset = (int)(sg_random() * 30.0);
263         _ident_count = offset / 4;
264         _last_ident_time = globals->get_time_params()->get_cur_time() -
265             offset;
266     }
267 }
268
269 void
270 ADF::set_bearing (double dt, double bearing_deg)
271 {
272     double old_bearing_deg = _bearing_node->getDoubleValue();
273
274     while ((bearing_deg - old_bearing_deg) >= 180)
275         old_bearing_deg += 360;
276     while ((bearing_deg - old_bearing_deg) <= -180)
277         old_bearing_deg -= 360;
278     bearing_deg += _error_node->getDoubleValue();
279     bearing_deg =
280         fgGetLowPass(old_bearing_deg, bearing_deg, dt * RESPONSIVENESS);
281
282     _bearing_node->setDoubleValue(bearing_deg);
283 }
284
285
286 // end of adf.cxx