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(""),
102 branch = "/instrumentation/" + name;
104 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
106 _longitude_node = fgGetNode("/position/longitude-deg", true);
107 _latitude_node = fgGetNode("/position/latitude-deg", true);
108 _altitude_node = fgGetNode("/position/altitude-ft", true);
109 _heading_node = fgGetNode("/orientation/heading-deg", true);
110 _yaw_node = fgGetNode("/orientation/side-slip-deg", true);
111 _serviceable_node = node->getChild("serviceable", 0, true);
112 _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
113 _ident_node = node->getChild("ident", 0, true);
114 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
115 _source_node = fnode->getChild("source", 0, true);
116 _frequency_node = fnode->getChild("selected-mhz", 0, true);
117 _channel_node = fnode->getChild("selected-channel", 1, true);
118 _channel_node = fnode->getChild("selected-channel", 2, true);
119 _channel_node = fnode->getChild("selected-channel", 3, true);
120 _channel_node = fnode->getChild("selected-channel", 4, true);
121 _in_range_node = node->getChild("in-range", 0, true);
122 _distance_node = node->getChild("indicated-distance-nm", 0, true);
123 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
124 _time_node = node->getChild("indicated-time-min", 0, true);
125 _name_node = node->getChild("name", 0, true);
126 _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
127 SGPropertyNode *dnode = node->getChild("display", 0, true);
128 _x_shift_node = dnode->getChild("x-shift", 0, true);
129 _y_shift_node = dnode->getChild("y-shift", 0, true);
130 _rotation_node = dnode->getChild("rotation", 0, true);
131 _channel_node = dnode->getChild("channel", 0, true);
133 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", num, false );
135 _carrier_name_node = cnode->getChild("name", 0, false);
137 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", num, false);
139 _tanker_callsign_node = tnode->getChild("callsign", 0, false);
141 SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", num, false);
143 _mp_callsign_node = mnode->getChild("callsign", 0, false);
147 TACAN::update (double delta_time_sec)
152 double mobile_az2 = 0;
153 double mobile_bearing = 0;
154 double mobile_distance = 0;
155 double frequency_mhz = 0;
157 string _channel, _last_channel, _channel_1, _channel_2,_channel_3, _channel_4;
159 // If it's off, don't waste any time.
160 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
161 _last_distance_nm = 0;
162 _in_range_node->setBoolValue(false);
163 _distance_node->setDoubleValue(0);
164 _speed_node->setDoubleValue(0);
165 _time_node->setDoubleValue(0);
166 SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
170 // Figure out the source
171 const char * source = _source_node->getStringValue();
173 if (source[0] == '\0') {
175 branch = "/instrumentation/" + name + "/frequencies/selected-channel";
176 _source_node->setStringValue(branch.c_str());
177 source = _source_node->getStringValue();
178 SG_LOG( SG_INSTR, SG_DEBUG, "source " << source );
181 _channel_1 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[1]");
182 _channel_2 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[2]");
183 _channel_3 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[3]");
184 _channel_4 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[4]");
186 SG_LOG( SG_INSTR, SG_DEBUG, "channels " << _channel_1 << _channel_2 << _channel_3 << _channel_4);
188 _channel = _channel_1 + _channel_2 + _channel_3 + _channel_4;
191 if (_channel != _last_channel) {
192 _time_before_search_sec = 0;
193 _last_channel = _channel;
194 frequency_mhz = searchChannel(_channel);
195 SG_LOG( SG_INSTR, SG_DEBUG, "frequency " << frequency_mhz );
196 _frequency_node->setDoubleValue(frequency_mhz);
199 SG_LOG( SG_INSTR, SG_DEBUG, "channel " << _channel );
200 // Get the aircraft position
201 double longitude_deg = _longitude_node->getDoubleValue();
202 double latitude_deg = _latitude_node->getDoubleValue();
203 double altitude_m = _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
204 double heading = _heading_node->getDoubleValue() ;
205 double yaw = _yaw_node->getDoubleValue() ;
206 double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
207 double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
209 // On timeout, scan again
210 _time_before_search_sec -= delta_time_sec;
211 if (_time_before_search_sec < 0 && frequency_mhz >= 0)
212 search(frequency_mhz, longitude_rad, latitude_rad, altitude_m);
214 // Calculate the distance to the transmitter
216 //calculate the bearing and range of the mobile from the aircraft
217 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_lat " << _mobile_lat);
218 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_lon " << _mobile_lon);
219 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_name " << _mobile_name);
220 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_valid " << _mobile_valid);
221 geo_inverse_wgs_84(altitude_m,
226 &mobile_bearing, &mobile_az2, &mobile_distance);
228 //calculate the bearing and range of the station from the aircraft
229 SGGeod pos = SGGeod::fromDegM(longitude_deg, latitude_deg, altitude_m);
230 geo_inverse_wgs_84(pos, _transmitter_pos,
231 &bearing, &az2, &distance);
235 if ( mobile_distance <= distance && _mobile_valid) {
236 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
237 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
238 bearing = mobile_bearing;
239 distance = mobile_distance;
240 _transmitter_pos.setElevationFt(_mobile_elevation_ft);
241 _transmitter_range_nm = _mobile_range_nm;
242 _transmitter_bias = _mobile_bias;
243 _transmitter_name = _mobile_name;
244 _name_node->setStringValue(_transmitter_name.c_str());
245 _transmitter_ident = _mobile_ident;
246 _ident_node->setStringValue(_transmitter_ident.c_str());
247 _channel_node->setStringValue(_channel.c_str());
250 //// calculate some values for boresight display
251 double distance_nm = distance * SG_METER_TO_NM;
253 //// calculate look left/right to target, without yaw correction
254 // double horiz_offset = bearing - heading;
256 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
257 // if (horiz_offset < -180.0) horiz_offset += 360.0;
259 //// now correct look left/right for yaw
260 // horiz_offset += yaw;
262 // use the bearing for a plan position indicator display
264 double horiz_offset = bearing;
266 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
267 << bearing << " horiz_offset " << horiz_offset);
269 // calculate values for radar display
270 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
271 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
273 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
277 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
278 altitude_m * SG_METER_TO_FEET,
279 _transmitter_range_nm);
281 if (distance_nm <= range_nm) {
282 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
283 ((1 / delta_time_sec) * 3600.0));
284 _last_distance_nm = distance_nm;
286 _in_range_node->setBoolValue(true);
287 double tmp_dist = distance_nm - _transmitter_bias;
288 if ( tmp_dist < 0.0 ) {
291 _distance_node->setDoubleValue( tmp_dist );
292 _speed_node->setDoubleValue(speed_kt);
293 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
294 _bearing_node->setDoubleValue(bearing);
295 _x_shift_node->setDoubleValue(x_shift);
296 _y_shift_node->setDoubleValue(y_shift);
297 _rotation_node->setDoubleValue(rotation);
299 _last_distance_nm = 0;
300 _in_range_node->setBoolValue(false);
301 _distance_node->setDoubleValue(0);
302 _speed_node->setDoubleValue(0);
303 _time_node->setDoubleValue(0);
304 _bearing_node->setDoubleValue(0);
305 _x_shift_node->setDoubleValue(0);
306 _y_shift_node->setDoubleValue(0);
307 _rotation_node->setDoubleValue(0);
310 // If we can't find a valid station set everything to zero
311 if (!_transmitter_valid && !_mobile_valid ) {
312 _in_range_node->setBoolValue(false);
313 _distance_node->setDoubleValue(0);
314 _speed_node->setDoubleValue(0);
315 _time_node->setDoubleValue(0);
316 _bearing_node->setDoubleValue(0);
317 _x_shift_node->setDoubleValue(0);
318 _y_shift_node->setDoubleValue(0);
319 _rotation_node->setDoubleValue(0);
320 _transmitter_name = "";
321 _name_node->setStringValue(_transmitter_name.c_str());
322 _transmitter_ident = "";
323 _ident_node->setStringValue(_transmitter_ident.c_str());
324 _channel_node->setStringValue(_channel.c_str());
327 } // end function update
330 TACAN::search (double frequency_mhz, double longitude_rad,
331 double latitude_rad, double altitude_m)
334 bool freq_valid = false;
336 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
339 _time_before_search_sec = 1.0;
341 //try any carriers first
342 FGNavRecord *mobile_tacan
343 = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
344 freq_valid = (mobile_tacan != NULL);
345 SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
349 string str1( mobile_tacan->get_name() );
351 SGPropertyNode * branch = fgGetNode("ai/models", true);
352 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
354 number = carrier.size();
356 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
358 for ( i = 0; i < number; ++i ) {
359 string str2 ( carrier[i]->getStringValue("name", ""));
360 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
362 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
363 unsigned int loc1= str1.find( str2, 0 );
364 if ( loc1 != string::npos && str2 != "" ) {
365 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
366 _mobile_lat = carrier[i]->getDoubleValue("position/latitude-deg");
367 _mobile_lon = carrier[i]->getDoubleValue("position/longitude-deg");
368 _mobile_elevation_ft = mobile_tacan->get_elev_ft();
369 _mobile_range_nm = mobile_tacan->get_range();
370 _mobile_bias = mobile_tacan->get_multiuse();
371 _mobile_name = mobile_tacan->get_name();
372 _mobile_ident = mobile_tacan->get_trans_ident();
373 _mobile_valid = true;
374 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
377 _mobile_valid = false;
378 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
382 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _mobile_name);
383 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
384 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
386 //try any AI tankers second
388 if ( !_mobile_valid) {
389 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
391 SGPropertyNode * branch = fgGetNode("ai/models", true);
392 vector<SGPropertyNode_ptr> tanker = branch->getChildren("aircraft");
394 number = tanker.size();
396 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
398 for ( i = 0; i < number; ++i ) {
399 string str4 ( tanker[i]->getStringValue("callsign", ""));
400 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
402 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
403 unsigned int loc1= str1.find( str4, 0 );
404 if ( loc1 != string::npos && str4 != "" ) {
405 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
406 _mobile_lat = tanker[i]->getDoubleValue("position/latitude-deg");
407 _mobile_lon = tanker[i]->getDoubleValue("position/longitude-deg");
408 _mobile_elevation_ft = tanker[i]->getDoubleValue("position/altitude-ft");
409 _mobile_range_nm = mobile_tacan->get_range();
410 _mobile_bias = mobile_tacan->get_multiuse();
411 _mobile_name = mobile_tacan->get_name();
412 _mobile_ident = mobile_tacan->get_trans_ident();
413 _mobile_valid = true;
414 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
417 _mobile_valid = false;
418 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
422 SG_LOG( SG_INSTR, SG_DEBUG, "tanker name " << _mobile_name);
423 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
424 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
425 SG_LOG( SG_INSTR, SG_DEBUG, "range " << _mobile_range_nm);
428 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
430 if ( !_mobile_valid ) {
431 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
433 SGPropertyNode * branch = fgGetNode("ai/models", true);
434 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
436 number = mp_tanker.size();
438 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
440 for ( i = 0; i < number; ++i ) {
441 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
442 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
444 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
445 unsigned int loc1= str1.find( str6, 0 );
446 if ( loc1 != string::npos && str6 != "" ) {
447 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
448 _mobile_lat = mp_tanker[i]->getDoubleValue("position/latitude-deg");
449 _mobile_lon = mp_tanker[i]->getDoubleValue("position/longitude-deg");
450 _mobile_elevation_ft = mp_tanker[i]->getDoubleValue("position/altitude-ft");
451 _mobile_range_nm = mobile_tacan->get_range();
452 _mobile_bias = mobile_tacan->get_multiuse();
453 _mobile_name = mobile_tacan->get_name();
454 _mobile_ident = mobile_tacan->get_trans_ident();
455 _mobile_valid = true;
457 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
458 SG_LOG( SG_INSTR, SG_DEBUG, " mp_tanker name " << _mobile_name);
459 SG_LOG( SG_INSTR, SG_DEBUG, " mp lat " << _mobile_lat << "lon " << _mobile_lon);
460 SG_LOG( SG_INSTR, SG_DEBUG, " mp elev " << _mobile_elevation_ft);
461 SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
464 _mobile_valid = false;
465 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
470 _mobile_valid = false;
471 SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
474 // try the TACAN/VORTAC list next
476 = globals->get_tacanlist()->findByFreq( frequency_mhz, longitude_rad,
477 latitude_rad, altitude_m);
479 _transmitter_valid = (tacan != NULL);
481 if ( _transmitter_valid ) {
482 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
484 _transmitter_pos = tacan->get_pos();
485 _transmitter_range_nm = tacan->get_range();
486 _transmitter_bias = tacan->get_multiuse();
487 _transmitter_name = tacan->get_name();
488 _name_node->setStringValue(_transmitter_name.c_str());
489 _transmitter_ident = tacan->get_trans_ident();
490 _ident_node->setStringValue(_transmitter_ident.c_str());
492 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
493 SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
496 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
501 TACAN::searchChannel (const string& _channel){
503 double frequency_khz = 0;
506 = globals->get_channellist()->findByChannel( _channel );
507 double _freq_valid = (freq != NULL);
508 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
510 frequency_khz = freq->get_freq();
511 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
513 if (frequency_khz >=9620 && frequency_khz <= 121300)
514 return frequency_khz/100;
516 return frequency_khz = 0;
517 } // end TACAN::searchChannel