]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/dme.cxx
Attempt #1 to sort out confusion between left / right / parking brake
[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/ilslist.hxx>
35 #include <Navaids/mkrbeacons.hxx>
36 #include <Navaids/navlist.hxx>
37
38 #include "dme.hxx"
39
40 #include <string>
41 SG_USING_STD(string);
42
43
44 /**
45  * Boy, this is ugly!  Make the VOR range vary by altitude difference.
46  */
47 static double kludgeRange ( double stationElev, double aircraftElev,
48                             double nominalRange)
49 {
50                                 // Assume that the nominal range (usually
51                                 // 50nm) applies at a 5,000 ft difference.
52                                 // Just a wild guess!
53   double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
54   double range = fabs(nominalRange * factor);
55
56                                 // Clamp the range to keep it sane; for
57                                 // now, never less than 25% or more than
58                                 // 500% of nominal range.
59   if (range < nominalRange/4.0) {
60     range = nominalRange/4.0;
61   } else if (range > nominalRange*5.0) {
62     range = nominalRange*5.0;
63   }
64
65   return range;
66 }
67
68
69 // Constructor
70 FGDME::FGDME() :
71     lon_node(fgGetNode("/position/longitude-deg", true)),
72     lat_node(fgGetNode("/position/latitude-deg", true)),
73     alt_node(fgGetNode("/position/altitude-ft", true)),
74     bus_power(fgGetNode("/systems/electrical/outputs/dme", true)),
75     navcom1_bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
76     navcom2_bus_power(fgGetNode("/systems/electrical/outputs/navcom[1]", true)),
77     navcom1_power_btn(fgGetNode("/radios/comm[0]/inputs/power-btn", true)),
78     navcom2_power_btn(fgGetNode("/radios/comm[1]/inputs/power-btn", true)),
79     navcom1_freq(fgGetNode("/radios/nav[0]/frequencies/selected-mhz", true)),
80     navcom2_freq(fgGetNode("/radios/nav[1]/frequencies/selected-mhz", true)),
81     need_update(true),
82     freq(0.0),
83     dist(0.0),
84     prev_dist(0.0),
85     spd(0.0),
86     ete(0.0)
87 {
88     last_time.stamp();
89 }
90
91
92 // Destructor
93 FGDME::~FGDME() 
94 {
95 }
96
97
98 void
99 FGDME::init ()
100 {
101 }
102
103 void
104 FGDME::bind ()
105 {
106
107                                 // User inputs
108     fgTie("/radios/dme/frequencies/selected-khz", this,
109           &FGDME::get_freq, &FGDME::set_freq);
110
111                                 // Radio outputs
112     fgTie("/radios/dme/in-range", this, &FGDME::get_inrange);
113
114     fgTie("/radios/dme/distance-nm", this, &FGDME::get_dist);
115
116     fgTie("/radios/dme/speed-kt", this, &FGDME::get_spd);
117
118     fgTie("/radios/dme/ete-min", this, &FGDME::get_ete);
119 }
120
121 void
122 FGDME::unbind ()
123 {
124     fgUntie("/radios/dme/frequencies/selected-khz");
125
126                                 // Radio outputs
127     fgUntie("/radios/dme/in-range");
128     fgUntie("/radios/dme/distance-nm");
129     fgUntie("/radios/dme/speed-kt");
130     fgUntie("/radios/dme/ete-min");
131 }
132
133
134 // Update the various nav values based on position and valid tuned in navs
135 void 
136 FGDME::update(double dt) 
137 {
138     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
139     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
140     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
141
142     need_update = false;
143
144     Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
145     Point3D station;
146
147     if ( valid && has_power() ) {
148         station = Point3D( x, y, z );
149         dist = aircraft.distance3D( station ) * SG_METER_TO_NM;
150         effective_range = kludgeRange(elev, elev, range);
151         if (dist < effective_range * SG_NM_TO_METER) {
152             inrange = true;
153         } else if (dist < 2 * effective_range * SG_NM_TO_METER) {
154             inrange = sg_random() <
155                 (2 * effective_range * SG_NM_TO_METER - dist) /
156                 (effective_range * SG_NM_TO_METER);
157         } else {
158             inrange = false;
159         }
160         if ( inrange ) {
161             SGTimeStamp current_time;
162             station = Point3D( x, y, z );
163             dist = aircraft.distance3D( station ) * SG_METER_TO_NM;
164             current_time.stamp();
165             long dMs = (current_time - last_time) / 1000;
166                                 // Update every second
167             if (dMs >= 1000) {
168                 double dDist = dist - prev_dist;
169                 spd = fabs((dDist/dMs) * 3600000);
170                                 // FIXME: the panel should be able to
171                                 // handle this!!!
172                 if (spd > 999.0)
173                     spd = 999.0;
174                 ete = fabs((dist/spd) * 60.0);
175                                 // FIXME: the panel should be able to
176                                 // handle this!!!
177                 if (ete > 99.0)
178                     ete = 99.0;
179                 prev_dist = dist;
180                 last_time.stamp();
181             }
182         }
183     } else {
184         inrange = false;
185         dist = 0.0;
186         prev_dist = 0.0;
187         spd = 0.0;
188         ete = 0.0;
189     }
190 }
191
192
193 // Update current nav/adf radio stations based on current postition
194 void FGDME::search() 
195 {
196     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
197     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
198     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
199
200                                 // FIXME: the panel should handle this
201                                 // don't worry about overhead for now,
202                                 // since this is handled only periodically
203     switch_pos = fgGetInt("/radios/dme/switch-position", 2);
204     if ( switch_pos == 1 && has_power() && navcom1_on() ) {
205         if ( freq != navcom1_freq->getDoubleValue() ) {
206             freq = navcom1_freq->getDoubleValue();
207             need_update = true;
208         }
209     } else if ( switch_pos == 3 && has_power() && navcom2_on() ) {
210         if ( freq != navcom2_freq->getDoubleValue() ) {
211             freq = navcom2_freq->getDoubleValue();
212             need_update = true;
213         }
214     } else if ( switch_pos == 2 && has_power() ) {
215         // no-op
216     } else {
217         freq = 0;
218         inrange = false;
219     }
220
221     FGILS *ils;
222     FGNav *nav;
223
224     if ( (ils = current_ilslist->findByFreq( freq, lon, lat, elev )) != NULL ) {
225         if ( ils->get_has_dme() ) {
226             valid = true;
227             lon = ils->get_loclon();
228             lat = ils->get_loclat();
229             elev = ils->get_gselev();
230             range = FG_ILS_DEFAULT_RANGE;
231             effective_range = kludgeRange(elev, elev, range);
232             x = ils->get_dme_x();
233             y = ils->get_dme_y();
234             z = ils->get_dme_z();
235         }
236     } else if ( (nav = current_navlist->findByFreq(freq, lon, lat, elev)) != NULL ) {
237         if (nav->get_has_dme()) {
238             valid = true;
239             lon = nav->get_lon();
240             lat = nav->get_lat();
241             elev = nav->get_elev();
242             range = nav->get_range();
243             effective_range = kludgeRange(elev, elev, range);
244             x = nav->get_x();
245             y = nav->get_y();
246             z = nav->get_z();
247         }
248     } else {
249         valid = false;
250         dist = 0;
251     }
252 }