]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
Merge branch 'vivian/cullsetting'
[flightgear.git] / src / Environment / environment_ctrl.hxx
1 // environment-ctrl.hxx -- controller for environment information.
2 //
3 // Written by David Megginson, started May 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifndef _ENVIRONMENT_CTRL_HXX
24 #define _ENVIRONMENT_CTRL_HXX
25
26 #include <simgear/compiler.h>
27 #include <simgear/structure/subsystem_mgr.hxx>
28
29 #if defined(ENABLE_THREADS)
30 # include <OpenThreads/Thread>
31 # include <simgear/threads/SGQueue.hxx>
32 #endif
33
34 #include <queue>
35 #include <vector>
36
37 #include <Navaids/positioned.hxx>
38 #include <Environment/environment.hxx>
39 #include "fgwind.hxx"
40
41 // forward decls
42 class SGPropertyNode;
43 class SGSampleGroup;
44 class FGMetar;
45
46 /**
47  * Interface to control environment information for a specific location.
48  */
49 class FGEnvironmentCtrl : public SGSubsystem
50 {
51
52 public:
53
54         FGEnvironmentCtrl ();
55         virtual ~FGEnvironmentCtrl ();
56
57         virtual void setEnvironment (FGEnvironment * environment);
58
59         virtual const FGEnvironment * getEnvironment () const { return _environment; }
60
61         virtual void setLongitudeDeg (double lon_deg);
62         virtual void setLatitudeDeg (double lat_deg);
63         virtual void setElevationFt (double elev_ft);
64         virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
65
66         virtual double getLongitudeDeg () const { return _lon_deg; }
67         virtual double getLatitudeDeg () const { return _lat_deg; }
68         virtual double getElevationFt () const { return _elev_ft; }
69
70 protected:
71
72         FGEnvironment * _environment;
73         double _lon_deg;
74         double _lat_deg;
75         double _elev_ft;
76
77 };
78
79
80 \f
81 /**
82  * Interplation controller using user-supplied parameters.
83  */
84 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
85 {
86 public:
87         FGInterpolateEnvironmentCtrl ();
88         virtual ~FGInterpolateEnvironmentCtrl ();
89         
90         virtual void init ();
91         virtual void reinit ();
92         virtual void update (double delta_time_sec);
93
94 private:
95         
96         struct bucket {
97                 double altitude_ft;
98                 FGEnvironment environment;
99                 bool operator< (const bucket &b) const;
100                 // LessThan predicate for bucket pointers.
101                 static bool lessThan(bucket *a, bucket *b);
102         };
103
104         void read_table (const SGPropertyNode * node, std::vector<bucket *> &table);
105         void do_interpolate (std::vector<bucket *> &table, double altitude_ft,
106                                                  FGEnvironment * environment);
107
108         FGEnvironment env1, env2;        // temporaries
109
110         std::vector<bucket *> _boundary_table;
111         std::vector<bucket *> _aloft_table;
112
113         SGPropertyNode_ptr altitude_n;
114         SGPropertyNode_ptr altitude_agl_n;
115         SGPropertyNode_ptr boundary_transition_n;
116         SGPropertyNode_ptr boundary_n;
117         SGPropertyNode_ptr aloft_n;
118 };
119
120
121 \f
122 /**
123  * Interplation controller using the FGMetar class
124  */
125
126 class FGMetarCtrl : public SGSubsystem
127 {
128 public:
129         FGMetarCtrl (SGSubsystem * environmentCtrl);
130         virtual ~FGMetarCtrl ();
131
132         virtual void init ();
133         virtual void reinit ();
134         virtual void update (double delta_time_sec);
135
136         void set_metar( const char * metar );
137         const char * get_metar(void) const;
138         bool get_valid(void) const { return metar_valid; }
139         void set_enabled(bool _enabled) { enabled = _enabled; }
140         bool get_enabled(void) const { return enabled; }
141         void set_setup_winds_aloft(bool _setup_winds_aloft) { setup_winds_aloft = _setup_winds_aloft; }
142         bool get_setup_winds_aloft(void) const { return setup_winds_aloft; }
143
144 private:
145         void bind();
146         void unbind();
147
148         SGSharedPtr<FGWindModulator> windModulator;
149         bool metar_valid;
150         bool enabled;
151         bool setup_winds_aloft;
152         bool first_update;
153         bool wind_interpolation_required;
154         string metar;
155         double metar_sealevel_temperature;
156         double metar_sealevel_dewpoint;
157         double interpolate_prop(const char * currentname, const char * requiredname, double dvalue);
158         double interpolate_val(double currentval, double requiredval, double dvalue);
159         const double MaxWindChangeKtsSec;                        // Max wind change in kts/sec
160         const double MaxVisChangePercentSec;            // Max visibility change in %/sec
161         const double MaxPressureChangeInHgSec;          // Max pressure change in InHg/sec
162         const double MaxTemperatureChangeDegcSec;       // Max temperature change in degc/s
163         const double MaxCloudAltitudeChangeFtSec;        // Max cloud altitude change in ft/s
164         const double MaxCloudThicknessChangeFtSec;      // Max cloud thickness change in ft/s
165         const double MaxCloudInterpolationHeightFt; // Max distance from aircraft to
166                                                                                                 // interpolate at. Any cloud
167                                                                                                 // changes above this height
168                                                                                                 // difference are not interpolated
169         const double MaxCloudInterpolationDeltaFt;      // Max difference in altitude to 
170                                                                                                 // interpolate. Any cloud changing height
171                                                                                                 // by more than this value is not 
172                                                                                                 // interpolated
173
174         SGSubsystem * _environmentCtrl;
175
176         SGPropertyNode_ptr metar_base_n;
177         SGPropertyNode_ptr station_id_n;
178         SGPropertyNode_ptr station_elevation_n;
179         SGPropertyNode_ptr min_visibility_n;
180         SGPropertyNode_ptr max_visibility_n;
181         SGPropertyNode_ptr base_wind_range_from_n;
182         SGPropertyNode_ptr base_wind_range_to_n;
183         SGPropertyNode_ptr base_wind_dir_n;
184         SGPropertyNode_ptr base_wind_speed_n;
185         SGPropertyNode_ptr gust_wind_speed_n;
186         SGPropertyNode_ptr temperature_n;
187         SGPropertyNode_ptr dewpoint_n;
188         SGPropertyNode_ptr humidity_n;
189         SGPropertyNode_ptr pressure_n;
190         SGPropertyNode_ptr clouds_n;
191         SGPropertyNode_ptr environment_clouds_n;
192         SGPropertyNode_ptr rain_n;
193         SGPropertyNode_ptr hail_n;
194         SGPropertyNode_ptr snow_n;
195         SGPropertyNode_ptr snow_cover_n;
196         SGPropertyNode_ptr ground_elevation_n;
197         SGPropertyNode_ptr longitude_n;
198         SGPropertyNode_ptr latitude_n;
199         SGPropertyNode_ptr magnetic_variation_n;
200
201         SGPropertyNode_ptr boundary_wind_speed_n;
202         SGPropertyNode_ptr boundary_wind_from_heading_n;
203         SGPropertyNode_ptr boundary_visibility_n;
204         SGPropertyNode_ptr boundary_sea_level_pressure_n;
205         SGPropertyNode_ptr boundary_sea_level_temperature_n;
206         SGPropertyNode_ptr boundary_sea_level_dewpoint_n;
207 private:
208
209 };
210
211 /*
212  * The subsyste to load real world weather
213  */
214 class FGMetarFetcher : public SGSubsystem
215 {
216 public:
217         FGMetarFetcher();
218         virtual ~FGMetarFetcher();
219
220         virtual void init ();
221         virtual void reinit ();
222         virtual void update (double delta_time_sec);
223
224 private:
225         friend class MetarThread;
226 #if defined(ENABLE_THREADS)
227         /**
228          * FIFO queue which holds a pointer to the metar requests.
229          */
230         SGBlockingQueue <string> request_queue;
231
232         OpenThreads::Thread * metar_thread;
233 #endif
234
235         void fetch( const string & id );
236
237         SGPropertyNode_ptr enable_n;
238
239         SGPropertyNode_ptr longitude_n;
240         SGPropertyNode_ptr latitude_n;
241
242         SGPropertyNode_ptr proxy_host_n;
243         SGPropertyNode_ptr proxy_port_n;
244         SGPropertyNode_ptr proxy_auth_n;
245         SGPropertyNode_ptr max_age_n;
246
247         SGPropertyNode_ptr output_n;
248
249         string current_airport_id;
250         double fetch_timer;
251         double search_timer;
252         double error_timer;
253
254         long _stale_count;
255         long _error_count;
256         bool enabled;
257 };
258
259
260 #endif // _ENVIRONMENT_CTRL_HXX