]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/marker_beacon.cxx
This set of changes impliments the following:
[flightgear.git] / src / Cockpit / 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 - curt@flightgear.org
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() :
44     lon_node(fgGetNode("/position/longitude-deg", true)),
45     lat_node(fgGetNode("/position/latitude-deg", true)),
46     alt_node(fgGetNode("/position/altitude-ft", true)),
47     bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
48     power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
49     audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
50     serviceable(fgGetNode("/instrumentation/marker-beacons/serviceable", true)),
51                  
52     need_update(true),
53     outer_blink(false),
54     middle_blink(false),
55     inner_blink(false)
56 {
57     SGPath path( globals->get_fg_root() );
58     SGPath term = path;
59     term.append( "Navaids/range.term" );
60     SGPath low = path;
61     low.append( "Navaids/range.low" );
62     SGPath high = path;
63     high.append( "Navaids/range.high" );
64
65     term_tbl = new SGInterpTable( term.str() );
66     low_tbl = new SGInterpTable( low.str() );
67     high_tbl = new SGInterpTable( high.str() );
68
69     power_btn->setBoolValue( true );
70     audio_btn->setBoolValue( true );
71     serviceable->setBoolValue( true );
72 }
73
74
75 // Destructor
76 FGMarkerBeacon::~FGMarkerBeacon() 
77 {
78     delete term_tbl;
79     delete low_tbl;
80     delete high_tbl;
81 }
82
83
84 void
85 FGMarkerBeacon::init ()
86 {
87     morse.init();
88     beacon.init();
89     blink.stamp();
90 }
91
92
93 void
94 FGMarkerBeacon::bind ()
95 {
96
97     fgTie("/radios/marker-beacon/inner", this,
98           &FGMarkerBeacon::get_inner_blink);
99
100     fgTie("/radios/marker-beacon/middle", this,
101           &FGMarkerBeacon::get_middle_blink);
102
103     fgTie("/radios/marker-beacon/outer", this,
104           &FGMarkerBeacon::get_outer_blink);
105 }
106
107
108 void
109 FGMarkerBeacon::unbind ()
110 {
111     fgUntie("/radios/marker-beacon/inner");
112     fgUntie("/radios/marker-beacon/middle");
113     fgUntie("/radios/marker-beacon/outer");
114 }
115
116
117 // Update the various nav values based on position and valid tuned in navs
118 void 
119 FGMarkerBeacon::update(double dt) 
120 {
121     need_update = false;
122
123     if ( has_power() && serviceable->getBoolValue() ) {
124         // marker beacon blinking
125         bool light_on = ( outer_blink || middle_blink || inner_blink );
126         SGTimeStamp current;
127         current.stamp();
128
129         if ( light_on && (current - blink > 400000) ) {
130             light_on = false;
131             blink.stamp();
132         } else if ( !light_on && (current - blink > 100000) ) {
133             light_on = true;
134             blink.stamp();
135         }
136
137         if ( outer_marker ) {
138             outer_blink = light_on;
139         } else {
140             outer_blink = false;
141         }
142
143         if ( middle_marker ) {
144             middle_blink = light_on;
145         } else {
146             middle_blink = false;
147         }
148
149         if ( inner_marker ) {
150             inner_blink = light_on;
151         } else {
152             inner_blink = false;
153         }
154
155         // cout << outer_blink << " " << middle_blink << " "
156         //      << inner_blink << endl;
157     } else {
158         inner_blink = middle_blink = outer_blink = false;
159     }
160 }
161
162
163 static bool check_beacon_range( double lon_rad, double lat_rad, double elev_m,
164                                 FGNavRecord *b )
165 {
166     Point3D aircraft = sgGeodToCart( Point3D(lon_rad, lat_rad, elev_m) );
167     Point3D station = Point3D( b->get_x(), b->get_y(), b->get_z() );
168     // cout << "    aircraft = " << aircraft << " station = " << station 
169     //      << endl;
170
171     double d = aircraft.distance3Dsquared( station ); // meters^2
172     // cout << "  distance = " << d << " (" 
173     //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
174     //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
175     //      << ")" << endl;
176     
177     // cout << "  range = " << sqrt(d) << endl;
178
179     // cout << "elev = " << elev * SG_METER_TO_FEET
180     //      << " current->get_elev() = " << current->get_elev() << endl;
181     double elev_ft = elev_m * SG_METER_TO_FEET;
182     double delev = elev_ft - b->get_elev_ft();
183
184     // max range is the area under r = 2.4 * alt or r^2 = 4000^2 - alt^2
185     // whichever is smaller.  The intersection point is 1538 ...
186     double maxrange2;   // feet^2
187     if ( delev < 1538.0 ) {
188         maxrange2 = 2.4 * 2.4 * delev * delev;
189     } else if ( delev < 4000.0 ) {
190         maxrange2 = 4000 * 4000 - delev * delev;
191     } else {
192         maxrange2 = 0.0;
193     }
194     maxrange2 *= SG_FEET_TO_METER * SG_FEET_TO_METER; // convert to meter^2
195     // cout << "delev = " << delev << " maxrange = " << maxrange << endl;
196
197     // match up to twice the published range so we can model
198     // reduced signal strength
199     if ( d < maxrange2 ) {
200         return true;
201     } else {
202         return false;
203     }
204 }
205
206 // Update current nav/adf radio stations based on current postition
207 void FGMarkerBeacon::search() 
208 {
209     static fgMkrBeacType last_beacon = NOBEACON;
210
211     double lon_rad = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
212     double lat_rad = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
213     double elev_m = alt_node->getDoubleValue() * SG_FEET_TO_METER;
214
215     ////////////////////////////////////////////////////////////////////////
216     // Beacons.
217     ////////////////////////////////////////////////////////////////////////
218
219     // get closest marker beacon
220     FGNavRecord *b
221         = globals->get_mkrlist()->findClosest( lon_rad, lat_rad, elev_m );
222
223     // cout << "marker beacon = " << b << " (" << b->get_type() << ")" << endl;
224
225     fgMkrBeacType beacon_type = NOBEACON;
226     bool inrange = false;
227     if ( b != NULL ) {
228         if ( b->get_type() == 7 ) {
229             beacon_type = OUTER;
230         } else if ( b->get_type() == 8 ) {
231             beacon_type = MIDDLE;
232         } else if ( b->get_type() == 9 ) {
233             beacon_type = INNER;
234         }
235         inrange = check_beacon_range( lon_rad, lat_rad, elev_m, b );
236         // cout << "  inrange = " << inrange << endl;
237     }
238
239     outer_marker = middle_marker = inner_marker = false;
240
241     if ( b == NULL || !inrange || !has_power() || !serviceable->getBoolValue() )
242     {
243         // cout << "no marker" << endl;
244         globals->get_soundmgr()->stop( "outer-marker" );
245         globals->get_soundmgr()->stop( "middle-marker" );
246         globals->get_soundmgr()->stop( "inner-marker" );
247     } else if ( beacon_type == OUTER ) {
248         outer_marker = true;
249         // cout << "OUTER MARKER" << endl;
250         if ( last_beacon != OUTER ) {
251             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
252                 SGSoundSample *sound = beacon.get_outer();
253                 sound->set_volume( 0.3 );
254                 globals->get_soundmgr()->add( sound, "outer-marker" );
255             }
256         }
257         if ( audio_btn->getBoolValue() ) {
258             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
259                 globals->get_soundmgr()->play_looped( "outer-marker" );
260             }
261         } else {
262             globals->get_soundmgr()->stop( "outer-marker" );
263         }
264     } else if ( beacon_type == MIDDLE ) {
265         middle_marker = true;
266         // cout << "MIDDLE MARKER" << endl;
267         if ( last_beacon != MIDDLE ) {
268             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
269                 SGSoundSample *sound = beacon.get_middle();
270                 sound->set_volume( 0.3 );
271                 globals->get_soundmgr()->add( sound, "middle-marker" );
272             }
273         }
274         if ( audio_btn->getBoolValue() ) {
275             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
276                 globals->get_soundmgr()->play_looped( "middle-marker" );
277             }
278         } else {
279             globals->get_soundmgr()->stop( "middle-marker" );
280         }
281     } else if ( beacon_type == INNER ) {
282         inner_marker = true;
283         // cout << "INNER MARKER" << endl;
284         if ( last_beacon != INNER ) {
285             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
286                 SGSoundSample *sound = beacon.get_inner();
287                 sound->set_volume( 0.3 );
288                 globals->get_soundmgr()->add( sound, "inner-marker" );
289             }
290         }
291         if ( audio_btn->getBoolValue() ) {
292             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
293                 globals->get_soundmgr()->play_looped( "inner-marker" );
294             }
295         } else {
296             globals->get_soundmgr()->stop( "inner-marker" );
297         }
298     }
299
300     if ( inrange ) {
301         last_beacon = beacon_type;
302     } else {
303         last_beacon = NOBEACON;
304     }
305 }