]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
Add various helper filters, to make use of type-ranges, and reduce the need
[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 FGPositioned::Type minType() const {
255     return FGPositioned::OM;
256   }
257
258   virtual FGPositioned::Type maxType()  const {
259     return FGPositioned::IM;
260   }
261
262 };
263
264 // Update current nav/adf radio stations based on current postition
265 void FGMarkerBeacon::search()
266 {
267     // reset search time
268     _time_before_search_sec = 1.0;
269
270     static fgMkrBeacType last_beacon = NOBEACON;
271
272     SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
273                                    lat_node->getDoubleValue(),
274                                    alt_node->getDoubleValue());
275
276     ////////////////////////////////////////////////////////////////////////
277     // Beacons.
278     ////////////////////////////////////////////////////////////////////////
279
280     // get closest marker beacon - within a 1nm cutoff
281     BeaconFilter filter;
282     FGPositionedRef b = FGPositioned::findClosest(pos, 1.0, &filter);
283      
284     fgMkrBeacType beacon_type = NOBEACON;
285     bool inrange = false;
286     if ( b != NULL ) {
287         if ( b->type() == FGPositioned::OM ) {
288             beacon_type = OUTER;
289         } else if ( b->type() == FGPositioned::MM ) {
290             beacon_type = MIDDLE;
291         } else if ( b->type() == FGPositioned::IM ) {
292             beacon_type = INNER;
293         }
294         inrange = check_beacon_range( pos, b.ptr() );
295     }
296
297     outer_marker = middle_marker = inner_marker = false;
298
299     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
300     {
301         // cout << "no marker" << endl;
302         globals->get_soundmgr()->stop( "outer-marker" );
303         globals->get_soundmgr()->stop( "middle-marker" );
304         globals->get_soundmgr()->stop( "inner-marker" );
305     } else {
306
307         string current_sound_name;
308
309         if ( beacon_type == OUTER ) {
310             outer_marker = true;
311             current_sound_name = "outer-marker";
312             // cout << "OUTER MARKER" << endl;
313             if ( last_beacon != OUTER ) {
314                 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
315                     SGSoundSample *sound = beacon.get_outer();
316                     if ( sound ) {
317                         globals->get_soundmgr()->add( sound, current_sound_name );
318                     }
319                 }
320             }
321             if ( audio_btn->getBoolValue() ) {
322                 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
323                     globals->get_soundmgr()->play_looped( current_sound_name );
324                 }
325             } else {
326                 globals->get_soundmgr()->stop( current_sound_name );
327             }
328         } else if ( beacon_type == MIDDLE ) {
329             middle_marker = true;
330             current_sound_name = "middle-marker";
331             // cout << "MIDDLE MARKER" << endl;
332             if ( last_beacon != MIDDLE ) {
333                 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
334                     SGSoundSample *sound = beacon.get_middle();
335                     if ( sound ) {
336                         globals->get_soundmgr()->add( sound, current_sound_name );
337                     }
338                 }
339             }
340             if ( audio_btn->getBoolValue() ) {
341                 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
342                     globals->get_soundmgr()->play_looped( current_sound_name );
343                 }
344             } else {
345                 globals->get_soundmgr()->stop( current_sound_name );
346             }
347         } else if ( beacon_type == INNER ) {
348             inner_marker = true;
349             current_sound_name = "inner-marker";
350             // cout << "INNER MARKER" << endl;
351             if ( last_beacon != INNER ) {
352                 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
353                     SGSoundSample *sound = beacon.get_inner();
354                     if ( sound ) {
355                         globals->get_soundmgr()->add( sound, current_sound_name );
356                     }
357                 }
358             }
359             if ( audio_btn->getBoolValue() ) {
360                 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
361                     globals->get_soundmgr()->play_looped( current_sound_name );
362                 }
363             } else {
364                 globals->get_soundmgr()->stop( current_sound_name );
365             }
366         }
367         // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
368         SGSoundSample * mkr = globals->get_soundmgr()->find( current_sound_name );
369         if (mkr)
370             mkr->set_volume( audio_vol->getDoubleValue() );
371     }
372
373     if ( inrange ) {
374         last_beacon = beacon_type;
375     } else {
376         last_beacon = NOBEACON;
377     }
378 }