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) ),
77 _props = globals->get_props();
78 fgSetBool("/sim/fdm-initialized", false);
80 _wind_north = _props->getNode("environment/wind-from-north-fps", true);
81 _wind_east = _props->getNode("environment/wind-from-east-fps", true);
82 _wind_down = _props->getNode("environment/wind-from-down-fps", true);
83 _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
84 _temp_degc = _props->getNode("environment/temperature-degc", true);
85 _pressure_inhg = _props->getNode("environment/pressure-inhg", true);
86 _density_slugft = _props->getNode("environment/density-slugft3", true);
87 _data_logging = _props->getNode("/sim/temp/fdm-data-logging", true);
88 _replay_master = _props->getNode("/sim/freeze/replay-state", true);
90 createImplementation();
93 void FDMShell::postinit()
95 _initialFdmProperties = new SGPropertyNode;
97 if (!copyProperties(_props->getNode("fdm", true),
98 _initialFdmProperties))
100 SG_LOG(SG_FLIGHT, SG_ALERT, "Failed to save initial FDM property state");
104 void FDMShell::shutdown()
107 fgSetBool("/sim/fdm-initialized", false);
116 _control_fdm_atmo.clear();
118 _pressure_inhg.clear();
119 _density_slugft .clear();
120 _data_logging.clear();
121 _replay_master.clear();
124 void FDMShell::reinit()
128 if ( copyProperties(_initialFdmProperties, fgGetNode("/fdm", true)) ) {
129 SG_LOG( SG_FLIGHT, SG_INFO, "Preserved state restored successfully" );
131 SG_LOG( SG_FLIGHT, SG_WARN,
132 "FDM: Some errors restoring preserved state" );
139 void FDMShell::bind()
141 _tankProperties.bind();
142 if (_impl && _impl->get_inited()) {
143 if (_impl->get_bound()) {
144 throw sg_exception("FDMShell::bind of bound FGInterface impl");
150 void FDMShell::unbind()
152 if( _impl ) _impl->unbind();
153 _tankProperties.unbind();
156 void FDMShell::update(double dt)
162 if (!_impl->get_inited()) {
163 // Check for scenery around the aircraft.
164 double lon = fgGetDouble("/sim/presets/longitude-deg");
165 double lat = fgGetDouble("/sim/presets/latitude-deg");
167 double range = 1000.0; // in meters
168 SGGeod geod = SGGeod::fromDeg(lon, lat);
169 if (globals->get_scenery()->scenery_available(geod, range)) {
170 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
172 if (_impl->get_bound()) {
177 fgSetBool("/sim/fdm-initialized", true);
178 fgSetBool("/sim/signals/fdm-initialized", true);
182 if (!_impl->get_inited()) {
183 return; // still waiting
186 // pull environmental data in, since the FDMs are lazy
187 _impl->set_Velocities_Local_Airmass(
188 _wind_north->getDoubleValue(),
189 _wind_east->getDoubleValue(),
190 _wind_down->getDoubleValue());
192 if (_control_fdm_atmo->getBoolValue()) {
193 // convert from Rankine to Celsius
194 double tempDegC = _temp_degc->getDoubleValue();
195 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
197 // convert from inHG to PSF
198 double pressureInHg = _pressure_inhg->getDoubleValue();
199 _impl->set_Static_pressure(pressureInHg * 70.726566);
200 // keep in slugs/ft^3
201 _impl->set_Density(_density_slugft->getDoubleValue());
204 bool doLog = _data_logging->getBoolValue();
205 if (doLog != _dataLogging) {
206 _dataLogging = doLog;
207 _impl->ToggleDataLogging(doLog);
210 switch(_replay_master->getIntValue())
213 // normal FDM operation
217 // resume FDM operation at current replay position
226 FGInterface* FDMShell::getInterface() const
231 void FDMShell::createImplementation()
235 double dt = 1.0 / fgGetInt("/sim/model-hz");
236 string model = fgGetString("/sim/flight-model");
238 bool fdmUnavailable = false;
240 if ( model == "ufo" ) {
241 _impl = new FGUFO( dt );
242 } else if ( model == "external" ) {
243 // external is a synonym for "--fdm=null" and is
244 // maintained here for backwards compatibility
245 _impl = new FGNullFDM( dt );
246 } else if ( model.find("network") == 0 ) {
247 string host = "localhost";
251 string net_options = model.substr(8);
252 string::size_type begin, end;
255 end = net_options.find( ",", begin );
256 if ( end != string::npos ) {
257 host = net_options.substr(begin, end - begin);
261 end = net_options.find( ",", begin );
262 if ( end != string::npos ) {
263 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
267 end = net_options.find( ",", begin );
268 if ( end != string::npos ) {
269 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
273 end = net_options.find( ",", begin );
274 if ( end != string::npos ) {
275 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
278 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
279 } else if ( model.find("pipe") == 0 ) {
280 // /* old */ string pipe_path = model.substr(5);
281 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
282 string pipe_path = "";
283 string pipe_protocol = "";
284 string pipe_options = model.substr(5);
285 string::size_type begin, end;
288 end = pipe_options.find( ",", begin );
289 if ( end != string::npos ) {
290 pipe_path = pipe_options.substr(begin, end - begin);
293 // protocol (last option)
294 pipe_protocol = pipe_options.substr(begin);
295 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
296 } else if ( model == "null" ) {
297 _impl = new FGNullFDM( dt );
299 else if ( model == "larcsim" ) {
300 #ifdef ENABLE_LARCSIM
301 _impl = new FGLaRCsim( dt );
303 fdmUnavailable = true;
306 else if ( model == "jsb" ) {
308 _impl = new FGJSBsim( dt );
310 fdmUnavailable = true;
314 else if ( model == "ada" ) {
315 _impl = new FGADA( dt );
316 } else if ( model == "acms" ) {
317 _impl = new FGACMS( dt );
318 } else if ( model == "balloon" ) {
319 _impl = new FGBalloonSim( dt );
320 } else if ( model == "magic" ) {
321 _impl = new FGMagicCarpet( dt );
324 else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
326 fdmUnavailable = true;
329 else if ( model == "yasim" ) {
331 _impl = new YASim( dt );
333 fdmUnavailable = true;
336 throw sg_exception(string("Unrecognized flight model '") + model
337 + "', cannot init flight dynamics model.");
342 // FDM type is known, but its support was disabled at compile-time.
343 throw sg_exception(string("Support for flight model '") + model
344 + ("' is not available with this binary (deprecated/disabled).\n"
345 "If you still need it, please rebuild FlightGear and enable its support."));