1 // marker_beacon.cxx -- class to manage the marker beacons
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/mkrbeacons.hxx>
35 #include <Time/FGEventMgr.hxx>
37 #include "marker_beacon.hxx"
44 * Boy, this is ugly! Make the VOR range vary by altitude difference.
46 static double kludgeRange ( double stationElev, double aircraftElev,
49 // Assume that the nominal range (usually
50 // 50nm) applies at a 5,000 ft difference.
52 double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
53 double range = fabs(nominalRange * factor);
55 // Clamp the range to keep it sane; for
56 // now, never less than 25% or more than
57 // 500% of nominal range.
58 if (range < nominalRange/4.0) {
59 range = nominalRange/4.0;
60 } else if (range > nominalRange*5.0) {
61 range = nominalRange*5.0;
69 FGMarkerBeacon::FGMarkerBeacon() :
70 lon_node(fgGetNode("/position/longitude-deg", true)),
71 lat_node(fgGetNode("/position/latitude-deg", true)),
72 alt_node(fgGetNode("/position/altitude-ft", true)),
73 bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
74 power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
75 audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
76 servicable(fgGetNode("/instrumentation/marker-beacons/servicable", true)),
83 SGPath path( globals->get_fg_root() );
85 term.append( "Navaids/range.term" );
87 low.append( "Navaids/range.low" );
89 high.append( "Navaids/range.high" );
91 term_tbl = new SGInterpTable( term.str() );
92 low_tbl = new SGInterpTable( low.str() );
93 high_tbl = new SGInterpTable( high.str() );
95 power_btn->setBoolValue( true );
96 audio_btn->setBoolValue( true );
97 servicable->setBoolValue( true );
102 FGMarkerBeacon::~FGMarkerBeacon()
111 FGMarkerBeacon::init ()
120 FGMarkerBeacon::bind ()
123 fgTie("/radios/marker-beacon/inner", this,
124 &FGMarkerBeacon::get_inner_blink);
126 fgTie("/radios/marker-beacon/middle", this,
127 &FGMarkerBeacon::get_middle_blink);
129 fgTie("/radios/marker-beacon/outer", this,
130 &FGMarkerBeacon::get_outer_blink);
135 FGMarkerBeacon::unbind ()
137 fgUntie("/radios/marker-beacon/inner");
138 fgUntie("/radios/marker-beacon/middle");
139 fgUntie("/radios/marker-beacon/outer");
143 // Update the various nav values based on position and valid tuned in navs
145 FGMarkerBeacon::update(double dt)
149 if ( has_power() && servicable->getBoolValue() ) {
150 // marker beacon blinking
151 bool light_on = ( outer_blink || middle_blink || inner_blink );
155 if ( light_on && (current - blink > 400000) ) {
158 } else if ( !light_on && (current - blink > 100000) ) {
163 if ( outer_marker ) {
164 outer_blink = light_on;
169 if ( middle_marker ) {
170 middle_blink = light_on;
172 middle_blink = false;
175 if ( inner_marker ) {
176 inner_blink = light_on;
181 // cout << outer_blink << " " << middle_blink << " "
182 // << inner_blink << endl;
184 inner_blink = middle_blink = outer_blink = false;
189 // Update current nav/adf radio stations based on current postition
190 void FGMarkerBeacon::search()
192 static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
194 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
195 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
196 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
198 ////////////////////////////////////////////////////////////////////////
200 ////////////////////////////////////////////////////////////////////////
202 FGMkrBeacon::fgMkrBeacType beacon_type
203 = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
204 lat * SGD_RADIANS_TO_DEGREES, elev );
206 outer_marker = middle_marker = inner_marker = false;
208 if ( beacon_type == FGMkrBeacon::NOBEACON
209 || !has_power() || !servicable->getBoolValue() )
211 // cout << "no marker" << endl;
212 beacon_type = FGMkrBeacon::NOBEACON;
213 globals->get_soundmgr()->stop( "outer-marker" );
214 globals->get_soundmgr()->stop( "middle-marker" );
215 globals->get_soundmgr()->stop( "inner-marker" );
216 } else if ( beacon_type == FGMkrBeacon::OUTER ) {
218 // cout << "OUTER MARKER" << endl;
219 if ( last_beacon != FGMkrBeacon::OUTER ) {
220 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
221 FGSimpleSound *sound = beacon.get_outer();
222 sound->set_volume( 0.3 );
223 globals->get_soundmgr()->add( sound, "outer-marker" );
226 if ( audio_btn->getBoolValue()
227 && !globals->get_soundmgr()->is_playing("outer-marker") )
229 globals->get_soundmgr()->play_looped( "outer-marker" );
231 globals->get_soundmgr()->stop( "outer-marker" );
233 } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
234 middle_marker = true;
235 // cout << "MIDDLE MARKER" << endl;
236 if ( last_beacon != FGMkrBeacon::MIDDLE ) {
237 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
238 FGSimpleSound *sound = beacon.get_middle();
239 sound->set_volume( 0.3 );
240 globals->get_soundmgr()->add( sound, "middle-marker" );
243 if ( audio_btn->getBoolValue()
244 && !globals->get_soundmgr()->is_playing("middle-marker") )
246 globals->get_soundmgr()->play_looped( "middle-marker" );
248 globals->get_soundmgr()->stop( "middle-marker" );
250 } else if ( beacon_type == FGMkrBeacon::INNER ) {
252 // cout << "INNER MARKER" << endl;
253 if ( last_beacon != FGMkrBeacon::INNER ) {
254 if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
255 FGSimpleSound *sound = beacon.get_inner();
256 sound->set_volume( 0.3 );
257 globals->get_soundmgr()->add( sound, "inner-marker" );
260 if ( audio_btn->getBoolValue()
261 && !globals->get_soundmgr()->is_playing("inner-marker") )
263 globals->get_soundmgr()->play_looped( "inner-marker" );
265 globals->get_soundmgr()->stop( "inner-marker" );
268 last_beacon = beacon_type;