]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/tacan.cxx
fix another crash at the poles
[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     return range_nm + (range_nm * rand * rand);
44 }
45
46
47 TACAN::TACAN ( SGPropertyNode *node ) :
48     _name(node->getStringValue("name", "tacan")),
49     _num(node->getIntValue("number", 0)),
50     _new_frequency(false),
51     _channel("0000"),
52     _last_distance_nm(0),
53     _frequency_mhz(-1),
54     _time_before_search_sec(0),
55     _mobile_valid(false),
56     _transmitter_valid(false),
57     _transmitter_pos(SGGeod::fromDeg(0, 0)),
58     _transmitter_range_nm(0),
59     _transmitter_bias(0.0),
60     _listener_active(0)
61 {
62 }
63
64 TACAN::~TACAN ()
65 {
66 }
67
68 void
69 TACAN::init ()
70 {
71     string branch = "/instrumentation/" + _name;
72
73     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
74
75     _serviceable_node = node->getChild("serviceable", 0, true);
76     _ident_node = node->getChild("ident", 0, true);
77
78     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
79     _frequency_node = fnode->getChild("selected-mhz", 0, true);
80
81     _channel_in0_node = fnode->getChild("selected-channel", 0, true);
82     _channel_in1_node = fnode->getChild("selected-channel", 1, true);
83     _channel_in2_node = fnode->getChild("selected-channel", 2, true);
84     _channel_in3_node = fnode->getChild("selected-channel", 3, true);
85     _channel_in4_node = fnode->getChild("selected-channel", 4, true);
86
87     _channel_in0_node->addChangeListener(this);
88     _channel_in1_node->addChangeListener(this);
89     _channel_in2_node->addChangeListener(this);
90     _channel_in3_node->addChangeListener(this);
91     _channel_in4_node->addChangeListener(this, true);
92
93     _in_range_node = node->getChild("in-range", 0, true);
94     _distance_node = node->getChild("indicated-distance-nm", 0, true);
95     _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
96     _time_node = node->getChild("indicated-time-min", 0, true);
97     _name_node = node->getChild("name", 0, true);
98     _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true);
99
100     SGPropertyNode *dnode = node->getChild("display", 0, true);
101     _x_shift_node = dnode->getChild("x-shift", 0, true);
102     _y_shift_node = dnode->getChild("y-shift", 0, true);
103     _rotation_node = dnode->getChild("rotation", 0, true);
104     _channel_node = dnode->getChild("channel", 0, true);
105
106     SGPropertyNode *cnode = fgGetNode("/ai/models/carrier", _num, false );
107     _carrier_name_node = cnode ? cnode->getChild("name", 0, false) : 0;
108
109     SGPropertyNode *tnode = fgGetNode("/ai/models/aircraft", _num, false);
110     _tanker_callsign_node = tnode ? tnode->getChild("callsign", 0, false) : 0;
111
112     SGPropertyNode *mnode = fgGetNode("/ai/models/multiplayer", _num, false);
113     _mp_callsign_node = mnode ? mnode->getChild("callsign", 0, false) : 0;
114
115     _heading_node = fgGetNode("/orientation/heading-deg", true);
116     _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
117 }
118
119 void
120 TACAN::reinit ()
121 {
122     _time_before_search_sec = 0;
123 }
124
125 void
126 TACAN::update (double delta_time_sec)
127 {
128     // don't do anything when paused
129     if (delta_time_sec == 0) return;
130
131     if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
132         _last_distance_nm = 0;
133         _in_range_node->setBoolValue(false);
134         _distance_node->setDoubleValue(0);
135         _speed_node->setDoubleValue(0);
136         _time_node->setDoubleValue(0);
137         return;
138     }
139
140     SGGeod pos(globals->get_aircraft_position());
141                                 // On timeout, scan again
142     _time_before_search_sec -= delta_time_sec;
143     if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
144         search(_frequency_mhz, pos);
145
146                                  // Calculate the distance to the transmitter
147
148     //calculate the bearing and range of the mobile from the aircraft
149     double mobile_az2 = 0;
150     double mobile_bearing = 0;
151     double mobile_distance = 0;
152     if (_mobile_valid) {
153         geo_inverse_wgs_84(pos, _mobilePos,
154                        &mobile_bearing, &mobile_az2, &mobile_distance);
155     }
156
157     //calculate the bearing and range of the station from the aircraft
158     double az2 = 0;
159     double bearing = 0;
160     double distance = 0;
161     if (_transmitter_valid) {
162         geo_inverse_wgs_84(pos, _transmitter_pos,
163                            &bearing, &az2, &distance);
164     }
165
166     //select the nearer
167     if ( mobile_distance <= distance && _mobile_valid) {
168         bearing = mobile_bearing;
169         distance = mobile_distance;
170         _transmitter_pos.setElevationFt(_mobilePos.getElevationFt());
171         _transmitter_range_nm = _mobile_range_nm;
172         _transmitter_bias = _mobile_bias;
173         _transmitter_name = _mobile_name;
174         _name_node->setStringValue(_transmitter_name.c_str());
175         _transmitter_ident = _mobile_ident;
176         _ident_node->setStringValue(_transmitter_ident.c_str());
177         _channel_node->setStringValue(_channel.c_str());
178     }
179
180     //// calculate some values for boresight display
181     double distance_nm = distance * SG_METER_TO_NM;
182
183     //// calculate look left/right to target, without yaw correction
184     // double horiz_offset = bearing - heading;
185     //
186     // if (horiz_offset > 180.0) horiz_offset -= 360.0;
187     // if (horiz_offset < -180.0) horiz_offset += 360.0;
188
189     //// now correct look left/right for yaw
190     // horiz_offset += yaw;
191
192     // use the bearing for a plan position indicator display
193
194     double horiz_offset = bearing;
195
196     // calculate values for radar display
197     double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
198     double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
199
200     double rotation = 0;
201
202     double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
203                                   pos.getElevationFt(),
204                                    _transmitter_range_nm);
205
206     if (distance_nm <= range_nm) {
207         double speed_kt = (fabs(distance_nm - _last_distance_nm) *
208                            ((1 / delta_time_sec) * 3600.0));
209         _last_distance_nm = distance_nm;
210
211         _in_range_node->setBoolValue(true);
212         double tmp_dist = distance_nm - _transmitter_bias;
213         if ( tmp_dist < 0.0 ) {
214             tmp_dist = 0.0;
215         }
216         _distance_node->setDoubleValue( tmp_dist );
217         _speed_node->setDoubleValue(speed_kt);
218         _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0);
219         _bearing_node->setDoubleValue(bearing);
220         _x_shift_node->setDoubleValue(x_shift);
221         _y_shift_node->setDoubleValue(y_shift);
222         _rotation_node->setDoubleValue(rotation);
223     } else {
224         _last_distance_nm = 0;
225         _in_range_node->setBoolValue(false);
226         _distance_node->setDoubleValue(0);
227         _speed_node->setDoubleValue(0);
228         _time_node->setDoubleValue(0);
229         _bearing_node->setDoubleValue(0);
230         _x_shift_node->setDoubleValue(0);
231         _y_shift_node->setDoubleValue(0);
232         _rotation_node->setDoubleValue(0);
233     }
234
235                                 // If we can't find a valid station set everything to zero
236     if (!_transmitter_valid && !_mobile_valid ) {
237         _in_range_node->setBoolValue(false);
238         _distance_node->setDoubleValue(0);
239         _speed_node->setDoubleValue(0);
240         _time_node->setDoubleValue(0);
241         _bearing_node->setDoubleValue(0);
242         _x_shift_node->setDoubleValue(0);
243         _y_shift_node->setDoubleValue(0);
244         _rotation_node->setDoubleValue(0);
245         _transmitter_name = "";
246         _name_node->setStringValue(_transmitter_name.c_str());
247         _transmitter_ident = "";
248         _ident_node->setStringValue(_transmitter_ident.c_str());
249         _channel_node->setStringValue(_channel.c_str());
250         return;
251     }
252 } // end function update
253
254 void
255 TACAN::search (double frequency_mhz,const SGGeod& pos)
256 {
257     int number, i;
258     _mobile_valid = false;
259
260     // reset search time
261     _time_before_search_sec = 1.0;
262
263     //try any carriers first
264     FGNavRecord *mobile_tacan = FGNavList::findByFreq( frequency_mhz, FGNavList::carrierFilter() );
265     bool freq_valid = (mobile_tacan != NULL);
266
267     if ( freq_valid ) {
268
269         string str1( mobile_tacan->name() );
270
271         SGPropertyNode * branch = fgGetNode("ai/models", true);
272         vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
273
274         number = carrier.size();
275
276         for ( i = 0; i < number; ++i ) {
277             string str2 ( carrier[i]->getStringValue("name", ""));
278
279             string::size_type loc1= str1.find( str2, 0 );
280             if ( loc1 != string::npos && str2 != "" ) {
281                 _mobilePos = SGGeod::fromDegFt(
282                              carrier[i]->getDoubleValue("position/longitude-deg"),
283                              carrier[i]->getDoubleValue("position/latitude-deg"),
284                              mobile_tacan->get_elev_ft());
285                 _mobile_range_nm = mobile_tacan->get_range();
286                 _mobile_bias = mobile_tacan->get_multiuse();
287                 _mobile_name = mobile_tacan->name();
288                 _mobile_ident = mobile_tacan->get_trans_ident();
289                 _mobile_valid = true;
290                 break;
291             } else {
292                 _mobile_valid = false;
293             }
294         }
295
296         //try any AI tankers second
297         if ( !_mobile_valid) {
298         SGPropertyNode * branch = fgGetNode("ai/models", true);
299         vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
300
301         number = tanker.size();
302
303         for ( i = 0; i < number; ++i ) {
304             string str4 ( tanker[i]->getStringValue("callsign", ""));
305             string::size_type loc1= str1.find( str4, 0 );
306             if ( loc1 != string::npos && str4 != "" ) {
307                 _mobilePos = SGGeod::fromDegFt(
308                                              tanker[i]->getDoubleValue("position/longitude-deg"),
309                                              tanker[i]->getDoubleValue("position/latitude-deg"),
310                                              tanker[i]->getDoubleValue("position/altitude-ft"));
311
312               
313                 _mobile_range_nm = mobile_tacan->get_range();
314                 _mobile_bias = mobile_tacan->get_multiuse();
315                 _mobile_name = mobile_tacan->name();
316                 _mobile_ident = mobile_tacan->get_trans_ident();
317                 _mobile_valid = true;
318                 break;
319             } else {
320                 _mobile_valid = false;
321             }
322         }
323     }
324
325     //try any mp tankers third, if we haven't found the tanker in the ai aircraft
326
327     if ( !_mobile_valid ) {
328         SGPropertyNode * branch = fgGetNode("ai/models", true);
329         vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
330
331         number = mp_tanker.size();
332
333         if ( number > 0 ) {       // don't do this if there are no MP aircraft
334             for ( i = 0; i < number; ++i ) {
335                 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
336                 string::size_type loc1= str1.find( str6, 0 );
337                 if ( loc1 != string::npos && str6 != "" ) {
338                   _mobilePos = SGGeod::fromDegFt(
339                                                  mp_tanker[i]->getDoubleValue("position/longitude-deg"),
340                                                  mp_tanker[i]->getDoubleValue("position/latitude-deg"),
341                                                  mp_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                     break;
350                 } else {
351                     _mobile_valid = false;
352                 }
353                 }
354             }
355         }
356     } else {
357         _mobile_valid = false;
358     }
359
360     // try the TACAN/VORTAC list next
361     FGNavRecord *tacan = FGNavList::findByFreq( frequency_mhz, pos, FGNavList::tacanFilter() );
362
363     _transmitter_valid = (tacan != NULL);
364
365     if ( _transmitter_valid ) {
366
367         _transmitter_pos = tacan->geod();
368         _transmitter_range_nm = tacan->get_range();
369         _transmitter_bias = tacan->get_multiuse();
370         _transmitter_name = tacan->name();
371         _name_node->setStringValue(_transmitter_name.c_str());
372         _transmitter_ident = tacan->get_trans_ident();
373         _ident_node->setStringValue(_transmitter_ident.c_str());
374     }
375 }
376
377 double
378 TACAN::searchChannel (const string& channel)
379 {
380     double frequency_khz = 0;
381
382     FGTACANRecord *freq
383         = globals->get_channellist()->findByChannel( channel );
384     bool _freq_valid = (freq != NULL);
385     SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
386     if ( _freq_valid ) {
387         frequency_khz = freq->get_freq();
388         SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
389         //check sanity
390         if (frequency_khz >= 9620 && frequency_khz <= 121300)
391             return frequency_khz/100;
392     }
393     return frequency_khz = 0;
394 } // end TACAN::searchChannel
395
396 /*
397  * Listener callback. Maintains channel input properties,
398  * searches new channel frequency, updates _channel and
399  * _frequency and sets boolean _new_frequency appropriately.
400  */
401 void
402 TACAN::valueChanged(SGPropertyNode *prop)
403 {
404     if (_listener_active)
405         return;
406     _listener_active++;
407
408     int index = prop->getIndex();
409     string channel = _channel;
410
411     if (index) {  // channel digit or X/Y input
412         int c;
413         if (isdigit(c = _channel_in1_node->getStringValue()[0]))
414             channel[0] = c;
415         if (isdigit(c = _channel_in2_node->getStringValue()[0]))
416             channel[1] = c;
417         if (isdigit(c = _channel_in3_node->getStringValue()[0]))
418             channel[2] = c;
419         c = _channel_in4_node->getStringValue()[0];
420         if (c == 'X' || c == 'Y')
421             channel[3] = c;
422
423     } else {      // channel number input
424         unsigned int f = prop->getIntValue();
425         if (f >= 1 && f <= 126) {
426             channel[0] = '0' + (f / 100) % 10;
427             channel[1] = '0' + (f / 10) % 10;
428             channel[2] = '0' + f % 10;
429         }
430     }
431
432     if (channel != _channel) {
433         SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
434
435         // write back result
436         _channel_in0_node->setIntValue((channel[0] - '0') * 100
437                 + (channel[1] - '0') * 10 + (channel[2] - '0'));
438         char s[2] = "0";
439         s[0] = channel[0], _channel_in1_node->setStringValue(s);
440         s[0] = channel[1], _channel_in2_node->setStringValue(s);
441         s[0] = channel[2], _channel_in3_node->setStringValue(s);
442         s[0] = channel[3], _channel_in4_node->setStringValue(s);
443
444         // search channel frequency
445         double freq = searchChannel(channel);
446         if (freq != _frequency_mhz) {
447             SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
448             _frequency_node->setDoubleValue(freq);
449             _frequency_mhz = freq;
450             _new_frequency = true;
451         }
452
453         _channel = channel;
454         _time_before_search_sec = 0;
455     }
456
457     _listener_active--;
458 }
459
460 // end of TACAN.cxx