]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
James Turner:
[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
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/navlist.hxx>
35
36 #include "marker_beacon.hxx"
37
38 #include <string>
39 using std::string;
40
41
42 // Constructor
43 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
44     audio_vol(NULL),
45     need_update(true),
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 {
53     SGPath path( globals->get_fg_root() );
54     SGPath term = path;
55     term.append( "Navaids/range.term" );
56     SGPath low = path;
57     low.append( "Navaids/range.low" );
58     SGPath high = path;
59     high.append( "Navaids/range.high" );
60
61     term_tbl = new SGInterpTable( term.str() );
62     low_tbl = new SGInterpTable( low.str() );
63     high_tbl = new SGInterpTable( high.str() );
64
65     int i;
66     for ( 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     power_btn->setBoolValue( true );
113     audio_btn->setBoolValue( true );
114     serviceable->setBoolValue( true );
115
116     morse.init();
117     beacon.init();
118     blink.stamp();
119
120     outer_marker = middle_marker = inner_marker = false;
121 }
122
123
124 void
125 FGMarkerBeacon::bind ()
126 {
127     string branch;
128     branch = "/instrumentation/" + name;
129
130     fgTie((branch + "/inner").c_str(), this,
131           &FGMarkerBeacon::get_inner_blink);
132
133     fgTie((branch + "/middle").c_str(), this,
134           &FGMarkerBeacon::get_middle_blink);
135
136     fgTie((branch + "/outer").c_str(), this,
137           &FGMarkerBeacon::get_outer_blink);
138 }
139
140
141 void
142 FGMarkerBeacon::unbind ()
143 {
144     string branch;
145     branch = "/instrumentation/" + name;
146
147     fgUntie((branch + "/inner").c_str());
148     fgUntie((branch + "/middle").c_str());
149     fgUntie((branch + "/outer").c_str());
150 }
151
152
153 // Update the various nav values based on position and valid tuned in navs
154 void
155 FGMarkerBeacon::update(double dt)
156 {
157     need_update = false;
158
159     if ( has_power() && serviceable->getBoolValue()
160             && !sound_pause->getBoolValue()) {
161
162         // On timeout, scan again
163         _time_before_search_sec -= dt;
164         if ( _time_before_search_sec < 0 ) {
165             search();
166         }
167         // marker beacon blinking
168         bool light_on = ( outer_blink || middle_blink || inner_blink );
169         SGTimeStamp current;
170         current.stamp();
171
172         if ( light_on && (current - blink > 400000) ) {
173             light_on = false;
174             blink.stamp();
175         } else if ( !light_on && (current - blink > 100000) ) {
176             light_on = true;
177             blink.stamp();
178         }
179
180         if ( outer_marker ) {
181             outer_blink = light_on;
182         } else {
183             outer_blink = false;
184         }
185
186         if ( middle_marker ) {
187             middle_blink = light_on;
188         } else {
189             middle_blink = false;
190         }
191
192         if ( inner_marker ) {
193             inner_blink = light_on;
194         } else {
195             inner_blink = false;
196         }
197
198         // cout << outer_blink << " " << middle_blink << " "
199         //      << inner_blink << endl;
200     } else {
201         inner_blink = middle_blink = outer_blink = false;
202     }
203 }
204
205
206 static bool check_beacon_range( const SGGeod& pos,
207                                 FGNavRecord *b )
208 {
209     double d = distSqr(b->get_cart(), SGVec3d::fromGeod(pos));
210     // cout << "  distance = " << d << " ("
211     //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
212     //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
213     //      << ")" << endl;
214
215     // cout << "  range = " << sqrt(d) << endl;
216
217     // cout << "elev = " << elev * SG_METER_TO_FEET
218     //      << " current->get_elev() = " << current->get_elev() << endl;
219     double elev_ft = pos.getElevationFt();
220     double delev = elev_ft - b->get_elev_ft();
221
222     // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
223     // whichever is smaller.  The intersection point is 1538 ...
224     double maxrange2;   // feet^2
225     if ( delev < 1538.0 ) {
226         maxrange2 = 2.4 * 2.4 * delev * delev;
227     } else if ( delev < 4000.0 ) {
228         maxrange2 = 4000 * 4000 - delev * delev;
229     } else {
230         maxrange2 = 0.0;
231     }
232     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
233     // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
234
235     // match up to twice the published range so we can model
236     // reduced signal strength
237     if ( d < maxrange2 ) {
238         return true;
239     } else {
240         return false;
241     }
242 }
243
244 // Update current nav/adf radio stations based on current postition
245 void FGMarkerBeacon::search()
246 {
247
248     // reset search time
249     _time_before_search_sec = 1.0;
250
251     static fgMkrBeacType last_beacon = NOBEACON;
252
253     SGGeod pos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
254                                    lat_node->getDoubleValue(),
255                                    alt_node->getDoubleValue());
256
257     ////////////////////////////////////////////////////////////////////////
258     // Beacons.
259     ////////////////////////////////////////////////////////////////////////
260
261     // get closest marker beacon
262     FGNavRecord *b
263       = globals->get_mkrlist()->findClosest( pos.getLongitudeRad(),
264                                              pos.getLatitudeRad(),
265                                              pos.getElevationM() );
266
267     // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
268
269     fgMkrBeacType beacon_type = NOBEACON;
270     bool inrange = false;
271     if ( b != NULL ) {
272         if ( b->type() == FGPositioned::OM ) {
273             beacon_type = OUTER;
274         } else if ( b->type() == FGPositioned::MM ) {
275             beacon_type = MIDDLE;
276         } else if ( b->type() == FGPositioned::IM ) {
277             beacon_type = INNER;
278         }
279         inrange = check_beacon_range( pos, b );
280         // cout << "  inrange = " << inrange << endl;
281     }
282
283     outer_marker = middle_marker = inner_marker = false;
284
285     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
286     {
287         // cout << "no marker" << endl;
288         globals->get_soundmgr()->stop( "outer-marker" );
289         globals->get_soundmgr()->stop( "middle-marker" );
290         globals->get_soundmgr()->stop( "inner-marker" );
291     } else {
292
293         string current_sound_name;
294
295         if ( beacon_type == OUTER ) {
296             outer_marker = true;
297             current_sound_name = "outer-marker";
298             // cout << "OUTER MARKER" << endl;
299             if ( last_beacon != OUTER ) {
300                 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
301                     SGSoundSample *sound = beacon.get_outer();
302                     if ( sound ) {
303                         globals->get_soundmgr()->add( sound, current_sound_name );
304                     }
305                 }
306             }
307             if ( audio_btn->getBoolValue() ) {
308                 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
309                     globals->get_soundmgr()->play_looped( current_sound_name );
310                 }
311             } else {
312                 globals->get_soundmgr()->stop( current_sound_name );
313             }
314         } else if ( beacon_type == MIDDLE ) {
315             middle_marker = true;
316             current_sound_name = "middle-marker";
317             // cout << "MIDDLE MARKER" << endl;
318             if ( last_beacon != MIDDLE ) {
319                 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
320                     SGSoundSample *sound = beacon.get_middle();
321                     if ( sound ) {
322                         globals->get_soundmgr()->add( sound, current_sound_name );
323                     }
324                 }
325             }
326             if ( audio_btn->getBoolValue() ) {
327                 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
328                     globals->get_soundmgr()->play_looped( current_sound_name );
329                 }
330             } else {
331                 globals->get_soundmgr()->stop( current_sound_name );
332             }
333         } else if ( beacon_type == INNER ) {
334             inner_marker = true;
335             current_sound_name = "inner-marker";
336             // cout << "INNER MARKER" << endl;
337             if ( last_beacon != INNER ) {
338                 if ( ! globals->get_soundmgr()->exists( current_sound_name ) ) {
339                     SGSoundSample *sound = beacon.get_inner();
340                     if ( sound ) {
341                         globals->get_soundmgr()->add( sound, current_sound_name );
342                     }
343                 }
344             }
345             if ( audio_btn->getBoolValue() ) {
346                 if ( !globals->get_soundmgr()->is_playing(current_sound_name) ) {
347                     globals->get_soundmgr()->play_looped( current_sound_name );
348                 }
349             } else {
350                 globals->get_soundmgr()->stop( current_sound_name );
351             }
352         }
353         // cout << "VOLUME " << audio_vol->getDoubleValue() << endl;
354         SGSoundSample * mkr = globals->get_soundmgr()->find( current_sound_name );
355         if (mkr)
356             mkr->set_volume( audio_vol->getDoubleValue() );
357     }
358
359     if ( inrange ) {
360         last_beacon = beacon_type;
361     } else {
362         last_beacon = NOBEACON;
363     }
364 }