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 FGMarkerBeacon::FGMarkerBeacon() :
45 lon_node(fgGetNode("/position/longitude-deg", true)),
46 lat_node(fgGetNode("/position/latitude-deg", true)),
47 alt_node(fgGetNode("/position/altitude-ft", true)),
48 bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
49 power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
50 audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
51 servicable(fgGetNode("/instrumentation/marker-beacons/servicable", true)),
58 SGPath path( globals->get_fg_root() );
60 term.append( "Navaids/range.term" );
62 low.append( "Navaids/range.low" );
64 high.append( "Navaids/range.high" );
66 term_tbl = new SGInterpTable( term.str() );
67 low_tbl = new SGInterpTable( low.str() );
68 high_tbl = new SGInterpTable( high.str() );
70 power_btn->setBoolValue( true );
71 audio_btn->setBoolValue( true );
72 servicable->setBoolValue( true );
77 FGMarkerBeacon::~FGMarkerBeacon()
86 FGMarkerBeacon::init ()
95 FGMarkerBeacon::bind ()
98 fgTie("/radios/marker-beacon/inner", this,
99 &FGMarkerBeacon::get_inner_blink);
101 fgTie("/radios/marker-beacon/middle", this,
102 &FGMarkerBeacon::get_middle_blink);
104 fgTie("/radios/marker-beacon/outer", this,
105 &FGMarkerBeacon::get_outer_blink);
110 FGMarkerBeacon::unbind ()
112 fgUntie("/radios/marker-beacon/inner");
113 fgUntie("/radios/marker-beacon/middle");
114 fgUntie("/radios/marker-beacon/outer");
118 // Update the various nav values based on position and valid tuned in navs
120 FGMarkerBeacon::update(double dt)
124 if ( has_power() && servicable->getBoolValue() ) {
125 // marker beacon blinking
126 bool light_on = ( outer_blink || middle_blink || inner_blink );
130 if ( light_on && (current - blink > 400000) ) {
133 } else if ( !light_on && (current - blink > 100000) ) {
138 if ( outer_marker ) {
139 outer_blink = light_on;
144 if ( middle_marker ) {
145 middle_blink = light_on;
147 middle_blink = false;
150 if ( inner_marker ) {
151 inner_blink = light_on;
156 // cout << outer_blink << " " << middle_blink << " "
157 // << inner_blink << endl;
159 inner_blink = middle_blink = outer_blink = false;
164 // Update current nav/adf radio stations based on current postition
165 void FGMarkerBeacon::search()
167 static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
169 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
170 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
171 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
173 ////////////////////////////////////////////////////////////////////////
175 ////////////////////////////////////////////////////////////////////////
177 FGMkrBeacon::fgMkrBeacType beacon_type
178 = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
179 lat * SGD_RADIANS_TO_DEGREES, elev );
181 outer_marker = middle_marker = inner_marker = false;
183 if ( beacon_type == FGMkrBeacon::NOBEACON
184 || !has_power() || !servicable->getBoolValue() )
186 // cout << "no marker" << endl;
187 beacon_type = FGMkrBeacon::NOBEACON;
188 globals->get_soundmgr()->stop( "outer-marker" );
189 globals->get_soundmgr()->stop( "middle-marker" );
190 globals->get_soundmgr()->stop( "inner-marker" );
191 } else if ( beacon_type == FGMkrBeacon::OUTER ) {
193 // cout << "OUTER MARKER" << endl;
194 if ( last_beacon != FGMkrBeacon::OUTER ) {
195 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
196 FGSimpleSound *sound = beacon.get_outer();
197 sound->set_volume( 0.3 );
198 globals->get_soundmgr()->add( sound, "outer-marker" );
201 if ( audio_btn->getBoolValue()
202 && !globals->get_soundmgr()->is_playing("outer-marker") )
204 globals->get_soundmgr()->play_looped( "outer-marker" );
206 globals->get_soundmgr()->stop( "outer-marker" );
208 } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
209 middle_marker = true;
210 // cout << "MIDDLE MARKER" << endl;
211 if ( last_beacon != FGMkrBeacon::MIDDLE ) {
212 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
213 FGSimpleSound *sound = beacon.get_middle();
214 sound->set_volume( 0.3 );
215 globals->get_soundmgr()->add( sound, "middle-marker" );
218 if ( audio_btn->getBoolValue()
219 && !globals->get_soundmgr()->is_playing("middle-marker") )
221 globals->get_soundmgr()->play_looped( "middle-marker" );
223 globals->get_soundmgr()->stop( "middle-marker" );
225 } else if ( beacon_type == FGMkrBeacon::INNER ) {
227 // cout << "INNER MARKER" << endl;
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" );
235 if ( audio_btn->getBoolValue()
236 && !globals->get_soundmgr()->is_playing("inner-marker") )
238 globals->get_soundmgr()->play_looped( "inner-marker" );
240 globals->get_soundmgr()->stop( "inner-marker" );
243 last_beacon = beacon_type;