]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_ctrl.hxx
Merge branch 'vivian/cullsetting'
[flightgear.git] / src / Environment / environment_ctrl.hxx
index 389419aa10d4554c7893828c669c215eeb2295b8..aec1442cb375f56f142facb786ac5d46865d60c9 100644 (file)
 
 #include <simgear/compiler.h>
 #include <simgear/structure/subsystem_mgr.hxx>
-#include <simgear/environment/metar.hxx>
 
 #if defined(ENABLE_THREADS)
-# include <simgear/threads/SGThread.hxx>
+# include <OpenThreads/Thread>
 # include <simgear/threads/SGQueue.hxx>
 #endif
 
-#ifdef SG_HAVE_STD_INCLUDES
-#  include <cmath>
-#else
-#  include <math.h>
-#endif
-
 #include <queue>
 #include <vector>
 
-SG_USING_STD(queue);
-SG_USING_STD(vector);
+#include <Navaids/positioned.hxx>
+#include <Environment/environment.hxx>
+#include "fgwind.hxx"
 
+// forward decls
 class SGPropertyNode;
+class SGSampleGroup;
+class FGMetar;
 
-#include "environment.hxx"
-#include "fgmetar.hxx"
-
-
-\f
 /**
  * Interface to control environment information for a specific location.
  */
@@ -59,204 +51,210 @@ class FGEnvironmentCtrl : public SGSubsystem
 
 public:
 
-  FGEnvironmentCtrl ();
-  virtual ~FGEnvironmentCtrl ();
+       FGEnvironmentCtrl ();
+       virtual ~FGEnvironmentCtrl ();
 
-  virtual void setEnvironment (FGEnvironment * environment);
+       virtual void setEnvironment (FGEnvironment * environment);
 
-  virtual const FGEnvironment * getEnvironment () const { return _environment; }
+       virtual const FGEnvironment * getEnvironment () const { return _environment; }
 
-  virtual void setLongitudeDeg (double lon_deg);
-  virtual void setLatitudeDeg (double lat_deg);
-  virtual void setElevationFt (double elev_ft);
-  virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
+       virtual void setLongitudeDeg (double lon_deg);
+       virtual void setLatitudeDeg (double lat_deg);
+       virtual void setElevationFt (double elev_ft);
+       virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
 
-  virtual double getLongitudeDeg () const { return _lon_deg; }
-  virtual double getLatitudeDeg () const { return _lat_deg; }
-  virtual double getElevationFt () const { return _elev_ft; }
+       virtual double getLongitudeDeg () const { return _lon_deg; }
+       virtual double getLatitudeDeg () const { return _lat_deg; }
+       virtual double getElevationFt () const { return _elev_ft; }
 
 protected:
 
-  FGEnvironment * _environment;
-  double _lon_deg;
-  double _lat_deg;
-  double _elev_ft;
+       FGEnvironment * _environment;
+       double _lon_deg;
+       double _lat_deg;
+       double _elev_ft;
 
 };
 
 
 \f
 /**
- * Environment controller using user-supplied parameters.
+ * Interplation controller using user-supplied parameters.
  */
-class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
+class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
 {
 public:
-  FGUserDefEnvironmentCtrl ();
-  virtual ~FGUserDefEnvironmentCtrl ();
-
-  virtual void init ();
-  virtual void update (double dt);
+       FGInterpolateEnvironmentCtrl ();
+       virtual ~FGInterpolateEnvironmentCtrl ();
+       
+       virtual void init ();
+       virtual void reinit ();
+       virtual void update (double delta_time_sec);
 
 private:
-
-  SGPropertyNode * _base_wind_speed_node;
-  SGPropertyNode * _gust_wind_speed_node;
-
-  double _current_wind_speed_kt;
-  double _delta_wind_speed_kt;
-
+       
+       struct bucket {
+               double altitude_ft;
+               FGEnvironment environment;
+               bool operator< (const bucket &b) const;
+               // LessThan predicate for bucket pointers.
+               static bool lessThan(bucket *a, bucket *b);
+       };
+
+       void read_table (const SGPropertyNode * node, std::vector<bucket *> &table);
+       void do_interpolate (std::vector<bucket *> &table, double altitude_ft,
+                                                FGEnvironment * environment);
+
+       FGEnvironment env1, env2;        // temporaries
+
+       std::vector<bucket *> _boundary_table;
+       std::vector<bucket *> _aloft_table;
+
+       SGPropertyNode_ptr altitude_n;
+       SGPropertyNode_ptr altitude_agl_n;
+       SGPropertyNode_ptr boundary_transition_n;
+       SGPropertyNode_ptr boundary_n;
+       SGPropertyNode_ptr aloft_n;
 };
 
 
 \f
 /**
- * Interplation controller using user-supplied parameters.
+ * Interplation controller using the FGMetar class
  */
