]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
Added Farenheit temperature conversion to the property tree.
[flightgear.git] / src / Environment / environment_mgr.cxx
1 // environment-mgr.cxx -- manager for natural environment information.
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/scene/sky/sky.hxx>
25 #include <simgear/environment/visual_enviro.hxx>
26
27 #include <Main/main.hxx>
28 #include <Main/fg_props.hxx>
29 #include <Aircraft/aircraft.hxx>
30
31 #include "environment.hxx"
32 #include "environment_ctrl.hxx"
33 #include "environment_mgr.hxx"
34 #include "fgclouds.hxx"
35
36 class SGSky;
37 extern SGSky *thesky;
38
39
40
41 FGEnvironmentMgr::FGEnvironmentMgr ()
42   : _environment(new FGEnvironment)
43 {
44
45    if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
46        _controller = new FGMetarEnvironmentCtrl;
47    else
48        _controller = new FGInterpolateEnvironmentCtrl;
49
50   _controller->setEnvironment(_environment);
51   set_subsystem("controller", _controller, 0.5);
52   fgClouds = new FGClouds;
53 }
54
55 FGEnvironmentMgr::~FGEnvironmentMgr ()
56 {
57   delete _environment;
58   delete _controller;
59   delete fgClouds;
60 }
61
62 void
63 FGEnvironmentMgr::init ()
64 {
65   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
66   SGSubsystemGroup::init();
67   _update_fdm();
68 }
69
70 void
71 FGEnvironmentMgr::reinit ()
72 {
73   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
74   SGSubsystemGroup::reinit();
75   _update_fdm();
76 }
77
78 void
79 FGEnvironmentMgr::bind ()
80 {
81   fgTie("/environment/visibility-m", _environment,
82         &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
83   fgSetArchivable("/environment/visibility-m");
84   fgTie("/environment/temperature-sea-level-degc", _environment,
85         &FGEnvironment::get_temperature_sea_level_degc,
86         &FGEnvironment::set_temperature_sea_level_degc);
87   fgSetArchivable("/environment/temperature-sea-level-degc");
88   fgTie("/environment/temperature-degc", _environment,
89         &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
90   fgTie("/environment/temperature-degf", _environment,
91         &FGEnvironment::get_temperature_degf); // FIXME: read-only for now
92   fgTie("/environment/dewpoint-sea-level-degc", _environment,
93         &FGEnvironment::get_dewpoint_sea_level_degc,
94         &FGEnvironment::set_dewpoint_sea_level_degc);
95   fgSetArchivable("/environment/dewpoint-sea-level-degc");
96   fgTie("/environment/dewpoint-degc", _environment,
97         &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
98   fgTie("/environment/pressure-sea-level-inhg", _environment,
99         &FGEnvironment::get_pressure_sea_level_inhg,
100         &FGEnvironment::set_pressure_sea_level_inhg);
101   fgSetArchivable("/environment/pressure-sea-level-inhg");
102   fgTie("/environment/pressure-inhg", _environment,
103         &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
104   fgTie("/environment/density-slugft3", _environment,
105         &FGEnvironment::get_density_slugft3); // read-only
106   fgTie("/environment/wind-from-heading-deg", _environment,
107         &FGEnvironment::get_wind_from_heading_deg,
108         &FGEnvironment::set_wind_from_heading_deg);
109   fgTie("/environment/wind-speed-kt", _environment,
110         &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
111   fgTie("/environment/wind-from-north-fps", _environment,
112         &FGEnvironment::get_wind_from_north_fps,
113         &FGEnvironment::set_wind_from_north_fps);
114   fgSetArchivable("/environment/wind-from-north-fps");
115   fgTie("/environment/wind-from-east-fps", _environment,
116         &FGEnvironment::get_wind_from_east_fps,
117         &FGEnvironment::set_wind_from_east_fps);
118   fgSetArchivable("/environment/wind-from-east-fps");
119   fgTie("/environment/wind-from-down-fps", _environment,
120         &FGEnvironment::get_wind_from_down_fps,
121         &FGEnvironment::set_wind_from_down_fps);
122   fgSetArchivable("/environment/wind-from-down-fps");
123   fgTie("/environment/turbulence/magnitude-norm", _environment,
124         &FGEnvironment::get_turbulence_magnitude_norm,
125         &FGEnvironment::set_turbulence_magnitude_norm);
126   fgSetArchivable("/environment/turbulence/magnitude-norm");
127   fgTie("/environment/turbulence/rate-hz", _environment,
128         &FGEnvironment::get_turbulence_rate_hz,
129         &FGEnvironment::set_turbulence_rate_hz);
130   fgSetArchivable("/environment/turbulence/rate-hz");
131
132   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
133     char buf[128];
134     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
135     fgTie(buf, this, i,
136           &FGEnvironmentMgr::get_cloud_layer_span_m,
137           &FGEnvironmentMgr::set_cloud_layer_span_m);
138     fgSetArchivable(buf);
139     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
140     fgTie(buf, this, i,
141           &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
142           &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
143     fgSetArchivable(buf);
144     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
145     fgTie(buf, this, i,
146           &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
147           &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
148     fgSetArchivable(buf);
149     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
150     fgTie(buf, this, i,
151           &FGEnvironmentMgr::get_cloud_layer_transition_ft,
152           &FGEnvironmentMgr::set_cloud_layer_transition_ft);
153     fgSetArchivable(buf);
154     sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
155     fgTie(buf, this, i,
156           &FGEnvironmentMgr::get_cloud_layer_coverage,
157           &FGEnvironmentMgr::set_cloud_layer_coverage);
158     fgSetArchivable(buf);
159   }
160   fgTie("/sim/rendering/clouds3d-enable", &sgEnviro,
161           &SGEnviro::get_clouds_enable_state,
162           &SGEnviro::set_clouds_enable_state);
163   fgTie("/sim/rendering/clouds3d-vis-range", &sgEnviro,
164           &SGEnviro::get_clouds_visibility,
165           &SGEnviro::set_clouds_visibility);
166   fgTie("/sim/rendering/clouds3d-density", &sgEnviro,
167           &SGEnviro::get_clouds_density,
168           &SGEnviro::set_clouds_density);
169   fgTie("/sim/rendering/clouds3d-cache-size", &sgEnviro,
170           &SGEnviro::get_clouds_CacheSize,
171           &SGEnviro::set_clouds_CacheSize);
172   fgTie("/sim/rendering/clouds3d-cache-resolution", &sgEnviro,
173           &SGEnviro::get_CacheResolution,
174           &SGEnviro::set_CacheResolution);
175   fgTie("/sim/rendering/precipitation-enable", &sgEnviro,
176           &SGEnviro::get_precipitation_enable_state,
177           &SGEnviro::set_precipitation_enable_state);
178   fgTie("/environment/rebuild_layers", fgClouds,
179       &FGClouds::get_update_event,
180       &FGClouds::set_update_event);
181   fgTie("/sim/rendering/lightning-enable", &sgEnviro,
182       &SGEnviro::get_lightning_enable_state,
183       &SGEnviro::set_lightning_enable_state);
184   fgTie("/environment/turbulence/use-cloud-turbulence", &sgEnviro,
185       &SGEnviro::get_turbulence_enable_state,
186       &SGEnviro::set_turbulence_enable_state);
187 }
188
189 void
190 FGEnvironmentMgr::unbind ()
191 {
192   fgUntie("/environment/visibility-m");
193   fgUntie("/environment/temperature-sea-level-degc");
194   fgUntie("/environment/temperature-degc");
195   fgUntie("/environment/dewpoint-sea-level-degc");
196   fgUntie("/environment/dewpoint-degc");
197   fgUntie("/environment/pressure-sea-level-inhg");
198   fgUntie("/environment/pressure-inhg");
199   fgUntie("/environment/density-inhg");
200   fgUntie("/environment/wind-from-north-fps");
201   fgUntie("/environment/wind-from-east-fps");
202   fgUntie("/environment/wind-from-down-fps");
203   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
204     char buf[128];
205     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
206     fgUntie(buf);
207     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
208     fgUntie(buf);
209     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
210     fgUntie(buf);
211     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
212     fgUntie(buf);
213     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
214     fgUntie(buf);
215   }
216   fgUntie("/sim/rendering/clouds3d-enable");
217   fgUntie("/sim/rendering/clouds3d-vis-range");
218   fgUntie("/sim/rendering/clouds3d-density");
219   fgUntie("/sim/rendering/clouds3d-cache-size");
220   fgUntie("/sim/rendering/clouds3d-cache-resolution");
221   fgUntie("/sim/rendering/precipitation-enable");
222   fgUntie("/environment/rebuild_layers");
223   fgUntie("/sim/rendering/lightning-enable");
224   fgUntie("/environment/turbulence/use-cloud-turbulence");
225 }
226
227 void
228 FGEnvironmentMgr::update (double dt)
229 {
230   SGSubsystemGroup::update(dt);
231
232                                 // FIXME: the FDMs should update themselves
233   current_aircraft.fdm_state
234     ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
235                                    _environment->get_wind_from_east_fps(),
236                                    _environment->get_wind_from_down_fps());
237   _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
238
239   _update_fdm();
240 }
241
242 FGEnvironment
243 FGEnvironmentMgr::getEnvironment () const
244 {
245   return *_environment;
246 }
247
248 FGEnvironment
249 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
250 {
251                                 // Always returns the same environment
252                                 // for now; we'll make it interesting
253                                 // later.
254   FGEnvironment env = *_environment;
255   env.set_elevation_ft(alt);
256   return env;
257 }
258
259 void
260 FGEnvironmentMgr::_update_fdm () const
261 {
262   //
263   // Pass atmosphere on to FDM
264   // FIXME: have FDMs read properties directly.
265   //
266   if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
267                                 // convert from Rankine to Celsius
268     cur_fdm_state
269       ->set_Static_temperature((9.0/5.0)
270                                * (_environment->get_temperature_degc() + 273.15));
271                                 // convert from inHG to PSF
272     cur_fdm_state
273       ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
274                                 // keep in slugs/ft^3
275     cur_fdm_state
276       ->set_Density(_environment->get_density_slugft3());
277   }
278 }
279
280 double
281 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
282 {
283   return thesky->get_cloud_layer(index)->getSpan_m();
284 }
285
286 void
287 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
288 {
289   thesky->get_cloud_layer(index)->setSpan_m(span_m);
290 }
291
292 double
293 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
294 {
295   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
296 }
297
298 void
299 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
300 {
301   FGEnvironment env = *_environment;
302   env.set_elevation_ft(elevation_ft);
303
304   thesky->get_cloud_layer(index)
305     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
306
307   thesky->get_cloud_layer(index)
308     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
309
310   thesky->get_cloud_layer(index)
311     ->setDirection(env.get_wind_from_heading_deg());
312 }
313
314 double
315 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
316 {
317   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
318 }
319
320 void
321 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
322 {
323   thesky->get_cloud_layer(index)
324     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
325 }
326
327 double
328 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
329 {
330   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
331 }
332
333 void
334 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
335                                                  double transition_ft)
336 {
337   thesky->get_cloud_layer(index)
338     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
339 }
340
341 const char *
342 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
343 {
344   switch (thesky->get_cloud_layer(index)->getCoverage()) {
345   case SGCloudLayer::SG_CLOUD_OVERCAST:
346     return "overcast";
347   case SGCloudLayer::SG_CLOUD_BROKEN:
348     return "broken";
349   case SGCloudLayer::SG_CLOUD_SCATTERED:
350     return "scattered";
351   case SGCloudLayer::SG_CLOUD_FEW:
352     return "few";
353   case SGCloudLayer::SG_CLOUD_CIRRUS:
354     return "cirrus";
355   case SGCloudLayer::SG_CLOUD_CLEAR:
356     return "clear";
357   default:
358     return "unknown";
359   }
360 }
361
362 void
363 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
364                                             const char * coverage_name)
365 {
366   SGCloudLayer::Coverage coverage;
367   if (!strcmp(coverage_name, "overcast"))
368     coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
369   else if (!strcmp(coverage_name, "broken"))
370     coverage = SGCloudLayer::SG_CLOUD_BROKEN;
371   else if (!strcmp(coverage_name, "scattered"))
372     coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
373   else if (!strcmp(coverage_name, "few"))
374     coverage = SGCloudLayer::SG_CLOUD_FEW;
375   else if (!strcmp(coverage_name, "cirrus"))
376     coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
377   else if (!strcmp(coverage_name, "clear"))
378     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
379   else {
380     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
381     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
382   }
383   thesky->get_cloud_layer(index)->setCoverage(coverage);
384 }
385
386
387
388 // end of environment-mgr.cxx