]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
Don't restore initial screen geometry because there is nothing in fg_os* to resize...
[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 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     sound_pause = fgGetNode("/sim/sound/pause", false);
102     lon_node = fgGetNode("/position/longitude-deg", true);
103     lat_node = fgGetNode("/position/latitude-deg", true);
104     alt_node = fgGetNode("/position/altitude-ft", true);
105     bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
106     power_btn = node->getChild("power-btn", 0, true);
107     audio_btn = node->getChild("audio-btn", 0, true);
108     serviceable = node->getChild("serviceable", 0, true);
109
110     power_btn->setBoolValue( true );
111     audio_btn->setBoolValue( true );
112     serviceable->setBoolValue( true );
113
114     morse.init();
115     beacon.init();
116     blink.stamp();
117
118     outer_marker = middle_marker = inner_marker = false;
119 }
120
121
122 void
123 FGMarkerBeacon::bind ()
124 {
125     string branch;
126     branch = "/instrumentation/" + name;
127
128     fgTie((branch + "/inner").c_str(), this,
129           &FGMarkerBeacon::get_inner_blink);
130
131     fgTie((branch + "/middle").c_str(), this,
132           &FGMarkerBeacon::get_middle_blink);
133
134     fgTie((branch + "/outer").c_str(), this,
135           &FGMarkerBeacon::get_outer_blink);
136 }
137
138
139 void
140 FGMarkerBeacon::unbind ()
141 {
142     string branch;
143     branch = "/instrumentation/" + name;
144
145     fgUntie((branch + "/inner").c_str());
146     fgUntie((branch + "/middle").c_str());
147     fgUntie((branch + "/outer").c_str());
148 }
149
150
151 // Update the various nav values based on position and valid tuned in navs
152 void 
153 FGMarkerBeacon::update(double dt) 
154 {
155     need_update = false;
156
157     if ( has_power() && serviceable->getBoolValue()
158             && !sound_pause->getBoolValue()) {
159
160         // On timeout, scan again
161         _time_before_search_sec -= dt;
162         if ( _time_before_search_sec < 0 ) {
163             search();
164         }
165         // marker beacon blinking
166         bool light_on = ( outer_blink || middle_blink || inner_blink );
167         SGTimeStamp current;
168         current.stamp();
169
170         if ( light_on && (current - blink > 400000) ) {
171             light_on = false;
172             blink.stamp();
173         } else if ( !light_on && (current - blink > 100000) ) {
174             light_on = true;
175             blink.stamp();
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( double lon_rad, double lat_rad, double elev_m,
205                                 FGNavRecord *b )
206 {
207     Point3D aircraft = sgGeodToCart( Point3D(lon_rad, lat_rad, elev_m) );
208     Point3D station = Point3D( b->get_x(), b->get_y(), b->get_z() );
209     // cout << "    aircraft = " << aircraft << " station = " << station 
210     //      << endl;
211
212     double d = aircraft.distance3Dsquared( station ); // meters^2
213     // cout << "  distance = " << d << " (" 
214     //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
215     //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
216     //      << ")" << endl;
217     
218     // cout << "  range = " << sqrt(d) << endl;
219
220     // cout << "elev = " << elev * SG_METER_TO_FEET
221     //      << " current->get_elev() = " << current->get_elev() << endl;
222     double elev_ft = elev_m * SG_METER_TO_FEET;
223     double delev = elev_ft - b->get_elev_ft();
224
225     // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
226     // whichever is smaller.  The intersection point is 1538 ...
227     double maxrange2;   // feet^2
228     if ( delev < 1538.0 ) {
229         maxrange2 = 2.4 * 2.4 * delev * delev;
230     } else if ( delev < 4000.0 ) {
231         maxrange2 = 4000 * 4000 - delev * delev;
232     } else {
233         maxrange2 = 0.0;
234     }
235     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
236     // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
237
238     // match up to twice the published range so we can model
239     // reduced signal strength
240     if ( d < maxrange2 ) {
241         return true;
242     } else {
243         return false;
244     }
245 }
246
247 // Update current nav/adf radio stations based on current postition
248 void FGMarkerBeacon::search() 
249 {
250
251     // reset search time
252     _time_before_search_sec = 1.0;
253
254     static fgMkrBeacType last_beacon = NOBEACON;
255
256     double lon_rad = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
257     double lat_rad = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
258     double elev_m = alt_node->getDoubleValue() * SG_FEET_TO_METER;
259
260     ////////////////////////////////////////////////////////////////////////
261     // Beacons.
262     ////////////////////////////////////////////////////////////////////////
263
264     // get closest marker beacon
265     FGNavRecord *b
266         = globals->get_mkrlist()->findClosest( lon_rad, lat_rad, elev_m );
267
268     // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
269
270     fgMkrBeacType beacon_type = NOBEACON;
271     bool inrange = false;
272     if ( b != NULL ) {
273         if ( b->get_type() == 7 ) {
274             beacon_type = OUTER;
275         } else if ( b->get_type() == 8 ) {
276             beacon_type = MIDDLE;
277         } else if ( b->get_type() == 9 ) {
278             beacon_type = INNER;
279         }
280         inrange = check_beacon_range( lon_rad, lat_rad, elev_m, b );
281         // cout << "  inrange = " << inrange << endl;
282     }
283
284     outer_marker = middle_marker = inner_marker = false;
285
286     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
287     {
288         // cout << "no marker" << endl;
289         globals->get_soundmgr()->stop( "outer-marker" );
290         globals->get_soundmgr()->stop( "middle-marker" );
291         globals->get_soundmgr()->stop( "inner-marker" );
292     } else if ( beacon_type == OUTER ) {
293         outer_marker = true;
294         // cout << "OUTER MARKER" << endl;
295         if ( last_beacon != OUTER ) {
296             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
297                 SGSoundSample *sound = beacon.get_outer();
298                 if ( sound ) {
299                     sound->set_volume( 0.3 );
300                     globals->get_soundmgr()->add( sound, "outer-marker" );
301                 }
302             }
303         }
304         if ( audio_btn->getBoolValue() ) {
305             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
306                 globals->get_soundmgr()->play_looped( "outer-marker" );
307             }
308         } else {
309             globals->get_soundmgr()->stop( "outer-marker" );
310         }
311     } else if ( beacon_type == MIDDLE ) {
312         middle_marker = true;
313         // cout << "MIDDLE MARKER" << endl;
314         if ( last_beacon != MIDDLE ) {
315             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
316                 SGSoundSample *sound = beacon.get_middle();
317                 if ( sound ) {
318                     sound->set_volume( 0.3 );
319                     globals->get_soundmgr()->add( sound, "middle-marker" );
320                 }
321             }
322         }
323         if ( audio_btn->getBoolValue() ) {
324             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
325                 globals->get_soundmgr()->play_looped( "middle-marker" );
326             }
327         } else {
328             globals->get_soundmgr()->stop( "middle-marker" );
329         }
330     } else if ( beacon_type == INNER ) {
331         inner_marker = true;
332         // cout << "INNER MARKER" << endl;
333         if ( last_beacon != INNER ) {
334             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
335                 SGSoundSample *sound = beacon.get_inner();
336                 if ( sound ) {
337                     sound->set_volume( 0.3 );
338                     globals->get_soundmgr()->add( sound, "inner-marker" );
339                 }
340             }
341         }
342         if ( audio_btn->getBoolValue() ) {
343             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
344                 globals->get_soundmgr()->play_looped( "inner-marker" );
345             }
346         } else {
347             globals->get_soundmgr()->stop( "inner-marker" );
348         }
349     }
350
351     if ( inrange ) {
352         last_beacon = beacon_type;
353     } else {
354         last_beacon = NOBEACON;
355     }
356 }