-class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
+
+class FGMetarCtrl : public SGSubsystem
 {
 public:
-    FGInterpolateEnvironmentCtrl ();
-    virtual ~FGInterpolateEnvironmentCtrl ();
-    
-    virtual void init ();
-    virtual void reinit ();
-    virtual void update (double delta_time_sec);
+       FGMetarCtrl (SGSubsystem * environmentCtrl);
+       virtual ~FGMetarCtrl ();
 
-private:
-    
-    struct bucket {
-        double altitude_ft;
-        FGEnvironment environment;
-        bool operator< (const bucket &b) const;
-    };
-
-    void read_table (const SGPropertyNode * node, vector<bucket *> &table);
-    void do_interpolate (vector<bucket *> &table, double altitude_ft,
-                         FGEnvironment * environment);
+       virtual void init ();
+       virtual void reinit ();
+       virtual void update (double delta_time_sec);
 
-    FGEnvironment env1, env2;   // temporaries
-
-    vector<bucket *> _boundary_table;
-    vector<bucket *> _aloft_table;
-};
+       void set_metar( const char * metar );
+       const char * get_metar(void) const;
+       bool get_valid(void) const { return metar_valid; }
+       void set_enabled(bool _enabled) { enabled = _enabled; }
+       bool get_enabled(void) const { return enabled; }
+       void set_setup_winds_aloft(bool _setup_winds_aloft) { setup_winds_aloft = _setup_winds_aloft; }
+       bool get_setup_winds_aloft(void) const { return setup_winds_aloft; }
 
+private:
+       void bind();
+       void unbind();
+
+       SGSharedPtr<FGWindModulator> windModulator;
+       bool metar_valid;
+       bool enabled;
+       bool setup_winds_aloft;
+       bool first_update;
+       bool wind_interpolation_required;
+       string metar;
+       double metar_sealevel_temperature;
+       double metar_sealevel_dewpoint;
+       double interpolate_prop(const char * currentname, const char * requiredname, double dvalue);
+       double interpolate_val(double currentval, double requiredval, double dvalue);
+       const double MaxWindChangeKtsSec;                        // Max wind change in kts/sec
+       const double MaxVisChangePercentSec;            // Max visibility change in %/sec
+       const double MaxPressureChangeInHgSec;          // Max pressure change in InHg/sec
+       const double MaxTemperatureChangeDegcSec;       // Max temperature change in degc/s
+       const double MaxCloudAltitudeChangeFtSec;        // Max cloud altitude change in ft/s
+       const double MaxCloudThicknessChangeFtSec;      // Max cloud thickness change in ft/s
+       const double MaxCloudInterpolationHeightFt; // Max distance from aircraft to
+                                                                                               // interpolate at. Any cloud
+                                                                                               // changes above this height
+                                                                                               // difference are not interpolated
+       const double MaxCloudInterpolationDeltaFt;      // Max difference in altitude to 
+                                                                                               // interpolate. Any cloud changing height
+                                                                                               // by more than this value is not 
+                                                                                               // interpolated
+
+       SGSubsystem * _environmentCtrl;
+
+       SGPropertyNode_ptr metar_base_n;
+       SGPropertyNode_ptr station_id_n;
+       SGPropertyNode_ptr station_elevation_n;
+       SGPropertyNode_ptr min_visibility_n;
+       SGPropertyNode_ptr max_visibility_n;
+       SGPropertyNode_ptr base_wind_range_from_n;
+       SGPropertyNode_ptr base_wind_range_to_n;
+       SGPropertyNode_ptr base_wind_dir_n;
+       SGPropertyNode_ptr base_wind_speed_n;
+       SGPropertyNode_ptr gust_wind_speed_n;
+       SGPropertyNode_ptr temperature_n;
+       SGPropertyNode_ptr dewpoint_n;
+       SGPropertyNode_ptr humidity_n;
+       SGPropertyNode_ptr pressure_n;
+       SGPropertyNode_ptr clouds_n;
+       SGPropertyNode_ptr environment_clouds_n;
+       SGPropertyNode_ptr rain_n;
+       SGPropertyNode_ptr hail_n;
+       SGPropertyNode_ptr snow_n;
+       SGPropertyNode_ptr snow_cover_n;
+       SGPropertyNode_ptr ground_elevation_n;
+       SGPropertyNode_ptr longitude_n;
+       SGPropertyNode_ptr latitude_n;
+       SGPropertyNode_ptr magnetic_variation_n;
+
+       SGPropertyNode_ptr boundary_wind_speed_n;
+       SGPropertyNode_ptr boundary_wind_from_heading_n;
+       SGPropertyNode_ptr boundary_visibility_n;
+       SGPropertyNode_ptr boundary_sea_level_pressure_n;
+       SGPropertyNode_ptr boundary_sea_level_temperature_n;
+       SGPropertyNode_ptr boundary_sea_level_dewpoint_n;
+private:
 
