]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
Merge branch 'ehofman/version'
[flightgear.git] / src / Instrumentation / marker_beacon.cxx
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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>      // snprintf
29
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
32 #include <simgear/misc/sg_path.hxx>
33
34 #include <Aircraft/aircraft.hxx>
35 #include <Navaids/navlist.hxx>
36
37 #include "marker_beacon.hxx"
38
39 #include <string>
40 using std::string;
41
42
43 // Constructor
44 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
45     audio_vol(NULL),
46     need_update(true),
47     outer_blink(false),
48     middle_blink(false),
49     inner_blink(false),
50     name("marker-beacon"),
51     num(0),
52     _time_before_search_sec(0.0),
53     _sgr(NULL)
54 {
55     SGPath path( globals->get_fg_root() );
56     SGPath term = path;
57     term.append( "Navaids/range.term" );
58     SGPath low = path;
59     low.append( "Navaids/range.low" );
60     SGPath high = path;
61     high.append( "Navaids/range.high" );
62
63     term_tbl = new SGInterpTable( term.str() );
64     low_tbl = new SGInterpTable( low.str() );
65     high_tbl = new SGInterpTable( high.str() );
66
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" ) {
72             name = cval;
73         } else if ( cname == "number" ) {
74             num = child->getIntValue();
75         } else {
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 );
80             }
81         }
82     }
83 }
84
85
86 // Destructor
87 FGMarkerBeacon::~FGMarkerBeacon()
88 {
89     delete term_tbl;
90     delete low_tbl;
91     delete high_tbl;
92 }
93
94
95 void
96 FGMarkerBeacon::init ()
97 {
98     string branch;
99     branch = "/instrumentation/" + name;
100
101     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
102     // Inputs
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);
112
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 );
119
120     SGSoundMgr *smgr = globals->get_soundmgr();
121     _sgr = smgr->find("avionics", true);
122     _sgr->tie_to_listener();
123
124     morse.init();
125     beacon.init();
126     blink.stamp();
127
128     outer_marker = middle_marker = inner_marker = false;
129 }
130
131
132 void
133 FGMarkerBeacon::bind ()
134 {
135     string branch;
136     branch = "/instrumentation/" + name;
137
138     fgTie((branch + "/inner").c_str(), this,
139           &FGMarkerBeacon::get_inner_blink);
140
141     fgTie((branch + "/middle").c_str(), this,
142           &FGMarkerBeacon::get_middle_blink);
143
144     fgTie((branch + "/outer").c_str(), this,
145           &FGMarkerBeacon::get_outer_blink);
146 }
147
148
149 void
150 FGMarkerBeacon::unbind ()
151 {
152     string branch;
153     branch = "/instrumentation/" + name;
154
155     fgUntie((branch + "/inner").c_str());
156     fgUntie((branch + "/middle").c_str());
157     fgUntie((branch + "/outer").c_str());
158 }
159
160
161 // Update the various nav values based on position and valid tuned in navs
162 void
163 FGMarkerBeacon::update(double dt)
164 {
165     need_update = false;
166
167     // On timeout, scan again, this needs to run every iteration no
168     // matter what the power or serviceable state.  If power is turned
169     // off or the unit becomes unserviceable while a beacon sound is
170     // playing, the search() routine still needs to be called so the
171     // sound effect can be properly disabled.
172
173     _time_before_search_sec -= dt;
174     if ( _time_before_search_sec < 0 ) {
175         search();
176     }
177
178     if ( has_power() && serviceable->getBoolValue()
179             && sound_working->getBoolValue()) {
180
181         // marker beacon blinking
182         bool light_on = ( outer_blink || middle_blink || inner_blink );
183         SGTimeStamp current = SGTimeStamp::now();
184
185         if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
186             light_on = false;
187             blink = current;
188         } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
189             light_on = true;
190             blink = current;
191         }
192
193         if ( outer_marker ) {
194             outer_blink = light_on;
195         } else {
196             outer_blink = false;
197         }
198
199         if ( middle_marker ) {
200             middle_blink = light_on;
201         } else {
202             middle_blink = false;
203         }
204
205         if ( inner_marker ) {
206             inner_blink = light_on;
207         } else {
208             inner_blink = false;
209         }
210
211         // cout << outer_blink << " " << middle_blink << " "
212         //      << inner_blink << endl;
213     } else {
214         inner_blink = middle_blink = outer_blink = false;
215     }
216 }
217
218
219 static bool check_beacon_range( const SGGeod& pos,
220                                 FGPositioned *b )
221 {
222     double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
223     // cout << "  distance = " << d << " ("
224     //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
225     //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
226     //      << ")" << endl;
227
228     //std::cout << "  range = " << sqrt(d) << std::endl;
229
230     // cout << "elev = " << elev * SG_METER_TO_FEET
231     //      << " current->get_elev() = " << current->get_elev() << endl;
232     double elev_ft = pos.getElevationFt();
233     double delev = elev_ft - b->elevation();
234
235     // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
236     // whichever is smaller.  The intersection point is 1538 ...
237     double maxrange2;   // feet^2
238     if ( delev < 1538.0 ) {
239         maxrange2 = 2.4 * 2.4 * delev * delev;
240     } else if ( delev < 4000.0 ) {
241         maxrange2 = 4000 * 4000 - delev * delev;
242     } else {
243         maxrange2 = 0.0;
244     }
245     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
246     //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
247
248     // match up to twice the published range so we can model
249     // reduced signal strength
250     if ( d < maxrange2 ) {
251         return true;
252     } else {
253         return false;
254     }
255 }
256
257 class BeaconFilter : public FGPositioned::Filter
258 {
259 public:
260   virtual FGPositioned::Type minType() const {
261     return FGPositioned::OM;
262   }
263
264   virtual FGPositioned::Type maxType()  const {
265     return FGPositioned::IM;
266   }
267
268 };
269
270 // Update current nav/adf radio stations based on current postition
271 void FGMarkerBeacon::search()
272 {
273     // reset search time
274     _time_before_search_sec = 1.0;
275
276     static fgMkrBeacType last_beacon = NOBEACON;
277
278     SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
279                                    lat_node->getDoubleValue(),
280                                    alt_node->getDoubleValue());
281
282     ////////////////////////////////////////////////////////////////////////
283     // Beacons.
284     ////////////////////////////////////////////////////////////////////////
285
286     // get closest marker beacon - within a 1nm cutoff
287     BeaconFilter filter;
288     FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
289
290     fgMkrBeacType beacon_type = NOBEACON;
291     bool inrange = false;
292     if ( b != NULL ) {
293         if ( b->type() == FGPositioned::OM ) {
294             beacon_type = OUTER;
295         } else if ( b->type() == FGPositioned::MM ) {
296             beacon_type = MIDDLE;
297         } else if ( b->type() == FGPositioned::IM ) {
298             beacon_type = INNER;
299         }
300         inrange = check_beacon_range( pos, b.ptr() );
301     }
302
303     outer_marker = middle_marker = inner_marker = false;
304
305     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
306     {
307         // cout << "no marker" << endl;
308         _sgr->stop( "outer-marker" );
309         _sgr->stop( "middle-marker" );
310         _sgr->stop( "inner-marker" );
311     } else {
312
313         string current_sound_name;
314
315         if ( beacon_type == OUTER ) {
316             outer_marker = true;
317             current_sound_name = "outer-marker";
318             // cout << "OUTER MARKER" << endl;
319             if ( last_beacon != OUTER ) {
320                 if ( ! _sgr->exists( current_sound_name ) ) {
321                     SGSoundSample *sound = beacon.get_outer();
322                     if ( sound ) {
323                         _sgr->add( sound, current_sound_name );
324                     }
325                 }
326             }
327             if ( audio_btn->getBoolValue() ) {
328                 if ( !_sgr->is_playing(current_sound_name) ) {
329                     _sgr->play_looped( current_sound_name );
330                 }
331             } else {
332                 _sgr->stop( current_sound_name );
333             }
334         } else if ( beacon_type == MIDDLE ) {
335             middle_marker = true;
336             current_sound_name = "middle-marker";
337             // cout << "MIDDLE MARKER" << endl;
338             if ( last_beacon != MIDDLE ) {
339                 if ( ! _sgr->exists( current_sound_name ) ) {
340                     SGSoundSample *sound = beacon.get_middle();
341                     if ( sound ) {
342                         _sgr->add( sound, current_sound_name );
343                     }
344                 }
345             }
346             if ( audio_btn->getBoolValue() ) {
347                 if ( !_sgr->is_playing(current_sound_name) ) {
348                     _sgr->play_looped( current_sound_name );
349                 }
350             } else {
351                 _sgr->stop( current_sound_name );
352             }
353         } else if ( beacon_type == INNER ) {
354             inner_marker = true;
355             current_sound_name = "inner-marker";
356             // cout << "INNER MARKER" << endl;
357             if ( last_beacon != INNER ) {
358                 if ( ! _sgr->exists( current_sound_name ) ) {
359                     SGSoundSample *sound = beacon.get_inner();
360                     if ( sound ) {
361                         _sgr->add( sound, current_sound_name );
362                     }
363                 }
364             }
365             if ( audio_btn->getBoolValue() ) {
366                 if ( !_sgr->is_playing(current_sound_name) ) {
367                     _sgr->play_looped( current_sound_name );
368                 }
369             } else {
370                 _sgr->stop( current_sound_name );
371             }
372         }
373         // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
374         SGSoundSample * mkr = _sgr->find( current_sound_name );
375         if (mkr)
376             mkr->set_volume( audio_vol->getFloatValue() );
377     }
378
379     if ( inrange ) {
380         last_beacon = beacon_type;
381     } else {
382         last_beacon = NOBEACON;
383     }
384 }