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_elevation_ft(0),
55 _transmitter_range_nm(0),
56 _transmitter_bias(0.0),
63 for ( i = 0; i < node->nChildren(); ++i ) {
64 SGPropertyNode *child = node->getChild(i);
65 string cname = child->getName();
66 string cval = child->getStringValue();
67 if ( cname == "name" ) {
69 } else if ( cname == "number" ) {
70 num = child->getIntValue();
72 SG_LOG( SG_INSTR, SG_DEBUG, "Error in TACAN config logic" );
73 if ( name.length() ) {
74 SG_LOG( SG_INSTR, SG_DEBUG, "Section = " << name );
81 : _last_distance_nm(0),
82 _last_frequency_mhz(-1),
83 _time_before_search_sec(0),
85 _transmitter_valid(false),
86 _transmitter_elevation_ft(0),
87 _transmitter_range_nm(0),
88 _transmitter_bearing_deg(0),
89 _transmitter_bias(0.0),
90 _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 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 geo_inverse_wgs_84(altitude_m,
234 &bearing, &az2, &distance);
238 if ( mobile_distance <= distance && _mobile_valid) {
239 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
240 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
241 bearing = mobile_bearing;
242 distance = mobile_distance;
243 _transmitter_elevation_ft = _mobile_elevation_ft;
244 _transmitter_range_nm = _mobile_range_nm;
245 _transmitter_bias = _mobile_bias;
246 _transmitter_name = _mobile_name;
247 _name_node->setStringValue(_transmitter_name.c_str());
248 _channel_node->setStringValue(_channel.c_str());
251 //// calculate some values for boresight display
252 double distance_nm = distance * SG_METER_TO_NM;
254 //// calculate look left/right to target, without yaw correction
255 // double horiz_offset = bearing - heading;
257 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
258 // if (horiz_offset < -180.0) horiz_offset += 360.0;
260 //// now correct look left/right for yaw
261 // horiz_offset += yaw;
263 // use the bearing for a plan position indicator display
265 double horiz_offset = bearing;
267 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
268 << bearing << " horiz_offset " << horiz_offset);
270 // calculate values for radar display
271 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
272 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
274 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
279 sgGeodToCart(Point3D(longitude_rad, latitude_rad, altitude_m));
280 double distance_nm = _transmitter.distance3D(location) * SG_METER_TO_NM;*/
282 double range_nm = adjust_range(_transmitter_elevation_ft,
283 altitude_m * SG_METER_TO_FEET,
284 _transmitter_range_nm);
286 if (distance_nm <= range_nm) {
287 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
288 ((1 / delta_time_sec) * 3600.0));
289 _last_distance_nm = distance_nm;
291 _in_range_node->setBoolValue(true);
292 double tmp_dist = distance_nm - _transmitter_bias;
293 if ( tmp_dist < 0.0 ) {
296 _distance_node->setDoubleValue( tmp_dist );
297 _speed_node->setDoubleValue(speed_kt);
298 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
299 _bearing_node->setDoubleValue(bearing);
300 _x_shift_node->setDoubleValue(x_shift);
301 _y_shift_node->setDoubleValue(y_shift);
302 _rotation_node->setDoubleValue(rotation);
304 _last_distance_nm = 0;
305 _in_range_node->setBoolValue(false);
306 _distance_node->setDoubleValue(0);
307 _speed_node->setDoubleValue(0);
308 _time_node->setDoubleValue(0);
309 _bearing_node->setDoubleValue(0);
310 _x_shift_node->setDoubleValue(0);
311 _y_shift_node->setDoubleValue(0);
312 _rotation_node->setDoubleValue(0);
315 // If we can't find a valid station set everything to zero
316 if (!_transmitter_valid && !_mobile_valid ) {
317 _in_range_node->setBoolValue(false);
318 _distance_node->setDoubleValue(0);
319 _speed_node->setDoubleValue(0);
320 _time_node->setDoubleValue(0);
321 _bearing_node->setDoubleValue(0);
322 _x_shift_node->setDoubleValue(0);
323 _y_shift_node->setDoubleValue(0);
324 _rotation_node->setDoubleValue(0);
325 _transmitter_name = "";
326 _name_node->setStringValue(_transmitter_name.c_str());
327 _channel_node->setStringValue(_channel.c_str());
330 } // end function update
333 TACAN::search (double frequency_mhz, double longitude_rad,
334 double latitude_rad, double altitude_m)
337 bool freq_valid = false;
339 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
342 _time_before_search_sec = 1.0;
344 //try any carriers first
345 FGNavRecord *mobile_tacan
346 = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
347 freq_valid = (mobile_tacan != NULL);
348 SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
352 string str1( mobile_tacan->get_name() );
354 SGPropertyNode * branch = fgGetNode("ai/models", true);
355 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
357 number = carrier.size();
359 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
361 for ( i = 0; i < number; ++i ) {
362 string str2 ( carrier[i]->getStringValue("name", ""));
363 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
365 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
366 unsigned int loc1= str1.find( str2, 0 );
367 if ( loc1 != string::npos && str2 != "" ) {
368 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
369 _mobile_lat = carrier[i]->getDoubleValue("position/latitude-deg");
370 _mobile_lon = carrier[i]->getDoubleValue("position/longitude-deg");
371 _mobile_elevation_ft = mobile_tacan->get_elev_ft();
372 _mobile_range_nm = mobile_tacan->get_range();
373 _mobile_bias = mobile_tacan->get_multiuse();
374 _mobile_name = mobile_tacan->get_name();
375 _mobile_valid = true;
376 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
379 _mobile_valid = false;
380 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
384 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _mobile_name);
385 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
386 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
388 //try any AI tankers second
390 if ( !_mobile_valid) {
391 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
393 SGPropertyNode * branch = fgGetNode("ai/models", true);
394 vector<SGPropertyNode_ptr> tanker = branch->getChildren("aircraft");
396 number = tanker.size();
398 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
400 for ( i = 0; i < number; ++i ) {
401 string str4 ( tanker[i]->getStringValue("callsign", ""));
402 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
404 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
405 unsigned int loc1= str1.find( str4, 0 );
406 if ( loc1 != string::npos && str4 != "" ) {
407 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
408 _mobile_lat = tanker[i]->getDoubleValue("position/latitude-deg");
409 _mobile_lon = tanker[i]->getDoubleValue("position/longitude-deg");
410 _mobile_elevation_ft = tanker[i]->getDoubleValue("position/altitude-ft");
411 _mobile_range_nm = mobile_tacan->get_range();
412 _mobile_bias = mobile_tacan->get_multiuse();
413 _mobile_name = mobile_tacan->get_name();
414 _mobile_valid = true;
415 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
418 _mobile_valid = false;
419 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
423 SG_LOG( SG_INSTR, SG_DEBUG, "tanker name " << _mobile_name);
424 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
425 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
426 SG_LOG( SG_INSTR, SG_DEBUG, "range " << _mobile_range_nm);
429 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
431 if ( !_mobile_valid ) {
432 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
434 SGPropertyNode * branch = fgGetNode("ai/models", true);
435 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
437 number = mp_tanker.size();
439 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
441 for ( i = 0; i < number; ++i ) {
442 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
443 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
445 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
446 unsigned int loc1= str1.find( str6, 0 );
447 if ( loc1 != string::npos && str6 != "" ) {
448 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
449 _mobile_lat = mp_tanker[i]->getDoubleValue("position/latitude-deg");
450 _mobile_lon = mp_tanker[i]->getDoubleValue("position/longitude-deg");
451 _mobile_elevation_ft = mp_tanker[i]->getDoubleValue("position/altitude-ft");
452 _mobile_range_nm = mobile_tacan->get_range();
453 _mobile_bias = mobile_tacan->get_multiuse();
454 _mobile_name = mobile_tacan->get_name();
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_lat = tacan->get_lat();
485 _transmitter_lon = tacan->get_lon();
486 _transmitter_elevation_ft = tacan->get_elev_ft();
487 _transmitter_range_nm = tacan->get_range();
488 _transmitter_bias = tacan->get_multiuse();
489 _transmitter_name = tacan->get_name();
490 _name_node->setStringValue(_transmitter_name.c_str());
492 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
493 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _transmitter_lat << "lon " << _transmitter_lon);
494 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _transmitter_elevation_ft);
497 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
502 TACAN::searchChannel (const string& _channel){
504 double frequency_khz = 0;
507 = globals->get_channellist()->findByChannel( _channel );
508 double _freq_valid = (freq != NULL);
509 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
511 frequency_khz = freq->get_freq();
512 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
514 if (frequency_khz >=9620 && frequency_khz <= 12130)
515 return frequency_khz/100;
517 return frequency_khz = 0;
518 } // end TACAN::searchChannel