]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/marker_beacon.cxx
A couple more straggling instrumentation clean ups.
[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 {
51     SGPath path( globals->get_fg_root() );
52     SGPath term = path;
53     term.append( "Navaids/range.term" );
54     SGPath low = path;
55     low.append( "Navaids/range.low" );
56     SGPath high = path;
57     high.append( "Navaids/range.high" );
58
59     term_tbl = new SGInterpTable( term.str() );
60     low_tbl = new SGInterpTable( low.str() );
61     high_tbl = new SGInterpTable( high.str() );
62
63     int i;
64     for ( i = 0; i < node->nChildren(); ++i ) {
65         SGPropertyNode *child = node->getChild(i);
66         string cname = child->getName();
67         string cval = child->getStringValue();
68         if ( cname == "name" ) {
69             name = cval;
70         } else if ( cname == "number" ) {
71             num = child->getIntValue();
72         } else {
73             SG_LOG( SG_INSTR, SG_WARN, 
74                     "Error in marker beacon config logic" );
75             if ( name.length() ) {
76                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
77             }
78         }
79     }
80 }
81
82
83 // Destructor
84 FGMarkerBeacon::~FGMarkerBeacon() 
85 {
86     delete term_tbl;
87     delete low_tbl;
88     delete high_tbl;
89 }
90
91
92 void
93 FGMarkerBeacon::init ()
94 {
95     string branch;
96     branch = "/instrumentation/" + name;
97
98     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
99     // Inputs
100     lon_node = fgGetNode("/position/longitude-deg", true);
101     lat_node = fgGetNode("/position/latitude-deg", true);
102     alt_node = fgGetNode("/position/altitude-ft", true);
103     bus_power = fgGetNode("/systems/electrical/outputs/nav[0]", true);
104     power_btn = node->getChild("power-btn", 0, true);
105     audio_btn = node->getChild("audio-btn", 0, true);
106     serviceable = node->getChild("serviceable", 0, true);
107
108     power_btn->setBoolValue( true );
109     audio_btn->setBoolValue( true );
110     serviceable->setBoolValue( true );
111
112     morse.init();
113     beacon.init();
114     blink.stamp();
115 }
116
117
118 void
119 FGMarkerBeacon::bind ()
120 {
121     string branch;
122     branch = "/instrumentation/" + name;
123
124     fgTie((branch + "/inner").c_str(), this,
125           &FGMarkerBeacon::get_inner_blink);
126
127     fgTie((branch + "/middle").c_str(), this,
128           &FGMarkerBeacon::get_middle_blink);
129
130     fgTie((branch + "/outer").c_str(), this,
131           &FGMarkerBeacon::get_outer_blink);
132 }
133
134
135 void
136 FGMarkerBeacon::unbind ()
137 {
138     string branch;
139     branch = "/instrumentation/" + name;
140
141     fgUntie((branch + "/inner").c_str());
142     fgUntie((branch + "/middle").c_str());
143     fgUntie((branch + "/outer").c_str());
144 }
145
146
147 // Update the various nav values based on position and valid tuned in navs
148 void 
149 FGMarkerBeacon::update(double dt) 
150 {
151     need_update = false;
152
153     if ( has_power() && serviceable->getBoolValue() ) {
154
155         // On timeout, scan again
156         _time_before_search_sec -= dt;
157         if ( _time_before_search_sec < 0 ) {
158             search();
159         }
160         // marker beacon blinking
161         bool light_on = ( outer_blink || middle_blink || inner_blink );
162         SGTimeStamp current;
163         current.stamp();
164
165         if ( light_on && (current - blink > 400000) ) {
166             light_on = false;
167             blink.stamp();
168         } else if ( !light_on && (current - blink > 100000) ) {
169             light_on = true;
170             blink.stamp();
171         }
172
173         if ( outer_marker ) {
174             outer_blink = light_on;
175         } else {
176             outer_blink = false;
177         }
178
179         if ( middle_marker ) {
180             middle_blink = light_on;
181         } else {
182             middle_blink = false;
183         }
184
185         if ( inner_marker ) {
186             inner_blink = light_on;
187         } else {
188             inner_blink = false;
189         }
190
191         // cout << outer_blink << " " << middle_blink << " "
192         //      << inner_blink << endl;
193     } else {
194         inner_blink = middle_blink = outer_blink = false;
195     }
196 }
197
198
199 static bool check_beacon_range( double lon_rad, double lat_rad, double elev_m,
200                                 FGNavRecord *b )
201 {
202     Point3D aircraft = sgGeodToCart( Point3D(lon_rad, lat_rad, elev_m) );
203     Point3D station = Point3D( b->get_x(), b->get_y(), b->get_z() );
204     // cout << "    aircraft = " << aircraft << " station = " << station 
205     //      << endl;
206
207     double d = aircraft.distance3Dsquared( station ); // meters^2
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     // cout << "  range = " << sqrt(d) << endl;
214
215     // cout << "elev = " << elev * SG_METER_TO_FEET
216     //      << " current->get_elev() = " << current->get_elev() << endl;
217     double elev_ft = elev_m * SG_METER_TO_FEET;
218     double delev = elev_ft - b->get_elev_ft();
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     // cout << "delev = " << delev << " maxrange = " << maxrange << 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 // Update current nav/adf radio stations based on current postition
243 void FGMarkerBeacon::search() 
244 {
245
246     // reset search time
247     _time_before_search_sec = 1.0;
248
249     static fgMkrBeacType last_beacon = NOBEACON;
250
251     double lon_rad = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
252     double lat_rad = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
253     double elev_m = alt_node->getDoubleValue() * SG_FEET_TO_METER;
254
255     ////////////////////////////////////////////////////////////////////////
256     // Beacons.
257     ////////////////////////////////////////////////////////////////////////
258
259     // get closest marker beacon
260     FGNavRecord *b
261         = globals->get_mkrlist()->findClosest( lon_rad, lat_rad, elev_m );
262
263     // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
264
265     fgMkrBeacType beacon_type = NOBEACON;
266     bool inrange = false;
267     if ( b != NULL ) {
268         if ( b->get_type() == 7 ) {
269             beacon_type = OUTER;
270         } else if ( b->get_type() == 8 ) {
271             beacon_type = MIDDLE;
272         } else if ( b->get_type() == 9 ) {
273             beacon_type = INNER;
274         }
275         inrange = check_beacon_range( lon_rad, lat_rad, elev_m, b );
276         // cout << "  inrange = " << inrange << endl;
277     }
278
279     outer_marker = middle_marker = inner_marker = false;
280
281     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
282     {
283         // cout << "no marker" << endl;
284         globals->get_soundmgr()->stop( "outer-marker" );
285         globals->get_soundmgr()->stop( "middle-marker" );
286         globals->get_soundmgr()->stop( "inner-marker" );
287     } else if ( beacon_type == OUTER ) {
288         outer_marker = true;
289         // cout << "OUTER MARKER" << endl;
290         if ( last_beacon != OUTER ) {
291             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
292                 SGSoundSample *sound = beacon.get_outer();
293                 sound->set_volume( 0.3 );
294                 globals->get_soundmgr()->add( sound, "outer-marker" );
295             }
296         }
297         if ( audio_btn->getBoolValue() ) {
298             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
299                 globals->get_soundmgr()->play_looped( "outer-marker" );
300             }
301         } else {
302             globals->get_soundmgr()->stop( "outer-marker" );
303         }
304     } else if ( beacon_type == MIDDLE ) {
305         middle_marker = true;
306         // cout << "MIDDLE MARKER" << endl;
307         if ( last_beacon != MIDDLE ) {
308             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
309                 SGSoundSample *sound = beacon.get_middle();
310                 sound->set_volume( 0.3 );
311                 globals->get_soundmgr()->add( sound, "middle-marker" );
312             }
313         }
314         if ( audio_btn->getBoolValue() ) {
315             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
316                 globals->get_soundmgr()->play_looped( "middle-marker" );
317             }
318         } else {
319             globals->get_soundmgr()->stop( "middle-marker" );
320         }
321     } else if ( beacon_type == INNER ) {
322         inner_marker = true;
323         // cout << "INNER MARKER" << endl;
324         if ( last_beacon != INNER ) {
325             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
326                 SGSoundSample *sound = beacon.get_inner();
327                 sound->set_volume( 0.3 );
328                 globals->get_soundmgr()->add( sound, "inner-marker" );
329             }
330         }
331         if ( audio_btn->getBoolValue() ) {
332             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
333                 globals->get_soundmgr()->play_looped( "inner-marker" );
334             }
335         } else {
336             globals->get_soundmgr()->stop( "inner-marker" );
337         }
338     }
339
340     if ( inrange ) {
341         last_beacon = beacon_type;
342     } else {
343         last_beacon = NOBEACON;
344     }
345 }