1 // environment.cxx -- routines to model the natural environment
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.
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
39 #include <Main/fg_props.hxx>
40 #include "environment.hxx"
43 FGEnvironment::FGEnvironment()
44 : visibility_m(32000),
45 temperature_sea_level_degc(20),
46 pressure_sea_level_inhg(28),
47 wind_from_heading_deg(0),
49 wind_from_north_fps(0),
50 wind_from_east_fps(0),
55 FGEnvironment::FGEnvironment (const FGEnvironment &env)
56 : elevation_ft(env.elevation_ft),
57 visibility_m(env.visibility_m),
58 temperature_sea_level_degc(env.temperature_sea_level_degc),
59 pressure_sea_level_inhg(env.pressure_sea_level_inhg),
60 wind_from_heading_deg(env.wind_from_heading_deg),
61 wind_speed_kt(env.wind_speed_kt),
62 wind_from_north_fps(env.wind_from_north_fps),
63 wind_from_east_fps(env.wind_from_east_fps),
64 wind_from_down_fps(env.wind_from_down_fps)
68 FGEnvironment::~FGEnvironment()
74 FGEnvironment::get_visibility_m () const
80 FGEnvironment::get_temperature_sea_level_degc () const
82 return temperature_sea_level_degc;
86 FGEnvironment::get_temperature_degc () const
88 return temperature_degc;
92 FGEnvironment::get_pressure_sea_level_inhg () const
94 return pressure_sea_level_inhg;
98 FGEnvironment::get_pressure_inhg () const
100 return pressure_inhg;
104 FGEnvironment::get_wind_from_heading_deg () const
106 return wind_from_heading_deg;
110 FGEnvironment::get_wind_speed_kt () const
112 return wind_speed_kt;
116 FGEnvironment::get_wind_from_north_fps () const
118 return wind_from_north_fps;
122 FGEnvironment::get_wind_from_east_fps () const
124 return wind_from_east_fps;
128 FGEnvironment::get_wind_from_down_fps () const
130 return wind_from_down_fps;
134 FGEnvironment::get_elevation_ft () const
142 FGEnvironment::set_visibility_m (double v)
148 FGEnvironment::set_temperature_sea_level_degc (double t)
150 temperature_sea_level_degc = t;
155 FGEnvironment::set_temperature_degc (double t)
157 temperature_degc = t;
162 FGEnvironment::set_pressure_sea_level_inhg (double p)
164 pressure_sea_level_inhg = p;
169 FGEnvironment::set_pressure_inhg (double p)
176 FGEnvironment::set_wind_from_heading_deg (double h)
178 wind_from_heading_deg = h;
183 FGEnvironment::set_wind_speed_kt (double s)
190 FGEnvironment::set_wind_from_north_fps (double n)
192 wind_from_north_fps = n;
197 FGEnvironment::set_wind_from_east_fps (double e)
199 wind_from_east_fps = e;
204 FGEnvironment::set_wind_from_down_fps (double d)
206 wind_from_down_fps = d;
211 FGEnvironment::set_elevation_ft (double e)
219 FGEnvironment::_recalc_hdgspd ()
223 if (wind_from_east_fps == 0) {
224 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
226 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
228 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
229 if (wind_from_east_fps >= 0)
230 wind_from_heading_deg = 90 - wind_from_heading_deg;
232 wind_from_heading_deg = 270 - wind_from_heading_deg;
234 wind_speed_kt = fabs(wind_from_east_fps
235 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
237 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
238 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
242 FGEnvironment::_recalc_ne ()
245 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
247 wind_from_north_fps = speed_fps *
248 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
249 wind_from_east_fps = speed_fps *
250 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
254 FGEnvironment::_recalc_sl_temp ()
256 // Earth atmosphere model from
257 // http://www.grc.nasa.gov/WWW/K-12/airplane/atmos.html
259 // Stratospheric temperatures are not really reversible, so use 15degC.
261 if (elevation_ft < 36152) // Troposphere
262 temperature_sea_level_degc =
263 temperature_degc + (.00649 * SG_FEET_TO_METER * elevation_ft);
264 // If we're in the stratosphere, leave sea-level temp alone
268 FGEnvironment::_recalc_alt_temp ()
270 // Earth atmosphere model from
271 // http://www.grc.nasa.gov/WWW/K-12/airplane/atmos.html
273 if (elevation_ft < 36152) // Troposphere
275 temperature_sea_level_degc - (.00649 * SG_FEET_TO_METER * elevation_ft);
276 else if (elevation_ft < 82345) // Lower Stratosphere
277 temperature_degc = -56.46;
279 temperature_degc = -131.21 + (.00299 * SG_FEET_TO_METER * elevation_ft);
283 FGEnvironment::_recalc_sl_press ()
285 // FIXME: calculate properly
286 pressure_sea_level_inhg = pressure_inhg;
290 FGEnvironment::_recalc_alt_press ()
292 // FIXME: calculate properly
293 pressure_inhg = pressure_sea_level_inhg;
296 // end of environment.cxx