1 // environment-ctrl.hxx -- controller for environment information.
3 // Written by David Megginson, started May 2002.
5 // Copyright (C) 2002 David Megginson - david@megginson.com
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.
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.
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.
23 #ifndef _ENVIRONMENT_CTRL_HXX
24 #define _ENVIRONMENT_CTRL_HXX
30 #include <simgear/compiler.h>
31 #include <simgear/structure/subsystem_mgr.hxx>
32 #include <simgear/environment/metar.hxx>
34 #if defined(ENABLE_THREADS)
35 # include <simgear/threads/SGThread.hxx>
36 # include <simgear/threads/SGQueue.hxx>
39 #ifdef SG_HAVE_STD_INCLUDES
54 #include "environment.hxx"
55 #include "fgmetar.hxx"
60 * Interface to control environment information for a specific location.
62 class FGEnvironmentCtrl : public SGSubsystem
68 virtual ~FGEnvironmentCtrl ();
70 virtual void setEnvironment (FGEnvironment * environment);
72 virtual const FGEnvironment * getEnvironment () const { return _environment; }
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);
79 virtual double getLongitudeDeg () const { return _lon_deg; }
80 virtual double getLatitudeDeg () const { return _lat_deg; }
81 virtual double getElevationFt () const { return _elev_ft; }
85 FGEnvironment * _environment;
95 * Environment controller using user-supplied parameters.
97 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
100 FGUserDefEnvironmentCtrl ();
101 virtual ~FGUserDefEnvironmentCtrl ();
103 virtual void init ();
104 virtual void update (double dt);
108 SGPropertyNode_ptr _base_wind_speed_node;
109 SGPropertyNode_ptr _gust_wind_speed_node;
111 double _current_wind_speed_kt;
112 double _delta_wind_speed_kt;
119 * Interplation controller using user-supplied parameters.
121 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
124 FGInterpolateEnvironmentCtrl ();
125 virtual ~FGInterpolateEnvironmentCtrl ();
127 virtual void init ();
128 virtual void reinit ();
129 virtual void update (double delta_time_sec);
135 FGEnvironment environment;
136 bool operator< (const bucket &b) const;
137 // LessThan predicate for bucket pointers.
138 static bool lessThan(bucket *a, bucket *b);
141 void read_table (const SGPropertyNode * node, vector<bucket *> &table);
142 void do_interpolate (vector<bucket *> &table, double altitude_ft,
143 FGEnvironment * environment);
145 FGEnvironment env1, env2; // temporaries
147 vector<bucket *> _boundary_table;
148 vector<bucket *> _aloft_table;
152 // A convenience wrapper around FGMetar
153 struct FGMetarResult {
161 * Interplation controller using the FGMetar class
163 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
166 FGMetarEnvironmentCtrl ();
167 virtual ~FGMetarEnvironmentCtrl ();
169 virtual void init ();
170 virtual void reinit ();
171 virtual void update (double delta_time_sec);
172 virtual void setEnvironment (FGEnvironment * environment);
175 FGInterpolateEnvironmentCtrl *env;
179 float station_elevation_ft;
180 float search_interval_sec;
181 float same_station_interval_sec;
182 float search_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;
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
209 #if defined(ENABLE_THREADS)
211 * FIFO queue which holds a pointer to the fetched metar data.
213 SGBlockingQueue < string > request_queue;
216 * FIFO queue which holds a pointer to the fetched metar data.
218 SGLockedQueue < FGMetarResult > result_queue;
221 * FIFO queue which holds a pointer to the fetched metar data.
223 queue < string > request_queue;
226 * FIFO queue which holds a pointer to the fetched metar data.
228 queue < FGMetarResult > result_queue;
231 #if defined(ENABLE_THREADS)
233 * This class represents the thread of execution responsible for
234 * fetching the metar data.
236 class MetarThread : public SGThread
239 MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
243 * Fetche the metar data from the NOAA.
248 FGMetarEnvironmentCtrl *fetcher;
253 MetarThread( const MetarThread& );
254 MetarThread& operator=( const MetarThread& );
257 friend class MetarThread;
260 * Metar data fetching thread.
265 #endif // ENABLE_THREADS
273 #endif // _ENVIRONMENT_CTRL_HXX