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>
53 * Evil global variable required by Network/FGNative,
54 * see that class for more information
56 FGInterface* evil_global_fdm_state = NULL;
58 FDMShell::FDMShell() :
71 _props = globals->get_props();
72 fgSetBool("/sim/fdm-initialized", false);
73 createImplementation();
76 void FDMShell::reinit()
79 fgSetBool("/sim/fdm-initialized", false);
80 evil_global_fdm_state = NULL;
91 if (_impl && _impl->get_inited()) {
92 if (_impl->get_bound()) {
93 throw sg_exception("FDMShell::bind of bound FGInterface impl");
100 void FDMShell::unbind()
102 if( _impl ) _impl->unbind();
105 void FDMShell::update(double dt)
111 if (!_impl->get_inited()) {
112 // Check for scenery around the aircraft.
113 double lon = fgGetDouble("/sim/presets/longitude-deg");
114 double lat = fgGetDouble("/sim/presets/latitude-deg");
116 double range = 1000.0; // in meters
117 SGGeod geod = SGGeod::fromDeg(lon, lat);
118 if (globals->get_scenery()->scenery_available(geod, range)) {
119 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
121 if (_impl->get_bound()) {
126 evil_global_fdm_state = _impl;
127 fgSetBool("/sim/fdm-initialized", true);
128 fgSetBool("/sim/signals/fdm-initialized", true);
132 if (!_impl->get_inited()) {
133 return; // still waiting
136 // pull environmental data in, since the FDMs are lazy
137 _impl->set_Velocities_Local_Airmass(
138 _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
139 _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
140 _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
142 if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
143 // convert from Rankine to Celsius
144 double tempDegC = _props->getDoubleValue("environment/temperature-degc");
145 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
147 // convert from inHG to PSF
148 double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
149 _impl->set_Static_pressure(pressureInHg * 70.726566);
150 // keep in slugs/ft^3
151 _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
154 bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
155 if (doLog != _dataLogging) {
156 _dataLogging = doLog;
157 _impl->ToggleDataLogging(doLog);
160 // FIXME - replay manager should handle most of this
161 int replayState = fgGetInt("/sim/freeze/replay-state", 0);
162 if (replayState == 0) {
163 _impl->update(dt); // normal code path
164 } else if (replayState == 1) {
165 // should be inside FGReplay!
166 SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
167 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
168 r->replay( replay_time->getDoubleValue() );
169 replay_time->setDoubleValue( replay_time->getDoubleValue()
171 * fgGetInt("/sim/speed-up") ) );
173 } else if (replayState == 2) {
174 // paused replay, no-op
176 throw sg_range_exception("unknown FGReplay state");
180 void FDMShell::createImplementation()
184 double dt = 1.0 / fgGetInt("/sim/model-hz");
185 string model = fgGetString("/sim/flight-model");
187 if ( model == "larcsim" ) {
188 _impl = new FGLaRCsim( dt );
189 } else if ( model == "jsb" ) {
190 _impl = new FGJSBsim( dt );
192 } else if ( model == "ada" ) {
193 _impl = new FGADA( dt );
194 } else if ( model == "acms" ) {
195 _impl = new FGACMS( dt );
196 } else if ( model == "balloon" ) {
197 _impl = new FGBalloonSim( dt );
198 } else if ( model == "magic" ) {
199 _impl = new FGMagicCarpet( dt );
201 } else if ( model == "ufo" ) {
202 _impl = new FGUFO( dt );
203 } else if ( model == "external" ) {
204 // external is a synonym for "--fdm=null" and is
205 // maintained here for backwards compatibility
206 _impl = new FGNullFDM( dt );
207 } else if ( model.find("network") == 0 ) {
208 string host = "localhost";
212 string net_options = model.substr(8);
213 string::size_type begin, end;
216 end = net_options.find( ",", begin );
217 if ( end != string::npos ) {
218 host = net_options.substr(begin, end - begin);
222 end = net_options.find( ",", begin );
223 if ( end != string::npos ) {
224 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
228 end = net_options.find( ",", begin );
229 if ( end != string::npos ) {
230 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
234 end = net_options.find( ",", begin );
235 if ( end != string::npos ) {
236 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
239 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
240 } else if ( model.find("pipe") == 0 ) {
241 // /* old */ string pipe_path = model.substr(5);
242 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
243 string pipe_path = "";
244 string pipe_protocol = "";
245 string pipe_options = model.substr(5);
246 string::size_type begin, end;
249 end = pipe_options.find( ",", begin );
250 if ( end != string::npos ) {
251 pipe_path = pipe_options.substr(begin, end - begin);
254 // protocol (last option)
255 pipe_protocol = pipe_options.substr(begin);
256 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
257 } else if ( model == "null" ) {
258 _impl = new FGNullFDM( dt );
259 } else if ( model == "yasim" ) {
260 _impl = new YASim( dt );
262 throw sg_exception(string("Unrecognized flight model '") + model
263 + "', cannot init flight dynamics model.");