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