]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/adf.cxx
Make more use of SGGeod
[flightgear.git] / src / Instrumentation / adf.cxx
1 // adf.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
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 <Main/util.hxx>
16 #include <Navaids/navlist.hxx>
17
18 #include "adf.hxx"
19
20 #include <iostream>
21 #include <string>
22 #include <sstream>
23
24
25 // Use a bigger number to be more responsive, or a smaller number
26 // to be more sluggish.
27 #define RESPONSIVENESS 0.5
28
29
30 /**
31  * Fiddle with the reception range a bit.
32  *
33  * TODO: better reception at night (??).
34  */
35 static double
36 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
37               double max_range_nm)
38 {
39     double delta_elevation_ft =
40         aircraft_altitude_ft - transmitter_elevation_ft;
41     double range_nm = max_range_nm;
42
43                                 // kludge slightly better reception at
44                                 // altitude
45     if (delta_elevation_ft < 0)
46         delta_elevation_ft = 200;
47     if (delta_elevation_ft <= 1000)
48         range_nm *= sqrt(delta_elevation_ft / 1000);
49     else if (delta_elevation_ft >= 5000)
50         range_nm *= sqrt(delta_elevation_ft / 5000);
51     if (range_nm >= max_range_nm * 3)
52         range_nm = max_range_nm * 3;
53
54     double rand = sg_random();
55     return range_nm + (range_nm * rand * rand);
56 }
57
58
59 ADF::ADF (SGPropertyNode *node )
60     :
61     _time_before_search_sec(0),
62     _last_frequency_khz(-1),
63     _transmitter_valid(false),
64     _transmitter_pos(SGGeod::fromDeg(0, 0)),
65     _transmitter_cart(0, 0, 0),
66     _transmitter_range_nm(0),
67     _ident_count(0),
68     _last_ident_time(0),
69     _last_volume(-1),
70     name("adf"),
71     num(0)
72 {
73     int i;
74     for ( i = 0; i < node->nChildren(); ++i ) {
75         SGPropertyNode *child = node->getChild(i);
76         string cname = child->getName();
77         string cval = child->getStringValue();
78         if ( cname == "name" ) {
79             name = cval;
80         } else if ( cname == "number" ) {
81             num = child->getIntValue();
82         } else {
83             SG_LOG( SG_INSTR, SG_WARN, "Error in adf config logic" );
84             if ( name.length() ) {
85                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
86             }
87         }
88     }
89 }
90
91
92 ADF::ADF ()
93     : _time_before_search_sec(0),
94       _last_frequency_khz(-1),
95       _transmitter_valid(false),
96       _transmitter_pos(SGGeod::fromDeg(0, 0)),
97       _transmitter_cart(0, 0, 0),
98       _transmitter_range_nm(0),
99       _ident_count(0),
100       _last_ident_time(0),
101       _last_volume(-1),
102       name("adf"),
103       num(0)
104 {
105 }
106
107 ADF::~ADF ()
108 {
109 }
110
111 void
112 ADF::init ()
113 {
114     string branch;
115     branch = "/instrumentation/" + name;
116
117     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
118     _longitude_node = fgGetNode("/position/longitude-deg", true);
119     _latitude_node = fgGetNode("/position/latitude-deg", true);
120     _altitude_node = fgGetNode("/position/altitude-ft", true);
121     _heading_node = fgGetNode("/orientation/heading-deg", true);
122     _serviceable_node = node->getChild("serviceable", 0, true);
123     _error_node = node->getChild("error-deg", 0, true);
124     _electrical_node = fgGetNode("/systems/electrical/outputs/adf", true);
125     branch = branch + "/frequencies";
126     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
127     _frequency_node = fnode->getChild("selected-khz", 0, true);
128     _mode_node = node->getChild("mode", 0, true);
129     _volume_node = node->getChild("volume-norm", 0, true);
130     _in_range_node = node->getChild("in-range", 0, true);
131     _bearing_node = node->getChild("indicated-bearing-deg", 0, true);
132     _ident_node = node->getChild("ident", 0, true);
133     _ident_audible_node = node->getChild("ident-audible", 0, true);
134
135     morse.init();
136
137     std::ostringstream temp;
138     temp << name << num;
139     adf_ident = temp.str();
140 }
141
142 void
143 ADF::update (double delta_time_sec)
144 {
145                                 // If it's off, don't waste any time.
146     if (!_electrical_node->getBoolValue() ||
147         !_serviceable_node->getBoolValue()) {
148         set_bearing(delta_time_sec, 90);
149         _ident_node->setStringValue("");
150         return;
151     }
152
153                                 // Get the frequency
154     int frequency_khz = _frequency_node->getIntValue();
155     if (frequency_khz != _last_frequency_khz) {
156         _time_before_search_sec = 0;
157         _last_frequency_khz = frequency_khz;
158     }
159
160                                 // Get the aircraft position
161     double longitude_deg = _longitude_node->getDoubleValue();
162     double latitude_deg = _latitude_node->getDoubleValue();
163     double altitude_m = _altitude_node->getDoubleValue();
164
165     double longitude_rad = longitude_deg * SGD_DEGREES_TO_RADIANS;
166     double latitude_rad = latitude_deg * SGD_DEGREES_TO_RADIANS;
167
168                                 // On timeout, scan again
169     _time_before_search_sec -= delta_time_sec;
170     if (_time_before_search_sec < 0)
171         search(frequency_khz, longitude_rad, latitude_rad, altitude_m);
172
173                                 // If it's off, don't bother.
174     string mode = _mode_node->getStringValue();
175     if (!_transmitter_valid || (mode != "bfo" && mode != "adf"))
176     {
177         set_bearing(delta_time_sec, 90);
178         _ident_node->setStringValue("");
179         return;
180     }
181
182                                 // Calculate the bearing to the transmitter
183     SGGeod geod = SGGeod::fromRadM(longitude_rad, latitude_rad, altitude_m);
184     SGVec3d location = SGVec3d::fromGeod(geod);
185     
186     double distance_nm = dist(_transmitter_cart, location) * SG_METER_TO_NM;
187     double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
188                                    altitude_m * SG_METER_TO_FEET,
189                                    _transmitter_range_nm);
190     if (distance_nm <= range_nm) {
191
192         double bearing, az2, s;
193         double heading = _heading_node->getDoubleValue();
194
195         geo_inverse_wgs_84(geod, _transmitter_pos,
196                            &bearing, &az2, &s);
197         _in_range_node->setBoolValue(true);
198
199         bearing -= heading;
200         if (bearing < 0)
201             bearing += 360;
202         set_bearing(delta_time_sec, bearing);
203
204         // adf ident sound
205         double volume;
206         if ( _ident_audible_node->getBoolValue() )
207             volume = _volume_node->getDoubleValue();
208         else
209             volume = 0.0;
210
211         if ( volume != _last_volume ) {
212             _last_volume = volume;
213
214             SGSoundSample *sound;
215             sound = globals->get_soundmgr()->find( adf_ident );
216             if ( sound != NULL )
217                 sound->set_volume( volume );
218             else
219                 SG_LOG( SG_GENERAL, SG_ALERT, "Can't find adf-ident sound" );
220         }
221
222         time_t cur_time = globals->get_time_params()->get_cur_time();
223         if ( _last_ident_time < cur_time - 30 ) {
224             _last_ident_time = cur_time;
225             _ident_count = 0;
226         }
227
228         if ( _ident_count < 4 ) {
229             if ( !globals->get_soundmgr()->is_playing(adf_ident) ) {
230                 globals->get_soundmgr()->play_once( adf_ident );
231                 ++_ident_count;
232             }
233         }
234     } else {
235         _in_range_node->setBoolValue(false);
236         set_bearing(delta_time_sec, 90);
237         _ident_node->setStringValue("");
238         globals->get_soundmgr()->stop( adf_ident );
239     }
240 }
241
242 void
243 ADF::search (double frequency_khz, double longitude_rad,
244              double latitude_rad, double altitude_m)
245 {
246     string ident = "";
247                                 // reset search time
248     _time_before_search_sec = 1.0;
249
250                                 // try the ILS list first
251     FGNavRecord *nav =
252         globals->get_navlist()->findByFreq(frequency_khz, longitude_rad,
253                                            latitude_rad, altitude_m);
254
255     _transmitter_valid = (nav != NULL);
256     if ( _transmitter_valid ) {
257         ident = nav->get_trans_ident();
258         if ( ident != _last_ident ) {
259             _transmitter_pos = nav->get_pos();
260             _transmitter_cart = nav->get_cart();
261             _transmitter_range_nm = nav->get_range();
262         }
263     }
264
265     if ( _last_ident != ident ) {
266         _last_ident = ident;
267         _ident_node->setStringValue(ident.c_str());
268
269         if ( globals->get_soundmgr()->exists( adf_ident ) ) {
270             // stop is required! -- remove alone wouldn't stop immediately
271             globals->get_soundmgr()->stop( adf_ident );
272             globals->get_soundmgr()->remove( adf_ident );
273         }
274
275         SGSoundSample *sound;
276         sound = morse.make_ident( ident, LO_FREQUENCY );
277         sound->set_volume(_last_volume = 0);
278         globals->get_soundmgr()->add( sound, adf_ident );
279
280         int offset = (int)(sg_random() * 30.0);
281         _ident_count = offset / 4;
282         _last_ident_time = globals->get_time_params()->get_cur_time() -
283             offset;
284     }
285 }
286
287 void
288 ADF::set_bearing (double dt, double bearing_deg)
289 {
290     double old_bearing_deg = _bearing_node->getDoubleValue();
291
292     while ((bearing_deg - old_bearing_deg) >= 180)
293         old_bearing_deg += 360;
294     while ((bearing_deg - old_bearing_deg) <= -180)
295         old_bearing_deg -= 360;
296     bearing_deg += _error_node->getDoubleValue();
297     bearing_deg =
298         fgGetLowPass(old_bearing_deg, bearing_deg, dt * RESPONSIVENESS);
299
300     _bearing_node->setDoubleValue(bearing_deg);
301 }
302
303
304 // end of adf.cxx