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 SG_LOG( SG_INSTR, SG_DEBUG, " tacan range " << range_nm << " max range " << max_range_nm);
44 return range_nm + (range_nm * rand * rand);
48 TACAN::TACAN ( SGPropertyNode *node ) :
49 _name(node->getStringValue("name", "tacan")),
50 _num(node->getIntValue("number", 0)),
51 _new_frequency(false),
55 _time_before_search_sec(0),
57 _transmitter_valid(false),
58 _transmitter_pos(SGGeod::fromDeg(0, 0)),
59 _transmitter_range_nm(0),
60 _transmitter_bias(0.0),
73 branch = "/instrumentation/" + _name;
75 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
77 _serviceable_node = node->getChild("serviceable", 0, true);
78 _ident_node = node->getChild("ident", 0, true);
80 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
81 _frequency_node = fnode->getChild("selected-mhz", 0, true);
83 _channel_in0_node = fnode->getChild("selected-channel", 0, true);
84 _channel_in1_node = fnode->getChild("selected-channel", 1, true);
85 _channel_in2_node = fnode->getChild("selected-channel", 2, true);
86 _channel_in3_node = fnode->getChild("selected-channel", 3, true);
87 _channel_in4_node = fnode->getChild("selected-channel", 4, true);
89 _channel_in0_node->addChangeListener(this);
90 _channel_in1_node->addChangeListener(this);
91 _channel_in2_node->addChangeListener(this);
92 _channel_in3_node->addChangeListener(this);
93 _channel_in4_node->addChangeListener(this, true);
95 _in_range_node = node->getChild("in-range", 0, true);
96 _distance_node = node->getChild("indicated-distance-nm", 0, true);
97 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
98 _time_node = node->getChild("indicated-time-min", 0, true);
99 _name_node = node->getChild("name", 0, true);
100 _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
102 SGPropertyNode *dnode = node->getChild("display", 0, true);
103 _x_shift_node = dnode->getChild("x-shift", 0, true);
104 _y_shift_node = dnode->getChild("y-shift", 0, true);
105 _rotation_node = dnode->getChild("rotation", 0, true);
106 _channel_node = dnode->getChild("channel", 0, true);
108 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
109 _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
111 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
112 _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
114 SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
115 _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
117 _heading_node = fgGetNode("/orientation/heading-deg", true);
118 _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
124 _time_before_search_sec = 0;
128 TACAN::update (double delta_time_sec)
130 // don't do anything when paused
131 if (delta_time_sec == 0) return;
133 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
134 _last_distance_nm = 0;
135 _in_range_node->setBoolValue(false);
136 _distance_node->setDoubleValue(0);
137 _speed_node->setDoubleValue(0);
138 _time_node->setDoubleValue(0);
139 SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
143 SGGeod pos(globals->get_aircraft_position());
144 // On timeout, scan again
145 _time_before_search_sec -= delta_time_sec;
146 if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
147 search(_frequency_mhz, pos);
149 // Calculate the distance to the transmitter
151 //calculate the bearing and range of the mobile from the aircraft
152 double mobile_az2 = 0;
153 double mobile_bearing = 0;
154 double mobile_distance = 0;
156 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_name " << _mobile_name);
157 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_valid " << _mobile_valid);
158 geo_inverse_wgs_84(pos, _mobilePos,
159 &mobile_bearing, &mobile_az2, &mobile_distance);
162 //calculate the bearing and range of the station from the aircraft
167 geo_inverse_wgs_84(pos, _transmitter_pos,
168 &bearing, &az2, &distance);
172 if ( mobile_distance <= distance && _mobile_valid) {
173 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
174 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
175 bearing = mobile_bearing;
176 distance = mobile_distance;
177 _transmitter_pos.setElevationFt(_mobilePos.getElevationFt());
178 _transmitter_range_nm = _mobile_range_nm;
179 _transmitter_bias = _mobile_bias;
180 _transmitter_name = _mobile_name;
181 _name_node->setStringValue(_transmitter_name.c_str());
182 _transmitter_ident = _mobile_ident;
183 _ident_node->setStringValue(_transmitter_ident.c_str());
184 _channel_node->setStringValue(_channel.c_str());
187 //// calculate some values for boresight display
188 double distance_nm = distance * SG_METER_TO_NM;
190 //// calculate look left/right to target, without yaw correction
191 // double horiz_offset = bearing - heading;
193 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
194 // if (horiz_offset < -180.0) horiz_offset += 360.0;
196 //// now correct look left/right for yaw
197 // horiz_offset += yaw;
199 // use the bearing for a plan position indicator display
201 double horiz_offset = bearing;
203 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
204 << bearing << " horiz_offset " << horiz_offset);
206 // calculate values for radar display
207 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
208 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
210 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
214 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
215 pos.getElevationFt(),
216 _transmitter_range_nm);
218 if (distance_nm <= range_nm) {
219 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
220 ((1 / delta_time_sec) * 3600.0));
221 _last_distance_nm = distance_nm;
223 _in_range_node->setBoolValue(true);
224 double tmp_dist = distance_nm - _transmitter_bias;
225 if ( tmp_dist < 0.0 ) {
228 _distance_node->setDoubleValue( tmp_dist );
229 _speed_node->setDoubleValue(speed_kt);
230 _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0);
231 _bearing_node->setDoubleValue(bearing);
232 _x_shift_node->setDoubleValue(x_shift);
233 _y_shift_node->setDoubleValue(y_shift);
234 _rotation_node->setDoubleValue(rotation);
236 _last_distance_nm = 0;
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);
247 // If we can't find a valid station set everything to zero
248 if (!_transmitter_valid && !_mobile_valid ) {
249 _in_range_node->setBoolValue(false);
250 _distance_node->setDoubleValue(0);
251 _speed_node->setDoubleValue(0);
252 _time_node->setDoubleValue(0);
253 _bearing_node->setDoubleValue(0);
254 _x_shift_node->setDoubleValue(0);
255 _y_shift_node->setDoubleValue(0);
256 _rotation_node->setDoubleValue(0);
257 _transmitter_name = "";
258 _name_node->setStringValue(_transmitter_name.c_str());
259 _transmitter_ident = "";
260 _ident_node->setStringValue(_transmitter_ident.c_str());
261 _channel_node->setStringValue(_channel.c_str());
264 } // end function update
267 TACAN::search (double frequency_mhz,const SGGeod& pos)
270 _mobile_valid = false;
272 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
275 _time_before_search_sec = 1.0;
277 //try any carriers first
278 FGNavRecord *mobile_tacan = FGNavList::findByFreq( frequency_mhz, FGNavList::carrierFilter() );
279 bool freq_valid = (mobile_tacan != NULL);
280 SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
284 string str1( mobile_tacan->name() );
286 SGPropertyNode * branch = fgGetNode("ai/models", true);
287 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
289 number = carrier.size();
291 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
292 for ( i = 0; i < number; ++i ) {
293 string str2 ( carrier[i]->getStringValue("name", ""));
294 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
296 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
297 string::size_type loc1= str1.find( str2, 0 );
298 if ( loc1 != string::npos && str2 != "" ) {
299 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
300 _mobilePos = SGGeod::fromDegFt(
301 carrier[i]->getDoubleValue("position/longitude-deg"),
302 carrier[i]->getDoubleValue("position/latitude-deg"),
303 mobile_tacan->get_elev_ft());
304 _mobile_range_nm = mobile_tacan->get_range();
305 _mobile_bias = mobile_tacan->get_multiuse();
306 _mobile_name = mobile_tacan->name();
307 _mobile_ident = mobile_tacan->get_trans_ident();
308 _mobile_valid = true;
309 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
312 _mobile_valid = false;
313 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
317 //try any AI tankers second
319 if ( !_mobile_valid) {
320 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
322 SGPropertyNode * branch = fgGetNode("ai/models", true);
323 vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
325 number = tanker.size();
327 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
329 for ( i = 0; i < number; ++i ) {
330 string str4 ( tanker[i]->getStringValue("callsign", ""));
331 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
333 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
334 string::size_type loc1= str1.find( str4, 0 );
335 if ( loc1 != string::npos && str4 != "" ) {
336 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
337 _mobilePos = SGGeod::fromDegFt(
338 tanker[i]->getDoubleValue("position/longitude-deg"),
339 tanker[i]->getDoubleValue("position/latitude-deg"),
340 tanker[i]->getDoubleValue("position/altitude-ft"));
343 _mobile_range_nm = mobile_tacan->get_range();
344 _mobile_bias = mobile_tacan->get_multiuse();
345 _mobile_name = mobile_tacan->name();
346 _mobile_ident = mobile_tacan->get_trans_ident();
347 _mobile_valid = true;
348 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
351 _mobile_valid = false;
352 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
357 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
359 if ( !_mobile_valid ) {
360 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
362 SGPropertyNode * branch = fgGetNode("ai/models", true);
363 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
365 number = mp_tanker.size();
367 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
369 if ( number > 0 ) { // don't do this if there are no MP aircraft
370 for ( i = 0; i < number; ++i ) {
371 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
372 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
374 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
375 string::size_type loc1= str1.find( str6, 0 );
376 if ( loc1 != string::npos && str6 != "" ) {
377 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
378 _mobilePos = SGGeod::fromDegFt(
379 mp_tanker[i]->getDoubleValue("position/longitude-deg"),
380 mp_tanker[i]->getDoubleValue("position/latitude-deg"),
381 mp_tanker[i]->getDoubleValue("position/altitude-ft"));
384 _mobile_range_nm = mobile_tacan->get_range();
385 _mobile_bias = mobile_tacan->get_multiuse();
386 _mobile_name = mobile_tacan->name();
387 _mobile_ident = mobile_tacan->get_trans_ident();
388 _mobile_valid = true;
390 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
391 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker name " << _mobile_name);
392 SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
395 _mobile_valid = false;
396 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
402 _mobile_valid = false;
403 SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
406 // try the TACAN/VORTAC list next
407 FGNavRecord *tacan = FGNavList::findByFreq( frequency_mhz, pos, FGNavList::tacanFilter() );
409 _transmitter_valid = (tacan != NULL);
411 if ( _transmitter_valid ) {
412 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
414 _transmitter_pos = tacan->geod();
415 _transmitter_range_nm = tacan->get_range();
416 _transmitter_bias = tacan->get_multiuse();
417 _transmitter_name = tacan->name();
418 _name_node->setStringValue(_transmitter_name.c_str());
419 _transmitter_ident = tacan->get_trans_ident();
420 _ident_node->setStringValue(_transmitter_ident.c_str());
422 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
423 SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
426 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
431 TACAN::searchChannel (const string& channel)
433 double frequency_khz = 0;
436 = globals->get_channellist()->findByChannel( channel );
437 bool _freq_valid = (freq != NULL);
438 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
440 frequency_khz = freq->get_freq();
441 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
443 if (frequency_khz >= 9620 && frequency_khz <= 121300)
444 return frequency_khz/100;
446 return frequency_khz = 0;
447 } // end TACAN::searchChannel
450 * Listener callback. Maintains channel input properties,
451 * searches new channel frequency, updates _channel and
452 * _frequency and sets boolean _new_frequency appropriately.
455 TACAN::valueChanged(SGPropertyNode *prop)
457 if (_listener_active)
461 int index = prop->getIndex();
462 string channel = _channel;
464 if (index) { // channel digit or X/Y input
466 if (isdigit(c = _channel_in1_node->getStringValue()[0]))
468 if (isdigit(c = _channel_in2_node->getStringValue()[0]))
470 if (isdigit(c = _channel_in3_node->getStringValue()[0]))
472 c = _channel_in4_node->getStringValue()[0];
473 if (c == 'X' || c == 'Y')
476 } else { // channel number input
477 unsigned int f = prop->getIntValue();
478 if (f >= 1 && f <= 126) {
479 channel[0] = '0' + (f / 100) % 10;
480 channel[1] = '0' + (f / 10) % 10;
481 channel[2] = '0' + f % 10;
485 if (channel != _channel) {
486 SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
489 _channel_in0_node->setIntValue((channel[0] - '0') * 100
490 + (channel[1] - '0') * 10 + (channel[2] - '0'));
492 s[0] = channel[0], _channel_in1_node->setStringValue(s);
493 s[0] = channel[1], _channel_in2_node->setStringValue(s);
494 s[0] = channel[2], _channel_in3_node->setStringValue(s);
495 s[0] = channel[3], _channel_in4_node->setStringValue(s);
497 // search channel frequency
498 double freq = searchChannel(channel);
499 if (freq != _frequency_mhz) {
500 SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
501 _frequency_node->setDoubleValue(freq);
502 _frequency_mhz = freq;
503 _new_frequency = true;
507 _time_before_search_sec = 0;