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