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)),
78 SGPath path( globals->get_fg_root() );
80 term.append( "Navaids/range.term" );
82 low.append( "Navaids/range.low" );
84 high.append( "Navaids/range.high" );
86 term_tbl = new SGInterpTable( term.str() );
87 low_tbl = new SGInterpTable( low.str() );
88 high_tbl = new SGInterpTable( high.str() );
93 FGMarkerBeacon::~FGMarkerBeacon()
102 FGMarkerBeacon::init ()
111 FGMarkerBeacon::bind ()
114 fgTie("/radios/marker-beacon/inner", this,
115 &FGMarkerBeacon::get_inner_blink);
117 fgTie("/radios/marker-beacon/middle", this,
118 &FGMarkerBeacon::get_middle_blink);
120 fgTie("/radios/marker-beacon/outer", this,
121 &FGMarkerBeacon::get_outer_blink);
126 FGMarkerBeacon::unbind ()
128 fgUntie("/radios/marker-beacon/inner");
129 fgUntie("/radios/marker-beacon/middle");
130 fgUntie("/radios/marker-beacon/outer");
134 // Update the various nav values based on position and valid tuned in navs
136 FGMarkerBeacon::update(double dt)
140 // marker beacon blinking
141 bool light_on = ( outer_blink || middle_blink || inner_blink );
145 if ( light_on && (current - blink > 400000) ) {
148 } else if ( !light_on && (current - blink > 100000) ) {
153 if ( outer_marker ) {
154 outer_blink = light_on;
159 if ( middle_marker ) {
160 middle_blink = light_on;
162 middle_blink = false;
165 if ( inner_marker ) {
166 inner_blink = light_on;
171 // cout << outer_blink << " " << middle_blink << " " << inner_blink << endl;
175 // Update current nav/adf radio stations based on current postition
176 void FGMarkerBeacon::search()
178 static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
180 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
181 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
182 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
184 ////////////////////////////////////////////////////////////////////////
186 ////////////////////////////////////////////////////////////////////////
188 FGMkrBeacon::fgMkrBeacType beacon_type
189 = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
190 lat * SGD_RADIANS_TO_DEGREES, elev );
192 outer_marker = middle_marker = inner_marker = false;
194 if ( beacon_type == FGMkrBeacon::OUTER ) {
196 // cout << "OUTER MARKER" << endl;
197 #ifdef ENABLE_AUDIO_SUPPORT
198 if ( last_beacon != FGMkrBeacon::OUTER ) {
199 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
200 FGSimpleSound *sound = beacon.get_outer();
201 sound->set_volume( 0.3 );
202 globals->get_soundmgr()->add( sound, "outer-marker" );
204 if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
205 globals->get_soundmgr()->play_looped( "outer-marker" );
209 } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
210 middle_marker = true;
211 // cout << "MIDDLE MARKER" << endl;
212 #ifdef ENABLE_AUDIO_SUPPORT
213 if ( last_beacon != FGMkrBeacon::MIDDLE ) {
214 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
215 FGSimpleSound *sound = beacon.get_middle();
216 sound->set_volume( 0.3 );
217 globals->get_soundmgr()->add( sound, "middle-marker" );
219 if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
220 globals->get_soundmgr()->play_looped( "middle-marker" );
224 } else if ( beacon_type == FGMkrBeacon::INNER ) {
226 // cout << "INNER MARKER" << endl;
227 #ifdef ENABLE_AUDIO_SUPPORT
228 if ( last_beacon != FGMkrBeacon::INNER ) {
229 if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
230 FGSimpleSound *sound = beacon.get_inner();
231 sound->set_volume( 0.3 );
232 globals->get_soundmgr()->add( sound, "inner-marker" );
234 if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
235 globals->get_soundmgr()->play_looped( "inner-marker" );
240 // cout << "no marker" << endl;
241 #ifdef ENABLE_AUDIO_SUPPORT
242 globals->get_soundmgr()->stop( "outer-marker" );
243 globals->get_soundmgr()->stop( "middle-marker" );
244 globals->get_soundmgr()->stop( "inner-marker" );
247 last_beacon = beacon_type;