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