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>
15 #include <Main/fg_props.hxx>
16 #include <Navaids/navlist.hxx>
17 #include <Sound/audioident.hxx>
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.
29 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
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)
39 double rand = sg_random();
40 return range_nm + (range_nm * rand * rand);
44 DME::DME ( SGPropertyNode *node )
45 : _last_distance_nm(0),
46 _last_frequency_mhz(-1),
47 _time_before_search_sec(0),
49 _name(node->getStringValue("name", "dme")),
50 _num(node->getIntValue("number", 0)),
64 branch = "/instrumentation/" + _name;
66 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
68 _serviceable_node = node->getChild("serviceable", 0, true);
69 _electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
70 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
71 _source_node = fnode->getChild("source", 0, true);
72 _frequency_node = fnode->getChild("selected-mhz", 0, true);
73 _in_range_node = node->getChild("in-range", 0, true);
74 _distance_node = node->getChild("indicated-distance-nm", 0, true);
75 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
76 _time_node = node->getChild("indicated-time-min", 0, true);
78 double d = node->getDoubleValue( "volume", 1.0 );
79 _volume_node = node->getChild("volume", 0, true);
80 _volume_node->setDoubleValue( d );
82 bool b = node->getBoolValue( "ident", false );
83 _ident_btn_node = node->getChild("ident", 0, true);
84 _ident_btn_node->setBoolValue( b );
86 std::ostringstream temp;
87 temp << _name << "-ident-" << _num;
88 if( NULL == _audioIdent )
89 _audioIdent = new DMEAudioIdent( temp.str() );
94 DME::update (double delta_time_sec)
96 if( delta_time_sec < SGLimitsd::min() )
99 // Figure out the source
100 const char * source = _source_node->getStringValue();
101 if (source[0] == '\0') {
103 branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
104 _source_node->setStringValue(branch.c_str());
105 source = _source_node->getStringValue();
109 double frequency_mhz = fgGetDouble(source, 108.0);
110 if (frequency_mhz != _last_frequency_mhz) {
111 _time_before_search_sec = 0;
112 _last_frequency_mhz = frequency_mhz;
114 _frequency_node->setDoubleValue(frequency_mhz);
116 // Get the aircraft position
117 // On timeout, scan again
118 _time_before_search_sec -= delta_time_sec;
119 if (_time_before_search_sec < 0) {
120 _time_before_search_sec = 1.0;
122 _navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
123 globals->get_aircraft_position());
126 // If it's off, don't bother.
127 if (!_serviceable_node->getBoolValue() ||
128 !_electrical_node->getBoolValue() ||
129 NULL == _navrecord ) {
130 _last_distance_nm = 0;
131 _in_range_node->setBoolValue(false);
132 _distance_node->setDoubleValue(0);
133 _speed_node->setDoubleValue(0);
134 _time_node->setDoubleValue(0);
135 _audioIdent->setIdent("", 0.0 );
139 // Calculate the distance to the transmitter
140 SGVec3d location = SGVec3d::fromGeod(globals->get_aircraft_position());
142 double distance_nm = dist(_navrecord->cart(), location) * SG_METER_TO_NM;
144 double range_nm = adjust_range(_navrecord->get_elev_ft(),
145 globals->get_aircraft_position().getElevationFt(),
146 _navrecord->get_range());
148 if (distance_nm <= range_nm) {
149 double volume = _volume_node->getDoubleValue();
150 if( false == _ident_btn_node->getBoolValue() )
153 _audioIdent->setIdent(_navrecord->ident(), volume );
155 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
156 ((1 / delta_time_sec) * 3600.0));
157 _last_distance_nm = distance_nm;
159 _in_range_node->setBoolValue(true);
160 double tmp_dist = distance_nm - _navrecord->get_multiuse();
161 if ( tmp_dist < 0.0 ) {
164 _distance_node->setDoubleValue( tmp_dist );
165 _speed_node->setDoubleValue(speed_kt);
166 if (SGLimitsd::min() < fabs(speed_kt))
167 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
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 );
178 _audioIdent->update( delta_time_sec );