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