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 : _last_distance_nm(0),
50 _last_frequency_mhz(-1),
51 _time_before_search_sec(0),
53 _transmitter_valid(false),
54 _transmitter_pos(SGGeod::fromDeg(0, 0)),
55 _transmitter_range_nm(0),
56 _transmitter_bias(0.0),
62 for ( i = 0; i < node->nChildren(); ++i ) {
63 SGPropertyNode *child = node->getChild(i);
64 string cname = child->getName();
65 string cval = child->getStringValue();
66 if ( cname == "name" ) {
68 } else if ( cname == "number" ) {
69 _num = child->getIntValue();
71 SG_LOG( SG_INSTR, SG_DEBUG, "Error in TACAN config logic" );
72 if ( _name.length() ) {
73 SG_LOG( SG_INSTR, SG_DEBUG, "Section = " << _name );
80 : _last_distance_nm(0),
81 _last_frequency_mhz(-1),
82 _time_before_search_sec(0),
84 _transmitter_valid(false),
85 _transmitter_pos(SGGeod::fromDeg(0, 0)),
86 _transmitter_range_nm(0),
87 _transmitter_bearing_deg(0),
88 _transmitter_bias(0.0),
89 _transmitter_name(""),
103 branch = "/instrumentation/" + _name;
105 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
107 _longitude_node = fgGetNode("/position/longitude-deg", true);
108 _latitude_node = fgGetNode("/position/latitude-deg", true);
109 _altitude_node = fgGetNode("/position/altitude-ft", true);
110 _heading_node = fgGetNode("/orientation/heading-deg", true);
111 _yaw_node = fgGetNode("/orientation/side-slip-deg", true);
112 _serviceable_node = node->getChild("serviceable", 0, true);
113 _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
114 _ident_node = node->getChild("ident", 0, true);
115 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
116 _source_node = fnode->getChild("source", 0, true);
117 _frequency_node = fnode->getChild("selected-mhz", 0, true);
118 _channel_node = fnode->getChild("selected-channel", 0, true);
119 fnode->getChild("selected-channel", 1, true);
120 fnode->getChild("selected-channel", 2, true);
121 fnode->getChild("selected-channel", 3, true);
122 fnode->getChild("selected-channel", 4, true);
123 _in_range_node = node->getChild("in-range", 0, true);
124 _distance_node = node->getChild("indicated-distance-nm", 0, true);
125 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
126 _time_node = node->getChild("indicated-time-min", 0, true);
127 _name_node = node->getChild("name", 0, true);
128 _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
129 SGPropertyNode *dnode = node->getChild("display", 0, true);
130 _x_shift_node = dnode->getChild("x-shift", 0, true);
131 _y_shift_node = dnode->getChild("y-shift", 0, true);
132 _rotation_node = dnode->getChild("rotation", 0, true);
133 _channel_node = dnode->getChild("channel", 0, true);
135 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
136 _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
138 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
139 _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
141 SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
142 _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
146 TACAN::update (double delta_time_sec)
151 double mobile_az2 = 0;
152 double mobile_bearing = 0;
153 double mobile_distance = 0;
154 double frequency_mhz = 0;
156 // If it's off, don't waste any time.
157 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
158 _last_distance_nm = 0;
159 _in_range_node->setBoolValue(false);
160 _distance_node->setDoubleValue(0);
161 _speed_node->setDoubleValue(0);
162 _time_node->setDoubleValue(0);
163 SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
167 // Figure out the source
168 const char * source = _source_node->getStringValue();
170 if (source[0] == '\0') {
172 branch = "/instrumentation/" + _name + "/frequencies/selected-channel";
173 _source_node->setStringValue(branch.c_str());
174 source = _source_node->getStringValue();
175 SG_LOG( SG_INSTR, SG_DEBUG, "source " << source );
177 // Get the channel FIXME always from instrument #1 ??
178 string channel_1 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[1]");
179 string channel_2 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[2]");
180 string channel_3 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[3]");
181 string channel_4 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[4]");
183 SG_LOG( SG_INSTR, SG_DEBUG, "channels " << channel_1 << channel_2 << channel_3 << channel_4);
185 string channel = channel_1 + channel_2 + channel_3 + channel_4;
188 frequency_mhz = _last_frequency_mhz;
189 if (channel != _last_channel) {
190 _time_before_search_sec = 0;
191 _last_channel = channel;
192 frequency_mhz = searchChannel(channel);
193 SG_LOG( SG_INSTR, SG_DEBUG, "frequency " << frequency_mhz );
194 _frequency_node->setDoubleValue(frequency_mhz);
197 SG_LOG( SG_INSTR, SG_DEBUG, "channel " << channel );
198 // Get the aircraft position
199 double longitude_deg = _longitude_node->getDoubleValue();
200 double latitude_deg = _latitude_node->getDoubleValue();
201 double altitude_m = _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
202 double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
203 double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
205 // On timeout, scan again
206 _time_before_search_sec -= delta_time_sec;
207 if (_time_before_search_sec < 0 && frequency_mhz >= 0
208 || frequency_mhz != _last_frequency_mhz) {
209 search(frequency_mhz, longitude_rad, latitude_rad, altitude_m);
210 _last_frequency_mhz = frequency_mhz;
213 // Calculate the distance to the transmitter
215 //calculate the bearing and range of the mobile from the aircraft
216 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_lat " << _mobile_lat);
217 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_lon " << _mobile_lon);
218 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_name " << _mobile_name);
219 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_valid " << _mobile_valid);
220 geo_inverse_wgs_84(altitude_m,
225 &mobile_bearing, &mobile_az2, &mobile_distance);
227 //calculate the bearing and range of the station from the aircraft
228 SGGeod pos = SGGeod::fromDegM(longitude_deg, latitude_deg, altitude_m);
229 geo_inverse_wgs_84(pos, _transmitter_pos,
230 &bearing, &az2, &distance);
234 if ( mobile_distance <= distance && _mobile_valid) {
235 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
236 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
237 bearing = mobile_bearing;
238 distance = mobile_distance;
239 _transmitter_pos.setElevationFt(_mobile_elevation_ft);
240 _transmitter_range_nm = _mobile_range_nm;
241 _transmitter_bias = _mobile_bias;
242 _transmitter_name = _mobile_name;
243 _name_node->setStringValue(_transmitter_name.c_str());
244 _transmitter_ident = _mobile_ident;
245 _ident_node->setStringValue(_transmitter_ident.c_str());
246 _channel_node->setStringValue(channel.c_str());
249 //// calculate some values for boresight display
250 double distance_nm = distance * SG_METER_TO_NM;
252 //// calculate look left/right to target, without yaw correction
253 // double horiz_offset = bearing - heading;
255 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
256 // if (horiz_offset < -180.0) horiz_offset += 360.0;
258 //// now correct look left/right for yaw
259 // horiz_offset += yaw;
261 // use the bearing for a plan position indicator display
263 double horiz_offset = bearing;
265 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
266 << bearing << " horiz_offset " << horiz_offset);
268 // calculate values for radar display
269 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
270 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
272 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
276 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
277 altitude_m * SG_METER_TO_FEET,
278 _transmitter_range_nm);
280 if (distance_nm <= range_nm) {
281 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
282 ((1 / delta_time_sec) * 3600.0));
283 _last_distance_nm = distance_nm;
285 _in_range_node->setBoolValue(true);
286 double tmp_dist = distance_nm - _transmitter_bias;
287 if ( tmp_dist < 0.0 ) {
290 _distance_node->setDoubleValue( tmp_dist );
291 _speed_node->setDoubleValue(speed_kt);
292 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
293 _bearing_node->setDoubleValue(bearing);
294 _x_shift_node->setDoubleValue(x_shift);
295 _y_shift_node->setDoubleValue(y_shift);
296 _rotation_node->setDoubleValue(rotation);
298 _last_distance_nm = 0;
299 _in_range_node->setBoolValue(false);
300 _distance_node->setDoubleValue(0);
301 _speed_node->setDoubleValue(0);
302 _time_node->setDoubleValue(0);
303 _bearing_node->setDoubleValue(0);
304 _x_shift_node->setDoubleValue(0);
305 _y_shift_node->setDoubleValue(0);
306 _rotation_node->setDoubleValue(0);
309 // If we can't find a valid station set everything to zero
310 if (!_transmitter_valid && !_mobile_valid ) {
311 _in_range_node->setBoolValue(false);
312 _distance_node->setDoubleValue(0);
313 _speed_node->setDoubleValue(0);
314 _time_node->setDoubleValue(0);
315 _bearing_node->setDoubleValue(0);
316 _x_shift_node->setDoubleValue(0);
317 _y_shift_node->setDoubleValue(0);
318 _rotation_node->setDoubleValue(0);
319 _transmitter_name = "";
320 _name_node->setStringValue(_transmitter_name.c_str());
321 _transmitter_ident = "";
322 _ident_node->setStringValue(_transmitter_ident.c_str());
323 _channel_node->setStringValue(channel.c_str());
326 } // end function update
329 TACAN::search (double frequency_mhz, double longitude_rad,
330 double latitude_rad, double altitude_m)
334 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
337 _time_before_search_sec = 1.0;
339 //try any carriers first
340 FGNavRecord *mobile_tacan
341 = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
342 bool freq_valid = (mobile_tacan != NULL);
343 SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
347 string str1( mobile_tacan->get_name() );
349 SGPropertyNode * branch = fgGetNode("ai/models", true);
350 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
352 number = carrier.size();
354 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
356 for ( i = 0; i < number; ++i ) {
357 string str2 ( carrier[i]->getStringValue("name", ""));
358 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
360 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
361 unsigned int loc1= str1.find( str2, 0 );
362 if ( loc1 != string::npos && str2 != "" ) {
363 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
364 _mobile_lat = carrier[i]->getDoubleValue("position/latitude-deg");
365 _mobile_lon = carrier[i]->getDoubleValue("position/longitude-deg");
366 _mobile_elevation_ft = mobile_tacan->get_elev_ft();
367 _mobile_range_nm = mobile_tacan->get_range();
368 _mobile_bias = mobile_tacan->get_multiuse();
369 _mobile_name = mobile_tacan->get_name();
370 _mobile_ident = mobile_tacan->get_trans_ident();
371 _mobile_valid = true;
372 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
375 _mobile_valid = false;
376 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
380 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _mobile_name);
381 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
382 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
384 //try any AI tankers second
386 if ( !_mobile_valid) {
387 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
389 SGPropertyNode * branch = fgGetNode("ai/models", true);
390 vector<SGPropertyNode_ptr> tanker = branch->getChildren("aircraft");
392 number = tanker.size();
394 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
396 for ( i = 0; i < number; ++i ) {
397 string str4 ( tanker[i]->getStringValue("callsign", ""));
398 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
400 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
401 unsigned int loc1= str1.find( str4, 0 );
402 if ( loc1 != string::npos && str4 != "" ) {
403 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
404 _mobile_lat = tanker[i]->getDoubleValue("position/latitude-deg");
405 _mobile_lon = tanker[i]->getDoubleValue("position/longitude-deg");
406 _mobile_elevation_ft = tanker[i]->getDoubleValue("position/altitude-ft");
407 _mobile_range_nm = mobile_tacan->get_range();
408 _mobile_bias = mobile_tacan->get_multiuse();
409 _mobile_name = mobile_tacan->get_name();
410 _mobile_ident = mobile_tacan->get_trans_ident();
411 _mobile_valid = true;
412 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
415 _mobile_valid = false;
416 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
420 SG_LOG( SG_INSTR, SG_DEBUG, "tanker name " << _mobile_name);
421 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
422 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
423 SG_LOG( SG_INSTR, SG_DEBUG, "range " << _mobile_range_nm);
426 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
428 if ( !_mobile_valid ) {
429 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
431 SGPropertyNode * branch = fgGetNode("ai/models", true);
432 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
434 number = mp_tanker.size();
436 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
438 for ( i = 0; i < number; ++i ) {
439 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
440 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
442 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
443 unsigned int loc1= str1.find( str6, 0 );
444 if ( loc1 != string::npos && str6 != "" ) {
445 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
446 _mobile_lat = mp_tanker[i]->getDoubleValue("position/latitude-deg");
447 _mobile_lon = mp_tanker[i]->getDoubleValue("position/longitude-deg");
448 _mobile_elevation_ft = mp_tanker[i]->getDoubleValue("position/altitude-ft");
449 _mobile_range_nm = mobile_tacan->get_range();
450 _mobile_bias = mobile_tacan->get_multiuse();
451 _mobile_name = mobile_tacan->get_name();
452 _mobile_ident = mobile_tacan->get_trans_ident();
453 _mobile_valid = true;
455 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
456 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker name " << _mobile_name);
457 SG_LOG( SG_INSTR, SG_DEBUG, " mp lat " << _mobile_lat << "lon " << _mobile_lon);
458 SG_LOG( SG_INSTR, SG_DEBUG, " mp elev " << _mobile_elevation_ft);
459 SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
462 _mobile_valid = false;
463 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
468 _mobile_valid = false;
469 SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
472 // try the TACAN/VORTAC list next
474 = globals->get_tacanlist()->findByFreq( frequency_mhz, longitude_rad,
475 latitude_rad, altitude_m);
477 _transmitter_valid = (tacan != NULL);
479 if ( _transmitter_valid ) {
480 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
482 _transmitter_pos = tacan->get_pos();
483 _transmitter_range_nm = tacan->get_range();
484 _transmitter_bias = tacan->get_multiuse();
485 _transmitter_name = tacan->get_name();
486 _name_node->setStringValue(_transmitter_name.c_str());
487 _transmitter_ident = tacan->get_trans_ident();
488 _ident_node->setStringValue(_transmitter_ident.c_str());
490 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
491 SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
494 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
499 TACAN::searchChannel (const string& channel)
501 double frequency_khz = 0;
504 = globals->get_channellist()->findByChannel( channel );
505 bool _freq_valid = (freq != NULL);
506 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
508 frequency_khz = freq->get_freq();
509 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
511 if (frequency_khz >= 9620 && frequency_khz <= 121300)
512 return frequency_khz/100;
514 return frequency_khz = 0;
515 } // end TACAN::searchChannel