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>
34 #include <Navaids/navlist.hxx>
36 #include "marker_beacon.hxx"
37 #include <Sound/beacon.hxx>
44 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
49 name("marker-beacon"),
51 _time_before_search_sec(0.0),
54 SGPath path( globals->get_fg_root() );
56 term.append( "Navaids/range.term" );
58 low.append( "Navaids/range.low" );
60 high.append( "Navaids/range.high" );
62 term_tbl = new SGInterpTable( term.str() );
63 low_tbl = new SGInterpTable( low.str() );
64 high_tbl = new SGInterpTable( high.str() );
66 for ( int 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_working = fgGetNode("/sim/sound/working", true);
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 if (power_btn->getType() == simgear::props::NONE)
113 power_btn->setBoolValue( true );
114 if (audio_btn->getType() == simgear::props::NONE)
115 audio_btn->setBoolValue( true );
116 if (serviceable->getType() == simgear::props::NONE)
117 serviceable->setBoolValue( true );
119 SGSoundMgr *smgr = globals->get_soundmgr();
120 _sgr = smgr->find("avionics", true);
121 _sgr->tie_to_listener();
127 FGMarkerBeacon::reinit ()
130 outer_marker = middle_marker = inner_marker = false;
131 _time_before_search_sec = 0.0;
135 FGMarkerBeacon::bind ()
138 branch = "/instrumentation/" + name;
140 fgTie((branch + "/inner").c_str(), this,
141 &FGMarkerBeacon::get_inner_blink);
143 fgTie((branch + "/middle").c_str(), this,
144 &FGMarkerBeacon::get_middle_blink);
146 fgTie((branch + "/outer").c_str(), this,
147 &FGMarkerBeacon::get_outer_blink);
152 FGMarkerBeacon::unbind ()
155 branch = "/instrumentation/" + name;
157 fgUntie((branch + "/inner").c_str());
158 fgUntie((branch + "/middle").c_str());
159 fgUntie((branch + "/outer").c_str());
163 // Update the various nav values based on position and valid tuned in navs
165 FGMarkerBeacon::update(double dt)
167 // On timeout, scan again, this needs to run every iteration no
168 // matter what the power or serviceable state. If power is turned
169 // off or the unit becomes unserviceable while a beacon sound is
170 // playing, the search() routine still needs to be called so the
171 // sound effect can be properly disabled.
173 _time_before_search_sec -= dt;
174 if ( _time_before_search_sec < 0 ) {
178 if ( has_power() && serviceable->getBoolValue()
179 && sound_working->getBoolValue()) {
181 // marker beacon blinking
182 bool light_on = ( outer_blink || middle_blink || inner_blink );
183 SGTimeStamp current = SGTimeStamp::now();
185 if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
188 } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
193 if ( outer_marker ) {
194 outer_blink = light_on;
199 if ( middle_marker ) {
200 middle_blink = light_on;
202 middle_blink = false;
205 if ( inner_marker ) {
206 inner_blink = light_on;
211 // cout << outer_blink << " " << middle_blink << " "
212 // << inner_blink << endl;
214 inner_blink = middle_blink = outer_blink = false;
219 static bool check_beacon_range( const SGGeod& pos,
222 double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
223 // cout << " distance = " << d << " ("
224 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
225 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
228 //std::cout << " range = " << sqrt(d) << std::endl;
230 // cout << "elev = " << elev * SG_METER_TO_FEET
231 // << " current->get_elev() = " << current->get_elev() << endl;
232 double elev_ft = pos.getElevationFt();
233 double delev = elev_ft - b->elevation();
235 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
236 // whichever is smaller. The intersection point is 1538 ...
237 double maxrange2; // feet^2
238 if ( delev < 1538.0 ) {
239 maxrange2 = 2.4 * 2.4 * delev * delev;
240 } else if ( delev < 4000.0 ) {
241 maxrange2 = 4000 * 4000 - delev * delev;
245 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
246 //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
248 // match up to twice the published range so we can model
249 // reduced signal strength
250 if ( d < maxrange2 ) {
257 class BeaconFilter : public FGPositioned::Filter
260 virtual FGPositioned::Type minType() const {
261 return FGPositioned::OM;
264 virtual FGPositioned::Type maxType() const {
265 return FGPositioned::IM;
270 // Update current nav/adf radio stations based on current postition
271 void FGMarkerBeacon::search()
274 _time_before_search_sec = 1.0;
276 static fgMkrBeacType last_beacon = NOBEACON;
278 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
279 lat_node->getDoubleValue(),
280 alt_node->getDoubleValue());
282 ////////////////////////////////////////////////////////////////////////
284 ////////////////////////////////////////////////////////////////////////
286 // get closest marker beacon - within a 1nm cutoff
288 FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
290 fgMkrBeacType beacon_type = NOBEACON;
291 bool inrange = false;
293 if ( b->type() == FGPositioned::OM ) {
295 } else if ( b->type() == FGPositioned::MM ) {
296 beacon_type = MIDDLE;
297 } else if ( b->type() == FGPositioned::IM ) {
300 inrange = check_beacon_range( pos, b.ptr() );
303 outer_marker = middle_marker = inner_marker = false;
305 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
307 // cout << "no marker" << endl;
308 _sgr->stop( "outer-marker" );
309 _sgr->stop( "middle-marker" );
310 _sgr->stop( "inner-marker" );
313 string current_sound_name;
315 if ( beacon_type == OUTER ) {
317 current_sound_name = "outer-marker";
318 // cout << "OUTER MARKER" << endl;
319 if ( last_beacon != OUTER ) {
320 if ( ! _sgr->exists( current_sound_name ) ) {
321 SGSoundSample *sound = FGBeacon::instance()->get_outer();
323 _sgr->add( sound, current_sound_name );
327 if ( audio_btn->getBoolValue() ) {
328 if ( !_sgr->is_playing(current_sound_name) ) {
329 _sgr->play_looped( current_sound_name );
332 _sgr->stop( current_sound_name );
334 } else if ( beacon_type == MIDDLE ) {
335 middle_marker = true;
336 current_sound_name = "middle-marker";
337 // cout << "MIDDLE MARKER" << endl;
338 if ( last_beacon != MIDDLE ) {
339 if ( ! _sgr->exists( current_sound_name ) ) {
340 SGSoundSample *sound = FGBeacon::instance()->get_middle();
342 _sgr->add( sound, current_sound_name );
346 if ( audio_btn->getBoolValue() ) {
347 if ( !_sgr->is_playing(current_sound_name) ) {
348 _sgr->play_looped( current_sound_name );
351 _sgr->stop( current_sound_name );
353 } else if ( beacon_type == INNER ) {
355 current_sound_name = "inner-marker";
356 // cout << "INNER MARKER" << endl;
357 if ( last_beacon != INNER ) {
358 if ( ! _sgr->exists( current_sound_name ) ) {
359 SGSoundSample *sound = FGBeacon::instance()->get_inner();
361 _sgr->add( sound, current_sound_name );
365 if ( audio_btn->getBoolValue() ) {
366 if ( !_sgr->is_playing(current_sound_name) ) {
367 _sgr->play_looped( current_sound_name );
370 _sgr->stop( current_sound_name );
373 // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
374 SGSoundSample * mkr = _sgr->find( current_sound_name );
376 mkr->set_volume( audio_vol->getFloatValue() );
380 last_beacon = beacon_type;
382 last_beacon = NOBEACON;