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),
73 branch = "/instrumentation/" + _name;
75 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
77 _serviceable_node = node->getChild("serviceable", 0, true);
78 _ident_node = node->getChild("ident", 0, true);
80 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
81 _frequency_node = fnode->getChild("selected-mhz", 0, true);
83 _channel_in0_node = fnode->getChild("selected-channel", 0, true);
84 _channel_in1_node = fnode->getChild("selected-channel", 1, true);
85 _channel_in2_node = fnode->getChild("selected-channel", 2, true);
86 _channel_in3_node = fnode->getChild("selected-channel", 3, true);
87 _channel_in4_node = fnode->getChild("selected-channel", 4, true);
89 _channel_in0_node->addChangeListener(this);
90 _channel_in1_node->addChangeListener(this);
91 _channel_in2_node->addChangeListener(this);
92 _channel_in3_node->addChangeListener(this);
93 _channel_in4_node->addChangeListener(this, true);
95 _in_range_node = node->getChild("in-range", 0, true);
96 _distance_node = node->getChild("indicated-distance-nm", 0, true);
97 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
98 _time_node = node->getChild("indicated-time-min", 0, true);
99 _name_node = node->getChild("name", 0, true);
100 _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
102 SGPropertyNode *dnode = node->getChild("display", 0, true);
103 _x_shift_node = dnode->getChild("x-shift", 0, true);
104 _y_shift_node = dnode->getChild("y-shift", 0, true);
105 _rotation_node = dnode->getChild("rotation", 0, true);
106 _channel_node = dnode->getChild("channel", 0, true);
108 SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
109 _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
111 SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
112 _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
114 SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
115 _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
117 _heading_node = fgGetNode("/orientation/heading-deg", true);
118 _yaw_node = fgGetNode("/orientation/side-slip-deg", true);
119 _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
125 _time_before_search_sec = 0;
129 TACAN::update (double delta_time_sec)
131 // don't do anything when paused
132 if (delta_time_sec == 0) return;
134 if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
135 _last_distance_nm = 0;
136 _in_range_node->setBoolValue(false);
137 _distance_node->setDoubleValue(0);
138 _speed_node->setDoubleValue(0);
139 _time_node->setDoubleValue(0);
140 SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
144 SGGeod pos(globals->get_aircraft_position());
145 // On timeout, scan again
146 _time_before_search_sec -= delta_time_sec;
147 if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
148 search(_frequency_mhz, pos);
150 // Calculate the distance to the transmitter
152 //calculate the bearing and range of the mobile from the aircraft
153 double mobile_az2 = 0;
154 double mobile_bearing = 0;
155 double mobile_distance = 0;
157 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_name " << _mobile_name);
158 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_valid " << _mobile_valid);
159 geo_inverse_wgs_84(pos, _mobilePos,
160 &mobile_bearing, &mobile_az2, &mobile_distance);
163 //calculate the bearing and range of the station from the aircraft
168 geo_inverse_wgs_84(pos, _transmitter_pos,
169 &bearing, &az2, &distance);
173 if ( mobile_distance <= distance && _mobile_valid) {
174 SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
175 SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
176 bearing = mobile_bearing;
177 distance = mobile_distance;
178 _transmitter_pos.setElevationFt(_mobilePos.getElevationFt());
179 _transmitter_range_nm = _mobile_range_nm;
180 _transmitter_bias = _mobile_bias;
181 _transmitter_name = _mobile_name;
182 _name_node->setStringValue(_transmitter_name.c_str());
183 _transmitter_ident = _mobile_ident;
184 _ident_node->setStringValue(_transmitter_ident.c_str());
185 _channel_node->setStringValue(_channel.c_str());
188 //// calculate some values for boresight display
189 double distance_nm = distance * SG_METER_TO_NM;
191 //// calculate look left/right to target, without yaw correction
192 // double horiz_offset = bearing - heading;
194 // if (horiz_offset > 180.0) horiz_offset -= 360.0;
195 // if (horiz_offset < -180.0) horiz_offset += 360.0;
197 //// now correct look left/right for yaw
198 // horiz_offset += yaw;
200 // use the bearing for a plan position indicator display
202 double horiz_offset = bearing;
204 SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
205 << bearing << " horiz_offset " << horiz_offset);
207 // calculate values for radar display
208 double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
209 double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
211 SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift << " x_shift " << x_shift);
215 double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
216 pos.getElevationFt(),
217 _transmitter_range_nm);
219 if (distance_nm <= range_nm) {
220 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
221 ((1 / delta_time_sec) * 3600.0));
222 _last_distance_nm = distance_nm;
224 _in_range_node->setBoolValue(true);
225 double tmp_dist = distance_nm - _transmitter_bias;
226 if ( tmp_dist < 0.0 ) {
229 _distance_node->setDoubleValue( tmp_dist );
230 _speed_node->setDoubleValue(speed_kt);
231 _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0);
232 _bearing_node->setDoubleValue(bearing);
233 _x_shift_node->setDoubleValue(x_shift);
234 _y_shift_node->setDoubleValue(y_shift);
235 _rotation_node->setDoubleValue(rotation);
237 _last_distance_nm = 0;
238 _in_range_node->setBoolValue(false);
239 _distance_node->setDoubleValue(0);
240 _speed_node->setDoubleValue(0);
241 _time_node->setDoubleValue(0);
242 _bearing_node->setDoubleValue(0);
243 _x_shift_node->setDoubleValue(0);
244 _y_shift_node->setDoubleValue(0);
245 _rotation_node->setDoubleValue(0);
248 // If we can't find a valid station set everything to zero
249 if (!_transmitter_valid && !_mobile_valid ) {
250 _in_range_node->setBoolValue(false);
251 _distance_node->setDoubleValue(0);
252 _speed_node->setDoubleValue(0);
253 _time_node->setDoubleValue(0);
254 _bearing_node->setDoubleValue(0);
255 _x_shift_node->setDoubleValue(0);
256 _y_shift_node->setDoubleValue(0);
257 _rotation_node->setDoubleValue(0);
258 _transmitter_name = "";
259 _name_node->setStringValue(_transmitter_name.c_str());
260 _transmitter_ident = "";
261 _ident_node->setStringValue(_transmitter_ident.c_str());
262 _channel_node->setStringValue(_channel.c_str());
265 } // end function update
268 TACAN::search (double frequency_mhz,const SGGeod& pos)
271 _mobile_valid = false;
273 SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
276 _time_before_search_sec = 1.0;
278 //try any carriers first
279 FGNavRecord *mobile_tacan = FGNavList::findByFreq( frequency_mhz, FGNavList::carrierFilter() );
280 bool freq_valid = (mobile_tacan != NULL);
281 SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
285 string str1( mobile_tacan->name() );
287 SGPropertyNode * branch = fgGetNode("ai/models", true);
288 vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
290 number = carrier.size();
292 SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
293 for ( i = 0; i < number; ++i ) {
294 string str2 ( carrier[i]->getStringValue("name", ""));
295 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
297 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
298 string::size_type loc1= str1.find( str2, 0 );
299 if ( loc1 != string::npos && str2 != "" ) {
300 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
301 _mobilePos = SGGeod::fromDegFt(
302 carrier[i]->getDoubleValue("position/longitude-deg"),
303 carrier[i]->getDoubleValue("position/latitude-deg"),
304 mobile_tacan->get_elev_ft());
305 _mobile_range_nm = mobile_tacan->get_range();
306 _mobile_bias = mobile_tacan->get_multiuse();
307 _mobile_name = mobile_tacan->name();
308 _mobile_ident = mobile_tacan->get_trans_ident();
309 _mobile_valid = true;
310 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
313 _mobile_valid = false;
314 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
318 //try any AI tankers second
320 if ( !_mobile_valid) {
321 SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
323 SGPropertyNode * branch = fgGetNode("ai/models", true);
324 vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
326 number = tanker.size();
328 SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
330 for ( i = 0; i < number; ++i ) {
331 string str4 ( tanker[i]->getStringValue("callsign", ""));
332 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
334 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
335 string::size_type loc1= str1.find( str4, 0 );
336 if ( loc1 != string::npos && str4 != "" ) {
337 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
338 _mobilePos = SGGeod::fromDegFt(
339 tanker[i]->getDoubleValue("position/longitude-deg"),
340 tanker[i]->getDoubleValue("position/latitude-deg"),
341 tanker[i]->getDoubleValue("position/altitude-ft"));
344 _mobile_range_nm = mobile_tacan->get_range();
345 _mobile_bias = mobile_tacan->get_multiuse();
346 _mobile_name = mobile_tacan->name();
347 _mobile_ident = mobile_tacan->get_trans_ident();
348 _mobile_valid = true;
349 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
352 _mobile_valid = false;
353 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
358 //try any mp tankers third, if we haven't found the tanker in the ai aircraft
360 if ( !_mobile_valid ) {
361 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
363 SGPropertyNode * branch = fgGetNode("ai/models", true);
364 vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
366 number = mp_tanker.size();
368 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
370 if ( number > 0 ) { // don't do this if there are no MP aircraft
371 for ( i = 0; i < number; ++i ) {
372 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
373 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
375 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
376 string::size_type loc1= str1.find( str6, 0 );
377 if ( loc1 != string::npos && str6 != "" ) {
378 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
379 _mobilePos = SGGeod::fromDegFt(
380 mp_tanker[i]->getDoubleValue("position/longitude-deg"),
381 mp_tanker[i]->getDoubleValue("position/latitude-deg"),
382 mp_tanker[i]->getDoubleValue("position/altitude-ft"));
385 _mobile_range_nm = mobile_tacan->get_range();
386 _mobile_bias = mobile_tacan->get_multiuse();
387 _mobile_name = mobile_tacan->name();
388 _mobile_ident = mobile_tacan->get_trans_ident();
389 _mobile_valid = true;
391 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
392 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker name " << _mobile_name);
393 SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
396 _mobile_valid = false;
397 SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
403 _mobile_valid = false;
404 SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
407 // try the TACAN/VORTAC list next
408 FGNavRecord *tacan = FGNavList::findByFreq( frequency_mhz, pos, FGNavList::tacanFilter() );
410 _transmitter_valid = (tacan != NULL);
412 if ( _transmitter_valid ) {
413 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
415 _transmitter_pos = tacan->geod();
416 _transmitter_range_nm = tacan->get_range();
417 _transmitter_bias = tacan->get_multiuse();
418 _transmitter_name = tacan->name();
419 _name_node->setStringValue(_transmitter_name.c_str());
420 _transmitter_ident = tacan->get_trans_ident();
421 _ident_node->setStringValue(_transmitter_ident.c_str());
423 SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
424 SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
427 SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
432 TACAN::searchChannel (const string& channel)
434 double frequency_khz = 0;
437 = globals->get_channellist()->findByChannel( channel );
438 bool _freq_valid = (freq != NULL);
439 SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
441 frequency_khz = freq->get_freq();
442 SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
444 if (frequency_khz >= 9620 && frequency_khz <= 121300)
445 return frequency_khz/100;
447 return frequency_khz = 0;
448 } // end TACAN::searchChannel
451 * Listener callback. Maintains channel input properties,
452 * searches new channel frequency, updates _channel and
453 * _frequency and sets boolean _new_frequency appropriately.
456 TACAN::valueChanged(SGPropertyNode *prop)
458 if (_listener_active)
462 int index = prop->getIndex();
463 string channel = _channel;
465 if (index) { // channel digit or X/Y input
467 if (isdigit(c = _channel_in1_node->getStringValue()[0]))
469 if (isdigit(c = _channel_in2_node->getStringValue()[0]))
471 if (isdigit(c = _channel_in3_node->getStringValue()[0]))
473 c = _channel_in4_node->getStringValue()[0];
474 if (c == 'X' || c == 'Y')
477 } else { // channel number input
478 unsigned int f = prop->getIntValue();
479 if (f >= 1 && f <= 126) {
480 channel[0] = '0' + (f / 100) % 10;
481 channel[1] = '0' + (f / 10) % 10;
482 channel[2] = '0' + f % 10;
486 if (channel != _channel) {
487 SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
490 _channel_in0_node->setIntValue((channel[0] - '0') * 100
491 + (channel[1] - '0') * 10 + (channel[2] - '0'));
493 s[0] = channel[0], _channel_in1_node->setStringValue(s);
494 s[0] = channel[1], _channel_in2_node->setStringValue(s);
495 s[0] = channel[2], _channel_in3_node->setStringValue(s);
496 s[0] = channel[3], _channel_in4_node->setStringValue(s);
498 // search channel frequency
499 double freq = searchChannel(channel);
500 if (freq != _frequency_mhz) {
501 SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
502 _frequency_node->setDoubleValue(freq);
503 _frequency_mhz = freq;
504 _new_frequency = true;
508 _time_before_search_sec = 0;