]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/marker_beacon.cxx
Added support for an audio panel.
[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     bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
74     power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
75     audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
76     servicable(fgGetNode("/instrumentation/marker-beacons/servicable", true)),
77                  
78     need_update(true),
79     outer_blink(false),
80     middle_blink(false),
81     inner_blink(false)
82 {
83     SGPath path( globals->get_fg_root() );
84     SGPath term = path;
85     term.append( "Navaids/range.term" );
86     SGPath low = path;
87     low.append( "Navaids/range.low" );
88     SGPath high = path;
89     high.append( "Navaids/range.high" );
90
91     term_tbl = new SGInterpTable( term.str() );
92     low_tbl = new SGInterpTable( low.str() );
93     high_tbl = new SGInterpTable( high.str() );
94
95     power_btn->setBoolValue( true );
96     audio_btn->setBoolValue( true );
97     servicable->setBoolValue( true );
98 }
99
100
101 // Destructor
102 FGMarkerBeacon::~FGMarkerBeacon() 
103 {
104     delete term_tbl;
105     delete low_tbl;
106     delete high_tbl;
107 }
108
109
110 void
111 FGMarkerBeacon::init ()
112 {
113     morse.init();
114     beacon.init();
115     blink.stamp();
116 }
117
118
119 void
120 FGMarkerBeacon::bind ()
121 {
122
123     fgTie("/radios/marker-beacon/inner", this,
124           &FGMarkerBeacon::get_inner_blink);
125
126     fgTie("/radios/marker-beacon/middle", this,
127           &FGMarkerBeacon::get_middle_blink);
128
129     fgTie("/radios/marker-beacon/outer", this,
130           &FGMarkerBeacon::get_outer_blink);
131 }
132
133
134 void
135 FGMarkerBeacon::unbind ()
136 {
137     fgUntie("/radios/marker-beacon/inner");
138     fgUntie("/radios/marker-beacon/middle");
139     fgUntie("/radios/marker-beacon/outer");
140 }
141
142
143 // Update the various nav values based on position and valid tuned in navs
144 void 
145 FGMarkerBeacon::update(double dt) 
146 {
147     need_update = false;
148
149     if ( has_power() && servicable->getBoolValue() ) {
150         // marker beacon blinking
151         bool light_on = ( outer_blink || middle_blink || inner_blink );
152         SGTimeStamp current;
153         current.stamp();
154
155         if ( light_on && (current - blink > 400000) ) {
156             light_on = false;
157             blink.stamp();
158         } else if ( !light_on && (current - blink > 100000) ) {
159             light_on = true;
160             blink.stamp();
161         }
162
163         if ( outer_marker ) {
164             outer_blink = light_on;
165         } else {
166             outer_blink = false;
167         }
168
169         if ( middle_marker ) {
170             middle_blink = light_on;
171         } else {
172             middle_blink = false;
173         }
174
175         if ( inner_marker ) {
176             inner_blink = light_on;
177         } else {
178             inner_blink = false;
179         }
180
181         // cout << outer_blink << " " << middle_blink << " "
182         //      << inner_blink << endl;
183     } else {
184         inner_blink = middle_blink = outer_blink = false;
185     }
186 }
187
188
189 // Update current nav/adf radio stations based on current postition
190 void FGMarkerBeacon::search() 
191 {
192     static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
193
194     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
195     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
196     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
197
198     ////////////////////////////////////////////////////////////////////////
199     // Beacons.
200     ////////////////////////////////////////////////////////////////////////
201
202     FGMkrBeacon::fgMkrBeacType beacon_type
203         = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
204                                   lat * SGD_RADIANS_TO_DEGREES, elev );
205
206     outer_marker = middle_marker = inner_marker = false;
207
208     if ( beacon_type == FGMkrBeacon::NOBEACON
209          || !has_power() || !servicable->getBoolValue() )
210     {
211         // cout << "no marker" << endl;
212         beacon_type = FGMkrBeacon::NOBEACON;
213         globals->get_soundmgr()->stop( "outer-marker" );
214         globals->get_soundmgr()->stop( "middle-marker" );
215         globals->get_soundmgr()->stop( "inner-marker" );
216     } else if ( beacon_type == FGMkrBeacon::OUTER ) {
217         outer_marker = true;
218         // cout << "OUTER MARKER" << endl;
219         if ( last_beacon != FGMkrBeacon::OUTER ) {
220             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
221                 FGSimpleSound *sound = beacon.get_outer();
222                 sound->set_volume( 0.3 );
223                 globals->get_soundmgr()->add( sound, "outer-marker" );
224             }
225         }
226         if ( audio_btn->getBoolValue() 
227              && !globals->get_soundmgr()->is_playing("outer-marker") )
228         {
229             globals->get_soundmgr()->play_looped( "outer-marker" );
230         } else {
231             globals->get_soundmgr()->stop( "outer-marker" );
232         }
233     } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
234         middle_marker = true;
235         // cout << "MIDDLE MARKER" << endl;
236         if ( last_beacon != FGMkrBeacon::MIDDLE ) {
237             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
238                 FGSimpleSound *sound = beacon.get_middle();
239                 sound->set_volume( 0.3 );
240                 globals->get_soundmgr()->add( sound, "middle-marker" );
241             }
242         }
243         if ( audio_btn->getBoolValue() 
244              && !globals->get_soundmgr()->is_playing("middle-marker") )
245         {
246             globals->get_soundmgr()->play_looped( "middle-marker" );
247         } else {
248             globals->get_soundmgr()->stop( "middle-marker" );
249         }
250     } else if ( beacon_type == FGMkrBeacon::INNER ) {
251         inner_marker = true;
252         // cout << "INNER MARKER" << endl;
253         if ( last_beacon != FGMkrBeacon::INNER ) {
254             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
255                 FGSimpleSound *sound = beacon.get_inner();
256                 sound->set_volume( 0.3 );
257                 globals->get_soundmgr()->add( sound, "inner-marker" );
258             }
259         }
260         if ( audio_btn->getBoolValue() 
261              && !globals->get_soundmgr()->is_playing("inner-marker") )
262         {
263             globals->get_soundmgr()->play_looped( "inner-marker" );
264         } else {
265             globals->get_soundmgr()->stop( "inner-marker" );
266         }
267     }
268     last_beacon = beacon_type;
269 }