]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/dme.cxx
Don't start paused for in air starts.
[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 #include <simgear/compiler.h>
7 #include <simgear/math/sg_geodesy.hxx>
8 #include <simgear/math/sg_random.h>
9
10 #include <Main/fg_props.hxx>
11 #include <Navaids/ilslist.hxx>
12 #include <Navaids/navlist.hxx>
13
14 #include "dme.hxx"
15
16
17 /**
18  * Adjust the range.
19  *
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.
23  */
24 static double
25 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
26               double max_range_nm)
27 {
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);
35 }
36
37
38 DME::DME ()
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)
45 {
46 }
47
48 DME::~DME ()
49 {
50 }
51
52 void
53 DME::init ()
54 {
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);
61     _frequency_node =
62         fgGetNode("/instrumentation/dme/frequencies/selected-mhz", true);
63
64     _in_range_node = fgGetNode("/instrumentation/dme/in-range", true);
65     _distance_node =
66         fgGetNode("/instrumentation/dme/indicated-distance-nm", true);
67     _speed_node =
68         fgGetNode("/instrumentation/dme/indicated-ground-speed-kt", true);
69     _time_node =
70         fgGetNode("/instrumentation/dme/indicated-time-min", true);
71 }
72
73 void
74 DME::update (double delta_time_sec)
75 {
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);
81     }
82
83                                 // Get the frequency
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;
88     }
89
90                                 // Get the aircraft position
91     double longitude_rad =
92         _longitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
93     double latitude_rad =
94         _latitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
95     double altitude_m =
96         _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
97
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);
103
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);
113         return;
114     }
115
116                                 // Calculate the distance to the transmitter
117     Point3D location =
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;
127
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);
132         
133     } else {
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);
139     }
140 }
141
142 void
143 DME::search (double frequency_mhz, double longitude_rad,
144              double latitude_rad, double altitude_m)
145 {
146                                 // reset search time
147     _time_before_search_sec = 1.0;
148
149                                 // try the ILS list first
150     FGILS * ils = current_ilslist->findByFreq(frequency_mhz,
151                                               longitude_rad,
152                                               latitude_rad,
153                                               altitude_m);
154     if (ils != 0 && ils->get_has_dme()) {
155         _transmitter_valid = true;
156         _transmitter = Point3D(ils->get_dme_x(),
157                                ils->get_dme_y(),
158                                ils->get_dme_z());
159         _transmitter_elevation_ft = ils->get_gselev() * SG_METER_TO_FEET;
160         _transmitter_range_nm = 50; // arbitrary
161         return;
162     }
163
164                                 // try the VORs next
165     FGNav * nav = current_navlist->findByFreq(frequency_mhz,
166                                               longitude_rad,
167                                               latitude_rad,
168                                               altitude_m);
169     if (nav != 0)
170     if (nav != 0 && nav->get_has_dme()) {
171         _transmitter_valid = true;
172         _transmitter = Point3D(nav->get_x(),
173                                nav->get_y(),
174                                nav->get_z());
175         _transmitter_elevation_ft = nav->get_elev() * SG_METER_TO_FEET;
176         _transmitter_range_nm = nav->get_range(); // fixme
177         return;
178     }
179
180     _transmitter_valid = false;
181 }
182
183 // end of dme.cxx