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>
30 #include <FDM/fdm_shell.hxx>
31 #include <FDM/flight.hxx>
32 #include <Aircraft/replay.hxx>
33 #include <Main/globals.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Scenery/scenery.hxx>
37 // all the FDMs, since we are the factory method
39 #include <FDM/SP/ADA.hxx>
40 #include <FDM/SP/ACMS.hxx>
41 #include <FDM/SP/MagicCarpet.hxx>
42 #include <FDM/SP/Balloon.h>
44 #include <FDM/ExternalNet/ExternalNet.hxx>
45 #include <FDM/ExternalPipe/ExternalPipe.hxx>
48 #include <FDM/JSBSim/JSBSim.hxx>
52 #include <FDM/LaRCsim/LaRCsim.hxx>
55 #include <FDM/UFO.hxx>
56 #include <FDM/NullFDM.hxx>
59 #include <FDM/YASim/YASim.hxx>
64 FDMShell::FDMShell() :
65 _tankProperties( fgGetNode("/consumables/fuel", true) ),
72 SG_LOG(SG_GENERAL, SG_INFO, "destorying FDM shell");
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::shutdown()
96 fgSetBool("/sim/fdm-initialized", false);
105 _control_fdm_atmo.clear();
107 _pressure_inhg.clear();
108 _density_slugft .clear();
109 _data_logging.clear();
110 _replay_master.clear();
113 void FDMShell::reinit()
119 void FDMShell::bind()
121 _tankProperties.bind();
122 if (_impl && _impl->get_inited()) {
123 if (_impl->get_bound()) {
124 throw sg_exception("FDMShell::bind of bound FGInterface impl");
130 void FDMShell::unbind()
132 if( _impl ) _impl->unbind();
133 _tankProperties.unbind();
136 void FDMShell::update(double dt)
142 if (!_impl->get_inited()) {
143 // Check for scenery around the aircraft.
144 double lon = fgGetDouble("/sim/presets/longitude-deg");
145 double lat = fgGetDouble("/sim/presets/latitude-deg");
147 double range = 1000.0; // in meters
148 SGGeod geod = SGGeod::fromDeg(lon, lat);
149 if (globals->get_scenery()->scenery_available(geod, range)) {
150 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
152 if (_impl->get_bound()) {
157 fgSetBool("/sim/fdm-initialized", true);
158 fgSetBool("/sim/signals/fdm-initialized", true);
162 if (!_impl->get_inited()) {
163 return; // still waiting
166 // pull environmental data in, since the FDMs are lazy
167 _impl->set_Velocities_Local_Airmass(
168 _wind_north->getDoubleValue(),
169 _wind_east->getDoubleValue(),
170 _wind_down->getDoubleValue());
172 if (_control_fdm_atmo->getBoolValue()) {
173 // convert from Rankine to Celsius
174 double tempDegC = _temp_degc->getDoubleValue();
175 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
177 // convert from inHG to PSF
178 double pressureInHg = _pressure_inhg->getDoubleValue();
179 _impl->set_Static_pressure(pressureInHg * 70.726566);
180 // keep in slugs/ft^3
181 _impl->set_Density(_density_slugft->getDoubleValue());
184 bool doLog = _data_logging->getBoolValue();
185 if (doLog != _dataLogging) {
186 _dataLogging = doLog;
187 _impl->ToggleDataLogging(doLog);
190 switch(_replay_master->getIntValue())
193 // normal FDM operation
197 // resume FDM operation at current replay position
206 FGInterface* FDMShell::getInterface() const
211 void FDMShell::createImplementation()
215 double dt = 1.0 / fgGetInt("/sim/model-hz");
216 string model = fgGetString("/sim/flight-model");
218 bool fdmUnavailable = false;
220 if ( model == "ufo" ) {
221 _impl = new FGUFO( dt );
222 } else if ( model == "external" ) {
223 // external is a synonym for "--fdm=null" and is
224 // maintained here for backwards compatibility
225 _impl = new FGNullFDM( dt );
226 } else if ( model.find("network") == 0 ) {
227 string host = "localhost";
231 string net_options = model.substr(8);
232 string::size_type begin, end;
235 end = net_options.find( ",", begin );
236 if ( end != string::npos ) {
237 host = net_options.substr(begin, end - begin);
241 end = net_options.find( ",", begin );
242 if ( end != string::npos ) {
243 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
247 end = net_options.find( ",", begin );
248 if ( end != string::npos ) {
249 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
253 end = net_options.find( ",", begin );
254 if ( end != string::npos ) {
255 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
258 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
259 } else if ( model.find("pipe") == 0 ) {
260 // /* old */ string pipe_path = model.substr(5);
261 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
262 string pipe_path = "";
263 string pipe_protocol = "";
264 string pipe_options = model.substr(5);
265 string::size_type begin, end;
268 end = pipe_options.find( ",", begin );
269 if ( end != string::npos ) {
270 pipe_path = pipe_options.substr(begin, end - begin);
273 // protocol (last option)
274 pipe_protocol = pipe_options.substr(begin);
275 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
276 } else if ( model == "null" ) {
277 _impl = new FGNullFDM( dt );
279 else if ( model == "larcsim" ) {
280 #ifdef ENABLE_LARCSIM
281 _impl = new FGLaRCsim( dt );
283 fdmUnavailable = true;
286 else if ( model == "jsb" ) {
288 _impl = new FGJSBsim( dt );
290 fdmUnavailable = true;
294 else if ( model == "ada" ) {
295 _impl = new FGADA( dt );
296 } else if ( model == "acms" ) {
297 _impl = new FGACMS( dt );
298 } else if ( model == "balloon" ) {
299 _impl = new FGBalloonSim( dt );
300 } else if ( model == "magic" ) {
301 _impl = new FGMagicCarpet( dt );
304 else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
306 fdmUnavailable = true;
309 else if ( model == "yasim" ) {
311 _impl = new YASim( dt );
313 fdmUnavailable = true;
316 throw sg_exception(string("Unrecognized flight model '") + model
317 + "', cannot init flight dynamics model.");
322 // FDM type is known, but its support was disabled at compile-time.
323 throw sg_exception(string("Support for flight model '") + model
324 + ("' is not available with this binary (deprecated/disabled).\n"
325 "If you still need it, please rebuild FlightGear and enable its support."));