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>
36 #include "marker_beacon.hxx"
43 FGMarkerBeacon::FGMarkerBeacon() :
44 lon_node(fgGetNode("/position/longitude-deg", true)),
45 lat_node(fgGetNode("/position/latitude-deg", true)),
46 alt_node(fgGetNode("/position/altitude-ft", true)),
47 bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
48 power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
49 audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
50 serviceable(fgGetNode("/instrumentation/marker-beacons/serviceable", true)),
57 SGPath path( globals->get_fg_root() );
59 term.append( "Navaids/range.term" );
61 low.append( "Navaids/range.low" );
63 high.append( "Navaids/range.high" );
65 term_tbl = new SGInterpTable( term.str() );
66 low_tbl = new SGInterpTable( low.str() );
67 high_tbl = new SGInterpTable( high.str() );
69 power_btn->setBoolValue( true );
70 audio_btn->setBoolValue( true );
71 serviceable->setBoolValue( true );
76 FGMarkerBeacon::~FGMarkerBeacon()
85 FGMarkerBeacon::init ()
94 FGMarkerBeacon::bind ()
97 fgTie("/radios/marker-beacon/inner", this,
98 &FGMarkerBeacon::get_inner_blink);
100 fgTie("/radios/marker-beacon/middle", this,
101 &FGMarkerBeacon::get_middle_blink);
103 fgTie("/radios/marker-beacon/outer", this,
104 &FGMarkerBeacon::get_outer_blink);
109 FGMarkerBeacon::unbind ()
111 fgUntie("/radios/marker-beacon/inner");
112 fgUntie("/radios/marker-beacon/middle");
113 fgUntie("/radios/marker-beacon/outer");
117 // Update the various nav values based on position and valid tuned in navs
119 FGMarkerBeacon::update(double dt)
123 if ( has_power() && serviceable->getBoolValue() ) {
124 // marker beacon blinking
125 bool light_on = ( outer_blink || middle_blink || inner_blink );
129 if ( light_on && (current - blink > 400000) ) {
132 } else if ( !light_on && (current - blink > 100000) ) {
137 if ( outer_marker ) {
138 outer_blink = light_on;
143 if ( middle_marker ) {
144 middle_blink = light_on;
146 middle_blink = false;
149 if ( inner_marker ) {
150 inner_blink = light_on;
155 // cout << outer_blink << " " << middle_blink << " "
156 // << inner_blink << endl;
158 inner_blink = middle_blink = outer_blink = false;
163 // Update current nav/adf radio stations based on current postition
164 void FGMarkerBeacon::search()
166 static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
168 double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
169 double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
170 double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
172 ////////////////////////////////////////////////////////////////////////
174 ////////////////////////////////////////////////////////////////////////
176 FGMkrBeacon::fgMkrBeacType beacon_type
177 = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
178 lat * SGD_RADIANS_TO_DEGREES, elev );
180 outer_marker = middle_marker = inner_marker = false;
182 if ( beacon_type == FGMkrBeacon::NOBEACON
183 || !has_power() || !serviceable->getBoolValue() )
185 // cout << "no marker" << endl;
186 beacon_type = FGMkrBeacon::NOBEACON;
187 globals->get_soundmgr()->stop( "outer-marker" );
188 globals->get_soundmgr()->stop( "middle-marker" );
189 globals->get_soundmgr()->stop( "inner-marker" );
190 } else if ( beacon_type == FGMkrBeacon::OUTER ) {
192 // cout << "OUTER MARKER" << endl;
193 if ( last_beacon != FGMkrBeacon::OUTER ) {
194 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
195 SGSimpleSound *sound = beacon.get_outer();
196 sound->set_volume( 0.3 );
197 globals->get_soundmgr()->add( sound, "outer-marker" );
200 if ( audio_btn->getBoolValue() ) {
201 if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
202 globals->get_soundmgr()->play_looped( "outer-marker" );
205 globals->get_soundmgr()->stop( "outer-marker" );
207 } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
208 middle_marker = true;
209 // cout << "MIDDLE MARKER" << endl;
210 if ( last_beacon != FGMkrBeacon::MIDDLE ) {
211 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
212 SGSimpleSound *sound = beacon.get_middle();
213 sound->set_volume( 0.3 );
214 globals->get_soundmgr()->add( sound, "middle-marker" );
217 if ( audio_btn->getBoolValue() ) {
218 if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
219 globals->get_soundmgr()->play_looped( "middle-marker" );
222 globals->get_soundmgr()->stop( "middle-marker" );
224 } else if ( beacon_type == FGMkrBeacon::INNER ) {
226 // cout << "INNER MARKER" << endl;
227 if ( last_beacon != FGMkrBeacon::INNER ) {
228 if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
229 SGSimpleSound *sound = beacon.get_inner();
230 sound->set_volume( 0.3 );
231 globals->get_soundmgr()->add( sound, "inner-marker" );
234 if ( audio_btn->getBoolValue() ) {
235 if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
236 globals->get_soundmgr()->play_looped( "inner-marker" );
239 globals->get_soundmgr()->stop( "inner-marker" );
242 last_beacon = beacon_type;