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