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.
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>
15 #include <Main/fg_props.hxx>
16 #include <Main/util.hxx>
17 #include <Navaids/navlist.hxx>
20 #include <Sound/morse.hxx>
21 #include <simgear/sound/sample_group.hxx>
29 // Use a bigger number to be more responsive, or a smaller number
30 // to be more sluggish.
31 #define RESPONSIVENESS 0.5
35 * Fiddle with the reception range a bit.
37 * TODO: better reception at night (??).
40 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
43 double delta_elevation_ft =
44 aircraft_altitude_ft - transmitter_elevation_ft;
45 double range_nm = max_range_nm;
47 // kludge slightly better reception at
49 if (delta_elevation_ft < 0)
50 delta_elevation_ft = 200;
51 if (delta_elevation_ft <= 1000)
52 range_nm *= sqrt(delta_elevation_ft / 1000);
53 else if (delta_elevation_ft >= 5000)
54 range_nm *= sqrt(delta_elevation_ft / 5000);
55 if (range_nm >= max_range_nm * 3)
56 range_nm = max_range_nm * 3;
58 double rand = sg_random();
59 return range_nm + (range_nm * rand * rand);
63 ADF::ADF (SGPropertyNode *node )
65 _name(node->getStringValue("name", "adf")),
66 _num(node->getIntValue("number", 0)),
67 _time_before_search_sec(0),
68 _last_frequency_khz(-1),
69 _transmitter_valid(false),
70 _transmitter_pos(SGGeod::fromDeg(0, 0)),
71 _transmitter_cart(0, 0, 0),
72 _transmitter_range_nm(0),
88 branch = "/instrumentation/" + _name;
89 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
91 // instrument properties
92 _error_node = node->getChild("error-deg", 0, true);
93 _mode_node = node->getChild("mode", 0, true);
94 _volume_node = node->getChild("volume-norm", 0, true);
95 _in_range_node = node->getChild("in-range", 0, true);
96 _bearing_node = node->getChild("indicated-bearing-deg", 0, true);
97 _ident_node = node->getChild("ident", 0, true);
98 _ident_audible_node = node->getChild("ident-audible", 0, true);
99 _serviceable_node = node->getChild("serviceable", 0, true);
100 _power_btn_node = node->getChild("power-btn", 0, true);
101 _operable_node = node->getChild("operable", 0, true);
103 // frequency properties
104 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
105 _frequency_node = fnode->getChild("selected-khz", 0, true);
107 // foreign simulator properties
108 _electrical_node = fgGetNode("/systems/electrical/outputs/adf", true);
109 _heading_node = fgGetNode("/orientation/heading-deg", true);
111 // backward compatibility check
112 if (_power_btn_node->getType() == simgear::props::NONE)
113 _power_btn_node->setBoolValue(true); // front end didn't implement a power button
115 // sound support (audible ident code)
116 SGSoundMgr *smgr = globals->get_soundmgr();
117 _sgr = smgr->find("avionics", true);
118 _sgr->tie_to_listener();
120 std::ostringstream temp;
121 temp << _name << _num;
122 _adf_ident = temp.str();
126 ADF::update (double delta_time_sec)
128 // If it's off, don't waste any time.
129 if (_electrical_node->getDoubleValue() < 8.0
130 || !_serviceable_node->getBoolValue()
131 || !_power_btn_node->getBoolValue() ) {
132 _in_range_node->setBoolValue(false);
133 _operable_node->setBoolValue(false);
134 _ident_node->setStringValue("");
138 _operable_node->setBoolValue(true);
140 string mode = _mode_node->getStringValue();
141 if (mode == "ant" || mode == "test") set_bearing(delta_time_sec, 90);
142 if (mode != "bfo" && mode != "adf") {
143 _in_range_node->setBoolValue(false);
144 _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 SGGeod acPos(globals->get_aircraft_position());
156 // On timeout, scan again
157 _time_before_search_sec -= delta_time_sec;
158 if (_time_before_search_sec < 0)
159 search(frequency_khz, acPos);
161 if (!_transmitter_valid) {
162 _in_range_node->setBoolValue(false);
163 _ident_node->setStringValue("");
167 // Calculate the bearing to the transmitter
168 SGVec3d location = globals->get_aircraft_position_cart();
170 double distance_nm = dist(_transmitter_cart, location) * SG_METER_TO_NM;
171 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
172 acPos.getElevationFt(),
173 _transmitter_range_nm);
175 if (distance_nm <= range_nm) {
177 double bearing, az2, s;
178 double heading = _heading_node->getDoubleValue();
180 geo_inverse_wgs_84(acPos, _transmitter_pos,
182 _in_range_node->setBoolValue(true);
183 _ident_node->setStringValue(_last_ident);
188 set_bearing(delta_time_sec, bearing);
192 if ( _ident_audible_node->getBoolValue() )
193 volume = _volume_node->getFloatValue();
197 if ( volume != _last_volume ) {
198 _last_volume = volume;
200 SGSoundSample *sound;
201 sound = _sgr->find( _adf_ident );
203 sound->set_volume( volume );
205 SG_LOG( SG_INSTR, SG_ALERT, "Can't find adf-ident sound" );
208 time_t cur_time = globals->get_time_params()->get_cur_time();
209 if ( _last_ident_time < cur_time - 30 ) {
210 _last_ident_time = cur_time;
214 if ( _ident_count < 4 ) {
215 if ( !_sgr->is_playing(_adf_ident) && (volume > 0.05) ) {
216 _sgr->play_once( _adf_ident );
221 _in_range_node->setBoolValue(false);
222 _ident_node->setStringValue("");
223 _sgr->stop( _adf_ident );
228 ADF::search (double frequency_khz, const SGGeod& pos)
232 _time_before_search_sec = 1.0;
234 FGNavList::TypeFilter filter(FGPositioned::NDB);
235 FGNavRecord *nav = FGNavList::findByFreq(frequency_khz, pos, &filter);
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();
247 if ( _last_ident != ident ) {
249 _ident_node->setStringValue(ident.c_str());
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 );
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 );
262 int offset = (int)(sg_random() * 30.0);
263 _ident_count = offset / 4;
264 _last_ident_time = globals->get_time_params()->get_cur_time() -
270 ADF::set_bearing (double dt, double bearing_deg)
272 double old_bearing_deg = _bearing_node->getDoubleValue();
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();
280 fgGetLowPass(old_bearing_deg, bearing_deg, dt * RESPONSIVENESS);
282 _bearing_node->setDoubleValue(bearing_deg);