1 // tacan.cxx - Tactical Navigation Beacon.
2 // Written by Vivian Meazaa, started 2005.
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>
14 #include <Main/fg_props.hxx>
15 #include <Navaids/navlist.hxx>
26 * Start by calculating the radar horizon based on the elevation
27 * difference, then clamp to the maximum, then add a fudge for
28 * borderline reception.
31 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
35 double delta_elevation_ft =
36 fabs(aircraft_altitude_ft - transmitter_elevation_ft);
37 double range_nm = 1.23 * sqrt(delta_elevation_ft);
38 if (range_nm > max_range_nm)
39 range_nm = max_range_nm;
40 else if (range_nm < 20.0)
42 double rand = sg_random();
43 return range_nm + (range_nm * rand * rand);
47 TACAN::TACAN ( SGPropertyNode *node ) :
48 _name(node->getStringValue("name", "tacan")),
49 _num(node->getIntValue("number", 0)),
50 _new_frequency(false),
54 _time_before_search_sec(0),
56 _transmitter_valid(false),
57 _transmitter_pos(SGGeod::fromDeg(0, 0)),
58 _transmitter_range_nm(0),
59 _transmitter_bias(0.0),
71 string branch = "/instrumentation/" + _name;
73 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
75 _serviceable_node = node->getChild("serviceable", 0, true);
76 _ident_node = node->getChild("ident", 0, true);
78 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
79 _frequency_node = fnode->getChild("selected-mhz", 0, true);
81 _channel_in0_node = fnode->getChild("selected-channel", 0, true);
82 _channel_in1_node = fnode->getChild("selected-channel", 1, true);
83 _channel_in2_node = fnode->getChild("selected-channel", 2, true);
84 _channel_in3_node = fnode->getChild("selected-channel", 3, true);
85 _channel_in4_node = fnode->getChild("selected-channel", 4, true);
87 _channel_in0_node->addChangeListener(this);
88 _channel_in1_node->addChangeListener(this);
89 _channel_in2_node->addChangeListener(this);
90 _channel_in3_node->addChangeListener(this);
91 _channel_in4_node->addChangeListener(this, true);
93 _in_range_node = node->getChild("in-range", 0, true);
94 _distance_node = node->getChild("indicated-distance-nm", 0, true);
95 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
96 _time_node = node->getChild("indicated-time-min", 0, true);
97 _name_node = node->getChild("name", 0, true);
98 _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
100 SGPropertyNode *dnode = node->getChild("display", 0, true);
101 _x_shift_node = dnode->getChild("x-shift", 0, true);
102 _y_shift_node = dnode->getChild("y-shift", 0, true);
103 _rotation_node = dnode->getChild("rotation", 0, true);
104 _channel_node = dnode->getChild("channel", 0, true);
106 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
107 _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
109 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
110 _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
112 SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
113 _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
115 _heading_node = fgGetNode("/orientation/heading-deg", true);
116 _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
122 _time_before_search_sec = 0;
126 TACAN::update (double delta_time_sec)
128 // don't do anything when paused
129 if (delta_time_sec == 0) return;
131 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
132 _last_distance_nm = 0;
133 _in_range_node->setBoolValue(false);
134 _distance_node->setDoubleValue(0);
135 _speed_node->setDoubleValue(0);
136 _time_node->setDoubleValue(0);
140 SGGeod pos(globals->get_aircraft_position());
141 // On timeout, scan again
142 _time_before_search_sec -= delta_time_sec;
143 if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
144 search(_frequency_mhz, pos);
146 // Calculate the distance to the transmitter
148 //calculate the bearing and range of the mobile from the aircraft
149 double mobile_az2 = 0;
150 double mobile_bearing = 0;
151 double mobile_distance = 0;
153 geo_inverse_wgs_84(pos, _mobilePos,
154 &mobile_bearing, &mobile_az2, &mobile_distance);
157 //calculate the bearing and range of the station from the aircraft
161 if (_transmitter_valid) {
162 geo_inverse_wgs_84(pos, _transmitter_pos,
163 &bearing, &az2, &distance);
167 if ( mobile_distance <= distance && _mobile_valid) {
168 bearing = mobile_bearing;
169 distance = mobile_distance;
170 _transmitter_pos.setElevationFt(_mobilePos.getElevationFt());
171 _transmitter_range_nm = _mobile_range_nm;
172 _transmitter_bias = _mobile_bias;
173 _transmitter_name = _mobile_name;
174 _name_node->setStringValue(_transmitter_name.c_str());
175 _transmitter_ident = _mobile_ident;
176 _ident_node->setStringValue(_transmitter_ident.c_str());
177 _channel_node->setStringValue(_channel.c_str());
180 //// calculate some values for boresight display
181 double distance_nm = distance * SG_METER_TO_NM;
183 //// calculate look left/right to target, without yaw correction
184 // double horiz_offset = bearing - heading;
186 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
187 // if (horiz_offset < -180.0) horiz_offset += 360.0;
189 //// now correct look left/right for yaw
190 // horiz_offset += yaw;
192 // use the bearing for a plan position indicator display
194 double horiz_offset = bearing;
196 // calculate values for radar display
197 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
198 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
202 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
203 pos.getElevationFt(),
204 _transmitter_range_nm);
206 if (distance_nm <= range_nm) {
207 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
208 ((1 / delta_time_sec) * 3600.0));
209 _last_distance_nm = distance_nm;
211 _in_range_node->setBoolValue(true);
212 double tmp_dist = distance_nm - _transmitter_bias;
213 if ( tmp_dist < 0.0 ) {
216 _distance_node->setDoubleValue( tmp_dist );
217 _speed_node->setDoubleValue(speed_kt);
218 _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0);
219 _bearing_node->setDoubleValue(bearing);
220 _x_shift_node->setDoubleValue(x_shift);
221 _y_shift_node->setDoubleValue(y_shift);
222 _rotation_node->setDoubleValue(rotation);
224 _last_distance_nm = 0;
225 _in_range_node->setBoolValue(false);
226 _distance_node->setDoubleValue(0);
227 _speed_node->setDoubleValue(0);
228 _time_node->setDoubleValue(0);
229 _bearing_node->setDoubleValue(0);
230 _x_shift_node->setDoubleValue(0);
231 _y_shift_node->setDoubleValue(0);
232 _rotation_node->setDoubleValue(0);
235 // If we can't find a valid station set everything to zero
236 if (!_transmitter_valid && !_mobile_valid ) {
237 _in_range_node->setBoolValue(false);
238 _distance_node->setDoubleValue(0);
239 _speed_node->setDoubleValue(0);
240 _time_node->setDoubleValue(0);
241 _bearing_node->setDoubleValue(0);
242 _x_shift_node->setDoubleValue(0);
243 _y_shift_node->setDoubleValue(0);
244 _rotation_node->setDoubleValue(0);
245 _transmitter_name = "";
246 _name_node->setStringValue(_transmitter_name.c_str());
247 _transmitter_ident = "";
248 _ident_node->setStringValue(_transmitter_ident.c_str());
249 _channel_node->setStringValue(_channel.c_str());
252 } // end function update
255 TACAN::search (double frequency_mhz,const SGGeod& pos)
258 _mobile_valid = false;
261 _time_before_search_sec = 1.0;
263 //try any carriers first
264 FGNavRecord *mobile_tacan = FGNavList::findByFreq( frequency_mhz, FGNavList::carrierFilter() );
265 bool freq_valid = (mobile_tacan != NULL);
269 string str1( mobile_tacan->name() );
271 SGPropertyNode * branch = fgGetNode("ai/models", true);
272 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
274 number = carrier.size();
276 for ( i = 0; i < number; ++i ) {
277 string str2 ( carrier[i]->getStringValue("name", ""));
279 string::size_type loc1= str1.find( str2, 0 );
280 if ( loc1 != string::npos && str2 != "" ) {
281 _mobilePos = SGGeod::fromDegFt(
282 carrier[i]->getDoubleValue("position/longitude-deg"),
283 carrier[i]->getDoubleValue("position/latitude-deg"),
284 mobile_tacan->get_elev_ft());
285 _mobile_range_nm = mobile_tacan->get_range();
286 _mobile_bias = mobile_tacan->get_multiuse();
287 _mobile_name = mobile_tacan->name();
288 _mobile_ident = mobile_tacan->get_trans_ident();
289 _mobile_valid = true;
292 _mobile_valid = false;
296 //try any AI tankers second
297 if ( !_mobile_valid) {
298 SGPropertyNode * branch = fgGetNode("ai/models", true);
299 vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
301 number = tanker.size();
303 for ( i = 0; i < number; ++i ) {
304 string str4 ( tanker[i]->getStringValue("callsign", ""));
305 string::size_type loc1= str1.find( str4, 0 );
306 if ( loc1 != string::npos && str4 != "" ) {
307 _mobilePos = SGGeod::fromDegFt(
308 tanker[i]->getDoubleValue("position/longitude-deg"),
309 tanker[i]->getDoubleValue("position/latitude-deg"),
310 tanker[i]->getDoubleValue("position/altitude-ft"));
313 _mobile_range_nm = mobile_tacan->get_range();
314 _mobile_bias = mobile_tacan->get_multiuse();
315 _mobile_name = mobile_tacan->name();
316 _mobile_ident = mobile_tacan->get_trans_ident();
317 _mobile_valid = true;
320 _mobile_valid = false;
325 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
327 if ( !_mobile_valid ) {
328 SGPropertyNode * branch = fgGetNode("ai/models", true);
329 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
331 number = mp_tanker.size();
333 if ( number > 0 ) { // don't do this if there are no MP aircraft
334 for ( i = 0; i < number; ++i ) {
335 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
336 string::size_type loc1= str1.find( str6, 0 );
337 if ( loc1 != string::npos && str6 != "" ) {
338 _mobilePos = SGGeod::fromDegFt(
339 mp_tanker[i]->getDoubleValue("position/longitude-deg"),
340 mp_tanker[i]->getDoubleValue("position/latitude-deg"),
341 mp_tanker[i]->getDoubleValue("position/altitude-ft"));
344 _mobile_range_nm = mobile_tacan->get_range();
345 _mobile_bias = mobile_tacan->get_multiuse();
346 _mobile_name = mobile_tacan->name();
347 _mobile_ident = mobile_tacan->get_trans_ident();
348 _mobile_valid = true;
351 _mobile_valid = false;
357 _mobile_valid = false;
360 // try the TACAN/VORTAC list next
361 FGNavRecord *tacan = FGNavList::findByFreq( frequency_mhz, pos, FGNavList::tacanFilter() );
363 _transmitter_valid = (tacan != NULL);
365 if ( _transmitter_valid ) {
367 _transmitter_pos = tacan->geod();
368 _transmitter_range_nm = tacan->get_range();
369 _transmitter_bias = tacan->get_multiuse();
370 _transmitter_name = tacan->name();
371 _name_node->setStringValue(_transmitter_name.c_str());
372 _transmitter_ident = tacan->get_trans_ident();
373 _ident_node->setStringValue(_transmitter_ident.c_str());
378 TACAN::searchChannel (const string& channel)
380 double frequency_khz = 0;
383 = globals->get_channellist()->findByChannel( channel );
384 bool _freq_valid = (freq != NULL);
385 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
387 frequency_khz = freq->get_freq();
388 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
390 if (frequency_khz >= 9620 && frequency_khz <= 121300)
391 return frequency_khz/100;
393 return frequency_khz = 0;
394 } // end TACAN::searchChannel
397 * Listener callback. Maintains channel input properties,
398 * searches new channel frequency, updates _channel and
399 * _frequency and sets boolean _new_frequency appropriately.
402 TACAN::valueChanged(SGPropertyNode *prop)
404 if (_listener_active)
408 int index = prop->getIndex();
409 string channel = _channel;
411 if (index) { // channel digit or X/Y input
413 if (isdigit(c = _channel_in1_node->getStringValue()[0]))
415 if (isdigit(c = _channel_in2_node->getStringValue()[0]))
417 if (isdigit(c = _channel_in3_node->getStringValue()[0]))
419 c = _channel_in4_node->getStringValue()[0];
420 if (c == 'X' || c == 'Y')
423 } else { // channel number input
424 unsigned int f = prop->getIntValue();
425 if (f >= 1 && f <= 126) {
426 channel[0] = '0' + (f / 100) % 10;
427 channel[1] = '0' + (f / 10) % 10;
428 channel[2] = '0' + f % 10;
432 if (channel != _channel) {
433 SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
436 _channel_in0_node->setIntValue((channel[0] - '0') * 100
437 + (channel[1] - '0') * 10 + (channel[2] - '0'));
439 s[0] = channel[0], _channel_in1_node->setStringValue(s);
440 s[0] = channel[1], _channel_in2_node->setStringValue(s);
441 s[0] = channel[2], _channel_in3_node->setStringValue(s);
442 s[0] = channel[3], _channel_in4_node->setStringValue(s);
444 // search channel frequency
445 double freq = searchChannel(channel);
446 if (freq != _frequency_mhz) {
447 SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
448 _frequency_node->setDoubleValue(freq);
449 _frequency_mhz = freq;
450 _new_frequency = true;
454 _time_before_search_sec = 0;