1 // marker_beacon.cxx -- class to manage the marker beacons
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <config.h>
25
26 #include <stdio.h> // snprintf
27
28 #include <simgear/compiler.h>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/sound/sample_group.hxx>
32
33 #include <Main/fg_props.hxx>
34 #include <Navaids/navlist.hxx>
35
36 #include "marker_beacon.hxx"
37 #include <Sound/beacon.hxx>
38
39 #include <string>
40 using std::string;
41
42 // Constructor
FGMarkerBeacon(SGPropertyNode * node)43 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
44 outer_blink(false),
45 middle_blink(false),
46 inner_blink(false),
47 _time_before_search_sec(0.0)
48 {
49 // backwards-compatability supply path
50 setDefaultPowerSupplyPath("/systems/electrical/outputs/nav[0]");
51 readConfig(node, "marker-beacon");
52 }
53
54
55 // Destructor
~FGMarkerBeacon()56 FGMarkerBeacon::~FGMarkerBeacon()
57 {
58 }
59
60
61 void
init()62 FGMarkerBeacon::init ()
63 {
64 SGPropertyNode *node = fgGetNode(nodePath(), true );
65 initServicePowerProperties(node);
66
67 // Inputs
68 sound_working = fgGetNode("/sim/sound/working", true);
69 lon_node = fgGetNode("/position/longitude-deg", true);
70 lat_node = fgGetNode("/position/latitude-deg", true);
71 alt_node = fgGetNode("/position/altitude-ft", true);
72 audio_btn = node->getChild("audio-btn", 0, true);
73 audio_vol = node->getChild("volume", 0, true);
74
75 if (audio_btn->getType() == simgear::props::NONE)
76 audio_btn->setBoolValue( true );
77
78 SGSoundMgr *smgr = globals->get_subsystem<SGSoundMgr>();
79 _sgr = smgr->find("avionics", true);
80 _sgr->tie_to_listener();
81
82 reinit();
83 }
84
85 void
reinit()86 FGMarkerBeacon::reinit ()
87 {
88 blink.stamp();
89 outer_marker = middle_marker = inner_marker = false;
90 _time_before_search_sec = 0.0;
91 }
92
93 void
bind()94 FGMarkerBeacon::bind ()
95 {
96 string branch = nodePath();
97
98 fgTie((branch + "/inner").c_str(), this,
99 &FGMarkerBeacon::get_inner_blink);
100
101 fgTie((branch + "/middle").c_str(), this,
102 &FGMarkerBeacon::get_middle_blink);
103
104 fgTie((branch + "/outer").c_str(), this,
105 &FGMarkerBeacon::get_outer_blink);
106 }
107
108
109 void
unbind()110 FGMarkerBeacon::unbind ()
111 {
112 string branch = nodePath();
113
114 fgUntie((branch + "/inner").c_str());
115 fgUntie((branch + "/middle").c_str());
116 fgUntie((branch + "/outer").c_str());
117
118 AbstractInstrument::unbind();
119 }
120
121
122 // Update the various nav values based on position and valid tuned in navs
123 void
update(double dt)124 FGMarkerBeacon::update(double dt)
125 {
126 // On timeout, scan again, this needs to run every iteration no
127 // matter what the power or serviceable state. If power is turned
128 // off or the unit becomes unserviceable while a beacon sound is
129 // playing, the search() routine still needs to be called so the
130 // sound effect can be properly disabled.
131
132 _time_before_search_sec -= dt;
133 if ( _time_before_search_sec < 0 ) {
134 search();
135 }
136
137 if ( isServiceableAndPowered() && sound_working->getBoolValue()) {
138
139 // marker beacon blinking
140 bool light_on = ( outer_blink || middle_blink || inner_blink );
141 SGTimeStamp current = SGTimeStamp::now();
142
143 if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
144 light_on = false;
145 blink = current;
146 } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
147 light_on = true;
148 blink = current;
149 }
150
151 if ( outer_marker ) {
152 outer_blink = light_on;
153 } else {
154 outer_blink = false;
155 }
156
157 if ( middle_marker ) {
158 middle_blink = light_on;
159 } else {
160 middle_blink = false;
161 }
162
163 if ( inner_marker ) {
164 inner_blink = light_on;
165 } else {
166 inner_blink = false;
167 }
168
169 // cout << outer_blink << " " << middle_blink << " "
170 // << inner_blink << endl;
171 } else {
172 inner_blink = middle_blink = outer_blink = false;
173 }
174 }
175
176
check_beacon_range(const SGGeod & pos,FGPositioned * b)177 static bool check_beacon_range( const SGGeod& pos,
178 FGPositioned *b )
179 {
180 double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
181 // cout << " distance = " << d << " ("
182 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
183 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
184 // << ")" << endl;
185
186 //std::cout << " range = " << sqrt(d) << std::endl;
187
188 // cout << "elev = " << elev * SG_METER_TO_FEET
189 // << " current->get_elev() = " << current->get_elev() << endl;
190 double elev_ft = pos.getElevationFt();
191 double delev = elev_ft - b->elevation();
192
193 // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
194 // whichever is smaller. The intersection point is 1538 ...
195 double maxrange2; // feet^2
196 if ( delev < 1538.0 ) {
197 maxrange2 = 2.4 * 2.4 * delev * delev;
198 } else if ( delev < 4000.0 ) {
199 maxrange2 = 4000 * 4000 - delev * delev;
200 } else {
201 maxrange2 = 0.0;
202 }
203 maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
204 //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
205
206 // match up to twice the published range so we can model
207 // reduced signal strength
208 if ( d < maxrange2 ) {
209 return true;
210 } else {
211 return false;
212 }
213 }
214
215 class BeaconFilter : public FGPositioned::Filter
216 {
217 public:
minType() const218 virtual FGPositioned::Type minType() const {
219 return FGPositioned::OM;
220 }
221
maxType() const222 virtual FGPositioned::Type maxType() const {
223 return FGPositioned::IM;
224 }
225
226 };
227
228 // Update current nav/adf radio stations based on current postition
search()229 void FGMarkerBeacon::search()
230 {
231 // reset search time
232 _time_before_search_sec = 1.0;
233
234 static fgMkrBeacType last_beacon = NOBEACON;
235
236 SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
237 lat_node->getDoubleValue(),
238 alt_node->getDoubleValue());
239
240 ////////////////////////////////////////////////////////////////////////
241 // Beacons.
242 ////////////////////////////////////////////////////////////////////////
243
244 // get closest marker beacon - within a 1nm cutoff
245 BeaconFilter filter;
246 FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
247
248 fgMkrBeacType beacon_type = NOBEACON;
249 bool inrange = false;
250 if ( b != NULL ) {
251 if ( b->type() == FGPositioned::OM ) {
252 beacon_type = OUTER;
253 } else if ( b->type() == FGPositioned::MM ) {
254 beacon_type = MIDDLE;
255 } else if ( b->type() == FGPositioned::IM ) {
256 beacon_type = INNER;
257 }
258 inrange = check_beacon_range( pos, b.ptr() );
259 }
260
261 outer_marker = middle_marker = inner_marker = false;
262
263 if ( b == NULL || !inrange || !isServiceableAndPowered())
264 {
265 // cout << "no marker" << endl;
266 _sgr->stop( "outer-marker" );
267 _sgr->stop( "middle-marker" );
268 _sgr->stop( "inner-marker" );
269 } else {
270
271 string current_sound_name;
272
273 if ( beacon_type == OUTER ) {
274 outer_marker = true;
275 current_sound_name = "outer-marker";
276 // cout << "OUTER MARKER" << endl;
277 if ( last_beacon != OUTER ) {
278 if ( ! _sgr->exists( current_sound_name ) ) {
279 SGSoundSample *sound = FGBeacon::instance()->get_outer();
280 if ( sound ) {
281 _sgr->add( sound, current_sound_name );
282 }
283 }
284 }
285 if ( audio_btn->getBoolValue() ) {
286 if ( !_sgr->is_playing(current_sound_name) ) {
287 _sgr->play_looped( current_sound_name );
288 }
289 } else {
290 _sgr->stop( current_sound_name );
291 }
292 } else if ( beacon_type == MIDDLE ) {
293 middle_marker = true;
294 current_sound_name = "middle-marker";
295 // cout << "MIDDLE MARKER" << endl;
296 if ( last_beacon != MIDDLE ) {
297 if ( ! _sgr->exists( current_sound_name ) ) {
298 SGSoundSample *sound = FGBeacon::instance()->get_middle();
299 if ( sound ) {
300 _sgr->add( sound, current_sound_name );
301 }
302 }
303 }
304 if ( audio_btn->getBoolValue() ) {
305 if ( !_sgr->is_playing(current_sound_name) ) {
306 _sgr->play_looped( current_sound_name );
307 }
308 } else {
309 _sgr->stop( current_sound_name );
310 }
311 } else if ( beacon_type == INNER ) {
312 inner_marker = true;
313 current_sound_name = "inner-marker";
314 // cout << "INNER MARKER" << endl;
315 if ( last_beacon != INNER ) {
316 if ( ! _sgr->exists( current_sound_name ) ) {
317 SGSoundSample *sound = FGBeacon::instance()->get_inner();
318 if ( sound ) {
319 _sgr->add( sound, current_sound_name );
320 }
321 }
322 }
323 if ( audio_btn->getBoolValue() ) {
324 if ( !_sgr->is_playing(current_sound_name) ) {
325 _sgr->play_looped( current_sound_name );
326 }
327 } else {
328 _sgr->stop( current_sound_name );
329 }
330 }
331 // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
332 SGSoundSample * mkr = _sgr->find( current_sound_name );
333 if (mkr)
334 mkr->set_volume( audio_vol->getFloatValue() );
335 }
336
337 if ( inrange ) {
338 last_beacon = beacon_type;
339 } else {
340 last_beacon = NOBEACON;
341 }
342 }
343
344 // Register the subsystem.
345 #if 0
346 SGSubsystemMgr::InstancedRegistrant<FGMarkerBeacon> registrantFGMarkerBeacon(
347 SGSubsystemMgr::FDM,
348 {{"instrumentation", SGSubsystemMgr::Dependency::HARD}},
349 0.2);
350 #endif
351