1 // fdm_shell.cxx -- encapsulate FDM implementations as well-behaved subsystems
3 // Written by James Turner, started June 2010.
5 // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/props/props_io.hxx>
31 #include <FDM/fdm_shell.hxx>
32 #include <FDM/flight.hxx>
33 #include <Aircraft/replay.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Scenery/scenery.hxx>
38 // all the FDMs, since we are the factory method
40 #include <FDM/SP/ADA.hxx>
41 #include <FDM/SP/ACMS.hxx>
42 #include <FDM/SP/MagicCarpet.hxx>
43 #include <FDM/SP/Balloon.h>
45 #include <FDM/ExternalNet/ExternalNet.hxx>
46 #include <FDM/ExternalPipe/ExternalPipe.hxx>
49 #include <FDM/JSBSim/JSBSim.hxx>
53 #include <FDM/LaRCsim/LaRCsim.hxx>
56 #include <FDM/UFO.hxx>
57 #include <FDM/NullFDM.hxx>
60 #include <FDM/YASim/YASim.hxx>
65 FDMShell::FDMShell() :
66 _tankProperties( fgGetNode("/consumables/fuel", true) ),
73 SG_LOG(SG_GENERAL, SG_INFO, "destorying FDM shell");
78 _props = globals->get_props();
79 fgSetBool("/sim/fdm-initialized", false);
81 _wind_north = _props->getNode("environment/wind-from-north-fps", true);
82 _wind_east = _props->getNode("environment/wind-from-east-fps", true);
83 _wind_down = _props->getNode("environment/wind-from-down-fps", true);
84 _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
85 _temp_degc = _props->getNode("environment/temperature-degc", true);
86 _pressure_inhg = _props->getNode("environment/pressure-inhg", true);
87 _density_slugft = _props->getNode("environment/density-slugft3", true);
88 _data_logging = _props->getNode("/sim/temp/fdm-data-logging", true);
89 _replay_master = _props->getNode("/sim/freeze/replay-state", true);
91 createImplementation();
94 void FDMShell::postinit()
96 _initialFdmProperties = new SGPropertyNode;
98 if (!copyProperties(_props->getNode("fdm", true),
99 _initialFdmProperties))
101 SG_LOG(SG_FLIGHT, SG_ALERT, "Failed to save initial FDM property state");
105 void FDMShell::shutdown()
108 fgSetBool("/sim/fdm-initialized", false);
117 _control_fdm_atmo.clear();
119 _pressure_inhg.clear();
120 _density_slugft .clear();
121 _data_logging.clear();
122 _replay_master.clear();
125 void FDMShell::reinit()
129 if ( copyProperties(_initialFdmProperties, fgGetNode("/fdm", true)) ) {
130 SG_LOG( SG_FLIGHT, SG_INFO, "Preserved state restored successfully" );
132 SG_LOG( SG_FLIGHT, SG_WARN,
133 "FDM: Some errors restoring preserved state" );
140 void FDMShell::bind()
142 _tankProperties.bind();
143 if (_impl && _impl->get_inited()) {
144 if (_impl->get_bound()) {
145 throw sg_exception("FDMShell::bind of bound FGInterface impl");
151 void FDMShell::unbind()
153 if( _impl ) _impl->unbind();
154 _tankProperties.unbind();
157 void FDMShell::update(double dt)
163 if (!_impl->get_inited()) {
164 // Check for scenery around the aircraft.
165 double lon = fgGetDouble("/sim/presets/longitude-deg");
166 double lat = fgGetDouble("/sim/presets/latitude-deg");
168 double range = 1000.0; // in meters
169 SGGeod geod = SGGeod::fromDeg(lon, lat);
170 if (globals->get_scenery()->scenery_available(geod, range)) {
171 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
173 if (_impl->get_bound()) {
178 fgSetBool("/sim/fdm-initialized", true);
179 fgSetBool("/sim/signals/fdm-initialized", true);
183 if (!_impl->get_inited()) {
184 return; // still waiting
187 // pull environmental data in, since the FDMs are lazy
188 _impl->set_Velocities_Local_Airmass(
189 _wind_north->getDoubleValue(),
190 _wind_east->getDoubleValue(),
191 _wind_down->getDoubleValue());
193 if (_control_fdm_atmo->getBoolValue()) {
194 // convert from Rankine to Celsius
195 double tempDegC = _temp_degc->getDoubleValue();
196 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
198 // convert from inHG to PSF
199 double pressureInHg = _pressure_inhg->getDoubleValue();
200 _impl->set_Static_pressure(pressureInHg * 70.726566);
201 // keep in slugs/ft^3
202 _impl->set_Density(_density_slugft->getDoubleValue());
205 bool doLog = _data_logging->getBoolValue();
206 if (doLog != _dataLogging) {
207 _dataLogging = doLog;
208 _impl->ToggleDataLogging(doLog);
211 switch(_replay_master->getIntValue())
214 // normal FDM operation
218 // resume FDM operation at current replay position
227 FGInterface* FDMShell::getInterface() const
232 void FDMShell::createImplementation()
236 double dt = 1.0 / fgGetInt("/sim/model-hz");
237 string model = fgGetString("/sim/flight-model");
239 bool fdmUnavailable = false;
241 if ( model == "ufo" ) {
242 _impl = new FGUFO( dt );
243 } else if ( model == "external" ) {
244 // external is a synonym for "--fdm=null" and is
245 // maintained here for backwards compatibility
246 _impl = new FGNullFDM( dt );
247 } else if ( model.find("network") == 0 ) {
248 string host = "localhost";
252 string net_options = model.substr(8);
253 string::size_type begin, end;
256 end = net_options.find( ",", begin );
257 if ( end != string::npos ) {
258 host = net_options.substr(begin, end - begin);
262 end = net_options.find( ",", begin );
263 if ( end != string::npos ) {
264 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
268 end = net_options.find( ",", begin );
269 if ( end != string::npos ) {
270 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
274 end = net_options.find( ",", begin );
275 if ( end != string::npos ) {
276 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
279 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
280 } else if ( model.find("pipe") == 0 ) {
281 // /* old */ string pipe_path = model.substr(5);
282 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
283 string pipe_path = "";
284 string pipe_protocol = "";
285 string pipe_options = model.substr(5);
286 string::size_type begin, end;
289 end = pipe_options.find( ",", begin );
290 if ( end != string::npos ) {
291 pipe_path = pipe_options.substr(begin, end - begin);
294 // protocol (last option)
295 pipe_protocol = pipe_options.substr(begin);
296 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
297 } else if ( model == "null" ) {
298 _impl = new FGNullFDM( dt );
300 else if ( model == "larcsim" ) {
301 #ifdef ENABLE_LARCSIM
302 _impl = new FGLaRCsim( dt );
304 fdmUnavailable = true;
307 else if ( model == "jsb" ) {
309 _impl = new FGJSBsim( dt );
311 fdmUnavailable = true;
315 else if ( model == "ada" ) {
316 _impl = new FGADA( dt );
317 } else if ( model == "acms" ) {
318 _impl = new FGACMS( dt );
319 } else if ( model == "balloon" ) {
320 _impl = new FGBalloonSim( dt );
321 } else if ( model == "magic" ) {
322 _impl = new FGMagicCarpet( dt );
325 else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
327 fdmUnavailable = true;
330 else if ( model == "yasim" ) {
332 _impl = new YASim( dt );
334 fdmUnavailable = true;
337 throw sg_exception(string("Unrecognized flight model '") + model
338 + "', cannot init flight dynamics model.");
343 // FDM type is known, but its support was disabled at compile-time.
344 throw sg_exception(string("Support for flight model '") + model
345 + ("' is not available with this binary (deprecated/disabled).\n"
346 "If you still need it, please rebuild FlightGear and enable its support."));