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