]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
Initialize the timer countdown value since this could conceivably start as
[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., 675 Mass Ave, Cambridge, MA 02139, 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 SG_USING_STD(string);
40
41
42 // Constructor
43 FGMarkerBeacon::FGMarkerBeacon(SGPropertyNode *node) :
44     need_update(true),
45     outer_blink(false),
46     middle_blink(false),
47     inner_blink(false),
48     name("marker-beacon"),
49     num(0),
50     _time_before_search_sec(0.0)
51 {
52     SGPath path( globals->get_fg_root() );
53     SGPath term = path;
54     term.append( "Navaids/range.term" );
55     SGPath low = path;
56     low.append( "Navaids/range.low" );
57     SGPath high = path;
58     high.append( "Navaids/range.high" );
59
60     term_tbl = new SGInterpTable( term.str() );
61     low_tbl = new SGInterpTable( low.str() );
62     high_tbl = new SGInterpTable( high.str() );
63
64     int i;
65     for ( i = 0; i < node->nChildren(); ++i ) {
66         SGPropertyNode *child = node->getChild(i);
67         string cname = child->getName();
68         string cval = child->getStringValue();
69         if ( cname == "name" ) {
70             name = cval;
71         } else if ( cname == "number" ) {
72             num = child->getIntValue();
73         } else {
74             SG_LOG( SG_INSTR, SG_WARN, 
75                     "Error in marker beacon config logic" );
76             if ( name.length() ) {
77                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
78             }
79         }
80     }
81 }
82
83
84 // Destructor
85 FGMarkerBeacon::~FGMarkerBeacon() 
86 {
87     delete term_tbl;
88     delete low_tbl;
89     delete high_tbl;
90 }
91
92
93 void
94 FGMarkerBeacon::init ()
95 {
96     string branch;
97     branch = "/instrumentation/" + name;
98
99     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
100     // Inputs
101     lon_node = fgGetNode("/position/longitude-deg", true);
102     lat_node = fgGetNode("/position/latitude-deg", true);
103     alt_node = fgGetNode("/position/altitude-ft", true);
104     bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
105     power_btn = node->getChild("power-btn", 0, true);
106     audio_btn = node->getChild("audio-btn", 0, true);
107     serviceable = node->getChild("serviceable", 0, true);
108
109     power_btn->setBoolValue( true );
110     audio_btn->setBoolValue( true );
111     serviceable->setBoolValue( true );
112
113     morse.init();
114     beacon.init();
115     blink.stamp();
116 }
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     need_update = false;
153
154     if ( has_power() && serviceable->getBoolValue() ) {
155
156         // On timeout, scan again
157         _time_before_search_sec -= dt;
158         if ( _time_before_search_sec < 0 ) {
159             search();
160         }
161         // marker beacon blinking
162         bool light_on = ( outer_blink || middle_blink || inner_blink );
163         SGTimeStamp current;
164         current.stamp();
165
166         if ( light_on && (current - blink > 400000) ) {
167             light_on = false;
168             blink.stamp();
169         } else if ( !light_on && (current - blink > 100000) ) {
170             light_on = true;
171             blink.stamp();
172         }
173
174         if ( outer_marker ) {
175             outer_blink = light_on;
176         } else {
177             outer_blink = false;
178         }
179
180         if ( middle_marker ) {
181             middle_blink = light_on;
182         } else {
183             middle_blink = false;
184         }
185
186         if ( inner_marker ) {
187             inner_blink = light_on;
188         } else {
189             inner_blink = false;
190         }
191
192         // cout << outer_blink << " " << middle_blink << " "
193         //      << inner_blink << endl;
194     } else {
195         inner_blink = middle_blink = outer_blink = false;
196     }
197 }
198
199
200 static bool check_beacon_range( double lon_rad, double lat_rad, double elev_m,
201                                 FGNavRecord *b )
202 {
203     Point3D aircraft = sgGeodToCart( Point3D(lon_rad, lat_rad, elev_m) );
204     Point3D station = Point3D( b->get_x(), b->get_y(), b->get_z() );
205     // cout << "    aircraft = " << aircraft << " station = " << station 
206     //      << endl;
207
208     double d = aircraft.distance3Dsquared( station ); // meters^2
209     // cout << "  distance = " << d << " (" 
210     //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
211     //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
212     //      << ")" << endl;
213     
214     // cout << "  range = " << sqrt(d) << endl;
215
216     // cout << "elev = " << elev * SG_METER_TO_FEET
217     //      << " current->get_elev() = " << current->get_elev() << endl;
218     double elev_ft = elev_m * SG_METER_TO_FEET;
219     double delev = elev_ft - b->get_elev_ft();
220
221     // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
222     // whichever is smaller.  The intersection point is 1538 ...
223     double maxrange2;   // feet^2
224     if ( delev < 1538.0 ) {
225         maxrange2 = 2.4 * 2.4 * delev * delev;
226     } else if ( delev < 4000.0 ) {
227         maxrange2 = 4000 * 4000 - delev * delev;
228     } else {
229         maxrange2 = 0.0;
230     }
231     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
232     // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
233
234     // match up to twice the published range so we can model
235     // reduced signal strength
236     if ( d < maxrange2 ) {
237         return true;
238     } else {
239         return false;
240     }
241 }
242
243 // Update current nav/adf radio stations based on current postition
244 void FGMarkerBeacon::search() 
245 {
246
247     // reset search time
248     _time_before_search_sec = 1.0;
249
250     static fgMkrBeacType last_beacon = NOBEACON;
251
252     double lon_rad = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
253     double lat_rad = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
254     double elev_m = alt_node->getDoubleValue() * SG_FEET_TO_METER;
255
256     ////////////////////////////////////////////////////////////////////////
257     // Beacons.
258     ////////////////////////////////////////////////////////////////////////
259
260     // get closest marker beacon
261     FGNavRecord *b
262         = globals->get_mkrlist()->findClosest( lon_rad, lat_rad, elev_m );
263
264     // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
265
266     fgMkrBeacType beacon_type = NOBEACON;
267     bool inrange = false;
268     if ( b != NULL ) {
269         if ( b->get_type() == 7 ) {
270             beacon_type = OUTER;
271         } else if ( b->get_type() == 8 ) {
272             beacon_type = MIDDLE;
273         } else if ( b->get_type() == 9 ) {
274             beacon_type = INNER;
275         }
276         inrange = check_beacon_range( lon_rad, lat_rad, elev_m, b );
277         // cout << "  inrange = " << inrange << endl;
278     }
279
280     outer_marker = middle_marker = inner_marker = false;
281
282     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
283     {
284         // cout << "no marker" << endl;
285         globals->get_soundmgr()->stop( "outer-marker" );
286         globals->get_soundmgr()->stop( "middle-marker" );
287         globals->get_soundmgr()->stop( "inner-marker" );
288     } else if ( beacon_type == OUTER ) {
289         outer_marker = true;
290         // cout << "OUTER MARKER" << endl;
291         if ( last_beacon != OUTER ) {
292             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
293                 SGSoundSample *sound = beacon.get_outer();
294                 sound->set_volume( 0.3 );
295                 globals->get_soundmgr()->add( sound, "outer-marker" );
296             }
297         }
298         if ( audio_btn->getBoolValue() ) {
299             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
300                 globals->get_soundmgr()->play_looped( "outer-marker" );
301             }
302         } else {
303             globals->get_soundmgr()->stop( "outer-marker" );
304         }
305     } else if ( beacon_type == MIDDLE ) {
306         middle_marker = true;
307         // cout << "MIDDLE MARKER" << endl;
308         if ( last_beacon != MIDDLE ) {
309             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
310                 SGSoundSample *sound = beacon.get_middle();
311                 sound->set_volume( 0.3 );
312                 globals->get_soundmgr()->add( sound, "middle-marker" );
313             }
314         }
315         if ( audio_btn->getBoolValue() ) {
316             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
317                 globals->get_soundmgr()->play_looped( "middle-marker" );
318             }
319         } else {
320             globals->get_soundmgr()->stop( "middle-marker" );
321         }
322     } else if ( beacon_type == INNER ) {
323         inner_marker = true;
324         // cout << "INNER MARKER" << endl;
325         if ( last_beacon != INNER ) {
326             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
327                 SGSoundSample *sound = beacon.get_inner();
328                 sound->set_volume( 0.3 );
329                 globals->get_soundmgr()->add( sound, "inner-marker" );
330             }
331         }
332         if ( audio_btn->getBoolValue() ) {
333             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
334                 globals->get_soundmgr()->play_looped( "inner-marker" );
335             }
336         } else {
337             globals->get_soundmgr()->stop( "inner-marker" );
338         }
339     }
340
341     if ( inrange ) {
342         last_beacon = beacon_type;
343     } else {
344         last_beacon = NOBEACON;
345     }
346 }