]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/tacan.cxx
Support for multiple data dirs.
[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     _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true);
119 }
120
121 void
122 TACAN::reinit ()
123 {
124     _time_before_search_sec = 0;
125 }
126
127 void
128 TACAN::update (double delta_time_sec)
129 {
130     // don't do anything when paused
131     if (delta_time_sec == 0) return;
132
133     if (!_serviceable_node->getBoolValue() || !_electrical_node->getBoolValue()) {
134         _last_distance_nm = 0;
135         _in_range_node->setBoolValue(false);
136         _distance_node->setDoubleValue(0);
137         _speed_node->setDoubleValue(0);
138         _time_node->setDoubleValue(0);
139         SG_LOG( SG_INSTR, SG_DEBUG, "skip tacan" );
140         return;
141     }
142
143   SGGeod pos(globals->get_aircraft_position());
144                                 // On timeout, scan again
145     _time_before_search_sec -= delta_time_sec;
146     if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0)
147         search(_frequency_mhz, pos);
148
149                                  // Calculate the distance to the transmitter
150
151     //calculate the bearing and range of the mobile from the aircraft
152     double mobile_az2 = 0;
153     double mobile_bearing = 0;
154     double mobile_distance = 0;
155
156     SG_LOG( SG_INSTR, SG_DEBUG, "mobile_name " << _mobile_name);
157     SG_LOG( SG_INSTR, SG_DEBUG, "mobile_valid " << _mobile_valid);
158     geo_inverse_wgs_84(pos, _mobilePos,
159                        &mobile_bearing, &mobile_az2, &mobile_distance);
160
161
162     //calculate the bearing and range of the station from the aircraft
163     double az2 = 0;
164     double bearing = 0;
165     double distance = 0;
166
167     geo_inverse_wgs_84(pos, _transmitter_pos,
168                        &bearing, &az2, &distance);
169
170
171     //select the nearer
172     if ( mobile_distance <= distance && _mobile_valid) {
173         SG_LOG( SG_INSTR, SG_DEBUG, "mobile_distance_m " << mobile_distance);
174         SG_LOG( SG_INSTR, SG_DEBUG, "distance_m " << distance);
175         bearing = mobile_bearing;
176         distance = mobile_distance;
177         _transmitter_pos.setElevationFt(_mobilePos.getElevationFt());
178         _transmitter_range_nm = _mobile_range_nm;
179         _transmitter_bias = _mobile_bias;
180         _transmitter_name = _mobile_name;
181         _name_node->setStringValue(_transmitter_name.c_str());
182         _transmitter_ident = _mobile_ident;
183         _ident_node->setStringValue(_transmitter_ident.c_str());
184         _channel_node->setStringValue(_channel.c_str());
185     }
186
187     //// calculate some values for boresight display
188     double distance_nm = distance * SG_METER_TO_NM;
189
190     //// calculate look left/right to target, without yaw correction
191     // double horiz_offset = bearing - heading;
192     //
193     // if (horiz_offset > 180.0) horiz_offset -= 360.0;
194     // if (horiz_offset < -180.0) horiz_offset += 360.0;
195
196     //// now correct look left/right for yaw
197     // horiz_offset += yaw;
198
199     // use the bearing for a plan position indicator display
200
201     double horiz_offset = bearing;
202
203     SG_LOG( SG_INSTR, SG_DEBUG, "distance_nm " << distance_nm << " bearing "
204             << bearing << " horiz_offset " << horiz_offset);
205
206     // calculate values for radar display
207     double y_shift = distance_nm * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
208     double x_shift = distance_nm * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
209
210     SG_LOG( SG_INSTR, SG_DEBUG, "y_shift " << y_shift  << " x_shift " << x_shift);
211
212     double rotation = 0;
213
214     double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
215                                   pos.getElevationFt(),
216                                    _transmitter_range_nm);
217
218     if (distance_nm <= range_nm) {
219         double speed_kt = (fabs(distance_nm - _last_distance_nm) *
220                            ((1 / delta_time_sec) * 3600.0));
221         _last_distance_nm = distance_nm;
222
223         _in_range_node->setBoolValue(true);
224         double tmp_dist = distance_nm - _transmitter_bias;
225         if ( tmp_dist < 0.0 ) {
226             tmp_dist = 0.0;
227         }
228         _distance_node->setDoubleValue( tmp_dist );
229         _speed_node->setDoubleValue(speed_kt);
230         _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0);
231         _bearing_node->setDoubleValue(bearing);
232         _x_shift_node->setDoubleValue(x_shift);
233         _y_shift_node->setDoubleValue(y_shift);
234         _rotation_node->setDoubleValue(rotation);
235     } else {
236         _last_distance_nm = 0;
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     }
246
247                                 // If we can't find a valid station set everything to zero
248     if (!_transmitter_valid && !_mobile_valid ) {
249         _in_range_node->setBoolValue(false);
250         _distance_node->setDoubleValue(0);
251         _speed_node->setDoubleValue(0);
252         _time_node->setDoubleValue(0);
253         _bearing_node->setDoubleValue(0);
254         _x_shift_node->setDoubleValue(0);
255         _y_shift_node->setDoubleValue(0);
256         _rotation_node->setDoubleValue(0);
257         _transmitter_name = "";
258         _name_node->setStringValue(_transmitter_name.c_str());
259         _transmitter_ident = "";
260         _ident_node->setStringValue(_transmitter_ident.c_str());
261         _channel_node->setStringValue(_channel.c_str());
262         return;
263     }
264 } // end function update
265
266 void
267 TACAN::search (double frequency_mhz,const SGGeod& pos)
268 {
269     int number, i;
270     _mobile_valid = false;
271
272     SG_LOG( SG_INSTR, SG_DEBUG, "tacan freq " << frequency_mhz );
273
274     // reset search time
275     _time_before_search_sec = 1.0;
276
277     //try any carriers first
278     FGNavRecord *mobile_tacan = FGNavList::findByFreq( frequency_mhz, FGNavList::carrierFilter() );
279     bool freq_valid = (mobile_tacan != NULL);
280     SG_LOG( SG_INSTR, SG_DEBUG, "mobile freqency valid " << freq_valid );
281
282     if ( freq_valid ) {
283
284         string str1( mobile_tacan->name() );
285
286         SGPropertyNode * branch = fgGetNode("ai/models", true);
287         vector<SGPropertyNode_ptr> carrier = branch->getChildren("carrier");
288
289         number = carrier.size();
290
291         SG_LOG( SG_INSTR, SG_DEBUG, "carrier " << number );
292         for ( i = 0; i < number; ++i ) {
293             string str2 ( carrier[i]->getStringValue("name", ""));
294             SG_LOG( SG_INSTR, SG_DEBUG, "carrier name " << str2 );
295
296             SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 2 " << str2 );
297             string::size_type loc1= str1.find( str2, 0 );
298             if ( loc1 != string::npos && str2 != "" ) {
299                 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
300                 _mobilePos = SGGeod::fromDegFt(
301                              carrier[i]->getDoubleValue("position/longitude-deg"),
302                              carrier[i]->getDoubleValue("position/latitude-deg"),
303                              mobile_tacan->get_elev_ft());
304                 _mobile_range_nm = mobile_tacan->get_range();
305                 _mobile_bias = mobile_tacan->get_multiuse();
306                 _mobile_name = mobile_tacan->name();
307                 _mobile_ident = mobile_tacan->get_trans_ident();
308                 _mobile_valid = true;
309                 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter valid " << _mobile_valid );
310                 break;
311             } else {
312                 _mobile_valid = false;
313                 SG_LOG( SG_INSTR, SG_DEBUG, " carrier transmitter invalid " << _mobile_valid );
314             }
315         }
316
317         //try any AI tankers second
318
319         if ( !_mobile_valid) {
320             SG_LOG( SG_INSTR, SG_DEBUG, "tanker transmitter valid start " << _mobile_valid );
321
322         SGPropertyNode * branch = fgGetNode("ai/models", true);
323         vector<SGPropertyNode_ptr> tanker = branch->getChildren("tanker");
324
325         number = tanker.size();
326
327         SG_LOG( SG_INSTR, SG_DEBUG, "tanker number " << number );
328
329         for ( i = 0; i < number; ++i ) {
330             string str4 ( tanker[i]->getStringValue("callsign", ""));
331             SG_LOG( SG_INSTR, SG_DEBUG, "tanker callsign " << str4 );
332
333             SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 4 " << str4 );
334             string::size_type loc1= str1.find( str4, 0 );
335             if ( loc1 != string::npos && str4 != "" ) {
336                 SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
337                 _mobilePos = SGGeod::fromDegFt(
338                                              tanker[i]->getDoubleValue("position/longitude-deg"),
339                                              tanker[i]->getDoubleValue("position/latitude-deg"),
340                                              tanker[i]->getDoubleValue("position/altitude-ft"));
341
342               
343                 _mobile_range_nm = mobile_tacan->get_range();
344                 _mobile_bias = mobile_tacan->get_multiuse();
345                 _mobile_name = mobile_tacan->name();
346                 _mobile_ident = mobile_tacan->get_trans_ident();
347                 _mobile_valid = true;
348                 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter valid " << _mobile_valid );
349                 break;
350             } else {
351                 _mobile_valid = false;
352                 SG_LOG( SG_INSTR, SG_DEBUG, " tanker transmitter invalid " << _mobile_valid );
353             }
354         }
355     }
356
357     //try any mp tankers third, if we haven't found the tanker in the ai aircraft
358
359     if ( !_mobile_valid ) {
360         SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid start " << _mobile_valid );
361
362         SGPropertyNode * branch = fgGetNode("ai/models", true);
363         vector<SGPropertyNode_ptr> mp_tanker = branch->getChildren("multiplayer");
364
365         number = mp_tanker.size();
366
367         SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker number " << number );
368
369         if ( number > 0 ) {       // don't do this if there are no MP aircraft
370             for ( i = 0; i < number; ++i ) {
371                 string str6 ( mp_tanker[i]->getStringValue("callsign", ""));
372                 SG_LOG( SG_INSTR, SG_DEBUG, "mp tanker callsign " << str6 );
373
374                 SG_LOG( SG_INSTR, SG_DEBUG, "strings 1 " << str1 << " 5 " << str6 );
375                 string::size_type loc1= str1.find( str6, 0 );
376                 if ( loc1 != string::npos && str6 != "" ) {
377                     SG_LOG( SG_INSTR, SG_DEBUG, " string found" );
378                   _mobilePos = SGGeod::fromDegFt(
379                                                  mp_tanker[i]->getDoubleValue("position/longitude-deg"),
380                                                  mp_tanker[i]->getDoubleValue("position/latitude-deg"),
381                                                  mp_tanker[i]->getDoubleValue("position/altitude-ft"));
382
383                   
384                     _mobile_range_nm = mobile_tacan->get_range();
385                     _mobile_bias = mobile_tacan->get_multiuse();
386                     _mobile_name = mobile_tacan->name();
387                     _mobile_ident = mobile_tacan->get_trans_ident();
388                     _mobile_valid = true;
389
390                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter valid " << _mobile_valid );
391                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker name " << _mobile_name);
392                     SG_LOG( SG_INSTR, SG_DEBUG, " mp range " << _mobile_range_nm);
393                     break;
394                 } else {
395                     _mobile_valid = false;
396                     SG_LOG( SG_INSTR, SG_DEBUG, " mp tanker transmitter invalid " << _mobile_valid );
397                     }
398                 }
399             }
400         }
401     } else {
402         _mobile_valid = false;
403         SG_LOG( SG_INSTR, SG_DEBUG, " mobile transmitter invalid " << _mobile_valid );
404     }
405
406     // try the TACAN/VORTAC list next
407     FGNavRecord *tacan = FGNavList::findByFreq( frequency_mhz, pos, FGNavList::tacanFilter() );
408
409     _transmitter_valid = (tacan != NULL);
410
411     if ( _transmitter_valid ) {
412         SG_LOG( SG_INSTR, SG_DEBUG, "transmitter valid " << _transmitter_valid );
413
414         _transmitter_pos = tacan->geod();
415         _transmitter_range_nm = tacan->get_range();
416         _transmitter_bias = tacan->get_multiuse();
417         _transmitter_name = tacan->name();
418         _name_node->setStringValue(_transmitter_name.c_str());
419         _transmitter_ident = tacan->get_trans_ident();
420         _ident_node->setStringValue(_transmitter_ident.c_str());
421
422         SG_LOG( SG_INSTR, SG_DEBUG, "name " << _transmitter_name);
423         SG_LOG( SG_INSTR, SG_DEBUG, _transmitter_pos);
424
425     } else {
426         SG_LOG( SG_INSTR, SG_DEBUG, "transmitter invalid " << _transmitter_valid );
427     }
428 }
429
430 double
431 TACAN::searchChannel (const string& channel)
432 {
433     double frequency_khz = 0;
434
435     FGTACANRecord *freq
436         = globals->get_channellist()->findByChannel( channel );
437     bool _freq_valid = (freq != NULL);
438     SG_LOG( SG_INSTR, SG_DEBUG, "freq valid " << _freq_valid );
439     if ( _freq_valid ) {
440         frequency_khz = freq->get_freq();
441         SG_LOG( SG_INSTR, SG_DEBUG, "freq output " << frequency_khz );
442         //check sanity
443         if (frequency_khz >= 9620 && frequency_khz <= 121300)
444             return frequency_khz/100;
445     }
446     return frequency_khz = 0;
447 } // end TACAN::searchChannel
448
449 /*
450  * Listener callback. Maintains channel input properties,
451  * searches new channel frequency, updates _channel and
452  * _frequency and sets boolean _new_frequency appropriately.
453  */
454 void
455 TACAN::valueChanged(SGPropertyNode *prop)
456 {
457     if (_listener_active)
458         return;
459     _listener_active++;
460
461     int index = prop->getIndex();
462     string channel = _channel;
463
464     if (index) {  // channel digit or X/Y input
465         int c;
466         if (isdigit(c = _channel_in1_node->getStringValue()[0]))
467             channel[0] = c;
468         if (isdigit(c = _channel_in2_node->getStringValue()[0]))
469             channel[1] = c;
470         if (isdigit(c = _channel_in3_node->getStringValue()[0]))
471             channel[2] = c;
472         c = _channel_in4_node->getStringValue()[0];
473         if (c == 'X' || c == 'Y')
474             channel[3] = c;
475
476     } else {      // channel number input
477         unsigned int f = prop->getIntValue();
478         if (f >= 1 && f <= 126) {
479             channel[0] = '0' + (f / 100) % 10;
480             channel[1] = '0' + (f / 10) % 10;
481             channel[2] = '0' + f % 10;
482         }
483     }
484
485     if (channel != _channel) {
486         SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel);
487
488         // write back result
489         _channel_in0_node->setIntValue((channel[0] - '0') * 100
490                 + (channel[1] - '0') * 10 + (channel[2] - '0'));
491         char s[2] = "0";
492         s[0] = channel[0], _channel_in1_node->setStringValue(s);
493         s[0] = channel[1], _channel_in2_node->setStringValue(s);
494         s[0] = channel[2], _channel_in3_node->setStringValue(s);
495         s[0] = channel[3], _channel_in4_node->setStringValue(s);
496
497         // search channel frequency
498         double freq = searchChannel(channel);
499         if (freq != _frequency_mhz) {
500             SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq);
501             _frequency_node->setDoubleValue(freq);
502             _frequency_mhz = freq;
503             _new_frequency = true;
504         }
505
506         _channel = channel;
507         _time_before_search_sec = 0;
508     }
509
510     _listener_active--;
511 }
512
513 // end of TACAN.cxx