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