]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
Fix rpmlint/Linux packager complaints
[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 #include <Sound/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     blink.stamp();
125
126     outer_marker = middle_marker = inner_marker = false;
127 }
128
129
130 void
131 FGMarkerBeacon::bind ()
132 {
133     string branch;
134     branch = "/instrumentation/" + name;
135
136     fgTie((branch + "/inner").c_str(), this,
137           &FGMarkerBeacon::get_inner_blink);
138
139     fgTie((branch + "/middle").c_str(), this,
140           &FGMarkerBeacon::get_middle_blink);
141
142     fgTie((branch + "/outer").c_str(), this,
143           &FGMarkerBeacon::get_outer_blink);
144 }
145
146
147 void
148 FGMarkerBeacon::unbind ()
149 {
150     string branch;
151     branch = "/instrumentation/" + name;
152
153     fgUntie((branch + "/inner").c_str());
154     fgUntie((branch + "/middle").c_str());
155     fgUntie((branch + "/outer").c_str());
156 }
157
158
159 // Update the various nav values based on position and valid tuned in navs
160 void
161 FGMarkerBeacon::update(double dt)
162 {
163     need_update = false;
164
165     // On timeout, scan again, this needs to run every iteration no
166     // matter what the power or serviceable state.  If power is turned
167     // off or the unit becomes unserviceable while a beacon sound is
168     // playing, the search() routine still needs to be called so the
169     // sound effect can be properly disabled.
170
171     _time_before_search_sec -= dt;
172     if ( _time_before_search_sec < 0 ) {
173         search();
174     }
175
176     if ( has_power() && serviceable->getBoolValue()
177             && sound_working->getBoolValue()) {
178
179         // marker beacon blinking
180         bool light_on = ( outer_blink || middle_blink || inner_blink );
181         SGTimeStamp current = SGTimeStamp::now();
182
183         if ( light_on && blink + SGTimeStamp::fromUSec(400000) < current ) {
184             light_on = false;
185             blink = current;
186         } else if ( !light_on && blink + SGTimeStamp::fromUSec(100000) < current ) {
187             light_on = true;
188             blink = current;
189         }
190
191         if ( outer_marker ) {
192             outer_blink = light_on;
193         } else {
194             outer_blink = false;
195         }
196
197         if ( middle_marker ) {
198             middle_blink = light_on;
199         } else {
200             middle_blink = false;
201         }
202
203         if ( inner_marker ) {
204             inner_blink = light_on;
205         } else {
206             inner_blink = false;
207         }
208
209         // cout << outer_blink << " " << middle_blink << " "
210         //      << inner_blink << endl;
211     } else {
212         inner_blink = middle_blink = outer_blink = false;
213     }
214 }
215
216
217 static bool check_beacon_range( const SGGeod& pos,
218                                 FGPositioned *b )
219 {
220     double d = distSqr(b->cart(), SGVec3d::fromGeod(pos));
221     // cout << "  distance = " << d << " ("
222     //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
223     //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
224     //      << ")" << endl;
225
226     //std::cout << "  range = " << sqrt(d) << std::endl;
227
228     // cout << "elev = " << elev * SG_METER_TO_FEET
229     //      << " current->get_elev() = " << current->get_elev() << endl;
230     double elev_ft = pos.getElevationFt();
231     double delev = elev_ft - b->elevation();
232
233     // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
234     // whichever is smaller.  The intersection point is 1538 ...
235     double maxrange2;   // feet^2
236     if ( delev < 1538.0 ) {
237         maxrange2 = 2.4 * 2.4 * delev * delev;
238     } else if ( delev < 4000.0 ) {
239         maxrange2 = 4000 * 4000 - delev * delev;
240     } else {
241         maxrange2 = 0.0;
242     }
243     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
244     //std::cout << "delev = " << delev << " maxrange = " << sqrt(maxrange2) << std::endl;
245
246     // match up to twice the published range so we can model
247     // reduced signal strength
248     if ( d < maxrange2 ) {
249         return true;
250     } else {
251         return false;
252     }
253 }
254
255 class BeaconFilter : public FGPositioned::Filter
256 {
257 public:
258   virtual FGPositioned::Type minType() const {
259     return FGPositioned::OM;
260   }
261
262   virtual FGPositioned::Type maxType()  const {
263     return FGPositioned::IM;
264   }
265
266 };
267
268 // Update current nav/adf radio stations based on current postition
269 void FGMarkerBeacon::search()
270 {
271     // reset search time
272     _time_before_search_sec = 1.0;
273
274     static fgMkrBeacType last_beacon = NOBEACON;
275
276     SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
277                                    lat_node->getDoubleValue(),
278                                    alt_node->getDoubleValue());
279
280     ////////////////////////////////////////////////////////////////////////
281     // Beacons.
282     ////////////////////////////////////////////////////////////////////////
283
284     // get closest marker beacon - within a 1nm cutoff
285     BeaconFilter filter;
286     FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
287
288     fgMkrBeacType beacon_type = NOBEACON;
289     bool inrange = false;
290     if ( b != NULL ) {
291         if ( b->type() == FGPositioned::OM ) {
292             beacon_type = OUTER;
293         } else if ( b->type() == FGPositioned::MM ) {
294             beacon_type = MIDDLE;
295         } else if ( b->type() == FGPositioned::IM ) {
296             beacon_type = INNER;
297         }
298         inrange = check_beacon_range( pos, b.ptr() );
299     }
300
301     outer_marker = middle_marker = inner_marker = false;
302
303     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
304     {
305         // cout << "no marker" << endl;
306         _sgr->stop( "outer-marker" );
307         _sgr->stop( "middle-marker" );
308         _sgr->stop( "inner-marker" );
309     } else {
310
311         string current_sound_name;
312
313         if ( beacon_type == OUTER ) {
314             outer_marker = true;
315             current_sound_name = "outer-marker";
316             // cout << "OUTER MARKER" << endl;
317             if ( last_beacon != OUTER ) {
318                 if ( ! _sgr->exists( current_sound_name ) ) {
319                     SGSoundSample *sound = FGBeacon::instance()->get_outer();
320                     if ( sound ) {
321                         _sgr->add( sound, current_sound_name );
322                     }
323                 }
324             }
325             if ( audio_btn->getBoolValue() ) {
326                 if ( !_sgr->is_playing(current_sound_name) ) {
327                     _sgr->play_looped( current_sound_name );
328                 }
329             } else {
330                 _sgr->stop( current_sound_name );
331             }
332         } else if ( beacon_type == MIDDLE ) {
333             middle_marker = true;
334             current_sound_name = "middle-marker";
335             // cout << "MIDDLE MARKER" << endl;
336             if ( last_beacon != MIDDLE ) {
337                 if ( ! _sgr->exists( current_sound_name ) ) {
338                     SGSoundSample *sound = FGBeacon::instance()->get_middle();
339                     if ( sound ) {
340                         _sgr->add( sound, current_sound_name );
341                     }
342                 }
343             }
344             if ( audio_btn->getBoolValue() ) {
345                 if ( !_sgr->is_playing(current_sound_name) ) {
346                     _sgr->play_looped( current_sound_name );
347                 }
348             } else {
349                 _sgr->stop( current_sound_name );
350             }
351         } else if ( beacon_type == INNER ) {
352             inner_marker = true;
353             current_sound_name = "inner-marker";
354             // cout << "INNER MARKER" << endl;
355             if ( last_beacon != INNER ) {
356                 if ( ! _sgr->exists( current_sound_name ) ) {
357                     SGSoundSample *sound = FGBeacon::instance()->get_inner();
358                     if ( sound ) {
359                         _sgr->add( sound, current_sound_name );
360                     }
361                 }
362             }
363             if ( audio_btn->getBoolValue() ) {
364                 if ( !_sgr->is_playing(current_sound_name) ) {
365                     _sgr->play_looped( current_sound_name );
366                 }
367             } else {
368                 _sgr->stop( current_sound_name );
369             }
370         }
371         // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
372         SGSoundSample * mkr = _sgr->find( current_sound_name );
373         if (mkr)
374             mkr->set_volume( audio_vol->getFloatValue() );
375     }
376
377     if ( inrange ) {
378         last_beacon = beacon_type;
379     } else {
380         last_beacon = NOBEACON;
381     }
382 }