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
26 #include <simgear/compiler.h>
27 #include <simgear/structure/subsystem_mgr.hxx>
29 #if defined(ENABLE_THREADS)
30 # include <OpenThreads/Thread>
31 # include <simgear/threads/SGQueue.hxx>
37 #include "Navaids/positioned.hxx"
38 #include "Environment/environment.hxx"
46 * Interface to control environment information for a specific location.
48 class FGEnvironmentCtrl : public SGSubsystem
54 virtual ~FGEnvironmentCtrl ();
56 virtual void setEnvironment (FGEnvironment * environment);
58 virtual const FGEnvironment * getEnvironment () const { return _environment; }
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);
65 virtual double getLongitudeDeg () const { return _lon_deg; }
66 virtual double getLatitudeDeg () const { return _lat_deg; }
67 virtual double getElevationFt () const { return _elev_ft; }
71 FGEnvironment * _environment;
81 * Environment controller using user-supplied parameters.
83 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
86 FGUserDefEnvironmentCtrl ();
87 virtual ~FGUserDefEnvironmentCtrl ();
90 virtual void update (double dt);
94 SGPropertyNode_ptr _base_wind_speed_node;
95 SGPropertyNode_ptr _gust_wind_speed_node;
97 double _current_wind_speed_kt;
98 double _delta_wind_speed_kt;
105 * Interplation controller using user-supplied parameters.
107 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
110 FGInterpolateEnvironmentCtrl ();
111 virtual ~FGInterpolateEnvironmentCtrl ();
113 virtual void init ();
114 virtual void reinit ();
115 virtual void update (double delta_time_sec);
121 FGEnvironment environment;
122 bool operator< (const bucket &b) const;
123 // LessThan predicate for bucket pointers.
124 static bool lessThan(bucket *a, bucket *b);
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);
131 FGEnvironment env1, env2; // temporaries
133 std::vector<bucket *> _boundary_table;
134 std::vector<bucket *> _aloft_table;
138 // A convenience wrapper around FGMetar
139 struct FGMetarResult {
147 * Interplation controller using the FGMetar class
149 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
152 FGMetarEnvironmentCtrl ();
153 virtual ~FGMetarEnvironmentCtrl ();
155 virtual void init ();
156 virtual void reinit ();
157 virtual void update (double delta_time_sec);
158 virtual void setEnvironment (FGEnvironment * environment);
161 FGInterpolateEnvironmentCtrl *env;
164 float station_elevation_ft;
165 float search_interval_sec;
166 float same_station_interval_sec;
167 float search_elapsed;
169 float interpolate_elapsed;
170 FGPositionedRef last_apt;
171 SGPropertyNode_ptr proxy_host;
172 SGPropertyNode_ptr proxy_port;
173 SGPropertyNode_ptr proxy_auth;
174 SGPropertyNode_ptr metar_max_age;
176 FGMetarResult fetch_data(FGAirport* apt);
177 virtual void update_metar_properties( const FGMetar *m );
178 void update_env_config();
179 double interpolate_prop(const char * currentname, const char * requiredname, double dvalue);
180 double interpolate_val(double currentval, double requiredval, double dvalue);
181 const double EnvironmentUpdatePeriodSec; // Seconds between interpolations
182 const double MaxWindChangeKtsSec; // Max wind change in kts/sec
183 const double MaxVisChangePercentSec; // Max visibility change in %/sec
184 const double MaxPressureChangeInHgSec; // Max pressure change in InHg/sec
185 const double MaxCloudAltitudeChangeFtSec; // Max cloud altitude change in ft/s
186 const double MaxCloudThicknessChangeFtSec; // Max cloud thickness change in ft/s
187 const double MaxCloudInterpolationHeightFt; // Max distance from aircraft to
188 // interpolate at. Any cloud
189 // changes above this height
190 // difference are not interpolated
191 const double MaxCloudInterpolationDeltaFt; // Max difference in altitude to
192 // interpolate. Any cloud changing height
193 // by more than this value is not
198 #if defined(ENABLE_THREADS)
200 * FIFO queue which holds a pointer to the fetched metar data.
202 SGBlockingQueue <FGAirport*> request_queue;
205 * FIFO queue which holds a pointer to the fetched metar data.
207 SGLockedQueue < FGMetarResult > result_queue;
210 * FIFO queue which holds a pointer to the fetched metar data.
212 std::queue <FGAirport*> request_queue;
215 * FIFO queue which holds a pointer to the fetched metar data.
217 std::queue < FGMetarResult > result_queue;
220 #if defined(ENABLE_THREADS)
222 * This class represents the thread of execution responsible for
223 * fetching the metar data.
225 class MetarThread : public OpenThreads::Thread
228 MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
232 * Fetche the metar data from the NOAA.
237 FGMetarEnvironmentCtrl *fetcher;
242 MetarThread( const MetarThread& );
243 MetarThread& operator=( const MetarThread& );
246 friend class MetarThread;
249 * Metar data fetching thread.
254 #endif // ENABLE_THREADS
262 #endif // _ENVIRONMENT_CTRL_HXX