5 #include <simgear/structure/exception.hxx>
7 #include <FDM/fdm_shell.hxx>
8 #include <FDM/flight.hxx>
9 #include <Aircraft/replay.hxx>
10 #include <Main/globals.hxx>
11 #include <Main/fg_props.hxx>
12 #include <Scenery/scenery.hxx>
14 // all the FDMs, since we are the factory method
16 #include <FDM/SP/ADA.hxx>
17 #include <FDM/SP/ACMS.hxx>
18 #include <FDM/SP/MagicCarpet.hxx>
19 #include <FDM/SP/Balloon.h>
21 #include <FDM/ExternalNet/ExternalNet.hxx>
22 #include <FDM/ExternalPipe/ExternalPipe.hxx>
23 #include <FDM/JSBSim/JSBSim.hxx>
24 #include <FDM/LaRCsim/LaRCsim.hxx>
25 #include <FDM/UFO.hxx>
26 #include <FDM/NullFDM.hxx>
27 #include <FDM/YASim/YASim.hxx>
30 * Evil global variable required by Network/FGNative,
31 * see that class for more information
33 FGInterface* evil_global_fdm_state = NULL;
35 FDMShell::FDMShell() :
48 _props = globals->get_props();
49 createImplementation();
52 void FDMShell::reinit()
55 fgSetBool("/sim/signals/fdm-initialized", false);
56 evil_global_fdm_state = NULL;
67 if (_impl && _impl->get_inited()) {
68 if (_impl->get_bound()) {
69 throw sg_exception("FDMShell::bind of bound FGInterface impl");
76 void FDMShell::unbind()
81 void FDMShell::update(double dt)
87 if (!_impl->get_inited()) {
88 // Check for scenery around the aircraft.
89 double lon = fgGetDouble("/sim/presets/longitude-deg");
90 double lat = fgGetDouble("/sim/presets/latitude-deg");
92 double range = 1000.0; // in metres
93 SGGeod geod = SGGeod::fromDeg(lon, lat);
94 if (globals->get_scenery()->scenery_available(geod, range)) {
95 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
97 if (_impl->get_bound()) {
102 evil_global_fdm_state = _impl;
103 fgSetBool("/sim/signals/fdm-initialized", true);
107 if (!_impl->get_inited()) {
108 return; // still waiting
111 // pull environmental data in, since the FDMs are lazy
112 _impl->set_Velocities_Local_Airmass(
113 _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
114 _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
115 _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
117 if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
118 // convert from Rankine to Celsius
119 double tempDegC = _props->getDoubleValue("environment/temperature-degc");
120 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
122 // convert from inHG to PSF
123 double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
124 _impl->set_Static_pressure(pressureInHg * 70.726566);
125 // keep in slugs/ft^3
126 _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
129 bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
130 if (doLog != _dataLogging) {
131 _dataLogging = doLog;
132 _impl->ToggleDataLogging(doLog);
135 // FIXME - replay manager should handle most of this
136 int replayState = fgGetInt("/sim/freeze/replay-state", 0);
137 if (replayState == 0) {
138 _impl->update(dt); // normal code path
139 } else if (replayState == 1) {
140 // should be inside FGReplay!
141 SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
142 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
143 r->replay( replay_time->getDoubleValue() );
144 replay_time->setDoubleValue( replay_time->getDoubleValue()
146 * fgGetInt("/sim/speed-up") ) );
148 } else if (replayState == 2) {
149 // paused replay, no-op
151 throw sg_range_exception("unknown FGReplay state");
155 void FDMShell::createImplementation()
159 double dt = 1.0 / fgGetInt("/sim/model-hz");
160 string model = fgGetString("/sim/flight-model");
162 if ( model == "larcsim" ) {
163 _impl = new FGLaRCsim( dt );
164 } else if ( model == "jsb" ) {
165 _impl = new FGJSBsim( dt );
167 } else if ( model == "ada" ) {
168 _impl = new FGADA( dt );
169 } else if ( model == "acms" ) {
170 _impl = new FGACMS( dt );
171 } else if ( model == "balloon" ) {
172 _impl = new FGBalloonSim( dt );
173 } else if ( model == "magic" ) {
174 _impl = new FGMagicCarpet( dt );
176 } else if ( model == "ufo" ) {
177 _impl = new FGUFO( dt );
178 } else if ( model == "external" ) {
179 // external is a synonym for "--fdm=null" and is
180 // maintained here for backwards compatibility
181 _impl = new FGNullFDM( dt );
182 } else if ( model.find("network") == 0 ) {
183 string host = "localhost";
187 string net_options = model.substr(8);
188 string::size_type begin, end;
191 end = net_options.find( ",", begin );
192 if ( end != string::npos ) {
193 host = net_options.substr(begin, end - begin);
197 end = net_options.find( ",", begin );
198 if ( end != string::npos ) {
199 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
203 end = net_options.find( ",", begin );
204 if ( end != string::npos ) {
205 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
209 end = net_options.find( ",", begin );
210 if ( end != string::npos ) {
211 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
214 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
215 } else if ( model.find("pipe") == 0 ) {
216 // /* old */ string pipe_path = model.substr(5);
217 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
218 string pipe_path = "";
219 string pipe_protocol = "";
220 string pipe_options = model.substr(5);
221 string::size_type begin, end;
224 end = pipe_options.find( ",", begin );
225 if ( end != string::npos ) {
226 pipe_path = pipe_options.substr(begin, end - begin);
229 // protocol (last option)
230 pipe_protocol = pipe_options.substr(begin);
231 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
232 } else if ( model == "null" ) {
233 _impl = new FGNullFDM( dt );
234 } else if ( model == "yasim" ) {
235 _impl = new YASim( dt );
237 throw sg_exception(string("Unrecognized flight model '") + model
238 + "', cannot init flight dynamics model.");