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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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() :
44 lon_node(fgGetNode("/position/longitude-deg", true)),
45 lat_node(fgGetNode("/position/latitude-deg", true)),
46 alt_node(fgGetNode("/position/altitude-ft", true)),
47 bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
48 power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
49 audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
50 serviceable(fgGetNode("/instrumentation/marker-beacons/serviceable", true)),
57 SGPath path( globals->get_fg_root() );
59 term.append( "Navaids/range.term" );
61 low.append( "Navaids/range.low" );
63 high.append( "Navaids/range.high" );
65 term_tbl = new SGInterpTable( term.str() );
66 low_tbl = new SGInterpTable( low.str() );
67 high_tbl = new SGInterpTable( high.str() );
69 power_btn->setBoolValue( true );
70 audio_btn->setBoolValue( true );
71 serviceable->setBoolValue( true );
76 FGMarkerBeacon::~FGMarkerBeacon()
85 FGMarkerBeacon::init ()
94 FGMarkerBeacon::bind ()
97 fgTie("/radios/marker-beacon/inner", this,
98 &FGMarkerBeacon::get_inner_blink);
100 fgTie("/radios/marker-beacon/middle", this,
101 &FGMarkerBeacon::get_middle_blink);
103 fgTie("/radios/marker-beacon/outer", this,
104 &FGMarkerBeacon::get_outer_blink);
109 FGMarkerBeacon::unbind ()
111 fgUntie("/radios/marker-beacon/inner");
112 fgUntie("/radios/marker-beacon/middle");
113 fgUntie("/radios/marker-beacon/outer");
117 // Update the various nav values based on position and valid tuned in navs
119 FGMarkerBeacon::update(double dt)
123 if ( has_power() && serviceable->getBoolValue() ) {
124 // marker beacon blinking
125 bool light_on = ( outer_blink || middle_blink || inner_blink );
129 if ( light_on && (current - blink > 400000) ) {
132 } else if ( !light_on && (current - blink > 100000) ) {
137 if ( outer_marker ) {
138 outer_blink = light_on;
143 if ( middle_marker ) {
144 middle_blink = light_on;
146 middle_blink = false;
149 if ( inner_marker ) {
150 inner_blink = light_on;
155 // cout << outer_blink << " " << middle_blink << " "
156 // << inner_blink << endl;
158 inner_blink = middle_blink = outer_blink = false;
163 static bool check_beacon_range( double lon_rad, double lat_rad, double elev_m,
166 Point3D aircraft = sgGeodToCart( Point3D(lon_rad, lat_rad, elev_m) );
167 Point3D station = Point3D( b->get_x(), b->get_y(), b->get_z() );
168 // cout << " aircraft = " << aircraft << " station = " << station
171 double d = aircraft.distance3Dsquared( station ); // meters^2
172 // cout << " distance = " << d << " ("
173 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
174 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
177 // cout << " range = " << sqrt(d) << endl;
179 // cout << "elev = " << elev * SG_METER_TO_FEET
180 // << " current->get_elev() = " << current->get_elev() << endl;
181 double elev_ft = elev_m * SG_METER_TO_FEET;
182 double delev = elev_ft - b->get_elev_ft();
184 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
185 // whichever is smaller. The intersection point is 1538 ...
186 double maxrange2; // feet^2
187 if ( delev < 1538.0 ) {
188 maxrange2 = 2.4 * 2.4 * delev * delev;
189 } else if ( delev < 4000.0 ) {
190 maxrange2 = 4000 * 4000 - delev * delev;
194 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
195 // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
197 // match up to twice the published range so we can model
198 // reduced signal strength
199 if ( d < maxrange2 ) {
206 // Update current nav/adf radio stations based on current postition
207 void FGMarkerBeacon::search()
209 static fgMkrBeacType last_beacon = NOBEACON;
211 double lon_rad = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
212 double lat_rad = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
213 double elev_m = alt_node->getDoubleValue() * SG_FEET_TO_METER;
215 ////////////////////////////////////////////////////////////////////////
217 ////////////////////////////////////////////////////////////////////////
219 // get closest marker beacon
221 = globals->get_mkrlist()->findClosest( lon_rad, lat_rad, elev_m );
223 // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
225 fgMkrBeacType beacon_type = NOBEACON;
226 bool inrange = false;
228 if ( b->get_type() == 7 ) {
230 } else if ( b->get_type() == 8 ) {
231 beacon_type = MIDDLE;
232 } else if ( b->get_type() == 9 ) {
235 inrange = check_beacon_range( lon_rad, lat_rad, elev_m, b );
236 // cout << " inrange = " << inrange << endl;
239 outer_marker = middle_marker = inner_marker = false;
241 if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
243 // cout << "no marker" << endl;
244 globals->get_soundmgr()->stop( "outer-marker" );
245 globals->get_soundmgr()->stop( "middle-marker" );
246 globals->get_soundmgr()->stop( "inner-marker" );
247 } else if ( beacon_type == OUTER ) {
249 // cout << "OUTER MARKER" << endl;
250 if ( last_beacon != OUTER ) {
251 if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
252 SGSoundSample *sound = beacon.get_outer();
253 sound->set_volume( 0.3 );
254 globals->get_soundmgr()->add( sound, "outer-marker" );
257 if ( audio_btn->getBoolValue() ) {
258 if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
259 globals->get_soundmgr()->play_looped( "outer-marker" );
262 globals->get_soundmgr()->stop( "outer-marker" );
264 } else if ( beacon_type == MIDDLE ) {
265 middle_marker = true;
266 // cout << "MIDDLE MARKER" << endl;
267 if ( last_beacon != MIDDLE ) {
268 if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
269 SGSoundSample *sound = beacon.get_middle();
270 sound->set_volume( 0.3 );
271 globals->get_soundmgr()->add( sound, "middle-marker" );
274 if ( audio_btn->getBoolValue() ) {
275 if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
276 globals->get_soundmgr()->play_looped( "middle-marker" );
279 globals->get_soundmgr()->stop( "middle-marker" );
281 } else if ( beacon_type == INNER ) {
283 // cout << "INNER MARKER" << endl;
284 if ( last_beacon != INNER ) {
285 if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
286 SGSoundSample *sound = beacon.get_inner();
287 sound->set_volume( 0.3 );
288 globals->get_soundmgr()->add( sound, "inner-marker" );
291 if ( audio_btn->getBoolValue() ) {
292 if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
293 globals->get_soundmgr()->play_looped( "inner-marker" );
296 globals->get_soundmgr()->stop( "inner-marker" );
301 last_beacon = beacon_type;
303 last_beacon = NOBEACON;