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) :
48 name("marker-beacon"),
50 _time_before_search_sec(0.0)
52 SGPath path( globals->get_fg_root() );
54 term.append( "Navaids/range.term" );
56 low.append( "Navaids/range.low" );
58 high.append( "Navaids/range.high" );
60 term_tbl = new SGInterpTable( term.str() );
61 low_tbl = new SGInterpTable( low.str() );
62 high_tbl = new SGInterpTable( high.str() );
65 for ( i = 0; i < node->nChildren(); ++i ) {
66 SGPropertyNode *child = node->getChild(i);
67 string cname = child->getName();
68 string cval = child->getStringValue();
69 if ( cname == "name" ) {
71 } else if ( cname == "number" ) {
72 num = child->getIntValue();
74 SG_LOG( SG_INSTR, SG_WARN,
75 "Error in marker beacon config logic" );
76 if ( name.length() ) {
77 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
85 FGMarkerBeacon::~FGMarkerBeacon()
94 FGMarkerBeacon::init ()
97 branch = "/instrumentation/" + name;
99 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
101 sound_pause = fgGetNode("/sim/sound/pause", false);
102 lon_node = fgGetNode("/position/longitude-deg", true);
103 lat_node = fgGetNode("/position/latitude-deg", true);
104 alt_node = fgGetNode("/position/altitude-ft", true);
105 bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
106 power_btn = node->getChild("power-btn", 0, true);
107 audio_btn = node->getChild("audio-btn", 0, true);
108 serviceable = node->getChild("serviceable", 0, true);
110 power_btn->setBoolValue( true );
111 audio_btn->setBoolValue( true );
112 serviceable->setBoolValue( true );
121 FGMarkerBeacon::bind ()
124 branch = "/instrumentation/" + name;
126 fgTie((branch + "/inner").c_str(), this,
127 &FGMarkerBeacon::get_inner_blink);
129 fgTie((branch + "/middle").c_str(), this,
130 &FGMarkerBeacon::get_middle_blink);
132 fgTie((branch + "/outer").c_str(), this,
133 &FGMarkerBeacon::get_outer_blink);
138 FGMarkerBeacon::unbind ()
141 branch = "/instrumentation/" + name;
143 fgUntie((branch + "/inner").c_str());
144 fgUntie((branch + "/middle").c_str());
145 fgUntie((branch + "/outer").c_str());
149 // Update the various nav values based on position and valid tuned in navs
151 FGMarkerBeacon::update(double dt)
155 if ( has_power() && serviceable->getBoolValue()
156 && !sound_pause->getBoolValue()) {
158 // On timeout, scan again
159 _time_before_search_sec -= dt;
160 if ( _time_before_search_sec < 0 ) {
163 // marker beacon blinking
164 bool light_on = ( outer_blink || middle_blink || inner_blink );
168 if ( light_on && (current - blink > 400000) ) {
171 } else if ( !light_on && (current - blink > 100000) ) {
176 if ( outer_marker ) {
177 outer_blink = light_on;
182 if ( middle_marker ) {
183 middle_blink = light_on;
185 middle_blink = false;
188 if ( inner_marker ) {
189 inner_blink = light_on;
194 // cout << outer_blink << " " << middle_blink << " "
195 // << inner_blink << endl;
197 inner_blink = middle_blink = outer_blink = false;
202 static bool check_beacon_range( double lon_rad, double lat_rad, double elev_m,
205 Point3D aircraft = sgGeodToCart( Point3D(lon_rad, lat_rad, elev_m) );
206 Point3D station = Point3D( b->get_x(), b->get_y(), b->get_z() );
207 // cout << " aircraft = " << aircraft << " station = " << station
210 double d = aircraft.distance3Dsquared( station ); // meters^2
211 // cout << " distance = " << d << " ("
212 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
213 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
216 // cout << " range = " << sqrt(d) << endl;
218 // cout << "elev = " << elev * SG_METER_TO_FEET
219 // << " current->get_elev() = " << current->get_elev() << endl;
220 double elev_ft = elev_m * SG_METER_TO_FEET;
221 double delev = elev_ft - b->get_elev_ft();
223 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
224 // whichever is smaller. The intersection point is 1538 ...
225 double maxrange2; // feet^2
226 if ( delev < 1538.0 ) {
227 maxrange2 = 2.4 * 2.4 * delev * delev;
228 } else if ( delev < 4000.0 ) {
229 maxrange2 = 4000 * 4000 - delev * delev;
233 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
234 // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
236 // match up to twice the published range so we can model
237 // reduced signal strength
238 if ( d < maxrange2 ) {
245 // Update current nav/adf radio stations based on current postition
246 void FGMarkerBeacon::search()
250 _time_before_search_sec = 1.0;
252 static fgMkrBeacType last_beacon = NOBEACON;
254 double lon_rad = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
255 double lat_rad = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
256 double elev_m = alt_node->getDoubleValue() * SG_FEET_TO_METER;
258 ////////////////////////////////////////////////////////////////////////
260 ////////////////////////////////////////////////////////////////////////
262 // get closest marker beacon
264 = globals->get_mkrlist()->findClosest( lon_rad, lat_rad, elev_m );
266 // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
268 fgMkrBeacType beacon_type = NOBEACON;
269 bool inrange = false;
271 if ( b->get_type() == 7 ) {
273 } else if ( b->get_type() == 8 ) {
274 beacon_type = MIDDLE;
275 } else if ( b->get_type() == 9 ) {
278 inrange = check_beacon_range( lon_rad, lat_rad, elev_m, b );
279 // cout << " inrange = " << inrange << endl;
282 outer_marker = middle_marker = inner_marker = false;
284 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
286 // cout << "no marker" << endl;
287 globals->get_soundmgr()->stop( "outer-marker" );
288 globals->get_soundmgr()->stop( "middle-marker" );
289 globals->get_soundmgr()->stop( "inner-marker" );
290 } else if ( beacon_type == OUTER ) {
292 // cout << "OUTER MARKER" << endl;
293 if ( last_beacon != OUTER ) {
294 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
295 SGSoundSample *sound = beacon.get_outer();
297 sound->set_volume( 0.3 );
298 globals->get_soundmgr()->add( sound, "outer-marker" );
302 if ( audio_btn->getBoolValue() ) {
303 if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
304 globals->get_soundmgr()->play_looped( "outer-marker" );
307 globals->get_soundmgr()->stop( "outer-marker" );
309 } else if ( beacon_type == MIDDLE ) {
310 middle_marker = true;
311 // cout << "MIDDLE MARKER" << endl;
312 if ( last_beacon != MIDDLE ) {
313 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
314 SGSoundSample *sound = beacon.get_middle();
316 sound->set_volume( 0.3 );
317 globals->get_soundmgr()->add( sound, "middle-marker" );
321 if ( audio_btn->getBoolValue() ) {
322 if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
323 globals->get_soundmgr()->play_looped( "middle-marker" );
326 globals->get_soundmgr()->stop( "middle-marker" );
328 } else if ( beacon_type == INNER ) {
330 // cout << "INNER MARKER" << endl;
331 if ( last_beacon != INNER ) {
332 if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
333 SGSoundSample *sound = beacon.get_inner();
335 sound->set_volume( 0.3 );
336 globals->get_soundmgr()->add( sound, "inner-marker" );
340 if ( audio_btn->getBoolValue() ) {
341 if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
342 globals->get_soundmgr()->play_looped( "inner-marker" );
345 globals->get_soundmgr()->stop( "inner-marker" );
350 last_beacon = beacon_type;
352 last_beacon = NOBEACON;