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