1 // adf.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
4 // This file is in the Public Domain and comes with no warranty.
6 #include <simgear/compiler.h>
7 #include <simgear/math/sg_geodesy.hxx>
8 #include <simgear/math/sg_random.h>
10 #include <Main/fg_props.hxx>
11 #include <Main/util.hxx>
12 #include <Navaids/navlist.hxx>
21 // Use a bigger number to be more responsive, or a smaller number
22 // to be more sluggish.
23 #define RESPONSIVENESS 0.5
27 * Fiddle with the reception range a bit.
29 * TODO: better reception at night (??).
32 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
35 double delta_elevation_ft =
36 aircraft_altitude_ft - transmitter_elevation_ft;
37 double range_nm = max_range_nm;
39 // kludge slightly better reception at
41 if (delta_elevation_ft < 0)
42 delta_elevation_ft = 200;
43 if (delta_elevation_ft <= 1000)
44 range_nm *= sqrt(delta_elevation_ft / 1000);
45 else if (delta_elevation_ft >= 5000)
46 range_nm *= sqrt(delta_elevation_ft / 5000);
47 if (range_nm >= max_range_nm * 3)
48 range_nm = max_range_nm * 3;
50 double rand = sg_random();
51 return range_nm + (range_nm * rand * rand);
55 ADF::ADF (SGPropertyNode *node )
57 _time_before_search_sec(0),
58 _last_frequency_khz(-1),
59 _transmitter_valid(false),
60 _transmitter_elevation_ft(0),
61 _transmitter_range_nm(0),
69 for ( i = 0; i < node->nChildren(); ++i ) {
70 SGPropertyNode *child = node->getChild(i);
71 string cname = child->getName();
72 string cval = child->getStringValue();
73 if ( cname == "name" ) {
75 } else if ( cname == "number" ) {
76 num = child->getIntValue();
78 SG_LOG( SG_INSTR, SG_WARN, "Error in adf config logic" );
79 if ( name.length() ) {
80 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
88 : _time_before_search_sec(0),
89 _last_frequency_khz(-1),
90 _transmitter_valid(false),
91 _transmitter_elevation_ft(0),
92 _transmitter_range_nm(0),
109 branch = "/instrumentation/" + name;
111 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
112 _longitude_node = fgGetNode("/position/longitude-deg", true);
113 _latitude_node = fgGetNode("/position/latitude-deg", true);
114 _altitude_node = fgGetNode("/position/altitude-ft", true);
115 _heading_node = fgGetNode("/orientation/heading-deg", true);
116 _serviceable_node = node->getChild("serviceable", 0, true);
117 _error_node = node->getChild("error-deg", 0, true);
118 _electrical_node = fgGetNode("/systems/electrical/outputs/adf", true);
119 branch = branch + "/frequencies";
120 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
121 _frequency_node = fnode->getChild("selected-khz", 0, true);
122 _mode_node = node->getChild("mode", 0, true);
123 _volume_node = node->getChild("volume-norm", 0, true);
124 _in_range_node = node->getChild("in-range", 0, true);
125 _bearing_node = node->getChild("indicated-bearing-deg", 0, true);
126 _ident_node = node->getChild("ident", 0, true);
127 _ident_audible_node = node->getChild("ident-audible", 0, true);
131 std::ostringstream temp;
133 adf_ident = temp.str();
137 ADF::update (double delta_time_sec)
139 // If it's off, don't waste any time.
140 if (!_electrical_node->getBoolValue() ||
141 !_serviceable_node->getBoolValue()) {
142 set_bearing(delta_time_sec, 90);
143 _ident_node->setStringValue("");
148 int frequency_khz = _frequency_node->getIntValue();
149 if (frequency_khz != _last_frequency_khz) {
150 _time_before_search_sec = 0;
151 _last_frequency_khz = frequency_khz;
154 // Get the aircraft position
155 double longitude_deg = _longitude_node->getDoubleValue();
156 double latitude_deg = _latitude_node->getDoubleValue();
157 double altitude_m = _altitude_node->getDoubleValue();
159 double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
160 double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
162 // On timeout, scan again
163 _time_before_search_sec -= delta_time_sec;
164 if (_time_before_search_sec < 0)
165 search(frequency_khz, longitude_rad, latitude_rad, altitude_m);
167 // If it's off, don't bother.
168 string mode = _mode_node->getStringValue();
169 if (!_transmitter_valid || (mode != "bfo" && mode != "adf"))
171 set_bearing(delta_time_sec, 90);
172 _ident_node->setStringValue("");
176 // Calculate the bearing to the transmitter
178 sgGeodToCart(Point3D(longitude_rad, latitude_rad, altitude_m));
180 double distance_nm = _transmitter.distance3D(location) * SG_METER_TO_NM;
181 double range_nm = adjust_range(_transmitter_elevation_ft,
182 altitude_m * SG_METER_TO_FEET,
183 _transmitter_range_nm);
184 if (distance_nm <= range_nm) {
186 double bearing, az2, s;
187 double heading = _heading_node->getDoubleValue();
189 geo_inverse_wgs_84(altitude_m,
192 _transmitter_lat_deg,
193 _transmitter_lon_deg,
195 _in_range_node->setBoolValue(true);
200 set_bearing(delta_time_sec, bearing);
204 if ( _ident_audible_node->getBoolValue() )
205 volume = _volume_node->getDoubleValue();
209 if ( volume != _last_volume ) {
210 _last_volume = volume;
212 SGSoundSample *sound;
213 sound = globals->get_soundmgr()->find( adf_ident );
215 sound->set_volume( volume );
217 SG_LOG( SG_GENERAL, SG_ALERT, "Can't find adf-ident sound" );
220 time_t cur_time = globals->get_time_params()->get_cur_time();
221 if ( _last_ident_time < cur_time - 30 ) {
222 _last_ident_time = cur_time;
226 if ( _ident_count < 4 ) {
227 if ( !globals->get_soundmgr()->is_playing(adf_ident) ) {
228 globals->get_soundmgr()->play_once( adf_ident );
233 _in_range_node->setBoolValue(false);
234 set_bearing(delta_time_sec, 90);
235 _ident_node->setStringValue("");
236 globals->get_soundmgr()->stop( adf_ident );
241 ADF::search (double frequency_khz, double longitude_rad,
242 double latitude_rad, double altitude_m)
246 _time_before_search_sec = 1.0;
248 // try the ILS list first
250 globals->get_navlist()->findByFreq(frequency_khz, longitude_rad,
251 latitude_rad, altitude_m);
253 _transmitter_valid = (nav != NULL);
254 if ( _transmitter_valid ) {
255 ident = nav->get_trans_ident();
256 if ( ident != _last_ident ) {
257 _transmitter_lon_deg = nav->get_lon();
258 _transmitter_lat_deg = nav->get_lat();
259 _transmitter = Point3D(nav->get_x(), nav->get_y(), nav->get_z());
260 _transmitter_elevation_ft = nav->get_elev_ft();
261 _transmitter_range_nm = nav->get_range();
265 if ( _last_ident != ident ) {
267 _ident_node->setStringValue(ident.c_str());
269 if ( globals->get_soundmgr()->exists( adf_ident ) ) {
270 // stop is required! -- remove alone wouldn't stop immediately
271 globals->get_soundmgr()->stop( adf_ident );
272 globals->get_soundmgr()->remove( adf_ident );
275 SGSoundSample *sound;
276 sound = morse.make_ident( ident, LO_FREQUENCY );
277 sound->set_volume(_last_volume = 0);
278 globals->get_soundmgr()->add( sound, adf_ident );
280 int offset = (int)(sg_random() * 30.0);
281 _ident_count = offset / 4;
282 _last_ident_time = globals->get_time_params()->get_cur_time() -
288 ADF::set_bearing (double dt, double bearing_deg)
290 double old_bearing_deg = _bearing_node->getDoubleValue();
292 while ((bearing_deg - old_bearing_deg) >= 180)
293 old_bearing_deg += 360;
294 while ((bearing_deg - old_bearing_deg) <= -180)
295 old_bearing_deg -= 360;
296 bearing_deg += _error_node->getDoubleValue();
298 fgGetLowPass(old_bearing_deg, bearing_deg, dt * RESPONSIVENESS);
300 _bearing_node->setDoubleValue(bearing_deg);