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>
63 * Evil global variable required by Network/FGNative,
64 * see that class for more information
66 FGInterface* evil_global_fdm_state = NULL;
68 FDMShell::FDMShell() :
69 _tankProperties( fgGetNode("/consumables/fuel", true) ),
82 _props = globals->get_props();
83 fgSetBool("/sim/fdm-initialized", false);
85 _wind_north = _props->getNode("environment/wind-from-north-fps", true);
86 _wind_east = _props->getNode("environment/wind-from-east-fps", true);
87 _wind_down = _props->getNode("environment/wind-from-down-fps", true);
88 _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
89 _temp_degc = _props->getNode("environment/temperature-degc", true);
90 _pressure_inhg = _props->getNode("environment/pressure-inhg", true);
91 _density_slugft = _props->getNode("environment/density-slugft3", true);
92 _data_logging = _props->getNode("/sim/temp/fdm-data-logging", true);
93 _replay_master = _props->getNode("/sim/freeze/replay-state", true);
95 createImplementation();
98 void FDMShell::reinit()
101 fgSetBool("/sim/fdm-initialized", false);
102 evil_global_fdm_state = NULL;
111 void FDMShell::bind()
113 _tankProperties.bind();
114 if (_impl && _impl->get_inited()) {
115 if (_impl->get_bound()) {
116 throw sg_exception("FDMShell::bind of bound FGInterface impl");
122 void FDMShell::unbind()
124 if( _impl ) _impl->unbind();
125 _tankProperties.unbind();
128 void FDMShell::update(double dt)
134 if (!_impl->get_inited()) {
135 // Check for scenery around the aircraft.
136 double lon = fgGetDouble("/sim/presets/longitude-deg");
137 double lat = fgGetDouble("/sim/presets/latitude-deg");
139 double range = 1000.0; // in meters
140 SGGeod geod = SGGeod::fromDeg(lon, lat);
141 if (globals->get_scenery()->scenery_available(geod, range)) {
142 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
144 if (_impl->get_bound()) {
149 evil_global_fdm_state = _impl;
150 fgSetBool("/sim/fdm-initialized", true);
151 fgSetBool("/sim/signals/fdm-initialized", true);
155 if (!_impl->get_inited()) {
156 return; // still waiting
159 // pull environmental data in, since the FDMs are lazy
160 _impl->set_Velocities_Local_Airmass(
161 _wind_north->getDoubleValue(),
162 _wind_east->getDoubleValue(),
163 _wind_down->getDoubleValue());
165 if (_control_fdm_atmo->getBoolValue()) {
166 // convert from Rankine to Celsius
167 double tempDegC = _temp_degc->getDoubleValue();
168 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
170 // convert from inHG to PSF
171 double pressureInHg = _pressure_inhg->getDoubleValue();
172 _impl->set_Static_pressure(pressureInHg * 70.726566);
173 // keep in slugs/ft^3
174 _impl->set_Density(_density_slugft->getDoubleValue());
177 bool doLog = _data_logging->getBoolValue();
178 if (doLog != _dataLogging) {
179 _dataLogging = doLog;
180 _impl->ToggleDataLogging(doLog);
183 switch(_replay_master->getIntValue())
186 // normal FDM operation
190 // resume FDM operation at current replay position
199 void FDMShell::createImplementation()
203 double dt = 1.0 / fgGetInt("/sim/model-hz");
204 string model = fgGetString("/sim/flight-model");
206 if ( model == "ufo" ) {
207 _impl = new FGUFO( dt );
208 } else if ( model == "external" ) {
209 // external is a synonym for "--fdm=null" and is
210 // maintained here for backwards compatibility
211 _impl = new FGNullFDM( dt );
212 } else if ( model.find("network") == 0 ) {
213 string host = "localhost";
217 string net_options = model.substr(8);
218 string::size_type begin, end;
221 end = net_options.find( ",", begin );
222 if ( end != string::npos ) {
223 host = net_options.substr(begin, end - begin);
227 end = net_options.find( ",", begin );
228 if ( end != string::npos ) {
229 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
233 end = net_options.find( ",", begin );
234 if ( end != string::npos ) {
235 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
239 end = net_options.find( ",", begin );
240 if ( end != string::npos ) {
241 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
244 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
245 } else if ( model.find("pipe") == 0 ) {
246 // /* old */ string pipe_path = model.substr(5);
247 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
248 string pipe_path = "";
249 string pipe_protocol = "";
250 string pipe_options = model.substr(5);
251 string::size_type begin, end;
254 end = pipe_options.find( ",", begin );
255 if ( end != string::npos ) {
256 pipe_path = pipe_options.substr(begin, end - begin);
259 // protocol (last option)
260 pipe_protocol = pipe_options.substr(begin);
261 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
262 } else if ( model == "null" ) {
263 _impl = new FGNullFDM( dt );
265 #ifdef ENABLE_LARCSIM
266 else if ( model == "larcsim" ) {
267 _impl = new FGLaRCsim( dt );
271 else if ( model == "jsb" ) {
272 _impl = new FGJSBsim( dt );
276 else if ( model == "ada" ) {
277 _impl = new FGADA( dt );
278 } else if ( model == "acms" ) {
279 _impl = new FGACMS( dt );
280 } else if ( model == "balloon" ) {
281 _impl = new FGBalloonSim( dt );
282 } else if ( model == "magic" ) {
283 _impl = new FGMagicCarpet( dt );
287 else if ( model == "yasim" ) {
288 _impl = new YASim( dt );
292 throw sg_exception(string("Unrecognized flight model '") + model
293 + "', cannot init flight dynamics model.");