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>
32 #include <simgear/misc/sg_path.hxx>
33 #include <simgear/sound/sample_group.hxx>
35 #include <Navaids/navlist.hxx>
37 #include "marker_beacon.hxx"
38 #include <Sound/beacon.hxx>
44 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
49 name("marker-beacon"),
51 _time_before_search_sec(0.0),
54 for ( int i = 0; i < node->nChildren(); ++i ) {
55 SGPropertyNode *child = node->getChild(i);
56 string cname = child->getName();
57 string cval = child->getStringValue();
58 if ( cname == "name" ) {
60 } else if ( cname == "number" ) {
61 num = child->getIntValue();
63 SG_LOG( SG_INSTR, SG_WARN,
64 "Error in marker beacon config logic" );
65 if ( name.length() ) {
66 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
74 FGMarkerBeacon::~FGMarkerBeacon()
80 FGMarkerBeacon::init ()
83 branch = "/instrumentation/" + name;
85 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
87 sound_working = fgGetNode("/sim/sound/working", true);
88 lon_node = fgGetNode("/position/longitude-deg", true);
89 lat_node = fgGetNode("/position/latitude-deg", true);
90 alt_node = fgGetNode("/position/altitude-ft", true);
91 bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
92 power_btn = node->getChild("power-btn", 0, true);
93 audio_btn = node->getChild("audio-btn", 0, true);
94 audio_vol = node->getChild("volume", 0, true);
95 serviceable = node->getChild("serviceable", 0, true);
97 if (power_btn->getType() == simgear::props::NONE)
98 power_btn->setBoolValue( true );
99 if (audio_btn->getType() == simgear::props::NONE)
100 audio_btn->setBoolValue( true );
101 if (serviceable->getType() == simgear::props::NONE)
102 serviceable->setBoolValue( true );
104 SGSoundMgr *smgr = globals->get_soundmgr();
105 _sgr = smgr->find("avionics", true);
106 _sgr->tie_to_listener();
112 FGMarkerBeacon::reinit ()
115 outer_marker = middle_marker = inner_marker = false;
116 _time_before_search_sec = 0.0;
120 FGMarkerBeacon::bind ()
123 branch = "/instrumentation/" + name;
125 fgTie((branch + "/inner").c_str(), this,
126 &FGMarkerBeacon::get_inner_blink);
128 fgTie((branch + "/middle").c_str(), this,
129 &FGMarkerBeacon::get_middle_blink);
131 fgTie((branch + "/outer").c_str(), this,
132 &FGMarkerBeacon::get_outer_blink);
137 FGMarkerBeacon::unbind ()
140 branch = "/instrumentation/" + name;
142 fgUntie((branch + "/inner").c_str());
143 fgUntie((branch + "/middle").c_str());
144 fgUntie((branch + "/outer").c_str());
148 // Update the various nav values based on position and valid tuned in navs
150 FGMarkerBeacon::update(double dt)
152 // On timeout, scan again, this needs to run every iteration no
153 // matter what the power or serviceable state. If power is turned
154 // off or the unit becomes unserviceable while a beacon sound is
155 // playing, the search() routine still needs to be called so the
156 // sound effect can be properly disabled.
158 _time_before_search_sec -= dt;
159 if ( _time_before_search_sec < 0 ) {
163 if ( has_power() && serviceable->getBoolValue()
164 && sound_working->getBoolValue()) {
166 // marker beacon blinking
167 bool light_on = ( outer_blink || middle_blink || inner_blink );
168 SGTimeStamp current = SGTimeStamp::now();
170 if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
173 } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
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 double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
208 // cout << " distance = " << d << " ("
209 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
210 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
213 //std::cout << " range = " << sqrt(d) << std::endl;
215 // cout << "elev = " << elev * SG_METER_TO_FEET
216 // << " current->get_elev() = " << current->get_elev() << endl;
217 double elev_ft = pos.getElevationFt();
218 double delev = elev_ft - b->elevation();
220 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
221 // whichever is smaller. The intersection point is 1538 ...
222 double maxrange2; // feet^2
223 if ( delev < 1538.0 ) {
224 maxrange2 = 2.4 * 2.4 * delev * delev;
225 } else if ( delev < 4000.0 ) {
226 maxrange2 = 4000 * 4000 - delev * delev;
230 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
231 //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
233 // match up to twice the published range so we can model
234 // reduced signal strength
235 if ( d < maxrange2 ) {
242 class BeaconFilter : public FGPositioned::Filter
245 virtual FGPositioned::Type minType() const {
246 return FGPositioned::OM;
249 virtual FGPositioned::Type maxType() const {
250 return FGPositioned::IM;
255 // Update current nav/adf radio stations based on current postition
256 void FGMarkerBeacon::search()
259 _time_before_search_sec = 1.0;
261 static fgMkrBeacType last_beacon = NOBEACON;
263 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
264 lat_node->getDoubleValue(),
265 alt_node->getDoubleValue());
267 ////////////////////////////////////////////////////////////////////////
269 ////////////////////////////////////////////////////////////////////////
271 // get closest marker beacon - within a 1nm cutoff
273 FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
275 fgMkrBeacType beacon_type = NOBEACON;
276 bool inrange = false;
278 if ( b->type() == FGPositioned::OM ) {
280 } else if ( b->type() == FGPositioned::MM ) {
281 beacon_type = MIDDLE;
282 } else if ( b->type() == FGPositioned::IM ) {
285 inrange = check_beacon_range( pos, b.ptr() );
288 outer_marker = middle_marker = inner_marker = false;
290 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
292 // cout << "no marker" << endl;
293 _sgr->stop( "outer-marker" );
294 _sgr->stop( "middle-marker" );
295 _sgr->stop( "inner-marker" );
298 string current_sound_name;
300 if ( beacon_type == OUTER ) {
302 current_sound_name = "outer-marker";
303 // cout << "OUTER MARKER" << endl;
304 if ( last_beacon != OUTER ) {
305 if ( ! _sgr->exists( current_sound_name ) ) {
306 SGSoundSample *sound = FGBeacon::instance()->get_outer();
308 _sgr->add( sound, current_sound_name );
312 if ( audio_btn->getBoolValue() ) {
313 if ( !_sgr->is_playing(current_sound_name) ) {
314 _sgr->play_looped( current_sound_name );
317 _sgr->stop( current_sound_name );
319 } else if ( beacon_type == MIDDLE ) {
320 middle_marker = true;
321 current_sound_name = "middle-marker";
322 // cout << "MIDDLE MARKER" << endl;
323 if ( last_beacon != MIDDLE ) {
324 if ( ! _sgr->exists( current_sound_name ) ) {
325 SGSoundSample *sound = FGBeacon::instance()->get_middle();
327 _sgr->add( sound, current_sound_name );
331 if ( audio_btn->getBoolValue() ) {
332 if ( !_sgr->is_playing(current_sound_name) ) {
333 _sgr->play_looped( current_sound_name );
336 _sgr->stop( current_sound_name );
338 } else if ( beacon_type == INNER ) {
340 current_sound_name = "inner-marker";
341 // cout << "INNER MARKER" << endl;
342 if ( last_beacon != INNER ) {
343 if ( ! _sgr->exists( current_sound_name ) ) {
344 SGSoundSample *sound = FGBeacon::instance()->get_inner();
346 _sgr->add( sound, current_sound_name );
350 if ( audio_btn->getBoolValue() ) {
351 if ( !_sgr->is_playing(current_sound_name) ) {
352 _sgr->play_looped( current_sound_name );
355 _sgr->stop( current_sound_name );
358 // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
359 SGSoundSample * mkr = _sgr->find( current_sound_name );
361 mkr->set_volume( audio_vol->getFloatValue() );
365 last_beacon = beacon_type;
367 last_beacon = NOBEACON;