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>
46 #include <FDM/JSBSim/JSBSim.hxx>
47 #include <FDM/LaRCsim/LaRCsim.hxx>
48 #include <FDM/UFO.hxx>
49 #include <FDM/NullFDM.hxx>
50 #include <FDM/YASim/YASim.hxx>
54 * Evil global variable required by Network/FGNative,
55 * see that class for more information
57 FGInterface* evil_global_fdm_state = NULL;
59 FDMShell::FDMShell() :
60 _tankProperties( fgGetNode("/consumables/fuel", true) ),
73 _props = globals->get_props();
74 fgSetBool("/sim/fdm-initialized", false);
75 createImplementation();
78 void FDMShell::reinit()
81 fgSetBool("/sim/fdm-initialized", false);
82 evil_global_fdm_state = NULL;
93 if (_impl && _impl->get_inited()) {
94 if (_impl->get_bound()) {
95 throw sg_exception("FDMShell::bind of bound FGInterface impl");
97 _tankProperties.bind();
102 void FDMShell::unbind()
104 if( _impl ) _impl->unbind();
105 _tankProperties.unbind();
108 void FDMShell::update(double dt)
114 if (!_impl->get_inited()) {
115 // Check for scenery around the aircraft.
116 double lon = fgGetDouble("/sim/presets/longitude-deg");
117 double lat = fgGetDouble("/sim/presets/latitude-deg");
119 double range = 1000.0; // in meters
120 SGGeod geod = SGGeod::fromDeg(lon, lat);
121 if (globals->get_scenery()->scenery_available(geod, range)) {
122 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
124 if (_impl->get_bound()) {
129 evil_global_fdm_state = _impl;
130 fgSetBool("/sim/fdm-initialized", true);
131 fgSetBool("/sim/signals/fdm-initialized", true);
135 if (!_impl->get_inited()) {
136 return; // still waiting
139 // pull environmental data in, since the FDMs are lazy
140 _impl->set_Velocities_Local_Airmass(
141 _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
142 _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
143 _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
145 if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
146 // convert from Rankine to Celsius
147 double tempDegC = _props->getDoubleValue("environment/temperature-degc");
148 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
150 // convert from inHG to PSF
151 double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
152 _impl->set_Static_pressure(pressureInHg * 70.726566);
153 // keep in slugs/ft^3
154 _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
157 bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
158 if (doLog != _dataLogging) {
159 _dataLogging = doLog;
160 _impl->ToggleDataLogging(doLog);
163 // FIXME - replay manager should handle most of this
164 int replayState = fgGetInt("/sim/freeze/replay-state", 0);
165 if (replayState == 0) {
166 _impl->update(dt); // normal code path
167 } else if (replayState == 1) {
168 // should be inside FGReplay!
169 SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
170 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
171 r->replay( replay_time->getDoubleValue() );
172 replay_time->setDoubleValue( replay_time->getDoubleValue()
174 * fgGetInt("/sim/speed-up") ) );
176 } else if (replayState == 2) {
177 // paused replay, no-op
179 throw sg_range_exception("unknown FGReplay state");
183 void FDMShell::createImplementation()
187 double dt = 1.0 / fgGetInt("/sim/model-hz");
188 string model = fgGetString("/sim/flight-model");
190 if ( model == "larcsim" ) {
191 _impl = new FGLaRCsim( dt );
192 } else if ( model == "jsb" ) {
193 _impl = new FGJSBsim( dt );
195 } else if ( model == "ada" ) {
196 _impl = new FGADA( dt );
197 } else if ( model == "acms" ) {
198 _impl = new FGACMS( dt );
199 } else if ( model == "balloon" ) {
200 _impl = new FGBalloonSim( dt );
201 } else if ( model == "magic" ) {
202 _impl = new FGMagicCarpet( dt );
204 } else if ( model == "ufo" ) {
205 _impl = new FGUFO( dt );
206 } else if ( model == "external" ) {
207 // external is a synonym for "--fdm=null" and is
208 // maintained here for backwards compatibility
209 _impl = new FGNullFDM( dt );
210 } else if ( model.find("network") == 0 ) {
211 string host = "localhost";
215 string net_options = model.substr(8);
216 string::size_type begin, end;
219 end = net_options.find( ",", begin );
220 if ( end != string::npos ) {
221 host = net_options.substr(begin, end - begin);
225 end = net_options.find( ",", begin );
226 if ( end != string::npos ) {
227 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
231 end = net_options.find( ",", begin );
232 if ( end != string::npos ) {
233 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
237 end = net_options.find( ",", begin );
238 if ( end != string::npos ) {
239 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
242 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
243 } else if ( model.find("pipe") == 0 ) {
244 // /* old */ string pipe_path = model.substr(5);
245 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
246 string pipe_path = "";
247 string pipe_protocol = "";
248 string pipe_options = model.substr(5);
249 string::size_type begin, end;
252 end = pipe_options.find( ",", begin );
253 if ( end != string::npos ) {
254 pipe_path = pipe_options.substr(begin, end - begin);
257 // protocol (last option)
258 pipe_protocol = pipe_options.substr(begin);
259 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
260 } else if ( model == "null" ) {
261 _impl = new FGNullFDM( dt );
262 } else if ( model == "yasim" ) {
263 _impl = new YASim( dt );
265 throw sg_exception(string("Unrecognized flight model '") + model
266 + "', cannot init flight dynamics model.");