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) :
50 name("marker-beacon"),
52 _time_before_search_sec(0.0),
55 SGPath path( globals->get_fg_root() );
57 term.append( "Navaids/range.term" );
59 low.append( "Navaids/range.low" );
61 high.append( "Navaids/range.high" );
63 term_tbl = new SGInterpTable( term.str() );
64 low_tbl = new SGInterpTable( low.str() );
65 high_tbl = new SGInterpTable( high.str() );
67 for ( int i = 0; i < node->nChildren(); ++i ) {
68 SGPropertyNode *child = node->getChild(i);
69 string cname = child->getName();
70 string cval = child->getStringValue();
71 if ( cname == "name" ) {
73 } else if ( cname == "number" ) {
74 num = child->getIntValue();
76 SG_LOG( SG_INSTR, SG_WARN,
77 "Error in marker beacon config logic" );
78 if ( name.length() ) {
79 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
87 FGMarkerBeacon::~FGMarkerBeacon()
96 FGMarkerBeacon::init ()
99 branch = "/instrumentation/" + name;
101 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
103 sound_working = fgGetNode("/sim/sound/working", true);
104 lon_node = fgGetNode("/position/longitude-deg", true);
105 lat_node = fgGetNode("/position/latitude-deg", true);
106 alt_node = fgGetNode("/position/altitude-ft", true);
107 bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
108 power_btn = node->getChild("power-btn", 0, true);
109 audio_btn = node->getChild("audio-btn", 0, true);
110 audio_vol = node->getChild("volume", 0, true);
111 serviceable = node->getChild("serviceable", 0, true);
113 if (power_btn->getType() == simgear::props::NONE)
114 power_btn->setBoolValue( true );
115 if (audio_btn->getType() == simgear::props::NONE)
116 audio_btn->setBoolValue( true );
117 if (serviceable->getType() == simgear::props::NONE)
118 serviceable->setBoolValue( true );
120 SGSoundMgr *smgr = globals->get_soundmgr();
121 _sgr = smgr->find("avionics", true);
122 _sgr->tie_to_listener();
126 outer_marker = middle_marker = inner_marker = false;
131 FGMarkerBeacon::bind ()
134 branch = "/instrumentation/" + name;
136 fgTie((branch + "/inner").c_str(), this,
137 &FGMarkerBeacon::get_inner_blink);
139 fgTie((branch + "/middle").c_str(), this,
140 &FGMarkerBeacon::get_middle_blink);
142 fgTie((branch + "/outer").c_str(), this,
143 &FGMarkerBeacon::get_outer_blink);
148 FGMarkerBeacon::unbind ()
151 branch = "/instrumentation/" + name;
153 fgUntie((branch + "/inner").c_str());
154 fgUntie((branch + "/middle").c_str());
155 fgUntie((branch + "/outer").c_str());
159 // Update the various nav values based on position and valid tuned in navs
161 FGMarkerBeacon::update(double dt)
165 // On timeout, scan again, this needs to run every iteration no
166 // matter what the power or serviceable state. If power is turned
167 // off or the unit becomes unserviceable while a beacon sound is
168 // playing, the search() routine still needs to be called so the
169 // sound effect can be properly disabled.
171 _time_before_search_sec -= dt;
172 if ( _time_before_search_sec < 0 ) {
176 if ( has_power() && serviceable->getBoolValue()
177 && sound_working->getBoolValue()) {
179 // marker beacon blinking
180 bool light_on = ( outer_blink || middle_blink || inner_blink );
181 SGTimeStamp current = SGTimeStamp::now();
183 if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
186 } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
191 if ( outer_marker ) {
192 outer_blink = light_on;
197 if ( middle_marker ) {
198 middle_blink = light_on;
200 middle_blink = false;
203 if ( inner_marker ) {
204 inner_blink = light_on;
209 // cout << outer_blink << " " << middle_blink << " "
210 // << inner_blink << endl;
212 inner_blink = middle_blink = outer_blink = false;
217 static bool check_beacon_range( const SGGeod& pos,
220 double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
221 // cout << " distance = " << d << " ("
222 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
223 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
226 //std::cout << " range = " << sqrt(d) << std::endl;
228 // cout << "elev = " << elev * SG_METER_TO_FEET
229 // << " current->get_elev() = " << current->get_elev() << endl;
230 double elev_ft = pos.getElevationFt();
231 double delev = elev_ft - b->elevation();
233 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
234 // whichever is smaller. The intersection point is 1538 ...
235 double maxrange2; // feet^2
236 if ( delev < 1538.0 ) {
237 maxrange2 = 2.4 * 2.4 * delev * delev;
238 } else if ( delev < 4000.0 ) {
239 maxrange2 = 4000 * 4000 - delev * delev;
243 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
244 //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
246 // match up to twice the published range so we can model
247 // reduced signal strength
248 if ( d < maxrange2 ) {
255 class BeaconFilter : public FGPositioned::Filter
258 virtual FGPositioned::Type minType() const {
259 return FGPositioned::OM;
262 virtual FGPositioned::Type maxType() const {
263 return FGPositioned::IM;
268 // Update current nav/adf radio stations based on current postition
269 void FGMarkerBeacon::search()
272 _time_before_search_sec = 1.0;
274 static fgMkrBeacType last_beacon = NOBEACON;
276 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
277 lat_node->getDoubleValue(),
278 alt_node->getDoubleValue());
280 ////////////////////////////////////////////////////////////////////////
282 ////////////////////////////////////////////////////////////////////////
284 // get closest marker beacon - within a 1nm cutoff
286 FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
288 fgMkrBeacType beacon_type = NOBEACON;
289 bool inrange = false;
291 if ( b->type() == FGPositioned::OM ) {
293 } else if ( b->type() == FGPositioned::MM ) {
294 beacon_type = MIDDLE;
295 } else if ( b->type() == FGPositioned::IM ) {
298 inrange = check_beacon_range( pos, b.ptr() );
301 outer_marker = middle_marker = inner_marker = false;
303 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
305 // cout << "no marker" << endl;
306 _sgr->stop( "outer-marker" );
307 _sgr->stop( "middle-marker" );
308 _sgr->stop( "inner-marker" );
311 string current_sound_name;
313 if ( beacon_type == OUTER ) {
315 current_sound_name = "outer-marker";
316 // cout << "OUTER MARKER" << endl;
317 if ( last_beacon != OUTER ) {
318 if ( ! _sgr->exists( current_sound_name ) ) {
319 SGSoundSample *sound = FGBeacon::instance()->get_outer();
321 _sgr->add( sound, current_sound_name );
325 if ( audio_btn->getBoolValue() ) {
326 if ( !_sgr->is_playing(current_sound_name) ) {
327 _sgr->play_looped( current_sound_name );
330 _sgr->stop( current_sound_name );
332 } else if ( beacon_type == MIDDLE ) {
333 middle_marker = true;
334 current_sound_name = "middle-marker";
335 // cout << "MIDDLE MARKER" << endl;
336 if ( last_beacon != MIDDLE ) {
337 if ( ! _sgr->exists( current_sound_name ) ) {
338 SGSoundSample *sound = FGBeacon::instance()->get_middle();
340 _sgr->add( sound, current_sound_name );
344 if ( audio_btn->getBoolValue() ) {
345 if ( !_sgr->is_playing(current_sound_name) ) {
346 _sgr->play_looped( current_sound_name );
349 _sgr->stop( current_sound_name );
351 } else if ( beacon_type == INNER ) {
353 current_sound_name = "inner-marker";
354 // cout << "INNER MARKER" << endl;
355 if ( last_beacon != INNER ) {
356 if ( ! _sgr->exists( current_sound_name ) ) {
357 SGSoundSample *sound = FGBeacon::instance()->get_inner();
359 _sgr->add( sound, current_sound_name );
363 if ( audio_btn->getBoolValue() ) {
364 if ( !_sgr->is_playing(current_sound_name) ) {
365 _sgr->play_looped( current_sound_name );
368 _sgr->stop( current_sound_name );
371 // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
372 SGSoundSample * mkr = _sgr->find( current_sound_name );
374 mkr->set_volume( audio_vol->getFloatValue() );
378 last_beacon = beacon_type;
380 last_beacon = NOBEACON;