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),
52 _carrier_valid(false),
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),
84 _carrier_valid(false),
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);
132 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", num, true );
133 _carrier_name_node = cnode->getChild("name", 0, true);
134 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", num, true );
135 _tanker_callsign_node = tnode->getChild("callsign", 0, true);
139 TACAN::update (double delta_time_sec)
144 double carrier_az2 = 0;
145 double carrier_bearing = 0;
146 double carrier_distance = 0;
147 double tanker_az2 = 0;
148 double tanker_bearing = 0;
149 double tanker_distance = 0;
150 double frequency_mhz = 0;
151 double mobile_bearing = 0;
152 double mobile_distance = 0;
153 double mobile_elevation_ft = 0;
154 double mobile_range_nm = 0;
155 double mobile_bias = 0;
156 string mobile_name = "";
157 bool mobile_valid = false;
159 string _channel, _last_channel, _channel_1, _channel_2,_channel_3, _channel_4;
161 // If it's off, don't waste any time.
162 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
163 _last_distance_nm = 0;
164 _in_range_node->setBoolValue(false);
165 _distance_node->setDoubleValue(0);
166 _speed_node->setDoubleValue(0);
167 _time_node->setDoubleValue(0);
168 SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
172 // Figure out the source
173 const char * source = _source_node->getStringValue();
175 if (source[0] == '\0') {
177 branch = "/instrumentation/" + name + "/frequencies/selected-channel";
178 _source_node->setStringValue(branch.c_str());
179 source = _source_node->getStringValue();
180 SG_LOG( SG_INSTR, SG_DEBUG, "source " << source );
183 _channel_1 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[1]");
184 _channel_2 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[2]");
185 _channel_3 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[3]");
186 _channel_4 = fgGetString("/instrumentation/tacan/frequencies/selected-channel[4]");
188 SG_LOG( SG_INSTR, SG_DEBUG, "channels " << _channel_1 << _channel_2 << _channel_3 << _channel_4);
190 _channel = _channel_1 + _channel_2 + _channel_3 + _channel_4;
193 if (_channel != _last_channel) {
194 _time_before_search_sec = 0;
195 _last_channel = _channel;
196 frequency_mhz = searchChannel(_channel);
197 SG_LOG( SG_INSTR, SG_DEBUG, "frequency " << frequency_mhz );
198 _frequency_node->setDoubleValue(frequency_mhz);
201 SG_LOG( SG_INSTR, SG_DEBUG, "channel " << _channel );
202 // Get the aircraft position
203 double longitude_deg = _longitude_node->getDoubleValue();
204 double latitude_deg = _latitude_node->getDoubleValue();
205 double altitude_m = _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
206 double heading = _heading_node->getDoubleValue() ;
207 double yaw = _yaw_node->getDoubleValue() ;
208 double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
209 double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
211 // On timeout, scan again
212 _time_before_search_sec -= delta_time_sec;
213 if (_time_before_search_sec < 0 && frequency_mhz >= 0)
214 search(frequency_mhz, longitude_rad, latitude_rad, altitude_m);
216 // Calculate the distance to the transmitter
218 //calculate the bearing and range of the carrier from the aircraft
219 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_lat " << _carrier_lat);
220 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_lon " << _carrier_lon);
221 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_name " << _carrier_name);
222 SG_LOG( SG_INSTR, SG_DEBUG, "carrier_valid " << _carrier_valid);
223 geo_inverse_wgs_84(altitude_m,
228 &carrier_bearing, &carrier_az2, &carrier_distance);
230 //calculate the bearing and range of the tanker from the aircraft
231 SG_LOG( SG_INSTR, SG_DEBUG, "tanker_lat " << _tanker_lat);
232 SG_LOG( SG_INSTR, SG_DEBUG, "tanker_lon " << _tanker_lon);
233 SG_LOG( SG_INSTR, SG_DEBUG, "tanker_name " << _tanker_name);
234 SG_LOG( SG_INSTR, SG_DEBUG, "tanker_valid " << _tanker_valid);
235 geo_inverse_wgs_84(altitude_m,
240 &tanker_bearing, &tanker_az2, &tanker_distance);
243 //calculate the bearing and range of the station from the aircraft
244 geo_inverse_wgs_84(altitude_m,
249 &bearing, &az2, &distance);
251 //select the nearest valid mobile transmitter
252 if ( _carrier_valid && _tanker_valid ){
253 if( carrier_distance <= tanker_distance ){
254 SG_LOG( SG_INSTR, SG_DEBUG, " select carrier (dist) " );
255 mobile_bearing = carrier_bearing;
256 mobile_distance = carrier_distance;
257 mobile_elevation_ft = _carrier_elevation_ft;
258 mobile_range_nm = _carrier_range_nm;
259 mobile_bias = _carrier_bias;
260 mobile_name = _carrier_name;
262 SG_LOG( SG_INSTR, SG_DEBUG, " select tanker (dist) " );
263 mobile_bearing = tanker_bearing;
264 mobile_distance = tanker_distance;
265 mobile_elevation_ft = _tanker_elevation_ft;
266 mobile_range_nm = _tanker_range_nm;
267 mobile_bias = _tanker_bias;
268 mobile_name = _tanker_name;
272 else if ( _carrier_valid && !_tanker_valid ){
273 SG_LOG( SG_INSTR, SG_DEBUG, " select carrier " );
274 mobile_bearing = carrier_bearing;
275 mobile_distance = carrier_distance;
276 mobile_elevation_ft = _carrier_elevation_ft;
277 mobile_range_nm = _carrier_range_nm;
278 mobile_bias = _carrier_bias;
279 mobile_name = _carrier_name;
282 else if ( !_carrier_valid && _tanker_valid ){
283 SG_LOG( SG_INSTR, SG_DEBUG, " select tanker " );
284 mobile_bearing = tanker_bearing;
285 mobile_distance = tanker_distance;
286 mobile_elevation_ft = _tanker_elevation_ft;
287 mobile_range_nm = _tanker_range_nm;
288 mobile_bias = _tanker_bias;
289 mobile_name = _tanker_name;
292 mobile_valid = false;
299 if ( mobile_distance <= distance && mobile_valid) {
300 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
301 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
302 bearing = mobile_bearing;
303 distance = mobile_distance;
304 _transmitter_elevation_ft = mobile_elevation_ft;
305 _transmitter_range_nm = mobile_range_nm;
306 _transmitter_bias = mobile_bias;
307 _transmitter_name = mobile_name;
308 _name_node->setStringValue(_transmitter_name.c_str());
309 _channel_node->setStringValue(_channel.c_str());
312 //// calculate some values for boresight display
313 double distance_nm = distance * SG_METER_TO_NM;
315 //// calculate look left/right to target, without yaw correction
316 // double horiz_offset = bearing - heading;
318 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
319 // if (horiz_offset < -180.0) horiz_offset += 360.0;
321 //// now correct look left/right for yaw
322 // horiz_offset += yaw;
324 // use the bearing for plan position indicator display
326 double horiz_offset = bearing;
328 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
329 << bearing << " horiz_offset " << horiz_offset);
331 // calculate values for radar display
332 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
333 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
335 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
340 sgGeodToCart(Point3D(longitude_rad, latitude_rad, altitude_m));
341 double distance_nm = _transmitter.distance3D(location) * SG_METER_TO_NM;*/
343 double range_nm = adjust_range(_transmitter_elevation_ft,
344 altitude_m * SG_METER_TO_FEET,
345 _transmitter_range_nm);
347 if (distance_nm <= range_nm) {
348 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
349 ((1 / delta_time_sec) * 3600.0));
350 _last_distance_nm = distance_nm;
352 _in_range_node->setBoolValue(true);
353 double tmp_dist = distance_nm - _transmitter_bias;
354 if ( tmp_dist < 0.0 ) {
357 _distance_node->setDoubleValue( tmp_dist );
358 _speed_node->setDoubleValue(speed_kt);
359 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
360 _bearing_node->setDoubleValue(bearing);
361 _x_shift_node->setDoubleValue(x_shift);
362 _y_shift_node->setDoubleValue(y_shift);
363 _rotation_node->setDoubleValue(rotation);
365 _last_distance_nm = 0;
366 _in_range_node->setBoolValue(false);
367 _distance_node->setDoubleValue(0);
368 _speed_node->setDoubleValue(0);
369 _time_node->setDoubleValue(0);
370 _bearing_node->setDoubleValue(0);
371 _x_shift_node->setDoubleValue(0);
372 _y_shift_node->setDoubleValue(0);
373 _rotation_node->setDoubleValue(0);
376 // If we can't find a valid station set everything to zero
377 if (!_transmitter_valid && !mobile_valid ) {
378 _in_range_node->setBoolValue(false);
379 _distance_node->setDoubleValue(0);
380 _speed_node->setDoubleValue(0);
381 _time_node->setDoubleValue(0);
382 _bearing_node->setDoubleValue(0);
383 _x_shift_node->setDoubleValue(0);
384 _y_shift_node->setDoubleValue(0);
385 _rotation_node->setDoubleValue(0);
386 _transmitter_name = "";
387 _name_node->setStringValue(_transmitter_name.c_str());
388 _channel_node->setStringValue(_channel.c_str());
391 } // end function update
394 TACAN::search (double frequency_mhz, double longitude_rad,
395 double latitude_rad, double altitude_m)
399 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
402 _time_before_search_sec = 1.0;
404 //try any carriers first
405 FGNavRecord *carrier_tacan
406 = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
407 _carrier_valid = (carrier_tacan != NULL);
409 if ( _carrier_valid ) {
410 SG_LOG( SG_INSTR, SG_DEBUG, "carrier transmitter valid " << _carrier_valid );
412 string str1( carrier_tacan->get_name() );
414 SGPropertyNode * branch = fgGetNode("ai/models", true);
415 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
417 number = carrier.size();
419 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
421 for ( i = 0; i < number; ++i ) {
422 string str2 ( carrier[i]->getStringValue("name", ""));
423 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
425 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
426 unsigned int loc1= str1.find( str2, 0 );
427 if ( loc1 != string::npos && str2 != "" ) {
428 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
429 _carrier_lat = carrier[i]->getDoubleValue("position/latitude-deg");
430 _carrier_lon = carrier[i]->getDoubleValue("position/longitude-deg");
431 _carrier_elevation_ft = carrier_tacan->get_elev_ft();
432 _carrier_range_nm = carrier_tacan->get_range();
433 _carrier_bias = carrier_tacan->get_multiuse();
434 _carrier_name = carrier_tacan->get_name();
436 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _carrier_valid );
440 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _carrier_valid );
444 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _carrier_name);
445 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _carrier_lat << "lon " << _carrier_lon);
446 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _carrier_elevation_ft);
449 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _carrier_valid );
452 //try any tankers second
453 FGNavRecord *tanker_tacan
454 = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
456 _tanker_valid = (tanker_tacan != NULL);
459 if ( _tanker_valid ) {
460 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _tanker_valid );
462 string str3( tanker_tacan->get_name() );
464 SGPropertyNode * branch = fgGetNode("ai/models", true);
465 vector<SGPropertyNode_ptr> tanker = branch->getChildren("aircraft");
467 number = tanker.size();
469 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
471 for ( i = 0; i < number; ++i ) {
472 string str4 ( tanker[i]->getStringValue("callsign", ""));
473 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
475 SG_LOG( SG_INSTR, SG_DEBUG, "strings 3 " << str3 << " 4 " << str4 );
476 unsigned int loc1= str3.find( str4, 0 );
477 if ( loc1 != string::npos && str4 != "" ) {
478 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
479 _tanker_lat = tanker[i]->getDoubleValue("position/latitude-deg");
480 _tanker_lon = tanker[i]->getDoubleValue("position/longitude-deg");
481 _tanker_elevation_ft = tanker[i]->getDoubleValue("position/altitude-ft");
482 _tanker_range_nm = tanker_tacan->get_range();
483 _tanker_bias = tanker_tacan->get_multiuse();
484 _tanker_name = tanker_tacan->get_name();
486 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _tanker_valid );
490 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _tanker_valid );
494 SG_LOG( SG_INSTR, SG_DEBUG, "tanker name " << _tanker_name);
495 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _tanker_lat << "lon " << _tanker_lon);
496 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _tanker_elevation_ft);
497 SG_LOG( SG_INSTR, SG_DEBUG, "range " << _tanker_range_nm);
500 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _tanker_valid );
503 // try the TACAN/VORTAC list next
505 = globals->get_tacanlist()->findByFreq( frequency_mhz, longitude_rad,
506 latitude_rad, altitude_m);
508 _transmitter_valid = (tacan != NULL);
510 if ( _transmitter_valid ) {
511 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
513 _transmitter_lat = tacan->get_lat();
514 _transmitter_lon = tacan->get_lon();
515 _transmitter_elevation_ft = tacan->get_elev_ft();
516 _transmitter_range_nm = tacan->get_range();
517 _transmitter_bias = tacan->get_multiuse();
518 _transmitter_name = tacan->get_name();
519 _name_node->setStringValue(_transmitter_name.c_str());
521 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
522 SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _transmitter_lat << "lon " << _transmitter_lon);
523 SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _transmitter_elevation_ft);
526 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
531 TACAN::searchChannel (const string& _channel){
533 double frequency_khz = 0;
536 = globals->get_channellist()->findByChannel( _channel );
537 double _freq_valid = (freq != NULL);
538 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
540 frequency_khz = freq->get_freq();
541 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
543 if (frequency_khz >=9620 && frequency_khz <= 12130)
544 return frequency_khz/100;
546 return frequency_khz = 0;
547 } // end TACAN::searchChannel