From 897f295988b446d50b099a51d81df20df88a3a60 Mon Sep 17 00:00:00 2001 From: ehofman Date: Sat, 21 Feb 2004 14:04:40 +0000 Subject: [PATCH] Encapsulate the interpolstion version of FGEnvironment and fix some bugs --- src/Environment/environment_ctrl.cxx | 120 ++++++++++++--------------- src/Environment/environment_ctrl.hxx | 19 +---- src/Environment/environment_mgr.cxx | 2 +- src/Main/options.cxx | 8 +- 4 files changed, 64 insertions(+), 85 deletions(-) diff --git a/src/Environment/environment_ctrl.cxx b/src/Environment/environment_ctrl.cxx index 3ded1f43b..2f08e9828 100644 --- a/src/Environment/environment_ctrl.cxx +++ b/src/Environment/environment_ctrl.cxx @@ -32,6 +32,9 @@ SG_USING_STD(sort); +// FIXME, from options.cxx +extern void fgSetupWind (double min_hdg, double max_hdg, double speed, double gust); + //////////////////////////////////////////////////////////////////////// @@ -310,7 +313,8 @@ FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const //////////////////////////////////////////////////////////////////////// FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl () - : _icao( strdup( fgGetString("/sim/presets/airport-id") ) ) + : env( new FGInterpolateEnvironmentCtrl ), + _icao( strdup( fgGetString("/sim/presets/airport-id") ) ) { } @@ -320,6 +324,9 @@ FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl () free(_icao); _icao = NULL; } + + delete env; + env = NULL; } @@ -331,30 +338,66 @@ FGMetarEnvironmentCtrl::init () _icao = NULL; } - read_table(_icao); - _base_wind_speed_node = - fgGetNode("/environment/metar/base-wind-range-from", true); - _gust_wind_speed_node = - fgGetNode("/environment/metar/gust-wind-speed-kt", true); + fetch_data(_icao); + fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"), + fgGetDouble("/environment/metar/base-wind-range-to"), + fgGetDouble("/environment/metar/base-wind-speed-kt"), + fgGetDouble("/environment/metar/gust-wind-speed-kt") ); fgSetDouble("/environment/visibility-m", fgGetDouble("/environment/metar/min-visibility-m")); fgSetDouble("/environment/temperature-degc", - fgGetDouble("/environment/metar/temperature_degc")); + fgGetDouble("/environment/metar/temperature-degc")); fgSetDouble("/environment/dewpoint-degc", fgGetDouble("/environment/metar/dewpoint-degc")); fgSetDouble("/environment/pressure-inhg", fgGetDouble("/environment/metar/pressure-inhg")); + + env->init(); } void FGMetarEnvironmentCtrl::reinit () { - init(); + if (_icao != NULL) { + free(_icao); + _icao = NULL; + } + + fetch_data(_icao); + fgSetupWind( fgGetDouble("/environment/metar/base-wind-range-from"), + fgGetDouble("/environment/metar/base-wind-range-to"), + fgGetDouble("/environment/metar/base-wind-speed-kt"), + fgGetDouble("/environment/metar/gust-wind-speed-kt") ); + + fgSetDouble("/environment/visibility-m", + fgGetDouble("/environment/metar/min-visibility-m")); + fgSetDouble("/environment/pressure-inhg", + fgGetDouble("/environment/metar/pressure-inhg")); + + // FIXME: The following seem to egt overriden? + fgSetDouble("/environment/temperature-degc", + fgGetDouble("/environment/metar/temperature-degc")); + fgSetDouble("/environment/dewpoint-degc", + fgGetDouble("/environment/metar/dewpoint-degc")); + + env->reinit(); } void -FGMetarEnvironmentCtrl::read_table (const char *icao) +FGMetarEnvironmentCtrl::update(double delta_time_sec) +{ + env->update(delta_time_sec); +} + +void +FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment) +{ + env->setEnvironment(environment); +} + +void +FGMetarEnvironmentCtrl::fetch_data (const char *icao) { char s[128]; double d, dt; @@ -381,8 +424,8 @@ FGMetarEnvironmentCtrl::read_table (const char *icao) SGMetarVisibility *dirvis = m->getDirVisibility(); for (i = 0; i < 8; i++, dirvis++) { - const char *min = "/environment/metar/visibility[%]/min-m"; - const char *max = "/environment/metar/visibility[%]/max-m"; + const char *min = "/environment/metar/visibility[%d]/min-m"; + const char *max = "/environment/metar/visibility[%d]/max-m"; char s[128]; d = dirvis->getVisibility_m(); @@ -420,7 +463,7 @@ FGMetarEnvironmentCtrl::read_table (const char *icao) m->getRelHumidity() ); } d = (d != SGMetarNaN) ? d : 15.0; - fgSetDouble("/environment/metar/temperature_degc", d); + fgSetDouble("/environment/metar/temperature-degc", d); d = m->getPressure_inHg(); d = (d != SGMetarNaN) ? d : 30.0; @@ -477,58 +520,5 @@ FGMetarEnvironmentCtrl::read_table (const char *icao) delete m; } -void -FGMetarEnvironmentCtrl::update (double delta_time_sec) -{ - double base_wind_speed = _base_wind_speed_node->getDoubleValue(); - double gust_wind_speed = _gust_wind_speed_node->getDoubleValue(); - - if (gust_wind_speed < base_wind_speed) { - gust_wind_speed = base_wind_speed; - _gust_wind_speed_node->setDoubleValue(gust_wind_speed); - } - - if (base_wind_speed == gust_wind_speed) { - _current_wind_speed_kt = base_wind_speed; - } else { - int rn = rand() % 128; - int sign = (_delta_wind_speed_kt < 0 ? -1 : 1); - double gust = _current_wind_speed_kt - base_wind_speed; - double incr = gust / 50; - - if (rn == 0) - _delta_wind_speed_kt = - _delta_wind_speed_kt; - else if (rn < 4) - _delta_wind_speed_kt -= incr * sign; - else if (rn < 16) - _delta_wind_speed_kt += incr * sign; - - _current_wind_speed_kt += _delta_wind_speed_kt; - - if (_current_wind_speed_kt < base_wind_speed) { - _current_wind_speed_kt = base_wind_speed; - _delta_wind_speed_kt = 0.01; - } else if (_current_wind_speed_kt > gust_wind_speed) { - _current_wind_speed_kt = gust_wind_speed; - _delta_wind_speed_kt = -0.01; - } - } - - if (_environment != 0) - _environment->set_wind_speed_kt(_current_wind_speed_kt); -} - -void -FGMetarEnvironmentCtrl::do_interpolate (vector &table, - double altitude_ft, - FGEnvironment * environment) -{ -} - -bool -FGMetarEnvironmentCtrl::bucket::operator< (const bucket &b) const -{ - return (altitude_ft < b.altitude_ft); -} // end of environment_ctrl.cxx diff --git a/src/Environment/environment_ctrl.hxx b/src/Environment/environment_ctrl.hxx index ca5c5eb8f..d22e30641 100644 --- a/src/Environment/environment_ctrl.hxx +++ b/src/Environment/environment_ctrl.hxx @@ -148,24 +148,13 @@ public: virtual void reinit (); virtual void update (double delta_time_sec); -private: + virtual void setEnvironment (FGEnvironment * environment); - struct bucket { - double altitude_ft; - FGEnvironment environment; - bool operator< (const bucket &b) const; - }; +private: + FGInterpolateEnvironmentCtrl *env; char *_icao; - SGPropertyNode * _base_wind_speed_node; - SGPropertyNode * _gust_wind_speed_node; - - double _current_wind_speed_kt; - double _delta_wind_speed_kt; - - void read_table (const char *icao); - void do_interpolate (vector &table, double altitude_ft, - FGEnvironment * environment); + void fetch_data (const char *icao); }; #endif // _ENVIRONMENT_CTRL_HXX diff --git a/src/Environment/environment_mgr.cxx b/src/Environment/environment_mgr.cxx index 0a692c2d6..172c41f1c 100644 --- a/src/Environment/environment_mgr.cxx +++ b/src/Environment/environment_mgr.cxx @@ -36,7 +36,7 @@ FGEnvironmentMgr::FGEnvironmentMgr () : _environment(new FGEnvironment) { - if (fgGetBool("/environment/params/use-real-wether-fetch") == true) + if (fgGetBool("/environment/params/real-world-weather-fetch") == true) _controller = new FGMetarEnvironmentCtrl; else _controller = new FGInterpolateEnvironmentCtrl; diff --git a/src/Main/options.cxx b/src/Main/options.cxx index 1d76c20bd..17611685d 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -535,8 +535,8 @@ add_channel( const string& type, const string& channel_str ) { } -static void -setup_wind (double min_hdg, double max_hdg, double speed, double gust) +void +fgSetupWind (double min_hdg, double max_hdg, double speed, double gust) { // Initialize to a reasonable state fgDefaultWeatherValue("wind-from-heading-deg", min_hdg); @@ -1026,7 +1026,7 @@ fgOptRandomWind( const char *arg ) double max_hdg = min_hdg + (20 - sqrt(sg_random() * 400)); double speed = sg_random() * sg_random() * 40; double gust = speed + (10 - sqrt(sg_random() * 100)); - setup_wind(min_hdg, max_hdg, speed, gust); + fgSetupWind(min_hdg, max_hdg, speed, gust); return FG_OPTIONS_OK; } @@ -1038,7 +1038,7 @@ fgOptWind( const char *arg ) SG_LOG( SG_GENERAL, SG_ALERT, "bad wind value " << arg ); return FG_OPTIONS_ERROR; } - setup_wind(min_hdg, max_hdg, speed, gust); + fgSetupWind(min_hdg, max_hdg, speed, gust); return FG_OPTIONS_OK; } -- 2.39.5