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