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 <OpenThreads/Thread>
36 # include <simgear/threads/SGQueue.hxx>
49 #include "environment.hxx"
50 #include "fgmetar.hxx"
55 * Interface to control environment information for a specific location.
57 class FGEnvironmentCtrl : public SGSubsystem
63 virtual ~FGEnvironmentCtrl ();
65 virtual void setEnvironment (FGEnvironment * environment);
67 virtual const FGEnvironment * getEnvironment () const { return _environment; }
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);
74 virtual double getLongitudeDeg () const { return _lon_deg; }
75 virtual double getLatitudeDeg () const { return _lat_deg; }
76 virtual double getElevationFt () const { return _elev_ft; }
80 FGEnvironment * _environment;
90 * Environment controller using user-supplied parameters.
92 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
95 FGUserDefEnvironmentCtrl ();
96 virtual ~FGUserDefEnvironmentCtrl ();
99 virtual void update (double dt);
103 SGPropertyNode_ptr _base_wind_speed_node;
104 SGPropertyNode_ptr _gust_wind_speed_node;
106 double _current_wind_speed_kt;
107 double _delta_wind_speed_kt;
114 * Interplation controller using user-supplied parameters.
116 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
119 FGInterpolateEnvironmentCtrl ();
120 virtual ~FGInterpolateEnvironmentCtrl ();
122 virtual void init ();
123 virtual void reinit ();
124 virtual void update (double delta_time_sec);
130 FGEnvironment environment;
131 bool operator< (const bucket &b) const;
132 // LessThan predicate for bucket pointers.
133 static bool lessThan(bucket *a, bucket *b);
136 void read_table (const SGPropertyNode * node, vector<bucket *> &table);
137 void do_interpolate (vector<bucket *> &table, double altitude_ft,
138 FGEnvironment * environment);
140 FGEnvironment env1, env2; // temporaries
142 vector<bucket *> _boundary_table;
143 vector<bucket *> _aloft_table;
147 // A convenience wrapper around FGMetar
148 struct FGMetarResult {
156 * Interplation controller using the FGMetar class
158 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
161 FGMetarEnvironmentCtrl ();
162 virtual ~FGMetarEnvironmentCtrl ();
164 virtual void init ();
165 virtual void reinit ();
166 virtual void update (double delta_time_sec);
167 virtual void setEnvironment (FGEnvironment * environment);
170 FGInterpolateEnvironmentCtrl *env;
174 float station_elevation_ft;
175 float search_interval_sec;
176 float same_station_interval_sec;
177 float search_elapsed;
179 float interpolate_elapsed;
180 const FGAirport *last_apt;
181 SGPropertyNode_ptr proxy_host;
182 SGPropertyNode_ptr proxy_port;
183 SGPropertyNode_ptr proxy_auth;
184 SGPropertyNode_ptr metar_max_age;
186 FGMetarResult fetch_data( const string &icao );
187 virtual void update_metar_properties( const FGMetar *m );
188 void update_env_config();
189 double interpolate_prop(const char * currentname, const char * requiredname, double dvalue);
190 double interpolate_val(double currentval, double requiredval, double dvalue);
191 const double EnvironmentUpdatePeriodSec; // Seconds between interpolations
192 const double MaxWindChangeKtsSec; // Max wind change in kts/sec
193 const double MaxVisChangePercentSec; // Max visibility change in %/sec
194 const double MaxPressureChangeInHgSec; // Max pressure change in InHg/sec
195 const double MaxCloudAltitudeChangeFtSec; // Max cloud altitude change in ft/s
196 const double MaxCloudThicknessChangeFtSec; // Max cloud thickness change in ft/s
197 const double MaxCloudInterpolationHeightFt; // Max distance from aircraft to
198 // interpolate at. Any cloud
199 // changes above this height
200 // difference are not interpolated
201 const double MaxCloudInterpolationDeltaFt; // Max difference in altitude to
202 // interpolate. Any cloud changing height
203 // by more than this value is not
208 #if defined(ENABLE_THREADS)
210 * FIFO queue which holds a pointer to the fetched metar data.
212 SGBlockingQueue < string > request_queue;
215 * FIFO queue which holds a pointer to the fetched metar data.
217 SGLockedQueue < FGMetarResult > result_queue;
220 * FIFO queue which holds a pointer to the fetched metar data.
222 queue < string > request_queue;
225 * FIFO queue which holds a pointer to the fetched metar data.
227 queue < FGMetarResult > result_queue;
230 #if defined(ENABLE_THREADS)
232 * This class represents the thread of execution responsible for
233 * fetching the metar data.
235 class MetarThread : public OpenThreads::Thread
238 MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
242 * Fetche the metar data from the NOAA.
247 FGMetarEnvironmentCtrl *fetcher;
252 MetarThread( const MetarThread& );
253 MetarThread& operator=( const MetarThread& );
256 friend class MetarThread;
259 * Metar data fetching thread.
264 #endif // ENABLE_THREADS
272 #endif // _ENVIRONMENT_CTRL_HXX