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"
43 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 outer_marker = middle_marker = inner_marker = false;
132 FGMarkerBeacon::bind ()
135 branch = "/instrumentation/" + name;
137 fgTie((branch + "/inner").c_str(), this,
138 &FGMarkerBeacon::get_inner_blink);
140 fgTie((branch + "/middle").c_str(), this,
141 &FGMarkerBeacon::get_middle_blink);
143 fgTie((branch + "/outer").c_str(), this,
144 &FGMarkerBeacon::get_outer_blink);
149 FGMarkerBeacon::unbind ()
152 branch = "/instrumentation/" + name;
154 fgUntie((branch + "/inner").c_str());
155 fgUntie((branch + "/middle").c_str());
156 fgUntie((branch + "/outer").c_str());
160 // Update the various nav values based on position and valid tuned in navs
162 FGMarkerBeacon::update(double dt)
166 // On timeout, scan again, this needs to run every iteration no
167 // matter what the power or serviceable state. If power is turned
168 // off or the unit becomes unserviceable while a beacon sound is
169 // playing, the search() routine still needs to be called so the
170 // sound effect can be properly disabled.
172 _time_before_search_sec -= dt;
173 if ( _time_before_search_sec < 0 ) {
177 if ( has_power() && serviceable->getBoolValue()
178 && sound_working->getBoolValue()) {
180 // marker beacon blinking
181 bool light_on = ( outer_blink || middle_blink || inner_blink );
182 SGTimeStamp current = SGTimeStamp::now();
184 if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
187 } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
192 if ( outer_marker ) {
193 outer_blink = light_on;
198 if ( middle_marker ) {
199 middle_blink = light_on;
201 middle_blink = false;
204 if ( inner_marker ) {
205 inner_blink = light_on;
210 // cout << outer_blink << " " << middle_blink << " "
211 // << inner_blink << endl;
213 inner_blink = middle_blink = outer_blink = false;
218 static bool check_beacon_range( const SGGeod& pos,
221 double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
222 // cout << " distance = " << d << " ("
223 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
224 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
227 //std::cout << " range = " << sqrt(d) << std::endl;
229 // cout << "elev = " << elev * SG_METER_TO_FEET
230 // << " current->get_elev() = " << current->get_elev() << endl;
231 double elev_ft = pos.getElevationFt();
232 double delev = elev_ft - b->elevation();
234 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
235 // whichever is smaller. The intersection point is 1538 ...
236 double maxrange2; // feet^2
237 if ( delev < 1538.0 ) {
238 maxrange2 = 2.4 * 2.4 * delev * delev;
239 } else if ( delev < 4000.0 ) {
240 maxrange2 = 4000 * 4000 - delev * delev;
244 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
245 //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
247 // match up to twice the published range so we can model
248 // reduced signal strength
249 if ( d < maxrange2 ) {
256 class BeaconFilter : public FGPositioned::Filter
259 virtual FGPositioned::Type minType() const {
260 return FGPositioned::OM;
263 virtual FGPositioned::Type maxType() const {
264 return FGPositioned::IM;
269 // Update current nav/adf radio stations based on current postition
270 void FGMarkerBeacon::search()
273 _time_before_search_sec = 1.0;
275 static fgMkrBeacType last_beacon = NOBEACON;
277 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
278 lat_node->getDoubleValue(),
279 alt_node->getDoubleValue());
281 ////////////////////////////////////////////////////////////////////////
283 ////////////////////////////////////////////////////////////////////////
285 // get closest marker beacon - within a 1nm cutoff
287 FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
289 fgMkrBeacType beacon_type = NOBEACON;
290 bool inrange = false;
292 if ( b->type() == FGPositioned::OM ) {
294 } else if ( b->type() == FGPositioned::MM ) {
295 beacon_type = MIDDLE;
296 } else if ( b->type() == FGPositioned::IM ) {
299 inrange = check_beacon_range( pos, b.ptr() );
302 outer_marker = middle_marker = inner_marker = false;
304 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
306 // cout << "no marker" << endl;
307 _sgr->stop( "outer-marker" );
308 _sgr->stop( "middle-marker" );
309 _sgr->stop( "inner-marker" );
312 string current_sound_name;
314 if ( beacon_type == OUTER ) {
316 current_sound_name = "outer-marker";
317 // cout << "OUTER MARKER" << endl;
318 if ( last_beacon != OUTER ) {
319 if ( ! _sgr->exists( current_sound_name ) ) {
320 SGSoundSample *sound = beacon.get_outer();
322 _sgr->add( sound, current_sound_name );
326 if ( audio_btn->getBoolValue() ) {
327 if ( !_sgr->is_playing(current_sound_name) ) {
328 _sgr->play_looped( current_sound_name );
331 _sgr->stop( current_sound_name );
333 } else if ( beacon_type == MIDDLE ) {
334 middle_marker = true;
335 current_sound_name = "middle-marker";
336 // cout << "MIDDLE MARKER" << endl;
337 if ( last_beacon != MIDDLE ) {
338 if ( ! _sgr->exists( current_sound_name ) ) {
339 SGSoundSample *sound = beacon.get_middle();
341 _sgr->add( sound, current_sound_name );
345 if ( audio_btn->getBoolValue() ) {
346 if ( !_sgr->is_playing(current_sound_name) ) {
347 _sgr->play_looped( current_sound_name );
350 _sgr->stop( current_sound_name );
352 } else if ( beacon_type == INNER ) {
354 current_sound_name = "inner-marker";
355 // cout << "INNER MARKER" << endl;
356 if ( last_beacon != INNER ) {
357 if ( ! _sgr->exists( current_sound_name ) ) {
358 SGSoundSample *sound = beacon.get_inner();
360 _sgr->add( sound, current_sound_name );
364 if ( audio_btn->getBoolValue() ) {
365 if ( !_sgr->is_playing(current_sound_name) ) {
366 _sgr->play_looped( current_sound_name );
369 _sgr->stop( current_sound_name );
372 // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
373 SGSoundSample * mkr = _sgr->find( current_sound_name );
375 mkr->set_volume( audio_vol->getFloatValue() );
379 last_beacon = beacon_type;
381 last_beacon = NOBEACON;