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>
28 // Use a bigger number to be more responsive, or a smaller number
29 // to be more sluggish.
30 #define RESPONSIVENESS 0.5
34 * Fiddle with the reception range a bit.
36 * TODO: better reception at night (??).
39 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
42 double delta_elevation_ft =
43 aircraft_altitude_ft - transmitter_elevation_ft;
44 double range_nm = max_range_nm;
46 // kludge slightly better reception at
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;
57 double rand = sg_random();
58 return range_nm + (range_nm * rand * rand);
62 ADF::ADF (SGPropertyNode *node )
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),
87 branch = "/instrumentation/" + _name;
88 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
90 // instrument properties
91 _error_node = node->getChild("error-deg", 0, true);
92 _mode_node = node->getChild("mode", 0, true);
93 _volume_node = node->getChild("volume-norm", 0, true);
94 _in_range_node = node->getChild("in-range", 0, true);
95 _bearing_node = node->getChild("indicated-bearing-deg", 0, true);
96 _ident_node = node->getChild("ident", 0, true);
97 _ident_audible_node = node->getChild("ident-audible", 0, true);
98 _serviceable_node = node->getChild("serviceable", 0, true);
99 _power_btn_node = node->getChild("power-btn", 0, true);
100 _operable_node = node->getChild("operable", 0, true);
102 // frequency properties
103 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
104 _frequency_node = fnode->getChild("selected-khz", 0, true);
106 // foreign simulator properties
107 _electrical_node = fgGetNode("/systems/electrical/outputs/adf", true);
108 _heading_node = fgGetNode("/orientation/heading-deg", true);
110 // backward compatibility check
111 if (_power_btn_node->getType() == simgear::props::NONE)
112 _power_btn_node->setBoolValue(true); // front end didn't implement a power button
114 // sound support (audible ident code)
115 SGSoundMgr *smgr = globals->get_soundmgr();
116 _sgr = smgr->find("avionics", true);
117 _sgr->tie_to_listener();
119 std::ostringstream temp;
120 temp << _name << _num;
121 _adf_ident = temp.str();
125 ADF::update (double delta_time_sec)
127 // If it's off, don't waste any time.
128 if (_electrical_node->getDoubleValue() < 8.0
129 || !_serviceable_node->getBoolValue()
130 || !_power_btn_node->getBoolValue() ) {
131 _in_range_node->setBoolValue(false);
132 _operable_node->setBoolValue(false);
133 _ident_node->setStringValue("");
137 _operable_node->setBoolValue(true);
139 string mode = _mode_node->getStringValue();
140 if (mode == "ant" || mode == "test") set_bearing(delta_time_sec, 90);
141 if (mode != "bfo" && mode != "adf") {
142 _in_range_node->setBoolValue(false);
143 _ident_node->setStringValue("");
147 int frequency_khz = _frequency_node->getIntValue();
148 if (frequency_khz != _last_frequency_khz) {
149 _time_before_search_sec = 0;
150 _last_frequency_khz = frequency_khz;
153 SGGeod acPos(globals->get_aircraft_position());
155 // On timeout, scan again
156 _time_before_search_sec -= delta_time_sec;
157 if (_time_before_search_sec < 0)
158 search(frequency_khz, acPos);
160 if (!_transmitter_valid) {
161 _in_range_node->setBoolValue(false);
162 _ident_node->setStringValue("");
166 // Calculate the bearing to the transmitter
167 SGVec3d location = globals->get_aircraft_position_cart();
169 double distance_nm = dist(_transmitter_cart, location) * SG_METER_TO_NM;
170 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
171 acPos.getElevationFt(),
172 _transmitter_range_nm);
174 if (distance_nm <= range_nm) {
176 double bearing, az2, s;
177 double heading = _heading_node->getDoubleValue();
179 geo_inverse_wgs_84(acPos, _transmitter_pos,
181 _in_range_node->setBoolValue(true);
186 set_bearing(delta_time_sec, bearing);
190 if ( _ident_audible_node->getBoolValue() )
191 volume = _volume_node->getFloatValue();
195 if ( volume != _last_volume ) {
196 _last_volume = volume;
198 SGSoundSample *sound;
199 sound = _sgr->find( _adf_ident );
201 sound->set_volume( volume );
203 SG_LOG( SG_INSTR, SG_ALERT, "Can't find adf-ident sound" );
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;
212 if ( _ident_count < 4 ) {
213 if ( !_sgr->is_playing(_adf_ident) && (volume > 0.05) ) {
214 _sgr->play_once( _adf_ident );
219 _in_range_node->setBoolValue(false);
220 _ident_node->setStringValue("");
221 _sgr->stop( _adf_ident );
226 ADF::search (double frequency_khz, const SGGeod& pos)
230 _time_before_search_sec = 1.0;
232 FGNavList::TypeFilter filter(FGPositioned::NDB);
233 FGNavRecord *nav = FGNavList::findByFreq(frequency_khz, pos, &filter);
235 _transmitter_valid = (nav != NULL);
236 if ( _transmitter_valid ) {
237 ident = nav->get_trans_ident();
238 if ( ident != _last_ident ) {
239 _transmitter_pos = nav->geod();
240 _transmitter_cart = nav->cart();
241 _transmitter_range_nm = nav->get_range();
245 if ( _last_ident != ident ) {
247 _ident_node->setStringValue(ident.c_str());
249 if ( _sgr->exists( _adf_ident ) ) {
250 // stop is required! -- remove alone wouldn't stop immediately
251 _sgr->stop( _adf_ident );
252 _sgr->remove( _adf_ident );
255 SGSoundSample *sound;
256 sound = FGMorse::instance()->make_ident( ident, FGMorse::LO_FREQUENCY );
257 sound->set_volume(_last_volume = 0);
258 _sgr->add( sound, _adf_ident );
260 int offset = (int)(sg_random() * 30.0);
261 _ident_count = offset / 4;
262 _last_ident_time = globals->get_time_params()->get_cur_time() -
268 ADF::set_bearing (double dt, double bearing_deg)
270 double old_bearing_deg = _bearing_node->getDoubleValue();
272 while ((bearing_deg - old_bearing_deg) >= 180)
273 old_bearing_deg += 360;
274 while ((bearing_deg - old_bearing_deg) <= -180)
275 old_bearing_deg -= 360;
276 bearing_deg += _error_node->getDoubleValue();
278 fgGetLowPass(old_bearing_deg, bearing_deg, dt * RESPONSIVENESS);
280 _bearing_node->setDoubleValue(bearing_deg);