]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
d06a7d108265eef8a1bb1ec503727f20f754687f
[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 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <simgear/compiler.h>
31 #include <simgear/structure/subsystem_mgr.hxx>
32 #include <simgear/environment/metar.hxx>
33
34 #if defined(ENABLE_THREADS)
35 # include <OpenThreads/Thread>
36 # include <simgear/threads/SGQueue.hxx>
37 #endif
38
39 #include <cmath>
40 #include <queue>
41 #include <vector>
42
43 using std::queue;
44 using std::vector;
45
46 class SGPropertyNode;
47 class FGAirport;
48
49 #include "environment.hxx"
50 #include "fgmetar.hxx"
51
52
53 \f
54 /**
55  * Interface to control environment information for a specific location.
56  */
57 class FGEnvironmentCtrl : public SGSubsystem
58 {
59
60 public:
61
62   FGEnvironmentCtrl ();
63   virtual ~FGEnvironmentCtrl ();
64
65   virtual void setEnvironment (FGEnvironment * environment);
66
67   virtual const FGEnvironment * getEnvironment () const { return _environment; }
68
69   virtual void setLongitudeDeg (double lon_deg);
70   virtual void setLatitudeDeg (double lat_deg);
71   virtual void setElevationFt (double elev_ft);
72   virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
73
74   virtual double getLongitudeDeg () const { return _lon_deg; }
75   virtual double getLatitudeDeg () const { return _lat_deg; }
76   virtual double getElevationFt () const { return _elev_ft; }
77
78 protected:
79
80   FGEnvironment * _environment;
81   double _lon_deg;
82   double _lat_deg;
83   double _elev_ft;
84
85 };
86
87
88 \f
89 /**
90  * Environment controller using user-supplied parameters.
91  */
92 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
93 {
94 public:
95   FGUserDefEnvironmentCtrl ();
96   virtual ~FGUserDefEnvironmentCtrl ();
97
98   virtual void init ();
99   virtual void update (double dt);
100
101 private:
102
103   SGPropertyNode_ptr _base_wind_speed_node;
104   SGPropertyNode_ptr _gust_wind_speed_node;
105
106   double _current_wind_speed_kt;
107   double _delta_wind_speed_kt;
108
109 };
110
111
112 \f
113 /**
114  * Interplation controller using user-supplied parameters.
115  */
116 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
117 {
118 public:
119     FGInterpolateEnvironmentCtrl ();
120     virtual ~FGInterpolateEnvironmentCtrl ();
121     
122     virtual void init ();
123     virtual void reinit ();
124     virtual void update (double delta_time_sec);
125
126 private:
127     
128     struct bucket {
129         double altitude_ft;
130         FGEnvironment environment;
131         bool operator< (const bucket &b) const;
132         // LessThan predicate for bucket pointers.
133         static bool lessThan(bucket *a, bucket *b);
134     };
135
136     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
137     void do_interpolate (vector<bucket *> &table, double altitude_ft,
138                          FGEnvironment * environment);
139
140     FGEnvironment env1, env2;   // temporaries
141
142     vector<bucket *> _boundary_table;
143     vector<bucket *> _aloft_table;
144 };
145
146
147 // A convenience wrapper around FGMetar
148 struct FGMetarResult {
149     string icao;
150     FGMetar *m;
151 };
152
153
154 \f
155 /**
156  * Interplation controller using the FGMetar class
157  */
158 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
159 {
160 public:
161     FGMetarEnvironmentCtrl ();
162     virtual ~FGMetarEnvironmentCtrl ();
163
164     virtual void init ();
165     virtual void reinit ();
166     virtual void update (double delta_time_sec);
167     virtual void setEnvironment (FGEnvironment * environment);
168
169 private:
170     FGInterpolateEnvironmentCtrl *env;
171
172     string _icao;
173     bool metar_loaded;
174     float station_elevation_ft;
175     float search_interval_sec;
176     float same_station_interval_sec;
177     float search_elapsed;
178     float fetch_elapsed;
179     float interpolate_elapsed;
180     const FGAirport *last_apt;
181     SGPropertyNode_ptr proxy_host;
182     SGPropertyNode_ptr proxy_port;
183     SGPropertyNode_ptr proxy_auth;
184     SGPropertyNode_ptr metar_max_age;
185
186     FGMetarResult fetch_data( const string &icao );
187     virtual void update_metar_properties( const FGMetar *m );
188     void update_env_config();
189     double interpolate_prop(const char * currentname, const char * requiredname, double dvalue);
190     double interpolate_val(double currentval, double requiredval, double dvalue);
191     const double EnvironmentUpdatePeriodSec;    // Seconds between interpolations
192     const double MaxWindChangeKtsSec;           // Max wind change in kts/sec
193     const double MaxVisChangePercentSec;        // Max visibility change in %/sec
194     const double MaxPressureChangeInHgSec;      // Max pressure change in InHg/sec
195     const double MaxCloudAltitudeChangeFtSec;   // Max cloud altitude change in ft/s
196     const double MaxCloudThicknessChangeFtSec;  // Max cloud thickness change in ft/s
197     const double MaxCloudInterpolationHeightFt; // Max distance from aircraft to
198                                                 // interpolate at. Any cloud
199                                                 // changes above this height
200                                                 // difference are not interpolated
201
202 private:
203
204 #if defined(ENABLE_THREADS)
205     /**
206      * FIFO queue which holds a pointer to the fetched metar data.
207      */
208     SGBlockingQueue < string > request_queue;
209
210     /**
211      * FIFO queue which holds a pointer to the fetched metar data.
212      */
213     SGLockedQueue < FGMetarResult > result_queue;
214 #else
215     /**
216      * FIFO queue which holds a pointer to the fetched metar data.
217      */
218     queue < string > request_queue;
219
220     /**
221      * FIFO queue which holds a pointer to the fetched metar data.
222      */
223     queue < FGMetarResult > result_queue;
224 #endif
225
226 #if defined(ENABLE_THREADS)
227     /**
228      * This class represents the thread of execution responsible for
229      * fetching the metar data.
230      */
231     class MetarThread : public OpenThreads::Thread
232     {
233     public:
234         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
235         ~MetarThread() {}
236
237         /**
238          * Fetche the metar data from the NOAA.
239          */
240         void run();
241
242     private:
243         FGMetarEnvironmentCtrl *fetcher;
244
245     private:
246         // not implemented.
247         MetarThread();
248         MetarThread( const MetarThread& );
249         MetarThread& operator=( const MetarThread& );
250     };
251
252     friend class MetarThread;
253
254     /**
255      * Metar data fetching thread.
256      */
257     MetarThread* thread;
258
259     void thread_stop();
260 #endif // ENABLE_THREADS
261
262     int _error_count;
263     int _stale_count;
264     double _dt;
265     double _error_dt;
266 };
267
268 #endif // _ENVIRONMENT_CTRL_HXX