1 // environment-mgr.cxx -- manager for natural environment information.
3 // Written by David Megginson, started February 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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/scene/sky/sky.hxx>
26 #include <Main/main.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/renderer.hxx>
29 #include <Aircraft/aircraft.hxx>
31 #include "environment.hxx"
32 #include "environment_ctrl.hxx"
33 #include "environment_mgr.hxx"
36 FGEnvironmentMgr::FGEnvironmentMgr ()
37 : _environment(new FGEnvironment)
40 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
41 _controller = new FGMetarEnvironmentCtrl;
43 _controller = new FGInterpolateEnvironmentCtrl;
45 _controller->setEnvironment(_environment);
46 set_subsystem("controller", _controller, 0.5);
49 FGEnvironmentMgr::~FGEnvironmentMgr ()
56 FGEnvironmentMgr::init ()
58 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
59 SGSubsystemGroup::init();
64 FGEnvironmentMgr::reinit ()
66 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
67 SGSubsystemGroup::reinit();
72 FGEnvironmentMgr::bind ()
74 fgTie("/environment/visibility-m", _environment,
75 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
76 fgSetArchivable("/environment/visibility-m");
77 fgTie("/environment/temperature-sea-level-degc", _environment,
78 &FGEnvironment::get_temperature_sea_level_degc,
79 &FGEnvironment::set_temperature_sea_level_degc);
80 fgSetArchivable("/environment/temperature-sea-level-degc");
81 fgTie("/environment/temperature-degc", _environment,
82 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
83 fgTie("/environment/dewpoint-sea-level-degc", _environment,
84 &FGEnvironment::get_dewpoint_sea_level_degc,
85 &FGEnvironment::set_dewpoint_sea_level_degc);
86 fgSetArchivable("/environment/dewpoint-sea-level-degc");
87 fgTie("/environment/dewpoint-degc", _environment,
88 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
89 fgTie("/environment/pressure-sea-level-inhg", _environment,
90 &FGEnvironment::get_pressure_sea_level_inhg,
91 &FGEnvironment::set_pressure_sea_level_inhg);
92 fgSetArchivable("/environment/pressure-sea-level-inhg");
93 fgTie("/environment/pressure-inhg", _environment,
94 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
95 fgTie("/environment/density-slugft3", _environment,
96 &FGEnvironment::get_density_slugft3); // read-only
97 fgTie("/environment/wind-from-heading-deg", _environment,
98 &FGEnvironment::get_wind_from_heading_deg,
99 &FGEnvironment::set_wind_from_heading_deg);
100 fgTie("/environment/wind-speed-kt", _environment,
101 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
102 fgTie("/environment/wind-from-north-fps", _environment,
103 &FGEnvironment::get_wind_from_north_fps,
104 &FGEnvironment::set_wind_from_north_fps);
105 fgSetArchivable("/environment/wind-from-north-fps");
106 fgTie("/environment/wind-from-east-fps", _environment,
107 &FGEnvironment::get_wind_from_east_fps,
108 &FGEnvironment::set_wind_from_east_fps);
109 fgSetArchivable("/environment/wind-from-east-fps");
110 fgTie("/environment/wind-from-down-fps", _environment,
111 &FGEnvironment::get_wind_from_down_fps,
112 &FGEnvironment::set_wind_from_down_fps);
113 fgSetArchivable("/environment/wind-from-down-fps");
114 fgTie("/environment/turbulence/magnitude-norm", _environment,
115 &FGEnvironment::get_turbulence_magnitude_norm,
116 &FGEnvironment::set_turbulence_magnitude_norm);
117 fgSetArchivable("/environment/turbulence/magnitude-norm");
118 fgTie("/environment/turbulence/rate-hz", _environment,
119 &FGEnvironment::get_turbulence_rate_hz,
120 &FGEnvironment::set_turbulence_rate_hz);
121 fgSetArchivable("/environment/turbulence/rate-hz");
123 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
125 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
127 &FGEnvironmentMgr::get_cloud_layer_span_m,
128 &FGEnvironmentMgr::set_cloud_layer_span_m);
129 fgSetArchivable(buf);
130 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
132 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
133 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
134 fgSetArchivable(buf);
135 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
137 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
138 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
139 fgSetArchivable(buf);
140 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
142 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
143 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
144 fgSetArchivable(buf);
145 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
147 &FGEnvironmentMgr::get_cloud_layer_coverage,
148 &FGEnvironmentMgr::set_cloud_layer_coverage);
149 fgSetArchivable(buf);
154 FGEnvironmentMgr::unbind ()
156 fgUntie("/environment/visibility-m");
157 fgUntie("/environment/temperature-sea-level-degc");
158 fgUntie("/environment/temperature-degc");
159 fgUntie("/environment/dewpoint-sea-level-degc");
160 fgUntie("/environment/dewpoint-degc");
161 fgUntie("/environment/pressure-sea-level-inhg");
162 fgUntie("/environment/pressure-inhg");
163 fgUntie("/environment/density-inhg");
164 fgUntie("/environment/wind-from-north-fps");
165 fgUntie("/environment/wind-from-east-fps");
166 fgUntie("/environment/wind-from-down-fps");
167 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
169 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
171 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
173 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
175 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
177 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
183 FGEnvironmentMgr::update (double dt)
185 SGSubsystemGroup::update(dt);
187 // FIXME: the FDMs should update themselves
188 current_aircraft.fdm_state
189 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
190 _environment->get_wind_from_east_fps(),
191 _environment->get_wind_from_down_fps());
192 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
198 FGEnvironmentMgr::getEnvironment () const
200 return *_environment;
204 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
206 // Always returns the same environment
207 // for now; we'll make it interesting
209 FGEnvironment env = *_environment;
210 env.set_elevation_ft(alt);
215 FGEnvironmentMgr::_update_fdm () const
218 // Pass atmosphere on to FDM
219 // FIXME: have FDMs read properties directly.
221 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
222 // convert from Rankine to Celsius
224 ->set_Static_temperature((9.0/5.0)
225 * (_environment->get_temperature_degc() + 273.15));
226 // convert from inHG to PSF
228 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
229 // keep in slugs/ft^3
231 ->set_Density(_environment->get_density_slugft3());
236 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
238 return thesky->get_cloud_layer(index)->getSpan_m();
242 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
244 thesky->get_cloud_layer(index)->setSpan_m(span_m);
248 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
250 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
254 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
256 FGEnvironment env = *_environment;
257 env.set_elevation_ft(elevation_ft);
259 thesky->get_cloud_layer(index)
260 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
262 thesky->get_cloud_layer(index)
263 ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s
265 thesky->get_cloud_layer(index)
266 ->setDirection(env.get_wind_from_heading_deg());
270 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
272 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
276 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
278 thesky->get_cloud_layer(index)
279 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
283 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
285 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
289 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
290 double transition_ft)
292 thesky->get_cloud_layer(index)
293 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
297 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
299 switch (thesky->get_cloud_layer(index)->getCoverage()) {
300 case SGCloudLayer::SG_CLOUD_OVERCAST:
302 case SGCloudLayer::SG_CLOUD_BROKEN:
304 case SGCloudLayer::SG_CLOUD_SCATTERED:
306 case SGCloudLayer::SG_CLOUD_FEW:
308 case SGCloudLayer::SG_CLOUD_CIRRUS:
310 case SGCloudLayer::SG_CLOUD_CLEAR:
318 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
319 const char * coverage_name)
321 SGCloudLayer::Coverage coverage;
322 if (!strcmp(coverage_name, "overcast"))
323 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
324 else if (!strcmp(coverage_name, "broken"))
325 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
326 else if (!strcmp(coverage_name, "scattered"))
327 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
328 else if (!strcmp(coverage_name, "few"))
329 coverage = SGCloudLayer::SG_CLOUD_FEW;
330 else if (!strcmp(coverage_name, "cirrus"))
331 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
332 else if (!strcmp(coverage_name, "clear"))
333 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
335 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
336 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
338 thesky->get_cloud_layer(index)->setCoverage(coverage);
343 // end of environment-mgr.cxx