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.
27 #include <simgear/structure/exception.hxx>
29 #include <FDM/fdm_shell.hxx>
30 #include <FDM/flight.hxx>
31 #include <Aircraft/replay.hxx>
32 #include <Main/globals.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Scenery/scenery.hxx>
36 // all the FDMs, since we are the factory method
38 #include <FDM/SP/ADA.hxx>
39 #include <FDM/SP/ACMS.hxx>
40 #include <FDM/SP/MagicCarpet.hxx>
41 #include <FDM/SP/Balloon.h>
43 #include <FDM/ExternalNet/ExternalNet.hxx>
44 #include <FDM/ExternalPipe/ExternalPipe.hxx>
45 #include <FDM/JSBSim/JSBSim.hxx>
46 #include <FDM/LaRCsim/LaRCsim.hxx>
47 #include <FDM/UFO.hxx>
48 #include <FDM/NullFDM.hxx>
49 #include <FDM/YASim/YASim.hxx>
52 * Evil global variable required by Network/FGNative,
53 * see that class for more information
55 FGInterface* evil_global_fdm_state = NULL;
57 FDMShell::FDMShell() :
70 _props = globals->get_props();
71 fgSetBool("/sim/fdm-initialized", false);
72 createImplementation();
75 void FDMShell::reinit()
78 fgSetBool("/sim/fdm-initialized", false);
79 evil_global_fdm_state = NULL;
90 if (_impl && _impl->get_inited()) {
91 if (_impl->get_bound()) {
92 throw sg_exception("FDMShell::bind of bound FGInterface impl");
99 void FDMShell::unbind()
101 if( _impl ) _impl->unbind();
104 void FDMShell::update(double dt)
110 if (!_impl->get_inited()) {
111 // Check for scenery around the aircraft.
112 double lon = fgGetDouble("/sim/presets/longitude-deg");
113 double lat = fgGetDouble("/sim/presets/latitude-deg");
115 double range = 1000.0; // in metres
116 SGGeod geod = SGGeod::fromDeg(lon, lat);
117 if (globals->get_scenery()->scenery_available(geod, range)) {
118 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
120 if (_impl->get_bound()) {
125 evil_global_fdm_state = _impl;
126 fgSetBool("/sim/fdm-initialized", true);
127 fgSetBool("/sim/signals/fdm-initialized", true);
131 if (!_impl->get_inited()) {
132 return; // still waiting
135 // pull environmental data in, since the FDMs are lazy
136 _impl->set_Velocities_Local_Airmass(
137 _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
138 _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
139 _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
141 if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
142 // convert from Rankine to Celsius
143 double tempDegC = _props->getDoubleValue("environment/temperature-degc");
144 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
146 // convert from inHG to PSF
147 double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
148 _impl->set_Static_pressure(pressureInHg * 70.726566);
149 // keep in slugs/ft^3
150 _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
153 bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
154 if (doLog != _dataLogging) {
155 _dataLogging = doLog;
156 _impl->ToggleDataLogging(doLog);
159 // FIXME - replay manager should handle most of this
160 int replayState = fgGetInt("/sim/freeze/replay-state", 0);
161 if (replayState == 0) {
162 _impl->update(dt); // normal code path
163 } else if (replayState == 1) {
164 // should be inside FGReplay!
165 SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
166 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
167 r->replay( replay_time->getDoubleValue() );
168 replay_time->setDoubleValue( replay_time->getDoubleValue()
170 * fgGetInt("/sim/speed-up") ) );
172 } else if (replayState == 2) {
173 // paused replay, no-op
175 throw sg_range_exception("unknown FGReplay state");
179 void FDMShell::createImplementation()
183 double dt = 1.0 / fgGetInt("/sim/model-hz");
184 string model = fgGetString("/sim/flight-model");
186 if ( model == "larcsim" ) {
187 _impl = new FGLaRCsim( dt );
188 } else if ( model == "jsb" ) {
189 _impl = new FGJSBsim( dt );
191 } else if ( model == "ada" ) {
192 _impl = new FGADA( dt );
193 } else if ( model == "acms" ) {
194 _impl = new FGACMS( dt );
195 } else if ( model == "balloon" ) {
196 _impl = new FGBalloonSim( dt );
197 } else if ( model == "magic" ) {
198 _impl = new FGMagicCarpet( dt );
200 } else if ( model == "ufo" ) {
201 _impl = new FGUFO( dt );
202 } else if ( model == "external" ) {
203 // external is a synonym for "--fdm=null" and is
204 // maintained here for backwards compatibility
205 _impl = new FGNullFDM( dt );
206 } else if ( model.find("network") == 0 ) {
207 string host = "localhost";
211 string net_options = model.substr(8);
212 string::size_type begin, end;
215 end = net_options.find( ",", begin );
216 if ( end != string::npos ) {
217 host = net_options.substr(begin, end - begin);
221 end = net_options.find( ",", begin );
222 if ( end != string::npos ) {
223 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
227 end = net_options.find( ",", begin );
228 if ( end != string::npos ) {
229 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
233 end = net_options.find( ",", begin );
234 if ( end != string::npos ) {
235 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
238 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
239 } else if ( model.find("pipe") == 0 ) {
240 // /* old */ string pipe_path = model.substr(5);
241 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
242 string pipe_path = "";
243 string pipe_protocol = "";
244 string pipe_options = model.substr(5);
245 string::size_type begin, end;
248 end = pipe_options.find( ",", begin );
249 if ( end != string::npos ) {
250 pipe_path = pipe_options.substr(begin, end - begin);
253 // protocol (last option)
254 pipe_protocol = pipe_options.substr(begin);
255 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
256 } else if ( model == "null" ) {
257 _impl = new FGNullFDM( dt );
258 } else if ( model == "yasim" ) {
259 _impl = new YASim( dt );
261 throw sg_exception(string("Unrecognized flight model '") + model
262 + "', cannot init flight dynamics model.");