]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
From Benoit Laniel: replace SG threading constructs with OpenThreads
[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 #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         // LessThan predicate for bucket pointers.
138         static bool lessThan(bucket *a, bucket *b);
139     };
140
141     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
142     void do_interpolate (vector<bucket *> &table, double altitude_ft,
143                          FGEnvironment * environment);
144
145     FGEnvironment env1, env2;   // temporaries
146
147     vector<bucket *> _boundary_table;
148     vector<bucket *> _aloft_table;
149 };
150
151
152 // A convenience wrapper around FGMetar
153 struct FGMetarResult {
154     string icao;
155     FGMetar *m;
156 };
157
158
159 \f
160 /**
161  * Interplation controller using the FGMetar class
162  */
163 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
164 {
165 public:
166     FGMetarEnvironmentCtrl ();
167     virtual ~FGMetarEnvironmentCtrl ();
168
169     virtual void init ();
170     virtual void reinit ();
171     virtual void update (double delta_time_sec);
172     virtual void setEnvironment (FGEnvironment * environment);
173
174 private:
175     FGInterpolateEnvironmentCtrl *env;
176
177     string _icao;
178     bool metar_loaded;
179     float station_elevation_ft;
180     float search_interval_sec;
181     float same_station_interval_sec;
182     float search_elapsed;
183     float fetch_elapsed;
184     float interpolate_elapsed;
185     const FGAirport *last_apt;
186     SGPropertyNode_ptr proxy_host;
187     SGPropertyNode_ptr proxy_port;
188     SGPropertyNode_ptr proxy_auth;
189     SGPropertyNode_ptr metar_max_age;
190
191     FGMetarResult fetch_data( const string &icao );
192     virtual void update_metar_properties( const FGMetar *m );
193     void update_env_config();
194     double interpolate_prop(const char * currentname, const char * requiredname, double dvalue);
195     double interpolate_val(double currentval, double requiredval, double dvalue);
196     const double EnvironmentUpdatePeriodSec;    // Seconds between interpolations
197     const double MaxWindChangeKtsSec;           // Max wind change in kts/sec
198     const double MaxVisChangePercentSec;        // Max visibility change in %/sec
199     const double MaxPressureChangeInHgSec;      // Max pressure change in InHg/sec
200     const double MaxCloudAltitudeChangeFtSec;   // Max cloud altitude change in ft/s
201     const double MaxCloudThicknessChangeFtSec;  // Max cloud thickness change in ft/s
202     const double MaxCloudInterpolationHeightFt; // Max distance from aircraft to
203                                                 // interpolate at. Any cloud
204                                                 // changes above this height
205                                                 // difference are not interpolated
206
207 private:
208
209 #if defined(ENABLE_THREADS)
210     /**
211      * FIFO queue which holds a pointer to the fetched metar data.
212      */
213     SGBlockingQueue < string > request_queue;
214
215     /**
216      * FIFO queue which holds a pointer to the fetched metar data.
217      */
218     SGLockedQueue < FGMetarResult > result_queue;
219 #else
220     /**
221      * FIFO queue which holds a pointer to the fetched metar data.
222      */
223     queue < string > request_queue;
224
225     /**
226      * FIFO queue which holds a pointer to the fetched metar data.
227      */
228     queue < FGMetarResult > result_queue;
229 #endif
230
231 #if defined(ENABLE_THREADS)
232     /**
233      * This class represents the thread of execution responsible for
234      * fetching the metar data.
235      */
236     class MetarThread : public OpenThreads::Thread
237     {
238     public:
239         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
240         ~MetarThread() {}
241
242         /**
243          * Fetche the metar data from the NOAA.
244          */
245         void run();
246
247     private:
248         FGMetarEnvironmentCtrl *fetcher;
249
250     private:
251         // not implemented.
252         MetarThread();
253         MetarThread( const MetarThread& );
254         MetarThread& operator=( const MetarThread& );
255     };
256
257     friend class MetarThread;
258
259     /**
260      * Metar data fetching thread.
261      */
262     MetarThread* thread;
263
264     void thread_stop();
265 #endif // ENABLE_THREADS
266
267     int _error_count;
268     int _stale_count;
269     double _dt;
270     double _error_dt;
271 };
272
273 #endif // _ENVIRONMENT_CTRL_HXX