]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
Durk Talsma:
[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., 675 Mass Ave, Cambridge, MA 02139, 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 #include <simgear/environment/metar.hxx>
29
30 #if defined(ENABLE_THREADS) && ENABLE_THREADS
31 # include <simgear/threads/SGThread.hxx>
32 # include <simgear/threads/SGQueue.hxx>
33 #endif
34
35 #ifdef SG_HAVE_STD_INCLUDES
36 #  include <cmath>
37 #else
38 #  include <math.h>
39 #endif
40
41 #include <queue>
42 #include <vector>
43
44 SG_USING_STD(queue);
45 SG_USING_STD(vector);
46
47 class SGPropertyNode;
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 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 * _base_wind_speed_node;
104   SGPropertyNode * _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     };
133
134     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
135     void do_interpolate (vector<bucket *> &table, double altitude_ft,
136                          FGEnvironment * environment);
137
138     FGEnvironment env1, env2;   // temporaries
139
140     vector<bucket *> _boundary_table;
141     vector<bucket *> _aloft_table;
142 };
143
144
145 // A convenience wrapper around FGMetar
146 struct FGMetarResult {
147     string icao;
148     FGMetar *m;
149 };
150
151
152 \f
153 /**
154  * Interplation controller using the FGMetar class
155  */
156 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
157 {
158 public:
159     FGMetarEnvironmentCtrl ();
160     virtual ~FGMetarEnvironmentCtrl ();
161
162     virtual void init ();
163     virtual void reinit ();
164     virtual void update (double delta_time_sec);
165     virtual void setEnvironment (FGEnvironment * environment);
166
167 private:
168     FGInterpolateEnvironmentCtrl *env;
169
170     string _icao;
171     float station_elevation_ft;
172     float search_interval_sec;
173     float same_station_interval_sec;
174     float search_elapsed;
175     float fetch_elapsed;
176     FGAirport last_apt;
177     SGPropertyNode *proxy_host;
178     SGPropertyNode *proxy_port;
179     SGPropertyNode *proxy_auth;
180
181     FGMetarResult fetch_data( const string &icao );
182     virtual void update_metar_properties( FGMetar *m );
183     void update_env_config();
184
185 private:
186
187 #if defined(ENABLE_THREADS) && ENABLE_THREADS
188     /**
189      * FIFO queue which holds a pointer to the fetched metar data.
190      */
191     SGBlockingQueue < string > request_queue;
192
193     /**
194      * FIFO queue which holds a pointer to the fetched metar data.
195      */
196     SGBlockingQueue < FGMetarResult > result_queue;
197 #else
198     /**
199      * FIFO queue which holds a pointer to the fetched metar data.
200      */
201     queue < string > request_queue;
202
203     /**
204      * FIFO queue which holds a pointer to the fetched metar data.
205      */
206     queue < FGMetarResult > result_queue;
207 #endif
208
209 #if defined(ENABLE_THREADS) && ENABLE_THREADS
210     /**
211      * This class represents the thread of execution responsible for
212      * fetching the metar data.
213      */
214     class MetarThread : public SGThread
215     {
216     public:
217         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
218         ~MetarThread() {}
219
220         /**
221          * Fetche the metar data from the NOAA.
222          */
223         void run();
224
225     private:
226         FGMetarEnvironmentCtrl *fetcher;
227
228     private:
229         // not implemented.
230         MetarThread();
231         MetarThread( const MetarThread& );
232         MetarThread& operator=( const MetarThread& );
233     };
234
235     friend class MetarThread;
236
237     /**
238      * Metar data fetching thread.
239      */
240     MetarThread* thread;
241
242     /**
243      * Lock and synchronize access to metar queue.
244      */
245     SGMutex mutex;
246     SGPthreadCond metar_cond;
247
248     /**
249      * Thread cleanup handler.
250      */
251     friend void metar_cleanup_handler( void* );
252
253 #endif // ENABLE_THREADS
254
255     int _error_count;
256     double _dt;
257     double _error_dt;
258 };
259
260 #endif // _ENVIRONMENT_CTRL_HXX