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),
75 branch = "/instrumentation/" + _name;
77 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
79 _serviceable_node = node->getChild("serviceable", 0, true);
80 _ident_node = node->getChild("ident", 0, true);
82 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
83 _frequency_node = fnode->getChild("selected-mhz", 0, true);
85 _channel_in0_node = fnode->getChild("selected-channel", 0, true);
86 _channel_in1_node = fnode->getChild("selected-channel", 1, true);
87 _channel_in2_node = fnode->getChild("selected-channel", 2, true);
88 _channel_in3_node = fnode->getChild("selected-channel", 3, true);
89 _channel_in4_node = fnode->getChild("selected-channel", 4, true);
91 _channel_in0_node->addChangeListener(this);
92 _channel_in1_node->addChangeListener(this);
93 _channel_in2_node->addChangeListener(this);
94 _channel_in3_node->addChangeListener(this);
95 _channel_in4_node->addChangeListener(this, true);
97 _in_range_node = node->getChild("in-range", 0, true);
98 _distance_node = node->getChild("indicated-distance-nm", 0, true);
99 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
100 _time_node = node->getChild("indicated-time-min", 0, true);
101 _name_node = node->getChild("name", 0, true);
102 _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
104 SGPropertyNode *dnode = node->getChild("display", 0, true);
105 _x_shift_node = dnode->getChild("x-shift", 0, true);
106 _y_shift_node = dnode->getChild("y-shift", 0, true);
107 _rotation_node = dnode->getChild("rotation", 0, true);
108 _channel_node = dnode->getChild("channel", 0, true);
110 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
111 _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
113 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
114 _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
116 SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
117 _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
119 _longitude_node = fgGetNode("/position/longitude-deg", true);
120 _latitude_node = fgGetNode("/position/latitude-deg", true);
121 _altitude_node = fgGetNode("/position/altitude-ft", true);
122 _heading_node = fgGetNode("/orientation/heading-deg", true);
123 _yaw_node = fgGetNode("/orientation/side-slip-deg", true);
124 _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
130 _time_before_search_sec = 0;
134 TACAN::update (double delta_time_sec)
136 // don't do anything when paused
137 if (delta_time_sec == 0) return;
139 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
140 _last_distance_nm = 0;
141 _in_range_node->setBoolValue(false);
142 _distance_node->setDoubleValue(0);
143 _speed_node->setDoubleValue(0);
144 _time_node->setDoubleValue(0);
145 SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
149 // Get the aircraft position
150 double longitude_deg = _longitude_node->getDoubleValue();
151 double latitude_deg = _latitude_node->getDoubleValue();
152 double altitude_m = _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
153 double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
154 double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
156 // On timeout, scan again
157 _time_before_search_sec -= delta_time_sec;
158 if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
159 search(_frequency_mhz, longitude_rad, latitude_rad, altitude_m);
161 // Calculate the distance to the transmitter
163 //calculate the bearing and range of the mobile from the aircraft
164 double mobile_az2 = 0;
165 double mobile_bearing = 0;
166 double mobile_distance = 0;
168 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_lat " << _mobile_lat);
169 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_lon " << _mobile_lon);
170 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_name " << _mobile_name);
171 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_valid " << _mobile_valid);
172 geo_inverse_wgs_84(altitude_m,
177 &mobile_bearing, &mobile_az2, &mobile_distance);
180 //calculate the bearing and range of the station from the aircraft
185 SGGeod pos = SGGeod::fromDegM(longitude_deg, latitude_deg, altitude_m);
186 geo_inverse_wgs_84(pos, _transmitter_pos,
187 &bearing, &az2, &distance);
191 if ( mobile_distance <= distance && _mobile_valid) {
192 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
193 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
194 bearing = mobile_bearing;
195 distance = mobile_distance;
196 _transmitter_pos.setElevationFt(_mobile_elevation_ft);
197 _transmitter_range_nm = _mobile_range_nm;
198 _transmitter_bias = _mobile_bias;
199 _transmitter_name = _mobile_name;
200 _name_node->setStringValue(_transmitter_name.c_str());
201 _transmitter_ident = _mobile_ident;
202 _ident_node->setStringValue(_transmitter_ident.c_str());
203 _channel_node->setStringValue(_channel.c_str());
206 //// calculate some values for boresight display
207 double distance_nm = distance * SG_METER_TO_NM;
209 //// calculate look left/right to target, without yaw correction
210 // double horiz_offset = bearing - heading;
212 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
213 // if (horiz_offset < -180.0) horiz_offset += 360.0;
215 //// now correct look left/right for yaw
216 // horiz_offset += yaw;
218 // use the bearing for a plan position indicator display
220 double horiz_offset = bearing;
222 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
223 << bearing << " horiz_offset " << horiz_offset);
225 // calculate values for radar display
226 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
227 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
229 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
233 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
234 altitude_m * SG_METER_TO_FEET,
235 _transmitter_range_nm);
237 if (distance_nm <= range_nm) {
238 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
239 ((1 / delta_time_sec) * 3600.0));
240 _last_distance_nm = distance_nm;
242 _in_range_node->setBoolValue(true);
243 double tmp_dist = distance_nm - _transmitter_bias;
244 if ( tmp_dist < 0.0 ) {
247 _distance_node->setDoubleValue( tmp_dist );
248 _speed_node->setDoubleValue(speed_kt);
249 _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0);
250 _bearing_node->setDoubleValue(bearing);
251 _x_shift_node->setDoubleValue(x_shift);
252 _y_shift_node->setDoubleValue(y_shift);
253 _rotation_node->setDoubleValue(rotation);
255 _last_distance_nm = 0;
256 _in_range_node->setBoolValue(false);
257 _distance_node->setDoubleValue(0);
258 _speed_node->setDoubleValue(0);
259 _time_node->setDoubleValue(0);
260 _bearing_node->setDoubleValue(0);
261 _x_shift_node->setDoubleValue(0);
262 _y_shift_node->setDoubleValue(0);
263 _rotation_node->setDoubleValue(0);
266 // If we can't find a valid station set everything to zero
267 if (!_transmitter_valid && !_mobile_valid ) {
268 _in_range_node->setBoolValue(false);
269 _distance_node->setDoubleValue(0);
270 _speed_node->setDoubleValue(0);
271 _time_node->setDoubleValue(0);
272 _bearing_node->setDoubleValue(0);
273 _x_shift_node->setDoubleValue(0);
274 _y_shift_node->setDoubleValue(0);
275 _rotation_node->setDoubleValue(0);
276 _transmitter_name = "";
277 _name_node->setStringValue(_transmitter_name.c_str());
278 _transmitter_ident = "";
279 _ident_node->setStringValue(_transmitter_ident.c_str());
280 _channel_node->setStringValue(_channel.c_str());
283 } // end function update
286 TACAN::search (double frequency_mhz, double longitude_rad,
287 double latitude_rad, double altitude_m)
290 _mobile_valid = false;
292 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
295 _time_before_search_sec = 1.0;
297 //try any carriers first
298 FGNavRecord *mobile_tacan
299 = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
300 bool freq_valid = (mobile_tacan != NULL);
301 SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
305 string str1( mobile_tacan->name() );
307 SGPropertyNode * branch = fgGetNode("ai/models", true);
308 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
310 number = carrier.size();
312 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
313 for ( i = 0; i < number; ++i ) {
314 string str2 ( carrier[i]->getStringValue("name", ""));
315 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
317 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
318 string::size_type loc1= str1.find( str2, 0 );
319 if ( loc1 != string::npos && str2 != "" ) {
320 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
321 _mobile_lat = carrier[i]->getDoubleValue("position/latitude-deg");
322 _mobile_lon = carrier[i]->getDoubleValue("position/longitude-deg");
323 _mobile_elevation_ft = mobile_tacan->get_elev_ft();
324 _mobile_range_nm = mobile_tacan->get_range();
325 _mobile_bias = mobile_tacan->get_multiuse();
326 _mobile_name = mobile_tacan->name();
327 _mobile_ident = mobile_tacan->get_trans_ident();
328 _mobile_valid = true;
329 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
332 _mobile_valid = false;
333 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
337 //try any AI tankers second
339 if ( !_mobile_valid) {
340 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
342 SGPropertyNode * branch = fgGetNode("ai/models", true);
343 vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
345 number = tanker.size();
347 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
349 for ( i = 0; i < number; ++i ) {
350 string str4 ( tanker[i]->getStringValue("callsign", ""));
351 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
353 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
354 string::size_type loc1= str1.find( str4, 0 );
355 if ( loc1 != string::npos && str4 != "" ) {
356 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
357 _mobile_lat = tanker[i]->getDoubleValue("position/latitude-deg");
358 _mobile_lon = tanker[i]->getDoubleValue("position/longitude-deg");
359 _mobile_elevation_ft = tanker[i]->getDoubleValue("position/altitude-ft");
360 _mobile_range_nm = mobile_tacan->get_range();
361 _mobile_bias = mobile_tacan->get_multiuse();
362 _mobile_name = mobile_tacan->name();
363 _mobile_ident = mobile_tacan->get_trans_ident();
364 _mobile_valid = true;
365 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
368 _mobile_valid = false;
369 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
374 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
376 if ( !_mobile_valid ) {
377 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
379 SGPropertyNode * branch = fgGetNode("ai/models", true);
380 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
382 number = mp_tanker.size();
384 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
386 if ( number > 0 ) { // don't do this if there are no MP aircraft
387 for ( i = 0; i < number; ++i ) {
388 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
389 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
391 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
392 string::size_type loc1= str1.find( str6, 0 );
393 if ( loc1 != string::npos && str6 != "" ) {
394 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
395 _mobile_lat = mp_tanker[i]->getDoubleValue("position/latitude-deg");
396 _mobile_lon = mp_tanker[i]->getDoubleValue("position/longitude-deg");
397 _mobile_elevation_ft = mp_tanker[i]->getDoubleValue("position/altitude-ft");
398 _mobile_range_nm = mobile_tacan->get_range();
399 _mobile_bias = mobile_tacan->get_multiuse();
400 _mobile_name = mobile_tacan->name();
401 _mobile_ident = mobile_tacan->get_trans_ident();
402 _mobile_valid = true;
404 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
405 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker name " << _mobile_name);
406 SG_LOG( SG_INSTR, SG_DEBUG, " mp lat " << _mobile_lat << "lon " << _mobile_lon);
407 SG_LOG( SG_INSTR, SG_DEBUG, " mp elev " << _mobile_elevation_ft);
408 SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
411 _mobile_valid = false;
412 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
418 _mobile_valid = false;
419 SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
422 // try the TACAN/VORTAC list next
423 FGNavRecord *tacan = globals->get_tacanlist()->findByFreq( frequency_mhz,
424 SGGeod::fromRadM(longitude_rad, latitude_rad, altitude_m));
426 _transmitter_valid = (tacan != NULL);
428 if ( _transmitter_valid ) {
429 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
431 _transmitter_pos = tacan->geod();
432 _transmitter_range_nm = tacan->get_range();
433 _transmitter_bias = tacan->get_multiuse();
434 _transmitter_name = tacan->name();
435 _name_node->setStringValue(_transmitter_name.c_str());
436 _transmitter_ident = tacan->get_trans_ident();
437 _ident_node->setStringValue(_transmitter_ident.c_str());
439 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
440 SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
443 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
448 TACAN::searchChannel (const string& channel)
450 double frequency_khz = 0;
453 = globals->get_channellist()->findByChannel( channel );
454 bool _freq_valid = (freq != NULL);
455 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
457 frequency_khz = freq->get_freq();
458 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
460 if (frequency_khz >= 9620 && frequency_khz <= 121300)
461 return frequency_khz/100;
463 return frequency_khz = 0;
464 } // end TACAN::searchChannel
467 * Listener callback. Maintains channel input properties,
468 * searches new channel frequency, updates _channel and
469 * _frequency and sets boolean _new_frequency appropriately.
472 TACAN::valueChanged(SGPropertyNode *prop)
474 if (_listener_active)
478 int index = prop->getIndex();
479 string channel = _channel;
481 if (index) { // channel digit or X/Y input
483 if (isdigit(c = _channel_in1_node->getStringValue()[0]))
485 if (isdigit(c = _channel_in2_node->getStringValue()[0]))
487 if (isdigit(c = _channel_in3_node->getStringValue()[0]))
489 c = _channel_in4_node->getStringValue()[0];
490 if (c == 'X' || c == 'Y')
493 } else { // channel number input
494 unsigned int f = prop->getIntValue();
495 if (f >= 1 && f <= 126) {
496 channel[0] = '0' + (f / 100) % 10;
497 channel[1] = '0' + (f / 10) % 10;
498 channel[2] = '0' + f % 10;
502 if (channel != _channel) {
503 SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
506 _channel_in0_node->setIntValue((channel[0] - '0') * 100
507 + (channel[1] - '0') * 10 + (channel[2] - '0'));
509 s[0] = channel[0], _channel_in1_node->setStringValue(s);
510 s[0] = channel[1], _channel_in2_node->setStringValue(s);
511 s[0] = channel[2], _channel_in3_node->setStringValue(s);
512 s[0] = channel[3], _channel_in4_node->setStringValue(s);
514 // search channel frequency
515 double freq = searchChannel(channel);
516 if (freq != _frequency_mhz) {
517 SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
518 _frequency_node->setDoubleValue(freq);
519 _frequency_mhz = freq;
520 _new_frequency = true;
524 _time_before_search_sec = 0;