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) :
49 name("marker-beacon"),
51 _time_before_search_sec(0.0)
53 SGPath path( globals->get_fg_root() );
55 term.append( "Navaids/range.term" );
57 low.append( "Navaids/range.low" );
59 high.append( "Navaids/range.high" );
61 term_tbl = new SGInterpTable( term.str() );
62 low_tbl = new SGInterpTable( low.str() );
63 high_tbl = new SGInterpTable( high.str() );
66 for ( i = 0; i < node->nChildren(); ++i ) {
67 SGPropertyNode *child = node->getChild(i);
68 string cname = child->getName();
69 string cval = child->getStringValue();
70 if ( cname == "name" ) {
72 } else if ( cname == "number" ) {
73 num = child->getIntValue();
75 SG_LOG( SG_INSTR, SG_WARN,
76 "Error in marker beacon config logic" );
77 if ( name.length() ) {
78 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
86 FGMarkerBeacon::~FGMarkerBeacon()
95 FGMarkerBeacon::init ()
98 branch = "/instrumentation/" + name;
100 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
102 sound_pause = fgGetNode("/sim/sound/pause", false);
103 lon_node = fgGetNode("/position/longitude-deg", true);
104 lat_node = fgGetNode("/position/latitude-deg", true);
105 alt_node = fgGetNode("/position/altitude-ft", true);
106 bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
107 power_btn = node->getChild("power-btn", 0, true);
108 audio_btn = node->getChild("audio-btn", 0, true);
109 audio_vol = node->getChild("volume", 0, true);
110 serviceable = node->getChild("serviceable", 0, true);
112 power_btn->setBoolValue( true );
113 audio_btn->setBoolValue( true );
114 serviceable->setBoolValue( true );
120 outer_marker = middle_marker = inner_marker = false;
125 FGMarkerBeacon::bind ()
128 branch = "/instrumentation/" + name;
130 fgTie((branch + "/inner").c_str(), this,
131 &FGMarkerBeacon::get_inner_blink);
133 fgTie((branch + "/middle").c_str(), this,
134 &FGMarkerBeacon::get_middle_blink);
136 fgTie((branch + "/outer").c_str(), this,
137 &FGMarkerBeacon::get_outer_blink);
142 FGMarkerBeacon::unbind ()
145 branch = "/instrumentation/" + name;
147 fgUntie((branch + "/inner").c_str());
148 fgUntie((branch + "/middle").c_str());
149 fgUntie((branch + "/outer").c_str());
153 // Update the various nav values based on position and valid tuned in navs
155 FGMarkerBeacon::update(double dt)
159 if ( has_power() && serviceable->getBoolValue()
160 && !sound_pause->getBoolValue()) {
162 // On timeout, scan again
163 _time_before_search_sec -= dt;
164 if ( _time_before_search_sec < 0 ) {
167 // marker beacon blinking
168 bool light_on = ( outer_blink || middle_blink || inner_blink );
172 if ( light_on && (current - blink > 400000) ) {
175 } else if ( !light_on && (current - blink > 100000) ) {
180 if ( outer_marker ) {
181 outer_blink = light_on;
186 if ( middle_marker ) {
187 middle_blink = light_on;
189 middle_blink = false;
192 if ( inner_marker ) {
193 inner_blink = light_on;
198 // cout << outer_blink << " " << middle_blink << " "
199 // << inner_blink << endl;
201 inner_blink = middle_blink = outer_blink = false;
206 static bool check_beacon_range( const SGGeod& pos,
209 SGVec3d aircraft = SGVec3d::fromGeod(pos);
210 SGVec3d station = b->get_cart();
211 // cout << " aircraft = " << aircraft << " station = " << station
214 SGVec3d tmp = station - aircraft;
215 double d = dot(tmp, tmp);
216 // cout << " distance = " << d << " ("
217 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
218 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
221 // cout << " range = " << sqrt(d) << endl;
223 // cout << "elev = " << elev * SG_METER_TO_FEET
224 // << " current->get_elev() = " << current->get_elev() << endl;
225 double elev_ft = pos.getElevationFt();
226 double delev = elev_ft - b->get_elev_ft();
228 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
229 // whichever is smaller. The intersection point is 1538 ...
230 double maxrange2; // feet^2
231 if ( delev < 1538.0 ) {
232 maxrange2 = 2.4 * 2.4 * delev * delev;
233 } else if ( delev < 4000.0 ) {
234 maxrange2 = 4000 * 4000 - delev * delev;
238 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
239 // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
241 // match up to twice the published range so we can model
242 // reduced signal strength
243 if ( d < maxrange2 ) {
250 // Update current nav/adf radio stations based on current postition
251 void FGMarkerBeacon::search()
255 _time_before_search_sec = 1.0;
257 static fgMkrBeacType last_beacon = NOBEACON;
259 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
260 lat_node->getDoubleValue(),
261 alt_node->getDoubleValue());
263 ////////////////////////////////////////////////////////////////////////
265 ////////////////////////////////////////////////////////////////////////
267 // get closest marker beacon
269 = globals->get_mkrlist()->findClosest( pos.getLongitudeRad(),
270 pos.getLatitudeRad(),
271 pos.getElevationM() );
273 // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
275 fgMkrBeacType beacon_type = NOBEACON;
276 bool inrange = false;
278 if ( b->get_type() == 7 ) {
280 } else if ( b->get_type() == 8 ) {
281 beacon_type = MIDDLE;
282 } else if ( b->get_type() == 9 ) {
285 inrange = check_beacon_range( pos, b );
286 // cout << " inrange = " << inrange << endl;
289 outer_marker = middle_marker = inner_marker = false;
291 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
293 // cout << "no marker" << endl;
294 globals->get_soundmgr()->stop( "outer-marker" );
295 globals->get_soundmgr()->stop( "middle-marker" );
296 globals->get_soundmgr()->stop( "inner-marker" );
299 string current_sound_name;
301 if ( beacon_type == OUTER ) {
303 current_sound_name = "outer-marker";
304 // cout << "OUTER MARKER" << endl;
305 if ( last_beacon != OUTER ) {
306 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
307 SGSoundSample *sound = beacon.get_outer();
309 globals->get_soundmgr()->add( sound, current_sound_name );
313 if ( audio_btn->getBoolValue() ) {
314 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
315 globals->get_soundmgr()->play_looped( current_sound_name );
318 globals->get_soundmgr()->stop( current_sound_name );
320 } else if ( beacon_type == MIDDLE ) {
321 middle_marker = true;
322 current_sound_name = "middle-marker";
323 // cout << "MIDDLE MARKER" << endl;
324 if ( last_beacon != MIDDLE ) {
325 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
326 SGSoundSample *sound = beacon.get_middle();
328 globals->get_soundmgr()->add( sound, current_sound_name );
332 if ( audio_btn->getBoolValue() ) {
333 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
334 globals->get_soundmgr()->play_looped( current_sound_name );
337 globals->get_soundmgr()->stop( current_sound_name );
339 } else if ( beacon_type == INNER ) {
341 current_sound_name = "inner-marker";
342 // cout << "INNER MARKER" << endl;
343 if ( last_beacon != INNER ) {
344 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
345 SGSoundSample *sound = beacon.get_inner();
347 globals->get_soundmgr()->add( sound, current_sound_name );
351 if ( audio_btn->getBoolValue() ) {
352 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
353 globals->get_soundmgr()->play_looped( current_sound_name );
356 globals->get_soundmgr()->stop( current_sound_name );
359 // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
360 SGSoundSample * mkr = globals->get_soundmgr()->find( current_sound_name );
362 mkr->set_volume( audio_vol->getDoubleValue() );
366 last_beacon = beacon_type;
368 last_beacon = NOBEACON;