1 // dme.cxx -- class to manage an instance of the DME
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #include <stdio.h> // snprintf
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
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>
46 * Boy, this is ugly! Make the VOR range vary by altitude difference.
48 static double kludgeRange ( double stationElev, double aircraftElev,
51 // Assume that the nominal range (usually
52 // 50nm) applies at a 5,000 ft difference.
54 double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
55 double range = fabs(nominalRange * factor);
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;
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)),
109 fgTie("/radios/dme/frequencies/selected-khz", this,
110 &FGDME::get_freq, &FGDME::set_freq);
113 fgTie("/radios/dme/in-range", this, &FGDME::get_inrange);
115 fgTie("/radios/dme/distance-nm", this, &FGDME::get_dist);
117 fgTie("/radios/dme/speed-kt", this, &FGDME::get_spd);
119 fgTie("/radios/dme/ete-min", this, &FGDME::get_ete);
125 fgUntie("/radios/dme/frequencies/selected-khz");
128 fgUntie("/radios/dme/in-range");
129 fgUntie("/radios/dme/distance-nm");
130 fgUntie("/radios/dme/speed-kt");
131 fgUntie("/radios/dme/ete-min");
135 // Update the various nav values based on position and valid tuned in navs
137 FGDME::update(double dt)
139 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
140 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
141 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
145 Point3D aircraft = sgGeodToCart( Point3D( lon, lat, elev ) );
148 if ( valid && has_power() ) {
149 station = Point3D( x, y, z );
150 dist = aircraft.distance3D( station ) * SG_METER_TO_NM;
151 effective_range = kludgeRange(elev, elev, range);
152 if (dist < effective_range * SG_NM_TO_METER) {
154 } else if (dist < 2 * effective_range * SG_NM_TO_METER) {
155 inrange = sg_random() <
156 (2 * effective_range * SG_NM_TO_METER - dist) /
157 (effective_range * SG_NM_TO_METER);
162 SGTimeStamp current_time;
163 station = Point3D( x, y, z );
164 dist = aircraft.distance3D( station ) * SG_METER_TO_NM;
165 current_time.stamp();
166 long dMs = (current_time - last_time) / 1000;
167 // Update every second
169 double dDist = dist - prev_dist;
170 spd = fabs((dDist/dMs) * 3600000);
171 // FIXME: the panel should be able to
175 ete = fabs((dist/spd) * 60.0);
176 // FIXME: the panel should be able to
194 // Update current nav/adf radio stations based on current postition
197 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
198 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
199 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
201 // FIXME: the panel should handle this
202 // don't worry about overhead for now,
203 // since this is handled only periodically
204 switch_pos = fgGetInt("/radios/dme/switch-position", 2);
205 if ( switch_pos == 1 && has_power() && navcom1_on() ) {
206 if ( freq != navcom1_freq->getDoubleValue() ) {
207 freq = navcom1_freq->getDoubleValue();
210 } else if ( switch_pos == 3 && has_power() && navcom2_on() ) {
211 if ( freq != navcom2_freq->getDoubleValue() ) {
212 freq = navcom2_freq->getDoubleValue();
223 if ( (ils = current_ilslist->findByFreq( freq, lon, lat, elev )) != NULL ) {
224 if ( ils->get_has_dme() ) {
226 lon = ils->get_loclon();
227 lat = ils->get_loclat();
228 elev = ils->get_gselev();
229 range = FG_ILS_DEFAULT_RANGE;
230 effective_range = kludgeRange(elev, elev, range);
231 x = ils->get_dme_x();
232 y = ils->get_dme_y();
233 z = ils->get_dme_z();
235 } else if ( (nav = current_navlist->findByFreq(freq, lon, lat, elev)) != NULL ) {
236 if (nav->get_has_dme()) {
238 lon = nav->get_lon();
239 lat = nav->get_lat();
240 elev = nav->get_elev();
241 range = nav->get_range();
242 effective_range = kludgeRange(elev, elev, range);