1 // dme.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
4 // This file is in the Public Domain and comes with no warranty.
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>
16 #include <Main/fg_props.hxx>
17 #include <Navaids/navlist.hxx>
18 #include <Sound/audioident.hxx>
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.
30 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
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)
40 double rand = sg_random();
41 return range_nm + (range_nm * rand * rand);
46 class DMEFilter : public FGNavList::TypeFilter
50 TypeFilter(FGPositioned::DME),
51 _locEnabled(fgGetBool("/sim/realism/dme-fallback-to-loc", true))
54 _mintype = FGPositioned::ILS;
58 virtual bool pass(FGPositioned* pos) const
60 switch (pos->type()) {
61 case FGPositioned::DME: return true;
62 case FGPositioned::ILS:
63 case FGPositioned::LOC: return _locEnabled;
64 default: return false;
69 const bool _locEnabled;
72 } // of anonymous namespace
75 DME::DME ( SGPropertyNode *node )
76 : _last_distance_nm(0),
77 _last_frequency_mhz(-1),
78 _time_before_search_sec(0),
80 _name(node->getStringValue("name", "dme")),
81 _num(node->getIntValue("number", 0)),
95 branch = "/instrumentation/" + _name;
97 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
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);
109 double d = node->getDoubleValue( "volume", 1.0 );
110 _volume_node = node->getChild("volume", 0, true);
111 _volume_node->setDoubleValue( d );
113 bool b = node->getBoolValue( "ident", false );
114 _ident_btn_node = node->getChild("ident", 0, true);
115 _ident_btn_node->setBoolValue( b );
117 std::ostringstream temp;
118 temp << _name << "-ident-" << _num;
119 if( NULL == _audioIdent )
120 _audioIdent = new DMEAudioIdent( temp.str() );
129 _time_before_search_sec = 0;
133 DME::update (double delta_time_sec)
135 if( delta_time_sec < SGLimitsd::min() )
138 // Figure out the source
139 const char * source = _source_node->getStringValue();
140 if (source[0] == '\0') {
142 branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
143 _source_node->setStringValue(branch.c_str());
144 source = _source_node->getStringValue();
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;
153 _frequency_node->setDoubleValue(frequency_mhz);
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;
161 SGGeod pos(globals->get_aircraft_position());
163 _navrecord = FGNavList::findByFreq(frequency_mhz, pos, &filter);
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 );
179 // Calculate the distance to the transmitter
180 SGVec3d location = SGVec3d::fromGeod(globals->get_aircraft_position());
182 double distance_nm = dist(_navrecord->cart(), location) * SG_METER_TO_NM;
184 double range_nm = adjust_range(_navrecord->get_elev_ft(),
185 globals->get_aircraft_position().getElevationFt(),
186 _navrecord->get_range());
188 if (distance_nm <= range_nm) {
189 double volume = _volume_node->getDoubleValue();
190 if( false == _ident_btn_node->getBoolValue() )
193 _audioIdent->setIdent(_navrecord->ident(), volume );
195 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
196 ((1 / delta_time_sec) * 3600.0));
197 _last_distance_nm = distance_nm;
199 _in_range_node->setBoolValue(true);
200 double tmp_dist = distance_nm - _navrecord->get_multiuse();
201 if ( tmp_dist < 0.0 ) {
204 _distance_node->setDoubleValue( tmp_dist );
205 _speed_node->setDoubleValue(speed_kt);
206 if (SGLimitsd::min() < fabs(speed_kt))
207 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
210 _last_distance_nm = 0;
211 _in_range_node->setBoolValue(false);
212 _distance_node->setDoubleValue(0);
213 _speed_node->setDoubleValue(0);
214 _time_node->setDoubleValue(0);
215 _audioIdent->setIdent("", 0.0 );
218 _audioIdent->update( delta_time_sec );