-// A convenience wrapper around FGMetar
-struct FGMetarResult {
-    string icao;
-    FGMetar *m;
 };
 
-
-\f
-/**
- * Interplation controller using the FGMetar class
+/*
+ * The subsyste to load real world weather
  */
-class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
+class FGMetarFetcher : public SGSubsystem
 {
 public:
-    FGMetarEnvironmentCtrl ();
-    virtual ~FGMetarEnvironmentCtrl ();
-
-    virtual void init ();
-    virtual void reinit ();
-    virtual void update (double delta_time_sec);
-    virtual void setEnvironment (FGEnvironment * environment);
+       FGMetarFetcher();
+       virtual ~FGMetarFetcher();
 
-private:
-    FGInterpolateEnvironmentCtrl *env;
-
-    string _icao;
-    float station_elevation_ft;
-    float search_interval_sec;
-    float same_station_interval_sec;
-    float search_elapsed;
-    float fetch_elapsed;
-    const FGAirport *last_apt;
-    SGPropertyNode *proxy_host;
-    SGPropertyNode *proxy_port;
-    SGPropertyNode *proxy_auth;
-    SGPropertyNode *metar_max_age;
-
-    FGMetarResult fetch_data( const string &icao );
-    virtual void update_metar_properties( const FGMetar *m );
-    void update_env_config();
+       virtual void init ();
+       virtual void reinit ();
+       virtual void update (double delta_time_sec);
 
 private:
-
+       friend class MetarThread;
 #if defined(ENABLE_THREADS)
-    /**
-     * FIFO queue which holds a pointer to the fetched metar data.
-     */
-    SGBlockingQueue < string > request_queue;
-
-    /**
-     * FIFO queue which holds a pointer to the fetched metar data.
-     */
-    SGBlockingQueue < FGMetarResult > result_queue;
-#else
-    /**
-     * FIFO queue which holds a pointer to the fetched metar data.
-     */
-    queue < string > request_queue;
-
-    /**
-     * FIFO queue which holds a pointer to the fetched metar data.
-     */
-    queue < FGMetarResult > result_queue;
+       /**
+        * FIFO queue which holds a pointer to the metar requests.
+        */
+       SGBlockingQueue <string> request_queue;
+
+       OpenThreads::Thread * metar_thread;
 #endif
 
-#if defined(ENABLE_THREADS)
-    /**
-     * This class represents the thread of execution responsible for
-     * fetching the metar data.
-     */
-    class MetarThread : public SGThread
-    {
-    public:
-        MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
-        ~MetarThread() {}
-
-        /**
-         * Fetche the metar data from the NOAA.
-         */
-        void run();
-
-    private:
-        FGMetarEnvironmentCtrl *fetcher;
-
-    private:
-        // not implemented.
-        MetarThread();
-        MetarThread( const MetarThread& );
-        MetarThread& operator=( const MetarThread& );
-    };
-
-    friend class MetarThread;
-
-    /**
-     * Metar data fetching thread.
-     */
-    MetarThread* thread;
-
-    /**
-     * Lock and synchronize access to metar queue.
-     */
-    SGMutex mutex;
-    SGPthreadCond metar_cond;
-
-    /**
-     * Thread cleanup handler.
-     */
-    friend void metar_cleanup_handler( void* );
-
-#endif // ENABLE_THREADS
-
-    int _error_count;
-    int _stale_count;
-    double _dt;
-    double _error_dt;
+       void fetch( const string & id );
+
+       SGPropertyNode_ptr enable_n;
+
+       SGPropertyNode_ptr longitude_n;
+       SGPropertyNode_ptr latitude_n;
+
+       SGPropertyNode_ptr proxy_host_n;
+       SGPropertyNode_ptr proxy_port_n;
+       SGPropertyNode_ptr proxy_auth_n;
+       SGPropertyNode_ptr max_age_n;
+
+       SGPropertyNode_ptr output_n;
+
+       string current_airport_id;
+       double fetch_timer;
+       double search_timer;
+       double error_timer;
+
+       long _stale_count;
+       long _error_count;
+       bool enabled;
 };
 
+
 #endif // _ENVIRONMENT_CTRL_HXX