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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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/navlist.hxx>
36 #include "marker_beacon.hxx"
43 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
48 name("marker-beacon"),
50 _time_before_search_sec(0.0)
52 SGPath path( globals->get_fg_root() );
54 term.append( "Navaids/range.term" );
56 low.append( "Navaids/range.low" );
58 high.append( "Navaids/range.high" );
60 term_tbl = new SGInterpTable( term.str() );
61 low_tbl = new SGInterpTable( low.str() );
62 high_tbl = new SGInterpTable( high.str() );
65 for ( i = 0; i < node->nChildren(); ++i ) {
66 SGPropertyNode *child = node->getChild(i);
67 string cname = child->getName();
68 string cval = child->getStringValue();
69 if ( cname == "name" ) {
71 } else if ( cname == "number" ) {
72 num = child->getIntValue();
74 SG_LOG( SG_INSTR, SG_WARN,
75 "Error in marker beacon config logic" );
76 if ( name.length() ) {
77 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
85 FGMarkerBeacon::~FGMarkerBeacon()
94 FGMarkerBeacon::init ()
97 branch = "/instrumentation/" + name;
99 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
101 sound_pause = fgGetNode("/sim/sound/pause", false);
102 lon_node = fgGetNode("/position/longitude-deg", true);
103 lat_node = fgGetNode("/position/latitude-deg", true);
104 alt_node = fgGetNode("/position/altitude-ft", true);
105 bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
106 power_btn = node->getChild("power-btn", 0, true);
107 audio_btn = node->getChild("audio-btn", 0, true);
108 serviceable = node->getChild("serviceable", 0, true);
110 power_btn->setBoolValue( true );
111 audio_btn->setBoolValue( true );
112 serviceable->setBoolValue( true );
118 outer_marker = middle_marker = inner_marker = false;
123 FGMarkerBeacon::bind ()
126 branch = "/instrumentation/" + name;
128 fgTie((branch + "/inner").c_str(), this,
129 &FGMarkerBeacon::get_inner_blink);
131 fgTie((branch + "/middle").c_str(), this,
132 &FGMarkerBeacon::get_middle_blink);
134 fgTie((branch + "/outer").c_str(), this,
135 &FGMarkerBeacon::get_outer_blink);
140 FGMarkerBeacon::unbind ()
143 branch = "/instrumentation/" + name;
145 fgUntie((branch + "/inner").c_str());
146 fgUntie((branch + "/middle").c_str());
147 fgUntie((branch + "/outer").c_str());
151 // Update the various nav values based on position and valid tuned in navs
153 FGMarkerBeacon::update(double dt)
157 if ( has_power() && serviceable->getBoolValue()
158 && !sound_pause->getBoolValue()) {
160 // On timeout, scan again
161 _time_before_search_sec -= dt;
162 if ( _time_before_search_sec < 0 ) {
165 // marker beacon blinking
166 bool light_on = ( outer_blink || middle_blink || inner_blink );
170 if ( light_on && (current - blink > 400000) ) {
173 } else if ( !light_on && (current - blink > 100000) ) {
178 if ( outer_marker ) {
179 outer_blink = light_on;
184 if ( middle_marker ) {
185 middle_blink = light_on;
187 middle_blink = false;
190 if ( inner_marker ) {
191 inner_blink = light_on;
196 // cout << outer_blink << " " << middle_blink << " "
197 // << inner_blink << endl;
199 inner_blink = middle_blink = outer_blink = false;
204 static bool check_beacon_range( const SGGeod& pos,
207 SGVec3d aircraft = SGVec3d::fromGeod(pos);
208 SGVec3d station = b->get_cart();
209 // cout << " aircraft = " << aircraft << " station = " << station
212 SGVec3d tmp = station - aircraft;
213 double d = dot(tmp, tmp);
214 // cout << " distance = " << d << " ("
215 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
216 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
219 // cout << " range = " << sqrt(d) << endl;
221 // cout << "elev = " << elev * SG_METER_TO_FEET
222 // << " current->get_elev() = " << current->get_elev() << endl;
223 double elev_ft = pos.getElevationFt();
224 double delev = elev_ft - b->get_elev_ft();
226 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
227 // whichever is smaller. The intersection point is 1538 ...
228 double maxrange2; // feet^2
229 if ( delev < 1538.0 ) {
230 maxrange2 = 2.4 * 2.4 * delev * delev;
231 } else if ( delev < 4000.0 ) {
232 maxrange2 = 4000 * 4000 - delev * delev;
236 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
237 // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
239 // match up to twice the published range so we can model
240 // reduced signal strength
241 if ( d < maxrange2 ) {
248 // Update current nav/adf radio stations based on current postition
249 void FGMarkerBeacon::search()
253 _time_before_search_sec = 1.0;
255 static fgMkrBeacType last_beacon = NOBEACON;
257 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
258 lat_node->getDoubleValue(),
259 alt_node->getDoubleValue());
261 ////////////////////////////////////////////////////////////////////////
263 ////////////////////////////////////////////////////////////////////////
265 // get closest marker beacon
267 = globals->get_mkrlist()->findClosest( pos.getLongitudeRad(),
268 pos.getLatitudeRad(),
269 pos.getElevationM() );
271 // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
273 fgMkrBeacType beacon_type = NOBEACON;
274 bool inrange = false;
276 if ( b->get_type() == 7 ) {
278 } else if ( b->get_type() == 8 ) {
279 beacon_type = MIDDLE;
280 } else if ( b->get_type() == 9 ) {
283 inrange = check_beacon_range( pos, b );
284 // cout << " inrange = " << inrange << endl;
287 outer_marker = middle_marker = inner_marker = false;
289 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
291 // cout << "no marker" << endl;
292 globals->get_soundmgr()->stop( "outer-marker" );
293 globals->get_soundmgr()->stop( "middle-marker" );
294 globals->get_soundmgr()->stop( "inner-marker" );
295 } else if ( beacon_type == OUTER ) {
297 // cout << "OUTER MARKER" << endl;
298 if ( last_beacon != OUTER ) {
299 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
300 SGSoundSample *sound = beacon.get_outer();
302 sound->set_volume( 0.3 );
303 globals->get_soundmgr()->add( sound, "outer-marker" );
307 if ( audio_btn->getBoolValue() ) {
308 if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
309 globals->get_soundmgr()->play_looped( "outer-marker" );
312 globals->get_soundmgr()->stop( "outer-marker" );
314 } else if ( beacon_type == MIDDLE ) {
315 middle_marker = true;
316 // cout << "MIDDLE MARKER" << endl;
317 if ( last_beacon != MIDDLE ) {
318 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
319 SGSoundSample *sound = beacon.get_middle();
321 sound->set_volume( 0.3 );
322 globals->get_soundmgr()->add( sound, "middle-marker" );
326 if ( audio_btn->getBoolValue() ) {
327 if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
328 globals->get_soundmgr()->play_looped( "middle-marker" );
331 globals->get_soundmgr()->stop( "middle-marker" );
333 } else if ( beacon_type == INNER ) {
335 // cout << "INNER MARKER" << endl;
336 if ( last_beacon != INNER ) {
337 if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
338 SGSoundSample *sound = beacon.get_inner();
340 sound->set_volume( 0.3 );
341 globals->get_soundmgr()->add( sound, "inner-marker" );
345 if ( audio_btn->getBoolValue() ) {
346 if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
347 globals->get_soundmgr()->play_looped( "inner-marker" );
350 globals->get_soundmgr()->stop( "inner-marker" );
355 last_beacon = beacon_type;
357 last_beacon = NOBEACON;