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.
6 #include <simgear/compiler.h>
7 #include <simgear/math/sg_geodesy.hxx>
8 #include <simgear/math/sg_random.h>
10 #include <Main/fg_props.hxx>
11 #include <Navaids/ilslist.hxx>
12 #include <Navaids/navlist.hxx>
20 * Start by calculating the radar horizon based on the elevation
21 * difference, then clamp to the maximum, then add a fudge for
22 * borderline reception.
25 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
28 double delta_elevation_ft =
29 fabs(aircraft_altitude_ft - transmitter_elevation_ft);
30 double range_nm = 1.23 * sqrt(delta_elevation_ft);
31 if (range_nm > max_range_nm)
32 range_nm = max_range_nm;
33 double rand = sg_random();
34 return range_nm + (range_nm * rand * rand);
39 : _last_distance_nm(0),
40 _last_frequency_mhz(-1),
41 _time_before_search_sec(0),
42 _transmitter_valid(false),
43 _transmitter_elevation_ft(0),
44 _transmitter_range_nm(0)
55 _longitude_node = fgGetNode("/position/longitude-deg", true);
56 _latitude_node = fgGetNode("/position/latitude-deg", true);
57 _altitude_node = fgGetNode("/position/altitude-ft", true);
58 _serviceable_node = fgGetNode("/instrumentation/dme/serviceable", true);
59 _electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
60 _source_node = fgGetNode("/instrumentation/dme/frequencies/source", true);
62 fgGetNode("/instrumentation/dme/frequencies/selected-mhz", true);
64 _in_range_node = fgGetNode("/instrumentation/dme/in-range", true);
66 fgGetNode("/instrumentation/dme/indicated-distance-nm", true);
68 fgGetNode("/instrumentation/dme/indicated-ground-speed-kt", true);
70 fgGetNode("/instrumentation/dme/indicated-time-min", true);
74 DME::update (double delta_time_sec)
76 // Figure out the source
77 const char * source = _source_node->getStringValue();
78 if (source[0] == '\0') {
79 source = "/instrumentation/dme/frequencies/selected-mhz";
80 _source_node->setStringValue(source);
84 double frequency_mhz = fgGetDouble(source, 108.0);
85 if (frequency_mhz != _last_frequency_mhz) {
86 _time_before_search_sec = 0;
87 _last_frequency_mhz = frequency_mhz;
90 // Get the aircraft position
91 double longitude_rad =
92 _longitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
94 _latitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
96 _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
98 // On timeout, scan again
99 _time_before_search_sec -= delta_time_sec;
100 if (_time_before_search_sec < 0)
101 search(frequency_mhz, longitude_rad,
102 latitude_rad, altitude_m);
104 // If it's off, don't bother.
105 if (!_serviceable_node->getBoolValue() ||
106 !_electrical_node->getBoolValue() ||
107 !_transmitter_valid) {
108 _last_distance_nm = 0;
109 _in_range_node->setBoolValue(false);
110 _distance_node->setDoubleValue(0);
111 _speed_node->setDoubleValue(0);
112 _time_node->setDoubleValue(0);
116 // Calculate the distance to the transmitter
118 sgGeodToCart(Point3D(longitude_rad, latitude_rad, altitude_m));
119 double distance_nm = _transmitter.distance3D(location) * SG_METER_TO_NM;
120 double range_nm = adjust_range(_transmitter_elevation_ft,
121 altitude_m * SG_METER_TO_FEET,
122 _transmitter_range_nm);
123 if (distance_nm <= range_nm) {
124 double speed_kt = (fabs(distance_nm - _last_distance_nm) *
125 ((1 / delta_time_sec) * 3600.0));
126 _last_distance_nm = distance_nm;
128 _in_range_node->setBoolValue(true);
129 _distance_node->setDoubleValue(distance_nm);
130 _speed_node->setDoubleValue(speed_kt);
131 _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
134 _last_distance_nm = 0;
135 _in_range_node->setBoolValue(false);
136 _distance_node->setDoubleValue(0);
137 _speed_node->setDoubleValue(0);
138 _time_node->setDoubleValue(0);
143 DME::search (double frequency_mhz, double longitude_rad,
144 double latitude_rad, double altitude_m)
147 _time_before_search_sec = 1.0;
149 // try the ILS list first
150 FGILS * ils = current_ilslist->findByFreq(frequency_mhz,
154 if (ils != 0 && ils->get_has_dme()) {
155 _transmitter_valid = true;
156 _transmitter = Point3D(ils->get_dme_x(),
159 _transmitter_elevation_ft = ils->get_gselev() * SG_METER_TO_FEET;
160 _transmitter_range_nm = 50; // arbitrary
165 FGNav * nav = current_navlist->findByFreq(frequency_mhz,
170 if (nav != 0 && nav->get_has_dme()) {
171 _transmitter_valid = true;
172 _transmitter = Point3D(nav->get_x(),
175 _transmitter_elevation_ft = nav->get_elev() * SG_METER_TO_FEET;
176 _transmitter_range_nm = nav->get_range(); // fixme
180 _transmitter_valid = false;