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