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/fg_props.hxx>
27 #include <Aircraft/aircraft.hxx>
29 #include "environment.hxx"
30 #include "environment_ctrl.hxx"
31 #include "environment_mgr.hxx"
33 extern SGSky *thesky; // FIXME: from main.cxx
36 FGEnvironmentMgr::FGEnvironmentMgr ()
37 : _environment(new FGEnvironment),
38 _controller(new FGInterpolateEnvironmentCtrl)
40 _controller->setEnvironment(_environment);
41 set_subsystem("controller", _controller, 0.5);
44 FGEnvironmentMgr::~FGEnvironmentMgr ()
51 FGEnvironmentMgr::init ()
53 SG_LOG( SG_GENERAL, SG_INFO, "Initializing environment subsystem");
54 FGSubsystemGroup::init();
59 FGEnvironmentMgr::reinit ()
61 SG_LOG( SG_GENERAL, SG_INFO, "Reinitializing environment subsystem");
62 FGSubsystemGroup::reinit();
67 FGEnvironmentMgr::bind ()
69 fgTie("/environment/visibility-m", _environment,
70 &FGEnvironment::get_visibility_m, &FGEnvironment::set_visibility_m);
71 fgSetArchivable("/environment/visibility-m");
72 fgTie("/environment/temperature-sea-level-degc", _environment,
73 &FGEnvironment::get_temperature_sea_level_degc,
74 &FGEnvironment::set_temperature_sea_level_degc);
75 fgSetArchivable("/environment/temperature-sea-level-degc");
76 fgTie("/environment/temperature-degc", _environment,
77 &FGEnvironment::get_temperature_degc); // FIXME: read-only for now
78 fgTie("/environment/dewpoint-sea-level-degc", _environment,
79 &FGEnvironment::get_dewpoint_sea_level_degc,
80 &FGEnvironment::set_dewpoint_sea_level_degc);
81 fgSetArchivable("/environment/dewpoint-sea-level-degc");
82 fgTie("/environment/dewpoint-degc", _environment,
83 &FGEnvironment::get_dewpoint_degc); // FIXME: read-only for now
84 fgTie("/environment/pressure-sea-level-inhg", _environment,
85 &FGEnvironment::get_pressure_sea_level_inhg,
86 &FGEnvironment::set_pressure_sea_level_inhg);
87 fgSetArchivable("/environment/pressure-sea-level-inhg");
88 fgTie("/environment/pressure-inhg", _environment,
89 &FGEnvironment::get_pressure_inhg); // FIXME: read-only for now
90 fgTie("/environment/density-slugft3", _environment,
91 &FGEnvironment::get_density_slugft3); // read-only
92 fgTie("/environment/wind-from-heading-deg", _environment,
93 &FGEnvironment::get_wind_from_heading_deg,
94 &FGEnvironment::set_wind_from_heading_deg);
95 fgTie("/environment/wind-speed-kt", _environment,
96 &FGEnvironment::get_wind_speed_kt, &FGEnvironment::set_wind_speed_kt);
97 fgTie("/environment/wind-from-north-fps", _environment,
98 &FGEnvironment::get_wind_from_north_fps,
99 &FGEnvironment::set_wind_from_north_fps);
100 fgSetArchivable("/environment/wind-from-north-fps");
101 fgTie("/environment/wind-from-east-fps", _environment,
102 &FGEnvironment::get_wind_from_east_fps,
103 &FGEnvironment::set_wind_from_east_fps);
104 fgSetArchivable("/environment/wind-from-east-fps");
105 fgTie("/environment/wind-from-down-fps", _environment,
106 &FGEnvironment::get_wind_from_down_fps,
107 &FGEnvironment::set_wind_from_down_fps);
108 fgSetArchivable("/environment/wind-from-down-fps");
109 fgTie("/environment/turbulence-norm", _environment,
110 &FGEnvironment::get_turbulence_norm,
111 &FGEnvironment::set_turbulence_norm);
112 fgSetArchivable("/environment/turbulence-norm");
114 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
116 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
118 &FGEnvironmentMgr::get_cloud_layer_span_m,
119 &FGEnvironmentMgr::set_cloud_layer_span_m);
120 fgSetArchivable(buf);
121 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
123 &FGEnvironmentMgr::get_cloud_layer_elevation_ft,
124 &FGEnvironmentMgr::set_cloud_layer_elevation_ft);
125 fgSetArchivable(buf);
126 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
128 &FGEnvironmentMgr::get_cloud_layer_thickness_ft,
129 &FGEnvironmentMgr::set_cloud_layer_thickness_ft);
130 fgSetArchivable(buf);
131 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
133 &FGEnvironmentMgr::get_cloud_layer_transition_ft,
134 &FGEnvironmentMgr::set_cloud_layer_transition_ft);
135 fgSetArchivable(buf);
136 sprintf(buf, "/environment/clouds/layer[%d]/coverage", i);
138 &FGEnvironmentMgr::get_cloud_layer_coverage,
139 &FGEnvironmentMgr::set_cloud_layer_coverage);
140 fgSetArchivable(buf);
145 FGEnvironmentMgr::unbind ()
147 fgUntie("/environment/visibility-m");
148 fgUntie("/environment/temperature-sea-level-degc");
149 fgUntie("/environment/temperature-degc");
150 fgUntie("/environment/dewpoint-sea-level-degc");
151 fgUntie("/environment/dewpoint-degc");
152 fgUntie("/environment/pressure-sea-level-inhg");
153 fgUntie("/environment/pressure-inhg");
154 fgUntie("/environment/density-inhg");
155 fgUntie("/environment/wind-from-north-fps");
156 fgUntie("/environment/wind-from-east-fps");
157 fgUntie("/environment/wind-from-down-fps");
158 for (int i = 0; i < MAX_CLOUD_LAYERS; i++) {
160 sprintf(buf, "/environment/clouds/layer[%d]/span-m", i);
162 sprintf(buf, "/environment/clouds/layer[%d]/elevation-ft", i);
164 sprintf(buf, "/environment/clouds/layer[%d]/thickness-ft", i);
166 sprintf(buf, "/environment/clouds/layer[%d]/transition-ft", i);
168 sprintf(buf, "/environment/clouds/layer[%d]/type", i);
174 FGEnvironmentMgr::update (double dt)
176 FGSubsystemGroup::update(dt);
178 // FIXME: the FDMs should update themselves
179 current_aircraft.fdm_state
180 ->set_Velocities_Local_Airmass(_environment->get_wind_from_north_fps(),
181 _environment->get_wind_from_east_fps(),
182 _environment->get_wind_from_down_fps());
183 _environment->set_elevation_ft(fgGetDouble("/position/altitude-ft"));
189 FGEnvironmentMgr::getEnvironment () const
191 return *_environment;
195 FGEnvironmentMgr::getEnvironment (double lat, double lon, double alt) const
197 // Always returns the same environment
198 // for now; we'll make it interesting
200 FGEnvironment env = *_environment;
201 env.set_elevation_ft(alt);
206 FGEnvironmentMgr::_update_fdm () const
209 // Pass atmosphere on to FDM
210 // FIXME: have FDMs read properties directly.
212 if (fgGetBool("/environment/params/control-fdm-atmosphere")) {
213 // convert from Rankine to Celsius
215 ->set_Static_temperature((9.0/5.0)
216 * (_environment->get_temperature_degc() + 273.15));
217 // convert from inHG to PSF
219 ->set_Static_pressure(_environment->get_pressure_inhg() * 70.726566);
220 // keep in slugs/ft^3
222 ->set_Density(_environment->get_density_slugft3());
227 FGEnvironmentMgr::get_cloud_layer_span_m (int index) const
229 return thesky->get_cloud_layer(index)->getSpan_m();
233 FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m)
235 thesky->get_cloud_layer(index)->setSpan_m(span_m);
239 FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const
241 return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET;
245 FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft)
247 thesky->get_cloud_layer(index)
248 ->setElevation_m(elevation_ft * SG_FEET_TO_METER);
252 FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const
254 return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET;
258 FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft)
260 thesky->get_cloud_layer(index)
261 ->setThickness_m(thickness_ft * SG_FEET_TO_METER);
265 FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const
267 return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET;
271 FGEnvironmentMgr::set_cloud_layer_transition_ft (int index,
272 double transition_ft)
274 thesky->get_cloud_layer(index)
275 ->setTransition_m(transition_ft * SG_FEET_TO_METER);
279 FGEnvironmentMgr::get_cloud_layer_coverage (int index) const
281 switch (thesky->get_cloud_layer(index)->getCoverage()) {
282 case SGCloudLayer::SG_CLOUD_OVERCAST:
284 case SGCloudLayer::SG_CLOUD_BROKEN:
286 case SGCloudLayer::SG_CLOUD_SCATTERED:
288 case SGCloudLayer::SG_CLOUD_FEW:
290 case SGCloudLayer::SG_CLOUD_CIRRUS:
292 case SGCloudLayer::SG_CLOUD_CLEAR:
300 FGEnvironmentMgr::set_cloud_layer_coverage (int index,
301 const char * coverage_name)
303 SGCloudLayer::Coverage coverage;
304 if (!strcmp(coverage_name, "overcast"))
305 coverage = SGCloudLayer::SG_CLOUD_OVERCAST;
306 else if (!strcmp(coverage_name, "broken"))
307 coverage = SGCloudLayer::SG_CLOUD_BROKEN;
308 else if (!strcmp(coverage_name, "scattered"))
309 coverage = SGCloudLayer::SG_CLOUD_SCATTERED;
310 else if (!strcmp(coverage_name, "few"))
311 coverage = SGCloudLayer::SG_CLOUD_FEW;
312 else if (!strcmp(coverage_name, "cirrus"))
313 coverage = SGCloudLayer::SG_CLOUD_CIRRUS;
314 else if (!strcmp(coverage_name, "clear"))
315 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
317 SG_LOG(SG_INPUT, SG_WARN, "Unknown cloud type " << coverage_name);
318 coverage = SGCloudLayer::SG_CLOUD_CLEAR;
320 thesky->get_cloud_layer(index)->setCoverage(coverage);
325 // end of environment-mgr.cxx