]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/marker_beacon.cxx
Fix an init order bug and do some minor cleanups.
[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/mkrbeacons.hxx>
35 #include <Time/FGEventMgr.hxx>
36
37 #include "marker_beacon.hxx"
38
39 #include <string>
40 SG_USING_STD(string);
41
42
43 /**
44  * Boy, this is ugly!  Make the VOR range vary by altitude difference.
45  */
46 static double kludgeRange ( double stationElev, double aircraftElev,
47                             double nominalRange)
48 {
49                                 // Assume that the nominal range (usually
50                                 // 50nm) applies at a 5,000 ft difference.
51                                 // Just a wild guess!
52   double factor = ((aircraftElev*SG_METER_TO_FEET) - stationElev) / 5000.0;
53   double range = fabs(nominalRange * factor);
54
55                                 // Clamp the range to keep it sane; for
56                                 // now, never less than 25% or more than
57                                 // 500% of nominal range.
58   if (range < nominalRange/4.0) {
59     range = nominalRange/4.0;
60   } else if (range > nominalRange*5.0) {
61     range = nominalRange*5.0;
62   }
63
64   return range;
65 }
66
67
68 // Constructor
69 FGMarkerBeacon::FGMarkerBeacon() :
70     lon_node(fgGetNode("/position/longitude-deg", true)),
71     lat_node(fgGetNode("/position/latitude-deg", true)),
72     alt_node(fgGetNode("/position/altitude-ft", true)),
73     need_update(true),
74     outer_blink(false),
75     middle_blink(false),
76     inner_blink(false)
77 {
78     SGPath path( globals->get_fg_root() );
79     SGPath term = path;
80     term.append( "Navaids/range.term" );
81     SGPath low = path;
82     low.append( "Navaids/range.low" );
83     SGPath high = path;
84     high.append( "Navaids/range.high" );
85
86     term_tbl = new SGInterpTable( term.str() );
87     low_tbl = new SGInterpTable( low.str() );
88     high_tbl = new SGInterpTable( high.str() );
89 }
90
91
92 // Destructor
93 FGMarkerBeacon::~FGMarkerBeacon() 
94 {
95     delete term_tbl;
96     delete low_tbl;
97     delete high_tbl;
98 }
99
100
101 void
102 FGMarkerBeacon::init ()
103 {
104     morse.init();
105     beacon.init();
106     blink.stamp();
107 }
108
109
110 void
111 FGMarkerBeacon::bind ()
112 {
113
114     fgTie("/radios/marker-beacon/inner", this,
115           &FGMarkerBeacon::get_inner_blink);
116
117     fgTie("/radios/marker-beacon/middle", this,
118           &FGMarkerBeacon::get_middle_blink);
119
120     fgTie("/radios/marker-beacon/outer", this,
121           &FGMarkerBeacon::get_outer_blink);
122 }
123
124
125 void
126 FGMarkerBeacon::unbind ()
127 {
128     fgUntie("/radios/marker-beacon/inner");
129     fgUntie("/radios/marker-beacon/middle");
130     fgUntie("/radios/marker-beacon/outer");
131 }
132
133
134 // Update the various nav values based on position and valid tuned in navs
135 void 
136 FGMarkerBeacon::update(double dt) 
137 {
138     need_update = false;
139
140     // marker beacon blinking
141     bool light_on = ( outer_blink || middle_blink || inner_blink );
142     SGTimeStamp current;
143     current.stamp();
144
145     if ( light_on && (current - blink > 400000) ) {
146         light_on = false;
147         blink.stamp();
148     } else if ( !light_on && (current - blink > 100000) ) {
149         light_on = true;
150         blink.stamp();
151     }
152
153     if ( outer_marker ) {
154         outer_blink = light_on;
155     } else {
156         outer_blink = false;
157     }
158
159     if ( middle_marker ) {
160         middle_blink = light_on;
161     } else {
162         middle_blink = false;
163     }
164
165     if ( inner_marker ) {
166         inner_blink = light_on;
167     } else {
168         inner_blink = false;
169     }
170
171     // cout << outer_blink << " " << middle_blink << " " << inner_blink << endl;
172 }
173
174
175 // Update current nav/adf radio stations based on current postition
176 void FGMarkerBeacon::search() 
177 {
178     static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
179
180     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
181     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
182     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
183
184     ////////////////////////////////////////////////////////////////////////
185     // Beacons.
186     ////////////////////////////////////////////////////////////////////////
187
188     FGMkrBeacon::fgMkrBeacType beacon_type
189         = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
190                                   lat * SGD_RADIANS_TO_DEGREES, elev );
191
192     outer_marker = middle_marker = inner_marker = false;
193
194     if ( beacon_type == FGMkrBeacon::OUTER ) {
195         outer_marker = true;
196         // cout << "OUTER MARKER" << endl;
197 #ifdef ENABLE_AUDIO_SUPPORT
198         if ( last_beacon != FGMkrBeacon::OUTER ) {
199             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
200                 FGSimpleSound *sound = beacon.get_outer();
201                 sound->set_volume( 0.3 );
202                 globals->get_soundmgr()->add( sound, "outer-marker" );
203             }
204             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
205                 globals->get_soundmgr()->play_looped( "outer-marker" );
206             }
207         }
208 #endif
209     } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
210         middle_marker = true;
211         // cout << "MIDDLE MARKER" << endl;
212 #ifdef ENABLE_AUDIO_SUPPORT
213         if ( last_beacon != FGMkrBeacon::MIDDLE ) {
214             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
215                 FGSimpleSound *sound = beacon.get_middle();
216                 sound->set_volume( 0.3 );
217                 globals->get_soundmgr()->add( sound, "middle-marker" );
218             }
219             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
220                 globals->get_soundmgr()->play_looped( "middle-marker" );
221             }
222         }
223 #endif
224     } else if ( beacon_type == FGMkrBeacon::INNER ) {
225         inner_marker = true;
226         // cout << "INNER MARKER" << endl;
227 #ifdef ENABLE_AUDIO_SUPPORT
228         if ( last_beacon != FGMkrBeacon::INNER ) {
229             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
230                 FGSimpleSound *sound = beacon.get_inner();
231                 sound->set_volume( 0.3 );
232                 globals->get_soundmgr()->add( sound, "inner-marker" );
233             }
234             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
235                 globals->get_soundmgr()->play_looped( "inner-marker" );
236             }
237         }
238 #endif
239     } else {
240         // cout << "no marker" << endl;
241 #ifdef ENABLE_AUDIO_SUPPORT
242         globals->get_soundmgr()->stop( "outer-marker" );
243         globals->get_soundmgr()->stop( "middle-marker" );
244         globals->get_soundmgr()->stop( "inner-marker" );
245 #endif
246     }
247     last_beacon = beacon_type;
248 }