]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/tacan.cxx
initialize variables before using them
[flightgear.git] / src / Instrumentation / tacan.cxx
1 // tacan.cxx - Tactical Navigation Beacon.
2 // Written by Vivian Meazaa, started 2005.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <simgear/compiler.h>
11 #include <simgear/math/sg_geodesy.hxx>
12 #include <simgear/math/sg_random.h>
13
14 #include <Main/fg_props.hxx>
15 #include <Navaids/navlist.hxx>
16 #include <vector>
17
18 #include "tacan.hxx"
19
20 SG_USING_STD(vector);
21
22
23 /**
24  * Adjust the range.
25  *
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.
29  */
30 static double
31 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
32               double max_range_nm)
33 {
34     max_range_nm = 150;
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)
41         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);
45 }
46
47
48 TACAN::TACAN ( SGPropertyNode *node ) :
49     _name(node->getStringValue("name", "tacan")),
50     _num(node->getIntValue("number", 0)),
51     _new_frequency(false),
52     _channel("0000"),
53     _last_distance_nm(0),
54     _frequency_mhz(-1),
55     _time_before_search_sec(0),
56     _mobile_valid(false),
57     _transmitter_valid(false),
58     _transmitter_pos(SGGeod::fromDeg(0, 0)),
59     _transmitter_range_nm(0),
60     _transmitter_bias(0.0),
61     _mobile_lat(0.0),
62     _mobile_lon(0.0),
63     _listener_active(0)
64 {
65 }
66
67 TACAN::~TACAN ()
68 {
69 }
70
71 void
72 TACAN::init ()
73 {
74     string branch;
75     branch = "/instrumentation/" + _name;
76
77     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
78
79     _serviceable_node = node->getChild("serviceable", 0, true);
80     _ident_node = node->getChild("ident", 0, true);
81
82     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
83     _frequency_node = fnode->getChild("selected-mhz", 0, true);
84
85     _channel_in0_node = fnode->getChild("selected-channel", 0, true);
86     _channel_in1_node = fnode->getChild("selected-channel", 1, true);
87     _channel_in2_node = fnode->getChild("selected-channel", 2, true);
88     _channel_in3_node = fnode->getChild("selected-channel", 3, true);
89     _channel_in4_node = fnode->getChild("selected-channel", 4, true);
90
91     _channel_in0_node->addChangeListener(this);
92     _channel_in1_node->addChangeListener(this);
93     _channel_in2_node->addChangeListener(this);
94     _channel_in3_node->addChangeListener(this);
95     _channel_in4_node->addChangeListener(this, true);
96
97     _in_range_node = node->getChild("in-range", 0, true);
98     _distance_node = node->getChild("indicated-distance-nm", 0, true);
99     _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
100     _time_node = node->getChild("indicated-time-min", 0, true);
101     _name_node = node->getChild("name", 0, true);
102     _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
103
104     SGPropertyNode *dnode = node->getChild("display", 0, true);
105     _x_shift_node = dnode->getChild("x-shift", 0, true);
106     _y_shift_node = dnode->getChild("y-shift", 0, true);
107     _rotation_node = dnode->getChild("rotation", 0, true);
108     _channel_node = dnode->getChild("channel", 0, true);
109
110     SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
111     _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
112
113     SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
114     _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
115
116     SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
117     _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
118
119     _longitude_node = fgGetNode("/position/longitude-deg", true);
120     _latitude_node = fgGetNode("/position/latitude-deg", true);
121     _altitude_node = fgGetNode("/position/altitude-ft", true);
122     _heading_node = fgGetNode("/orientation/heading-deg", true);
123     _yaw_node = fgGetNode("/orientation/side-slip-deg", true);
124     _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
125 }
126
127 void
128 TACAN::update (double delta_time_sec)
129 {
130     if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
131         _last_distance_nm = 0;
132         _in_range_node->setBoolValue(false);
133         _distance_node->setDoubleValue(0);
134         _speed_node->setDoubleValue(0);
135         _time_node->setDoubleValue(0);
136         SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
137         return;
138     }
139
140                                 // Get the aircraft position
141     double longitude_deg = _longitude_node->getDoubleValue();
142     double latitude_deg  = _latitude_node->getDoubleValue();
143     double altitude_m    = _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
144     double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
145     double latitude_rad  = latitude_deg * SGD_DEGREES_TO_RADIANS;
146
147                                 // On timeout, scan again
148     _time_before_search_sec -= delta_time_sec;
149     if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
150         search(_frequency_mhz, longitude_rad, latitude_rad, altitude_m);
151
152                                  // Calculate the distance to the transmitter
153
154     //calculate the bearing and range of the mobile from the aircraft
155     double mobile_az2 = 0;
156     double mobile_bearing = 0;
157     double mobile_distance = 0;
158
159     SG_LOG( SG_INSTR, SG_DEBUG, "mobile_lat " << _mobile_lat);
160     SG_LOG( SG_INSTR, SG_DEBUG, "mobile_lon " << _mobile_lon);
161     SG_LOG( SG_INSTR, SG_DEBUG, "mobile_name " << _mobile_name);
162     SG_LOG( SG_INSTR, SG_DEBUG, "mobile_valid " << _mobile_valid);
163     geo_inverse_wgs_84(altitude_m,
164                        latitude_deg,
165                        longitude_deg,
166                        _mobile_lat,
167                        _mobile_lon,
168                        &mobile_bearing, &mobile_az2, &mobile_distance);
169
170
171     //calculate the bearing and range of the station from the aircraft
172     double az2 = 0;
173     double bearing = 0;
174     double distance = 0;
175
176     SGGeod pos = SGGeod::fromDegM(longitude_deg, latitude_deg, altitude_m);
177     geo_inverse_wgs_84(pos, _transmitter_pos,
178                        &bearing, &az2, &distance);
179
180
181     //select the nearer
182     if ( mobile_distance <= distance && _mobile_valid) {
183         SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
184         SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
185         bearing = mobile_bearing;
186         distance = mobile_distance;
187         _transmitter_pos.setElevationFt(_mobile_elevation_ft);
188         _transmitter_range_nm = _mobile_range_nm;
189         _transmitter_bias = _mobile_bias;
190         _transmitter_name = _mobile_name;
191         _name_node->setStringValue(_transmitter_name.c_str());
192         _transmitter_ident = _mobile_ident;
193         _ident_node->setStringValue(_transmitter_ident.c_str());
194         _channel_node->setStringValue(_channel.c_str());
195     }
196
197     //// calculate some values for boresight display
198     double distance_nm = distance * SG_METER_TO_NM;
199
200     //// calculate look left/right to target, without yaw correction
201     // double horiz_offset = bearing - heading;
202     //
203     // if (horiz_offset > 180.0) horiz_offset -= 360.0;
204     // if (horiz_offset < -180.0) horiz_offset += 360.0;
205
206     //// now correct look left/right for yaw
207     // horiz_offset += yaw;
208
209     // use the bearing for a plan position indicator display
210
211     double horiz_offset = bearing;
212
213     SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
214             << bearing << " horiz_offset " << horiz_offset);
215
216     // calculate values for radar display
217     double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
218     double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
219
220     SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift  << " x_shift " << x_shift);
221
222     double rotation = 0;
223
224     double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
225                                    altitude_m * SG_METER_TO_FEET,
226                                    _transmitter_range_nm);
227
228     if (distance_nm <= range_nm) {
229         double speed_kt = (fabs(distance_nm - _last_distance_nm) *
230                            ((1 / delta_time_sec) * 3600.0));
231         _last_distance_nm = distance_nm;
232
233         _in_range_node->setBoolValue(true);
234         double tmp_dist = distance_nm - _transmitter_bias;
235         if ( tmp_dist < 0.0 ) {
236             tmp_dist = 0.0;
237         }
238         _distance_node->setDoubleValue( tmp_dist );
239         _speed_node->setDoubleValue(speed_kt);
240         _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
241         _bearing_node->setDoubleValue(bearing);
242         _x_shift_node->setDoubleValue(x_shift);
243         _y_shift_node->setDoubleValue(y_shift);
244         _rotation_node->setDoubleValue(rotation);
245     } else {
246         _last_distance_nm = 0;
247         _in_range_node->setBoolValue(false);
248         _distance_node->setDoubleValue(0);
249         _speed_node->setDoubleValue(0);
250         _time_node->setDoubleValue(0);
251         _bearing_node->setDoubleValue(0);
252         _x_shift_node->setDoubleValue(0);
253         _y_shift_node->setDoubleValue(0);
254         _rotation_node->setDoubleValue(0);
255     }
256
257                                 // If we can't find a valid station set everything to zero
258     if (!_transmitter_valid && !_mobile_valid ) {
259         _in_range_node->setBoolValue(false);
260         _distance_node->setDoubleValue(0);
261         _speed_node->setDoubleValue(0);
262         _time_node->setDoubleValue(0);
263         _bearing_node->setDoubleValue(0);
264         _x_shift_node->setDoubleValue(0);
265         _y_shift_node->setDoubleValue(0);
266         _rotation_node->setDoubleValue(0);
267         _transmitter_name = "";
268         _name_node->setStringValue(_transmitter_name.c_str());
269         _transmitter_ident = "";
270         _ident_node->setStringValue(_transmitter_ident.c_str());
271         _channel_node->setStringValue(_channel.c_str());
272         return;
273     }
274 } // end function update
275
276 void
277 TACAN::search (double frequency_mhz, double longitude_rad,
278                double latitude_rad, double altitude_m)
279 {
280     int number, i;
281     _mobile_valid = false;
282
283     SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
284
285     // reset search time
286     _time_before_search_sec = 1.0;
287
288     //try any carriers first
289     FGNavRecord *mobile_tacan
290           = globals->get_carrierlist()->findStationByFreq( frequency_mhz );
291     bool freq_valid = (mobile_tacan != NULL);
292     SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
293
294     if ( freq_valid ) {
295
296         string str1( mobile_tacan->get_name() );
297
298         SGPropertyNode * branch = fgGetNode("ai/models", true);
299         vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
300
301         number = carrier.size();
302
303         SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
304         if ( number > 0 ) {       // don't do this if there are no carriers
305             for ( i = 0; i < number; ++i ) {
306                 string str2 ( carrier[i]->getStringValue("name", ""));
307                 SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
308
309                 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
310                 string::size_type loc1= str1.find( str2, 0 );
311                 if ( loc1 != string::npos && str2 != "" ) {
312                     SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
313                     _mobile_lat = carrier[i]->getDoubleValue("position/latitude-deg");
314                     _mobile_lon = carrier[i]->getDoubleValue("position/longitude-deg");
315                     _mobile_elevation_ft = mobile_tacan->get_elev_ft();
316                     _mobile_range_nm = mobile_tacan->get_range();
317                     _mobile_bias = mobile_tacan->get_multiuse();
318                     _mobile_name = mobile_tacan->get_name();
319                     _mobile_ident = mobile_tacan->get_trans_ident();
320                     _mobile_valid = true;
321                     SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
322                     break;
323                 } else {
324                     _mobile_valid = false;
325                     SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
326                 }
327             }
328         }
329
330         SG_LOG( SG_INSTR, SG_DEBUG, "name " << _mobile_name);
331         SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
332         SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
333
334         //try any AI tankers second
335
336         if ( !_mobile_valid) {
337             SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
338
339         SGPropertyNode * branch = fgGetNode("ai/models", true);
340         vector<SGPropertyNode_ptr> tanker = branch->getChildren("aircraft");
341
342         number = tanker.size();
343
344         SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
345
346         if ( number > 0 ) {       // don't do this if there are no AI aircraft
347             for ( i = 0; i < number; ++i ) {
348                 string str4 ( tanker[i]->getStringValue("callsign", ""));
349                 SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
350
351                 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
352                 string::size_type loc1= str1.find( str4, 0 );
353                 if ( loc1 != string::npos && str4 != "" ) {
354                     SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
355                     _mobile_lat = tanker[i]->getDoubleValue("position/latitude-deg");
356                     _mobile_lon = tanker[i]->getDoubleValue("position/longitude-deg");
357                     _mobile_elevation_ft = tanker[i]->getDoubleValue("position/altitude-ft");
358                     _mobile_range_nm = mobile_tacan->get_range();
359                     _mobile_bias = mobile_tacan->get_multiuse();
360                     _mobile_name = mobile_tacan->get_name();
361                     _mobile_ident = mobile_tacan->get_trans_ident();
362                     _mobile_valid = true;
363                     SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
364                     break;
365                 } else {
366                     _mobile_valid = false;
367                     SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
368                 }
369             }
370         }
371
372         SG_LOG( SG_INSTR, SG_DEBUG, "tanker name " << _mobile_name);
373         SG_LOG( SG_INSTR, SG_DEBUG, "lat " << _mobile_lat << "lon " << _mobile_lon);
374         SG_LOG( SG_INSTR, SG_DEBUG, "elev " << _mobile_elevation_ft);
375         SG_LOG( SG_INSTR, SG_DEBUG, "range " << _mobile_range_nm);
376     }
377
378     //try any mp tankers third, if we haven't found the tanker in the ai aircraft
379
380     if ( !_mobile_valid ) {
381         SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
382
383         SGPropertyNode * branch = fgGetNode("ai/models", true);
384         vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
385
386         number = mp_tanker.size();
387
388         SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
389
390         if ( number > 0 ) {       // don't do this if there are no MP aircraft
391             for ( i = 0; i < number; ++i ) {
392                 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
393                 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
394
395                 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
396                 string::size_type loc1= str1.find( str6, 0 );
397                 if ( loc1 != string::npos && str6 != "" ) {
398                     SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
399                     _mobile_lat = mp_tanker[i]->getDoubleValue("position/latitude-deg");
400                     _mobile_lon = mp_tanker[i]->getDoubleValue("position/longitude-deg");
401                     _mobile_elevation_ft = mp_tanker[i]->getDoubleValue("position/altitude-ft");
402                     _mobile_range_nm = mobile_tacan->get_range();
403                     _mobile_bias = mobile_tacan->get_multiuse();
404                     _mobile_name = mobile_tacan->get_name();
405                     _mobile_ident = mobile_tacan->get_trans_ident();
406                     _mobile_valid = true;
407
408                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
409                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker name " << _mobile_name);
410                     SG_LOG( SG_INSTR, SG_DEBUG, " mp lat " << _mobile_lat << "lon " << _mobile_lon);
411                     SG_LOG( SG_INSTR, SG_DEBUG, " mp elev " << _mobile_elevation_ft);
412                     SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
413                     break;
414                 } else {
415                     _mobile_valid = false;
416                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
417                     }
418                 }
419             }
420         }
421     } else {
422         _mobile_valid = false;
423         SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
424     }
425
426     // try the TACAN/VORTAC list next
427     FGNavRecord *tacan
428         = globals->get_tacanlist()->findByFreq( frequency_mhz, longitude_rad,
429                                                 latitude_rad, altitude_m);
430
431     _transmitter_valid = (tacan != NULL);
432
433     if ( _transmitter_valid ) {
434         SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
435
436         _transmitter_pos = tacan->get_pos();
437         _transmitter_range_nm = tacan->get_range();
438         _transmitter_bias = tacan->get_multiuse();
439         _transmitter_name = tacan->get_name();
440         _name_node->setStringValue(_transmitter_name.c_str());
441         _transmitter_ident = tacan->get_trans_ident();
442         _ident_node->setStringValue(_transmitter_ident.c_str());
443
444         SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
445         SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
446
447     } else {
448         SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
449     }
450 }
451
452 double
453 TACAN::searchChannel (const string& channel)
454 {
455     double frequency_khz = 0;
456
457     FGTACANRecord *freq
458         = globals->get_channellist()->findByChannel( channel );
459     bool _freq_valid = (freq != NULL);
460     SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
461     if ( _freq_valid ) {
462         frequency_khz = freq->get_freq();
463         SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
464         //check sanity
465         if (frequency_khz >= 9620 && frequency_khz <= 121300)
466             return frequency_khz/100;
467     }
468     return frequency_khz = 0;
469 } // end TACAN::searchChannel
470
471 /*
472  * Listener callback. Maintains channel input properties,
473  * searches new channel frequency, updates _channel and
474  * _frequency and sets boolean _new_frequency appropriately.
475  */
476 void
477 TACAN::valueChanged(SGPropertyNode *prop)
478 {
479     if (_listener_active)
480         return;
481     _listener_active++;
482
483     int index = prop->getIndex();
484     string channel = _channel;
485
486     if (index) {  // channel digit or X/Y input
487         int c;
488         if (isdigit(c = _channel_in1_node->getStringValue()[0]))
489             channel[0] = c;
490         if (isdigit(c = _channel_in2_node->getStringValue()[0]))
491             channel[1] = c;
492         if (isdigit(c = _channel_in3_node->getStringValue()[0]))
493             channel[2] = c;
494         c = _channel_in4_node->getStringValue()[0];
495         if (c == 'X' || c == 'Y')
496             channel[3] = c;
497
498     } else {      // channel number input
499         unsigned int f = prop->getIntValue();
500         if (f >= 1 && f <= 126) {
501             channel[0] = '0' + (f / 100) % 10;
502             channel[1] = '0' + (f / 10) % 10;
503             channel[2] = '0' + f % 10;
504         }
505     }
506
507     if (channel != _channel) {
508         SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
509
510         // write back result
511         _channel_in0_node->setIntValue((channel[0] - '0') * 100
512                 + (channel[1] - '0') * 10 + (channel[2] - '0'));
513         char s[2] = "0";
514         s[0] = channel[0], _channel_in1_node->setStringValue(s);
515         s[0] = channel[1], _channel_in2_node->setStringValue(s);
516         s[0] = channel[2], _channel_in3_node->setStringValue(s);
517         s[0] = channel[3], _channel_in4_node->setStringValue(s);
518
519         // search channel frequency
520         double freq = searchChannel(channel);
521         if (freq != _frequency_mhz) {
522             SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
523             _frequency_node->setDoubleValue(freq);
524             _frequency_mhz = freq;
525             _new_frequency = true;
526         }
527
528         _channel = channel;
529         _time_before_search_sec = 0;
530     }
531
532     _listener_active--;
533 }
534
535 // end of TACAN.cxx