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/math/sg_geodesy.hxx>
12 #include <simgear/math/sg_random.h>
14 #include <Main/fg_props.hxx>
15 #include <Navaids/navlist.hxx>
23 * Start by calculating the radar horizon based on the elevation
24 * difference, then clamp to the maximum, then add a fudge for
25 * borderline reception.
28 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
31 double delta_elevation_ft =
32 fabs(aircraft_altitude_ft - transmitter_elevation_ft);
33 double range_nm = 1.23 * sqrt(delta_elevation_ft);
34 if (range_nm > max_range_nm)
35 range_nm = max_range_nm;
36 else if (range_nm < 20.0)
38 double rand = sg_random();
39 return range_nm + (range_nm * rand * rand);
43 DME::DME ( SGPropertyNode *node )
44 : _last_distance_nm(0),
45 _last_frequency_mhz(-1),
46 _time_before_search_sec(0),
47 _transmitter_valid(false),
48 _transmitter_elevation_ft(0),
49 _transmitter_range_nm(0),
50 _transmitter_bias(0.0),
51 _name(node->getStringValue("name", "dme")),
52 _num(node->getIntValue("number", 0))
64 branch = "/instrumentation/" + _name;
66 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
68 _longitude_node = fgGetNode("/position/longitude-deg", true);
69 _latitude_node = fgGetNode("/position/latitude-deg", true);
70 _altitude_node = fgGetNode("/position/altitude-ft", true);
71 _serviceable_node = node->getChild("serviceable", 0, true);
72 _electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
73 SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
74 _source_node = fnode->getChild("source", 0, true);
75 _frequency_node = fnode->getChild("selected-mhz", 0, true);
76 _in_range_node = node->getChild("in-range", 0, true);
77 _distance_node = node->getChild("indicated-distance-nm", 0, true);
78 _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
79 _time_node = node->getChild("indicated-time-min", 0, true);
83 DME::update (double delta_time_sec)
85 // Figure out the source
86 const char * source = _source_node->getStringValue();
87 if (source[0] == '\0') {
89 branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
90 _source_node->setStringValue(branch.c_str());
91 source = _source_node->getStringValue();
95 double frequency_mhz = fgGetDouble(source, 108.0);
96 if (frequency_mhz != _last_frequency_mhz) {
97 _time_before_search_sec = 0;
98 _last_frequency_mhz = frequency_mhz;
100 _frequency_node->setDoubleValue(frequency_mhz);
102 // Get the aircraft position
103 double longitude_rad =
104 _longitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
105 double latitude_rad =
106 _latitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
108 _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
110 // On timeout, scan again
111 _time_before_search_sec -= delta_time_sec;
112 if (_time_before_search_sec < 0)
113 search(frequency_mhz, longitude_rad,
114 latitude_rad, altitude_m);
116 // If it's off, don't bother.
117 if (!_serviceable_node->getBoolValue() ||
118 !_electrical_node->getBoolValue() ||
119 !_transmitter_valid) {
120 _last_distance_nm = 0;
121 _in_range_node->setBoolValue(false);
122 _distance_node->setDoubleValue(0);
123 _speed_node->setDoubleValue(0);
124 _time_node->setDoubleValue(0);
128 // Calculate the distance to the transmitter
129 SGGeod geod = SGGeod::fromRadM(longitude_rad, latitude_rad, altitude_m);
130 SGVec3d location = SGVec3d::fromGeod(geod);
132 double distance_nm = dist(_transmitter, location) * SG_METER_TO_NM;
134 double range_nm = adjust_range(_transmitter_elevation_ft,
135 altitude_m * SG_METER_TO_FEET,
136 _transmitter_range_nm);
138 if (distance_nm <= range_nm) {
139 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
140 ((1 / delta_time_sec) * 3600.0));
141 _last_distance_nm = distance_nm;
143 _in_range_node->setBoolValue(true);
144 double tmp_dist = distance_nm - _transmitter_bias;
145 if ( tmp_dist < 0.0 ) {
148 _distance_node->setDoubleValue( tmp_dist );
149 _speed_node->setDoubleValue(speed_kt);
150 if (SGLimitsd::min() < fabs(speed_kt))
151 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
154 _last_distance_nm = 0;
155 _in_range_node->setBoolValue(false);
156 _distance_node->setDoubleValue(0);
157 _speed_node->setDoubleValue(0);
158 _time_node->setDoubleValue(0);
163 DME::search (double frequency_mhz, double longitude_rad,
164 double latitude_rad, double altitude_m)
167 _time_before_search_sec = 1.0;
169 // try the ILS list first
171 FGNavRecord *dme = globals->get_dmelist()->findByFreq( frequency_mhz,
172 SGGeod::fromRadM(longitude_rad, latitude_rad, altitude_m));
174 _transmitter_valid = (dme != NULL);
176 if ( _transmitter_valid ) {
177 _transmitter = dme->cart();
178 _transmitter_elevation_ft = dme->get_elev_ft();
179 _transmitter_range_nm = dme->get_range();
180 _transmitter_bias = dme->get_multiuse();