]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
Fix bug 191, uninitialised HUD color.
[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 <Navaids/navlist.hxx>
35
36 #include "marker_beacon.hxx"
37
38 #include <string>
39 using std::string;
40
41
42 // Constructor
43 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
44     audio_vol(NULL),
45     need_update(true),
46     outer_blink(false),
47     middle_blink(false),
48     inner_blink(false),
49     name("marker-beacon"),
50     num(0),
51     _time_before_search_sec(0.0),
52     _sgr(NULL)
53 {
54     SGPath path( globals->get_fg_root() );
55     SGPath term = path;
56     term.append( "Navaids/range.term" );
57     SGPath low = path;
58     low.append( "Navaids/range.low" );
59     SGPath high = path;
60     high.append( "Navaids/range.high" );
61
62     term_tbl = new SGInterpTable( term.str() );
63     low_tbl = new SGInterpTable( low.str() );
64     high_tbl = new SGInterpTable( high.str() );
65
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" ) {
71             name = cval;
72         } else if ( cname == "number" ) {
73             num = child->getIntValue();
74         } else {
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 );
79             }
80         }
81     }
82 }
83
84
85 // Destructor
86 FGMarkerBeacon::~FGMarkerBeacon()
87 {
88     delete term_tbl;
89     delete low_tbl;
90     delete high_tbl;
91 }
92
93
94 void
95 FGMarkerBeacon::init ()
96 {
97     string branch;
98     branch = "/instrumentation/" + name;
99
100     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
101     // Inputs
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);
111
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 );
118
119     SGSoundMgr *smgr = globals->get_soundmgr();
120     _sgr = smgr->find("avionics", true);
121     _sgr->tie_to_listener();
122
123     morse.init();
124     beacon.init();
125     blink.stamp();
126
127     outer_marker = middle_marker = inner_marker = false;
128 }
129
130
131 void
132 FGMarkerBeacon::bind ()
133 {
134     string branch;
135     branch = "/instrumentation/" + name;
136
137     fgTie((branch + "/inner").c_str(), this,
138           &FGMarkerBeacon::get_inner_blink);
139
140     fgTie((branch + "/middle").c_str(), this,
141           &FGMarkerBeacon::get_middle_blink);
142
143     fgTie((branch + "/outer").c_str(), this,
144           &FGMarkerBeacon::get_outer_blink);
145 }
146
147
148 void
149 FGMarkerBeacon::unbind ()
150 {
151     string branch;
152     branch = "/instrumentation/" + name;
153
154     fgUntie((branch + "/inner").c_str());
155     fgUntie((branch + "/middle").c_str());
156     fgUntie((branch + "/outer").c_str());
157 }
158
159
160 // Update the various nav values based on position and valid tuned in navs
161 void
162 FGMarkerBeacon::update(double dt)
163 {
164     need_update = false;
165
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.
171
172     _time_before_search_sec -= dt;
173     if ( _time_before_search_sec < 0 ) {
174         search();
175     }
176
177     if ( has_power() && serviceable->getBoolValue()
178             && sound_working->getBoolValue()) {
179
180         // marker beacon blinking
181         bool light_on = ( outer_blink || middle_blink || inner_blink );
182         SGTimeStamp current = SGTimeStamp::now();
183
184         if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
185             light_on = false;
186             blink = current;
187         } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
188             light_on = true;
189             blink = current;
190         }
191
192         if ( outer_marker ) {
193             outer_blink = light_on;
194         } else {
195             outer_blink = false;
196         }
197
198         if ( middle_marker ) {
199             middle_blink = light_on;
200         } else {
201             middle_blink = false;
202         }
203
204         if ( inner_marker ) {
205             inner_blink = light_on;
206         } else {
207             inner_blink = false;
208         }
209
210         // cout << outer_blink << " " << middle_blink << " "
211         //      << inner_blink << endl;
212     } else {
213         inner_blink = middle_blink = outer_blink = false;
214     }
215 }
216
217
218 static bool check_beacon_range( const SGGeod& pos,
219                                 FGPositioned *b )
220 {
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
225     //      << ")" << endl;
226
227     //std::cout << "  range = " << sqrt(d) << std::endl;
228
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();
233
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;
241     } else {
242         maxrange2 = 0.0;
243     }
244     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
245     //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
246
247     // match up to twice the published range so we can model
248     // reduced signal strength
249     if ( d < maxrange2 ) {
250         return true;
251     } else {
252         return false;
253     }
254 }
255
256 class BeaconFilter : public FGPositioned::Filter
257 {
258 public:
259   virtual FGPositioned::Type minType() const {
260     return FGPositioned::OM;
261   }
262
263   virtual FGPositioned::Type maxType()  const {
264     return FGPositioned::IM;
265   }
266
267 };
268
269 // Update current nav/adf radio stations based on current postition
270 void FGMarkerBeacon::search()
271 {
272     // reset search time
273     _time_before_search_sec = 1.0;
274
275     static fgMkrBeacType last_beacon = NOBEACON;
276
277     SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
278                                    lat_node->getDoubleValue(),
279                                    alt_node->getDoubleValue());
280
281     ////////////////////////////////////////////////////////////////////////
282     // Beacons.
283     ////////////////////////////////////////////////////////////////////////
284
285     // get closest marker beacon - within a 1nm cutoff
286     BeaconFilter filter;
287     FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
288
289     fgMkrBeacType beacon_type = NOBEACON;
290     bool inrange = false;
291     if ( b != NULL ) {
292         if ( b->type() == FGPositioned::OM ) {
293             beacon_type = OUTER;
294         } else if ( b->type() == FGPositioned::MM ) {
295             beacon_type = MIDDLE;
296         } else if ( b->type() == FGPositioned::IM ) {
297             beacon_type = INNER;
298         }
299         inrange = check_beacon_range( pos, b.ptr() );
300     }
301
302     outer_marker = middle_marker = inner_marker = false;
303
304     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
305     {
306         // cout << "no marker" << endl;
307         _sgr->stop( "outer-marker" );
308         _sgr->stop( "middle-marker" );
309         _sgr->stop( "inner-marker" );
310     } else {
311
312         string current_sound_name;
313
314         if ( beacon_type == OUTER ) {
315             outer_marker = true;
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();
321                     if ( sound ) {
322                         _sgr->add( sound, current_sound_name );
323                     }
324                 }
325             }
326             if ( audio_btn->getBoolValue() ) {
327                 if ( !_sgr->is_playing(current_sound_name) ) {
328                     _sgr->play_looped( current_sound_name );
329                 }
330             } else {
331                 _sgr->stop( current_sound_name );
332             }
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();
340                     if ( sound ) {
341                         _sgr->add( sound, current_sound_name );
342                     }
343                 }
344             }
345             if ( audio_btn->getBoolValue() ) {
346                 if ( !_sgr->is_playing(current_sound_name) ) {
347                     _sgr->play_looped( current_sound_name );
348                 }
349             } else {
350                 _sgr->stop( current_sound_name );
351             }
352         } else if ( beacon_type == INNER ) {
353             inner_marker = true;
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();
359                     if ( sound ) {
360                         _sgr->add( sound, current_sound_name );
361                     }
362                 }
363             }
364             if ( audio_btn->getBoolValue() ) {
365                 if ( !_sgr->is_playing(current_sound_name) ) {
366                     _sgr->play_looped( current_sound_name );
367                 }
368             } else {
369                 _sgr->stop( current_sound_name );
370             }
371         }
372         // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
373         SGSoundSample * mkr = _sgr->find( current_sound_name );
374         if (mkr)
375             mkr->set_volume( audio_vol->getFloatValue() );
376     }
377
378     if ( inrange ) {
379         last_beacon = beacon_type;
380     } else {
381         last_beacon = NOBEACON;
382     }
383 }