]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_mgr.cxx
d44d77e8973d94c29bfb041cad9f2fbe135362f0
[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
26 #include <Main/main.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/renderer.hxx>
29 #include <Aircraft/aircraft.hxx>
30
31 #include "environment.hxx"
32 #include "environment_ctrl.hxx"
33 #include "environment_mgr.hxx"
34
35
36 FGEnvironmentMgr::FGEnvironmentMgr ()
37   : _environment(new FGEnvironment)
38 {
39
40    if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
41        _controller = new FGMetarEnvironmentCtrl;
42    else
43        _controller = new FGInterpolateEnvironmentCtrl;
44
45   _controller->setEnvironment(_environment);
46   set_subsystem("controller", _controller, 0.5);
47 }
48
49 FGEnvironmentMgr::~FGEnvironmentMgr ()
50 {
51   delete _environment;
52   delete _controller;
53 }
54
55 void
56 FGEnvironmentMgr::init ()
57 {
58   SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
59   SGSubsystemGroup::init();
60   _update_fdm();
61 }
62
63 void
64 FGEnvironmentMgr::reinit ()
65 {
66   SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
67   SGSubsystemGroup::reinit();
68   _update_fdm();
69 }
70
71 void
72 FGEnvironmentMgr::bind ()
73 {
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");
122
123   for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
124     char buf[128];
125     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
126     fgTie(buf, this, 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);
131     fgTie(buf, this, 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);
136     fgTie(buf, this, 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);
141     fgTie(buf, this, 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);
146     fgTie(buf, this, i,
147           &FGEnvironmentMgr::get_cloud_layer_coverage,
148           &FGEnvironmentMgr::set_cloud_layer_coverage);
149     fgSetArchivable(buf);
150   }
151 }
152
153 void
154 FGEnvironmentMgr::unbind ()
155 {
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++) {
168     char buf[128];
169     sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
170     fgUntie(buf);
171     sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
172     fgUntie(buf);
173     sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
174     fgUntie(buf);
175     sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
176     fgUntie(buf);
177     sprintf(buf, "/environment/clouds/layer[%d]/type", i);
178     fgUntie(buf);
179   }
180 }
181
182 void
183 FGEnvironmentMgr::update (double dt)
184 {
185   SGSubsystemGroup::update(dt);
186
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"));
193
194   _update_fdm();
195 }
196
197 FGEnvironment
198 FGEnvironmentMgr::getEnvironment () const
199 {
200   return *_environment;
201 }
202
203 FGEnvironment
204 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
205 {
206                                 // Always returns the same environment
207                                 // for now; we'll make it interesting
208                                 // later.
209   FGEnvironment env = *_environment;
210   env.set_elevation_ft(alt);
211   return env;
212 }
213
214 void
215 FGEnvironmentMgr::_update_fdm () const
216 {
217   //
218   // Pass atmosphere on to FDM
219   // FIXME: have FDMs read properties directly.
220   //
221   if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
222                                 // convert from Rankine to Celsius
223     cur_fdm_state
224       ->set_Static_temperature((9.0/5.0)
225                                * (_environment->get_temperature_degc() + 273.15));
226                                 // convert from inHG to PSF
227     cur_fdm_state
228       ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
229                                 // keep in slugs/ft^3
230     cur_fdm_state
231       ->set_Density(_environment->get_density_slugft3());
232   }
233 }
234
235 double
236 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
237 {
238   return thesky->get_cloud_layer(index)->getSpan_m();
239 }
240
241 void
242 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
243 {
244   thesky->get_cloud_layer(index)->setSpan_m(span_m);
245 }
246
247 double
248 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
249 {
250   return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
251 }
252
253 void
254 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
255 {
256   FGEnvironment env = *_environment;
257   env.set_elevation_ft(elevation_ft);
258
259   thesky->get_cloud_layer(index)
260     ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
261
262   thesky->get_cloud_layer(index)
263     ->setSpeed(env.get_wind_speed_kt() * 0.5151);       // 1 kt = 0.5151 m/s
264
265   thesky->get_cloud_layer(index)
266     ->setDirection(env.get_wind_from_heading_deg());
267 }
268
269 double
270 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
271 {
272   return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
273 }
274
275 void
276 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
277 {
278   thesky->get_cloud_layer(index)
279     ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
280 }
281
282 double
283 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
284 {
285   return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
286 }
287
288 void
289 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
290                                                  double transition_ft)
291 {
292   thesky->get_cloud_layer(index)
293     ->setTransition_m(transition_ft * SG_FEET_TO_METER);
294 }
295
296 const char *
297 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
298 {
299   switch (thesky->get_cloud_layer(index)->getCoverage()) {
300   case SGCloudLayer::SG_CLOUD_OVERCAST:
301     return "overcast";
302   case SGCloudLayer::SG_CLOUD_BROKEN:
303     return "broken";
304   case SGCloudLayer::SG_CLOUD_SCATTERED:
305     return "scattered";
306   case SGCloudLayer::SG_CLOUD_FEW:
307     return "few";
308   case SGCloudLayer::SG_CLOUD_CIRRUS:
309     return "cirrus";
310   case SGCloudLayer::SG_CLOUD_CLEAR:
311     return "clear";
312   default:
313     return "unknown";
314   }
315 }
316
317 void
318 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
319                                             const char * coverage_name)
320 {
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;
334   else {
335     SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
336     coverage = SGCloudLayer::SG_CLOUD_CLEAR;
337   }
338   thesky->get_cloud_layer(index)->setCoverage(coverage);
339 }
340
341
342
343 // end of environment-mgr.cxx