]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/marker_beacon.cxx
More code shuffling.
[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     search();
109     update(0);                  // FIXME: use dt
110 }
111
112
113 void
114 FGMarkerBeacon::bind ()
115 {
116
117     fgTie("/radios/marker-beacon/inner", this,
118           &FGMarkerBeacon::get_inner_blink);
119
120     fgTie("/radios/marker-beacon/middle", this,
121           &FGMarkerBeacon::get_middle_blink);
122
123     fgTie("/radios/marker-beacon/outer", this,
124           &FGMarkerBeacon::get_outer_blink);
125 }
126
127
128 void
129 FGMarkerBeacon::unbind ()
130 {
131     fgUntie("/radios/marker-beacon/inner");
132     fgUntie("/radios/marker-beacon/middle");
133     fgUntie("/radios/marker-beacon/outer");
134 }
135
136
137 // Update the various nav values based on position and valid tuned in navs
138 void 
139 FGMarkerBeacon::update(double dt) 
140 {
141     need_update = false;
142
143     // marker beacon blinking
144     bool light_on = ( outer_blink || middle_blink || inner_blink );
145     SGTimeStamp current;
146     current.stamp();
147
148     if ( light_on && (current - blink > 400000) ) {
149         light_on = false;
150         blink.stamp();
151     } else if ( !light_on && (current - blink > 100000) ) {
152         light_on = true;
153         blink.stamp();
154     }
155
156     if ( outer_marker ) {
157         outer_blink = light_on;
158     } else {
159         outer_blink = false;
160     }
161
162     if ( middle_marker ) {
163         middle_blink = light_on;
164     } else {
165         middle_blink = false;
166     }
167
168     if ( inner_marker ) {
169         inner_blink = light_on;
170     } else {
171         inner_blink = false;
172     }
173
174     // cout << outer_blink << " " << middle_blink << " " << inner_blink << endl;
175 }
176
177
178 // Update current nav/adf radio stations based on current postition
179 void FGMarkerBeacon::search() 
180 {
181     static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
182
183     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
184     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
185     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
186
187     ////////////////////////////////////////////////////////////////////////
188     // Beacons.
189     ////////////////////////////////////////////////////////////////////////
190
191     FGMkrBeacon::fgMkrBeacType beacon_type
192         = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
193                                   lat * SGD_RADIANS_TO_DEGREES, elev );
194
195     outer_marker = middle_marker = inner_marker = false;
196
197     if ( beacon_type == FGMkrBeacon::OUTER ) {
198         outer_marker = true;
199         // cout << "OUTER MARKER" << endl;
200 #ifdef ENABLE_AUDIO_SUPPORT
201         if ( last_beacon != FGMkrBeacon::OUTER ) {
202             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
203                 FGSimpleSound *sound = beacon.get_outer();
204                 sound->set_volume( 0.3 );
205                 globals->get_soundmgr()->add( sound, "outer-marker" );
206             }
207             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
208                 globals->get_soundmgr()->play_looped( "outer-marker" );
209             }
210         }
211 #endif
212     } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
213         middle_marker = true;
214         // cout << "MIDDLE MARKER" << endl;
215 #ifdef ENABLE_AUDIO_SUPPORT
216         if ( last_beacon != FGMkrBeacon::MIDDLE ) {
217             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
218                 FGSimpleSound *sound = beacon.get_middle();
219                 sound->set_volume( 0.3 );
220                 globals->get_soundmgr()->add( sound, "middle-marker" );
221             }
222             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
223                 globals->get_soundmgr()->play_looped( "middle-marker" );
224             }
225         }
226 #endif
227     } else if ( beacon_type == FGMkrBeacon::INNER ) {
228         inner_marker = true;
229         // cout << "INNER MARKER" << endl;
230 #ifdef ENABLE_AUDIO_SUPPORT
231         if ( last_beacon != FGMkrBeacon::INNER ) {
232             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
233                 FGSimpleSound *sound = beacon.get_inner();
234                 sound->set_volume( 0.3 );
235                 globals->get_soundmgr()->add( sound, "inner-marker" );
236             }
237             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
238                 globals->get_soundmgr()->play_looped( "inner-marker" );
239             }
240         }
241 #endif
242     } else {
243         // cout << "no marker" << endl;
244 #ifdef ENABLE_AUDIO_SUPPORT
245         globals->get_soundmgr()->stop( "outer-marker" );
246         globals->get_soundmgr()->stop( "middle-marker" );
247         globals->get_soundmgr()->stop( "inner-marker" );
248 #endif
249     }
250     last_beacon = beacon_type;
251 }