]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/dme.cxx
Implement a persistent cache for navigation data.
[flightgear.git] / src / Instrumentation / dme.cxx
1 // dme.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/sg_inlines.h>
12 #include <simgear/math/sg_geodesy.hxx>
13 #include <simgear/math/sg_random.h>
14
15 #include <Main/fg_props.hxx>
16 #include <Navaids/navlist.hxx>
17 #include <Sound/audioident.hxx>
18
19 #include "dme.hxx"
20
21 /**
22  * Adjust the range.
23  *
24  * Start by calculating the radar horizon based on the elevation
25  * difference, then clamp to the maximum, then add a fudge for
26  * borderline reception.
27  */
28 static double
29 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
30               double max_range_nm)
31 {
32     double delta_elevation_ft =
33         fabs(aircraft_altitude_ft - transmitter_elevation_ft);
34     double range_nm = 1.23 * sqrt(delta_elevation_ft);
35     if (range_nm > max_range_nm)
36         range_nm = max_range_nm;
37     else if (range_nm < 20.0)
38         range_nm = 20.0;
39     double rand = sg_random();
40     return range_nm + (range_nm * rand * rand);
41 }
42
43 namespace {
44   
45   class DMEFilter : public FGNavList::TypeFilter
46   {
47   public:
48     DMEFilter() :
49       TypeFilter(FGPositioned::DME),
50       _locEnabled(fgGetBool("/sim/realism/dme-fallback-to-loc", true))
51     {
52       if (_locEnabled) {
53         _mintype = FGPositioned::ILS;
54       }
55     }
56     
57     virtual bool pass(FGPositioned* pos) const
58     {
59       switch (pos->type()) {
60       case FGPositioned::DME: return true;
61       case FGPositioned::ILS:
62       case FGPositioned::LOC: return _locEnabled;
63       default: return false;
64       }
65     }
66     
67   private:
68     const bool _locEnabled;
69   };
70   
71 } // of anonymous namespace
72
73
74 DME::DME ( SGPropertyNode *node )
75     : _last_distance_nm(0),
76       _last_frequency_mhz(-1),
77       _time_before_search_sec(0),
78       _navrecord(NULL),
79       _name(node->getStringValue("name", "dme")),
80       _num(node->getIntValue("number", 0)),
81       _audioIdent(NULL)
82 {
83 }
84
85 DME::~DME ()
86 {
87     delete _audioIdent;
88 }
89
90 void
91 DME::init ()
92 {
93     string branch;
94     branch = "/instrumentation/" + _name;
95
96     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
97
98     _serviceable_node = node->getChild("serviceable", 0, true);
99     _electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
100     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
101     _source_node = fnode->getChild("source", 0, true);
102     _frequency_node = fnode->getChild("selected-mhz", 0, true);
103     _in_range_node = node->getChild("in-range", 0, true);
104     _distance_node = node->getChild("indicated-distance-nm", 0, true);
105     _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
106     _time_node = node->getChild("indicated-time-min", 0, true);
107
108     double d = node->getDoubleValue( "volume", 1.0 );
109     _volume_node = node->getChild("volume", 0, true);
110     _volume_node->setDoubleValue( d );
111
112     bool b = node->getBoolValue( "ident", false );
113     _ident_btn_node = node->getChild("ident", 0, true);
114     _ident_btn_node->setBoolValue( b );
115
116     std::ostringstream temp;
117     temp << _name << "-ident-" << _num;
118     if( NULL == _audioIdent ) 
119         _audioIdent = new DMEAudioIdent( temp.str() );
120     _audioIdent->init();
121
122     reinit();
123 }
124
125 void
126 DME::reinit ()
127 {
128     _time_before_search_sec = 0;
129 }
130
131 void
132 DME::update (double delta_time_sec)
133 {
134     if( delta_time_sec < SGLimitsd::min() )
135         return;  //paused
136
137                                 // Figure out the source
138     const char * source = _source_node->getStringValue();
139     if (source[0] == '\0') {
140         string branch;
141         branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
142         _source_node->setStringValue(branch.c_str());
143         source = _source_node->getStringValue();
144     }
145                                 // Get the frequency
146
147     double frequency_mhz = fgGetDouble(source, 108.0);
148     if (frequency_mhz != _last_frequency_mhz) {
149         _time_before_search_sec = 0;
150         _last_frequency_mhz = frequency_mhz;
151     }
152     _frequency_node->setDoubleValue(frequency_mhz);
153
154                                 // Get the aircraft position
155     // On timeout, scan again
156     _time_before_search_sec -= delta_time_sec;
157     if (_time_before_search_sec < 0) {
158         _time_before_search_sec = 1.0;
159
160       SGGeod pos(globals->get_aircraft_position());
161       DMEFilter filter;
162       _navrecord = FGNavList::findByFreq(frequency_mhz, pos, &filter);
163     }
164
165     // If it's off, don't bother.
166     if (!_serviceable_node->getBoolValue() ||
167         !_electrical_node->getBoolValue() ||
168         NULL == _navrecord ) {
169         _last_distance_nm = 0;
170         _in_range_node->setBoolValue(false);
171         _distance_node->setDoubleValue(0);
172         _speed_node->setDoubleValue(0);
173         _time_node->setDoubleValue(0);
174         _audioIdent->setIdent("", 0.0 );
175         return;
176     }
177
178     // Calculate the distance to the transmitter
179     SGVec3d location = SGVec3d::fromGeod(globals->get_aircraft_position());
180     
181     double distance_nm = dist(_navrecord->cart(), location) * SG_METER_TO_NM;
182
183     double range_nm = adjust_range(_navrecord->get_elev_ft(),
184                                    globals->get_aircraft_position().getElevationFt(),
185                                    _navrecord->get_range());
186
187     if (distance_nm <= range_nm) {
188         double volume = _volume_node->getDoubleValue();
189         if( false == _ident_btn_node->getBoolValue() )
190             volume = 0.0;
191
192         _audioIdent->setIdent(_navrecord->ident(), volume );
193
194         double speed_kt = (fabs(distance_nm - _last_distance_nm) *
195                            ((1 / delta_time_sec) * 3600.0));
196         _last_distance_nm = distance_nm;
197
198         _in_range_node->setBoolValue(true);
199         double tmp_dist = distance_nm - _navrecord->get_multiuse();
200         if ( tmp_dist < 0.0 ) {
201             tmp_dist = 0.0;
202         }
203         _distance_node->setDoubleValue( tmp_dist );
204         _speed_node->setDoubleValue(speed_kt);
205         if (SGLimitsd::min() < fabs(speed_kt))
206           _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
207         
208     } else {
209         _last_distance_nm = 0;
210         _in_range_node->setBoolValue(false);
211         _distance_node->setDoubleValue(0);
212         _speed_node->setDoubleValue(0);
213         _time_node->setDoubleValue(0);
214         _audioIdent->setIdent("", 0.0 );
215     }
216     
217     _audioIdent->update( delta_time_sec );
218 }
219
220 // end of dme.cxx