]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/dme.cxx
DME units report a distance based on the assumption that the ground station
[flightgear.git] / src / Cockpit / dme.cxx
1 // dme.cxx -- class to manage an instance of the DME
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>      // snprintf
29
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
32
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/navlist.hxx>
35
36 #include "dme.hxx"
37
38 #include <string>
39 SG_USING_STD(string);
40
41
42 /**
43  * Boy, this is ugly!  Make the VOR range vary by altitude difference.
44  */
45 static double kludgeRange ( double stationElev, double aircraftElev,
46                             double nominalRange)
47 {
48                                 // Assume that the nominal range (usually
49                                 // 50nm) applies at a 5,000 ft difference.
50                                 // Just a wild guess!
51   double factor = (aircraftElev - stationElev)*SG_METER_TO_FEET / 5000.0;
52   double range = fabs(nominalRange * factor);
53
54                                 // Clamp the range to keep it sane; for
55                                 // now, never less than 25% or more than
56                                 // 500% of nominal range.
57   if (range < nominalRange/4.0) {
58     range = nominalRange/4.0;
59   } else if (range > nominalRange*5.0) {
60     range = nominalRange*5.0;
61   }
62
63   return range;
64 }
65
66
67 // Constructor
68 FGDME::FGDME() :
69     lon_node(fgGetNode("/position/longitude-deg", true)),
70     lat_node(fgGetNode("/position/latitude-deg", true)),
71     alt_node(fgGetNode("/position/altitude-ft", true)),
72     bus_power(fgGetNode("/systems/electrical/outputs/dme", true)),
73     navcom1_bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
74     navcom2_bus_power(fgGetNode("/systems/electrical/outputs/navcom[1]", true)),
75     navcom1_power_btn(fgGetNode("/radios/comm[0]/inputs/power-btn", true)),
76     navcom2_power_btn(fgGetNode("/radios/comm[1]/inputs/power-btn", true)),
77     navcom1_freq(fgGetNode("/radios/nav[0]/frequencies/selected-mhz", true)),
78     navcom2_freq(fgGetNode("/radios/nav[1]/frequencies/selected-mhz", true)),
79     need_update(true),
80     freq(0.0),
81     bias(0.0),
82     dist(0.0),
83     prev_dist(0.0),
84     spd(0.0),
85     ete(0.0)
86 {
87     last_time.stamp();
88 }
89
90
91 // Destructor
92 FGDME::~FGDME() 
93 {
94 }
95
96
97 void
98 FGDME::init ()
99 {
100 }
101
102 void
103 FGDME::bind ()
104 {
105
106                                 // User inputs
107     fgTie("/radios/dme/frequencies/selected-khz", this,
108           &FGDME::get_freq, &FGDME::set_freq);
109
110                                 // Radio outputs
111     fgTie("/radios/dme/in-range", this, &FGDME::get_inrange);
112
113     fgTie("/radios/dme/distance-nm", this, &FGDME::get_dist);
114
115     fgTie("/radios/dme/speed-kt", this, &FGDME::get_spd);
116
117     fgTie("/radios/dme/ete-min", this, &FGDME::get_ete);
118 }
119
120 void
121 FGDME::unbind ()
122 {
123     fgUntie("/radios/dme/frequencies/selected-khz");
124
125                                 // Radio outputs
126     fgUntie("/radios/dme/in-range");
127     fgUntie("/radios/dme/distance-nm");
128     fgUntie("/radios/dme/speed-kt");
129     fgUntie("/radios/dme/ete-min");
130 }
131
132
133 // Update the various nav values based on position and valid tuned in navs
134 void 
135 FGDME::update(double dt) 
136 {
137     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
138     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
139     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
140
141     need_update = false;
142
143     Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
144     Point3D station;
145
146     if ( valid && has_power() ) {
147         station = Point3D( x, y, z );
148         dist = aircraft.distance3D( station ) * SG_METER_TO_NM;
149         effective_range = kludgeRange(elev, elev, range);
150         if (dist < effective_range * SG_NM_TO_METER) {
151             inrange = true;
152         } else if (dist < 2 * effective_range * SG_NM_TO_METER) {
153             inrange = sg_random() <
154                 (2 * effective_range * SG_NM_TO_METER - dist) /
155                 (effective_range * SG_NM_TO_METER);
156         } else {
157             inrange = false;
158         }
159         if ( inrange ) {
160             SGTimeStamp current_time;
161             station = Point3D( x, y, z );
162             dist = aircraft.distance3D( station ) * SG_METER_TO_NM;
163             dist -= bias;
164             if ( dist < 0.0 ) {
165                 dist = 0.0;
166             }
167
168             current_time.stamp();
169             long dMs = (current_time - last_time) / 1000;
170                                 // Update every second
171             if (dMs >= 1000) {
172                 double dDist = dist - prev_dist;
173                 spd = fabs((dDist/dMs) * 3600000);
174                                 // FIXME: the panel should be able to
175                                 // handle this!!!
176                 if (spd > 999.0)
177                     spd = 999.0;
178                 ete = fabs((dist/spd) * 60.0);
179                                 // FIXME: the panel should be able to
180                                 // handle this!!!
181                 if (ete > 99.0)
182                     ete = 99.0;
183                 prev_dist = dist;
184                 last_time.stamp();
185             }
186         }
187     } else {
188         inrange = false;
189         dist = 0.0;
190         prev_dist = 0.0;
191         spd = 0.0;
192         ete = 0.0;
193     }
194 }
195
196
197 // Update current nav/adf radio stations based on current postition
198 void FGDME::search() 
199 {
200     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
201     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
202     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
203
204                                 // FIXME: the panel should handle this
205                                 // don't worry about overhead for now,
206                                 // since this is handled only periodically
207     switch_pos = fgGetInt("/radios/dme/switch-position", 2);
208     if ( switch_pos == 1 && has_power() && navcom1_on() ) {
209         if ( freq != navcom1_freq->getDoubleValue() ) {
210             freq = navcom1_freq->getDoubleValue();
211             need_update = true;
212         }
213     } else if ( switch_pos == 3 && has_power() && navcom2_on() ) {
214         if ( freq != navcom2_freq->getDoubleValue() ) {
215             freq = navcom2_freq->getDoubleValue();
216             need_update = true;
217         }
218     } else if ( switch_pos == 2 && has_power() ) {
219         // no-op
220     } else {
221         freq = 0;
222         inrange = false;
223     }
224
225     FGNavRecord *dme
226         = globals->get_dmelist()->findByFreq( freq, lon, lat, elev );
227
228     if ( dme != NULL ) {
229         valid = true;
230         lon = dme->get_lon();
231         lat = dme->get_lat();
232         elev = dme->get_elev_ft();
233         bias = dme->get_multiuse();
234         range = FG_LOC_DEFAULT_RANGE;
235         effective_range = kludgeRange(elev, elev, range);
236         x = dme->get_x();
237         y = dme->get_y();
238         z = dme->get_z();
239     } else {
240         valid = false;
241         dist = 0;
242     }
243 }