]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/tacan.cxx
Improve gyro heading indicator failure simulation
[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 using std::vector;
21 using std::string;
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     _listener_active(0)
62 {
63 }
64
65 TACAN::~TACAN ()
66 {
67 }
68
69 void
70 TACAN::init ()
71 {
72     string branch;
73     branch = "/instrumentation/" + _name;
74
75     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
76
77     _serviceable_node = node->getChild("serviceable", 0, true);
78     _ident_node = node->getChild("ident", 0, true);
79
80     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
81     _frequency_node = fnode->getChild("selected-mhz", 0, true);
82
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);
88
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);
94
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);
101
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);
107
108     SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
109     _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
110
111     SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
112     _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
113
114     SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
115     _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
116
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);
120 }
121
122 void
123 TACAN::reinit ()
124 {
125     _time_before_search_sec = 0;
126 }
127
128 void
129 TACAN::update (double delta_time_sec)
130 {
131     // don't do anything when paused
132     if (delta_time_sec == 0) return;
133
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" );
141         return;
142     }
143
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);
149
150                                  // Calculate the distance to the transmitter
151
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;
156
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);
161
162
163     //calculate the bearing and range of the station from the aircraft
164     double az2 = 0;
165     double bearing = 0;
166     double distance = 0;
167
168     geo_inverse_wgs_84(pos, _transmitter_pos,
169                        &bearing, &az2, &distance);
170
171
172     //select the nearer
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());
186     }
187
188     //// calculate some values for boresight display
189     double distance_nm = distance * SG_METER_TO_NM;
190
191     //// calculate look left/right to target, without yaw correction
192     // double horiz_offset = bearing - heading;
193     //
194     // if (horiz_offset > 180.0) horiz_offset -= 360.0;
195     // if (horiz_offset < -180.0) horiz_offset += 360.0;
196
197     //// now correct look left/right for yaw
198     // horiz_offset += yaw;
199
200     // use the bearing for a plan position indicator display
201
202     double horiz_offset = bearing;
203
204     SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
205             << bearing << " horiz_offset " << horiz_offset);
206
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);
210
211     SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift  << " x_shift " << x_shift);
212
213     double rotation = 0;
214
215     double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
216                                   pos.getElevationFt(),
217                                    _transmitter_range_nm);
218
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;
223
224         _in_range_node->setBoolValue(true);
225         double tmp_dist = distance_nm - _transmitter_bias;
226         if ( tmp_dist < 0.0 ) {
227             tmp_dist = 0.0;
228         }
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);
236     } else {
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);
246     }
247
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());
263         return;
264     }
265 } // end function update
266
267 void
268 TACAN::search (double frequency_mhz,const SGGeod& pos)
269 {
270     int number, i;
271     _mobile_valid = false;
272
273     SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
274
275     // reset search time
276     _time_before_search_sec = 1.0;
277
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 );
282
283     if ( freq_valid ) {
284
285         string str1( mobile_tacan->name() );
286
287         SGPropertyNode * branch = fgGetNode("ai/models", true);
288         vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
289
290         number = carrier.size();
291
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 );
296
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 );
311                 break;
312             } else {
313                 _mobile_valid = false;
314                 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
315             }
316         }
317
318         //try any AI tankers second
319
320         if ( !_mobile_valid) {
321             SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
322
323         SGPropertyNode * branch = fgGetNode("ai/models", true);
324         vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
325
326         number = tanker.size();
327
328         SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
329
330         for ( i = 0; i < number; ++i ) {
331             string str4 ( tanker[i]->getStringValue("callsign", ""));
332             SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
333
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"));
342
343               
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 );
350                 break;
351             } else {
352                 _mobile_valid = false;
353                 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
354             }
355         }
356     }
357
358     //try any mp tankers third, if we haven't found the tanker in the ai aircraft
359
360     if ( !_mobile_valid ) {
361         SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
362
363         SGPropertyNode * branch = fgGetNode("ai/models", true);
364         vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
365
366         number = mp_tanker.size();
367
368         SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
369
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 );
374
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"));
383
384                   
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;
390
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);
394                     break;
395                 } else {
396                     _mobile_valid = false;
397                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
398                     }
399                 }
400             }
401         }
402     } else {
403         _mobile_valid = false;
404         SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
405     }
406
407     // try the TACAN/VORTAC list next
408     FGNavRecord *tacan = FGNavList::findByFreq( frequency_mhz, pos, FGNavList::tacanFilter() );
409
410     _transmitter_valid = (tacan != NULL);
411
412     if ( _transmitter_valid ) {
413         SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
414
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());
422
423         SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
424         SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
425
426     } else {
427         SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
428     }
429 }
430
431 double
432 TACAN::searchChannel (const string& channel)
433 {
434     double frequency_khz = 0;
435
436     FGTACANRecord *freq
437         = globals->get_channellist()->findByChannel( channel );
438     bool _freq_valid = (freq != NULL);
439     SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
440     if ( _freq_valid ) {
441         frequency_khz = freq->get_freq();
442         SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
443         //check sanity
444         if (frequency_khz >= 9620 && frequency_khz <= 121300)
445             return frequency_khz/100;
446     }
447     return frequency_khz = 0;
448 } // end TACAN::searchChannel
449
450 /*
451  * Listener callback. Maintains channel input properties,
452  * searches new channel frequency, updates _channel and
453  * _frequency and sets boolean _new_frequency appropriately.
454  */
455 void
456 TACAN::valueChanged(SGPropertyNode *prop)
457 {
458     if (_listener_active)
459         return;
460     _listener_active++;
461
462     int index = prop->getIndex();
463     string channel = _channel;
464
465     if (index) {  // channel digit or X/Y input
466         int c;
467         if (isdigit(c = _channel_in1_node->getStringValue()[0]))
468             channel[0] = c;
469         if (isdigit(c = _channel_in2_node->getStringValue()[0]))
470             channel[1] = c;
471         if (isdigit(c = _channel_in3_node->getStringValue()[0]))
472             channel[2] = c;
473         c = _channel_in4_node->getStringValue()[0];
474         if (c == 'X' || c == 'Y')
475             channel[3] = c;
476
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;
483         }
484     }
485
486     if (channel != _channel) {
487         SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
488
489         // write back result
490         _channel_in0_node->setIntValue((channel[0] - '0') * 100
491                 + (channel[1] - '0') * 10 + (channel[2] - '0'));
492         char s[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);
497
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;
505         }
506
507         _channel = channel;
508         _time_before_search_sec = 0;
509     }
510
511     _listener_active--;
512 }
513
514 // end of TACAN.cxx