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 createImplementation();
74 void FDMShell::reinit()
77 fgSetBool("/sim/signals/fdm-initialized", false);
78 evil_global_fdm_state = NULL;
89 if (_impl && _impl->get_inited()) {
90 if (_impl->get_bound()) {
91 throw sg_exception("FDMShell::bind of bound FGInterface impl");
98 void FDMShell::unbind()
103 void FDMShell::update(double dt)
109 if (!_impl->get_inited()) {
110 // Check for scenery around the aircraft.
111 double lon = fgGetDouble("/sim/presets/longitude-deg");
112 double lat = fgGetDouble("/sim/presets/latitude-deg");
114 double range = 1000.0; // in metres
115 SGGeod geod = SGGeod::fromDeg(lon, lat);
116 if (globals->get_scenery()->scenery_available(geod, range)) {
117 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
119 if (_impl->get_bound()) {
124 evil_global_fdm_state = _impl;
125 fgSetBool("/sim/signals/fdm-initialized", true);
129 if (!_impl->get_inited()) {
130 return; // still waiting
133 // pull environmental data in, since the FDMs are lazy
134 _impl->set_Velocities_Local_Airmass(
135 _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
136 _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
137 _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
139 if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
140 // convert from Rankine to Celsius
141 double tempDegC = _props->getDoubleValue("environment/temperature-degc");
142 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
144 // convert from inHG to PSF
145 double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
146 _impl->set_Static_pressure(pressureInHg * 70.726566);
147 // keep in slugs/ft^3
148 _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
151 bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
152 if (doLog != _dataLogging) {
153 _dataLogging = doLog;
154 _impl->ToggleDataLogging(doLog);
157 // FIXME - replay manager should handle most of this
158 int replayState = fgGetInt("/sim/freeze/replay-state", 0);
159 if (replayState == 0) {
160 _impl->update(dt); // normal code path
161 } else if (replayState == 1) {
162 // should be inside FGReplay!
163 SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
164 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
165 r->replay( replay_time->getDoubleValue() );
166 replay_time->setDoubleValue( replay_time->getDoubleValue()
168 * fgGetInt("/sim/speed-up") ) );
170 } else if (replayState == 2) {
171 // paused replay, no-op
173 throw sg_range_exception("unknown FGReplay state");
177 void FDMShell::createImplementation()
181 double dt = 1.0 / fgGetInt("/sim/model-hz");
182 string model = fgGetString("/sim/flight-model");
184 if ( model == "larcsim" ) {
185 _impl = new FGLaRCsim( dt );
186 } else if ( model == "jsb" ) {
187 _impl = new FGJSBsim( dt );
189 } else if ( model == "ada" ) {
190 _impl = new FGADA( dt );
191 } else if ( model == "acms" ) {
192 _impl = new FGACMS( dt );
193 } else if ( model == "balloon" ) {
194 _impl = new FGBalloonSim( dt );
195 } else if ( model == "magic" ) {
196 _impl = new FGMagicCarpet( dt );
198 } else if ( model == "ufo" ) {
199 _impl = new FGUFO( dt );
200 } else if ( model == "external" ) {
201 // external is a synonym for "--fdm=null" and is
202 // maintained here for backwards compatibility
203 _impl = new FGNullFDM( dt );
204 } else if ( model.find("network") == 0 ) {
205 string host = "localhost";
209 string net_options = model.substr(8);
210 string::size_type begin, end;
213 end = net_options.find( ",", begin );
214 if ( end != string::npos ) {
215 host = net_options.substr(begin, end - begin);
219 end = net_options.find( ",", begin );
220 if ( end != string::npos ) {
221 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
225 end = net_options.find( ",", begin );
226 if ( end != string::npos ) {
227 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
231 end = net_options.find( ",", begin );
232 if ( end != string::npos ) {
233 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
236 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
237 } else if ( model.find("pipe") == 0 ) {
238 // /* old */ string pipe_path = model.substr(5);
239 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
240 string pipe_path = "";
241 string pipe_protocol = "";
242 string pipe_options = model.substr(5);
243 string::size_type begin, end;
246 end = pipe_options.find( ",", begin );
247 if ( end != string::npos ) {
248 pipe_path = pipe_options.substr(begin, end - begin);
251 // protocol (last option)
252 pipe_protocol = pipe_options.substr(begin);
253 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
254 } else if ( model == "null" ) {
255 _impl = new FGNullFDM( dt );
256 } else if ( model == "yasim" ) {
257 _impl = new YASim( dt );
259 throw sg_exception(string("Unrecognized flight model '") + model
260 + "', cannot init flight dynamics model.");