]> git.mxchange.org Git - flightgear.git/blob - src/FDM/fdm_shell.cxx
Synchronized FG with the removal of 'using std::*' in simgear's easyxml
[flightgear.git] / src / FDM / fdm_shell.cxx
1 // fdm_shell.cxx -- encapsulate FDM implementations as well-behaved subsystems
2 //
3 // Written by James Turner, started June 2010.
4 //
5 // Copyright (C) 2010  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <cassert>
28 #include <simgear/structure/exception.hxx>
29
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>
36
37 // all the FDMs, since we are the factory method
38 #ifdef ENABLE_SP_FDM
39 #include <FDM/SP/ADA.hxx>
40 #include <FDM/SP/ACMS.hxx>
41 #include <FDM/SP/MagicCarpet.hxx>
42 #include <FDM/SP/Balloon.h>
43 #endif
44 #include <FDM/ExternalNet/ExternalNet.hxx>
45 #include <FDM/ExternalPipe/ExternalPipe.hxx>
46
47 #ifdef ENABLE_JSBSIM
48 #include <FDM/JSBSim/JSBSim.hxx>
49 #endif
50
51 #ifdef ENABLE_LARCSIM
52 #include <FDM/LaRCsim/LaRCsim.hxx>
53 #endif
54
55 #include <FDM/UFO.hxx>
56 #include <FDM/NullFDM.hxx>
57
58 #ifdef ENABLE_YASIM
59 #include <FDM/YASim/YASim.hxx>
60 #endif
61
62 using std::string;
63
64 FDMShell::FDMShell() :
65   _tankProperties( fgGetNode("/consumables/fuel", true) ),
66   _impl(NULL),
67   _dataLogging(false)
68 {
69 }
70
71 FDMShell::~FDMShell()
72 {
73   delete _impl;
74 }
75
76 void FDMShell::init()
77 {
78   _props = globals->get_props();
79   fgSetBool("/sim/fdm-initialized", false);
80
81   _wind_north       = _props->getNode("environment/wind-from-north-fps",    true);
82   _wind_east        = _props->getNode("environment/wind-from-east-fps",     true);
83   _wind_down        = _props->getNode("environment/wind-from-down-fps",     true);
84   _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
85   _temp_degc        = _props->getNode("environment/temperature-degc",       true);
86   _pressure_inhg    = _props->getNode("environment/pressure-inhg",          true);
87   _density_slugft   = _props->getNode("environment/density-slugft3",        true);
88   _data_logging     = _props->getNode("/sim/temp/fdm-data-logging",         true);
89   _replay_master    = _props->getNode("/sim/freeze/replay-state",           true);
90
91   createImplementation();
92 }
93
94 void FDMShell::reinit()
95 {
96   if (_impl) {
97     fgSetBool("/sim/fdm-initialized", false);
98     _impl->unbind();
99     delete _impl;
100     _impl = NULL;
101   }
102   
103   init();
104 }
105
106 void FDMShell::bind()
107 {
108   _tankProperties.bind();
109   if (_impl && _impl->get_inited()) {
110     if (_impl->get_bound()) {
111       throw sg_exception("FDMShell::bind of bound FGInterface impl");
112     }
113     _impl->bind();
114   }
115 }
116
117 void FDMShell::unbind()
118 {
119   if( _impl ) _impl->unbind();
120   _tankProperties.unbind();
121 }
122
123 void FDMShell::update(double dt)
124 {
125   if (!_impl) {
126     return;
127   }
128   
129   if (!_impl->get_inited()) {
130     // Check for scenery around the aircraft.
131     double lon = fgGetDouble("/sim/presets/longitude-deg");
132     double lat = fgGetDouble("/sim/presets/latitude-deg");
133         
134     double range = 1000.0; // in meters
135     SGGeod geod = SGGeod::fromDeg(lon, lat);
136     if (globals->get_scenery()->scenery_available(geod, range)) {
137         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
138         _impl->init();
139         if (_impl->get_bound()) {
140           _impl->unbind();
141         }
142         _impl->bind();
143         
144         fgSetBool("/sim/fdm-initialized", true);
145         fgSetBool("/sim/signals/fdm-initialized", true);
146     }
147   }
148
149   if (!_impl->get_inited()) {
150     return; // still waiting
151   }
152
153 // pull environmental data in, since the FDMs are lazy
154   _impl->set_Velocities_Local_Airmass(
155           _wind_north->getDoubleValue(),
156           _wind_east->getDoubleValue(),
157           _wind_down->getDoubleValue());
158
159   if (_control_fdm_atmo->getBoolValue()) {
160     // convert from Rankine to Celsius
161     double tempDegC = _temp_degc->getDoubleValue();
162     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
163     
164     // convert from inHG to PSF
165     double pressureInHg = _pressure_inhg->getDoubleValue();
166     _impl->set_Static_pressure(pressureInHg * 70.726566);
167     // keep in slugs/ft^3
168     _impl->set_Density(_density_slugft->getDoubleValue());
169   }
170
171   bool doLog = _data_logging->getBoolValue();
172   if (doLog != _dataLogging) {
173     _dataLogging = doLog;
174     _impl->ToggleDataLogging(doLog);
175   }
176
177   switch(_replay_master->getIntValue())
178   {
179       case 0:
180           // normal FDM operation
181           _impl->update(dt);
182           break;
183       case 3:
184           // resume FDM operation at current replay position
185           _impl->reinit();
186           break;
187       default:
188           // replay is active
189           break;
190   }
191 }
192
193 FGInterface* FDMShell::getInterface() const
194 {
195     return _impl;
196 }
197
198 void FDMShell::createImplementation()
199 {
200   assert(!_impl);
201   
202   double dt = 1.0 / fgGetInt("/sim/model-hz");
203   string model = fgGetString("/sim/flight-model");
204
205   bool fdmUnavailable = false;
206
207   if ( model == "ufo" ) {
208     _impl = new FGUFO( dt );
209   } else if ( model == "external" ) {
210     // external is a synonym for "--fdm=null" and is
211     // maintained here for backwards compatibility
212     _impl = new FGNullFDM( dt );
213   } else if ( model.find("network") == 0 ) {
214     string host = "localhost";
215     int port1 = 5501;
216     int port2 = 5502;
217     int port3 = 5503;
218     string net_options = model.substr(8);
219     string::size_type begin, end;
220     begin = 0;
221     // host
222     end = net_options.find( ",", begin );
223     if ( end != string::npos ) {
224       host = net_options.substr(begin, end - begin);
225       begin = end + 1;
226     }
227     // port1
228     end = net_options.find( ",", begin );
229     if ( end != string::npos ) {
230       port1 = atoi( net_options.substr(begin, end - begin).c_str() );
231       begin = end + 1;
232     }
233     // port2
234     end = net_options.find( ",", begin );
235     if ( end != string::npos ) {
236       port2 = atoi( net_options.substr(begin, end - begin).c_str() );
237       begin = end + 1;
238     }
239     // port3
240     end = net_options.find( ",", begin );
241     if ( end != string::npos ) {
242       port3 = atoi( net_options.substr(begin, end - begin).c_str() );
243       begin = end + 1;
244     }
245     _impl = new FGExternalNet( dt, host, port1, port2, port3 );
246   } else if ( model.find("pipe") == 0 ) {
247     // /* old */ string pipe_path = model.substr(5);
248     // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
249     string pipe_path = "";
250     string pipe_protocol = "";
251     string pipe_options = model.substr(5);
252     string::size_type begin, end;
253     begin = 0;
254     // pipe file path
255     end = pipe_options.find( ",", begin );
256     if ( end != string::npos ) {
257       pipe_path = pipe_options.substr(begin, end - begin);
258       begin = end + 1;
259     }
260     // protocol (last option)
261     pipe_protocol = pipe_options.substr(begin);
262     _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
263   } else if ( model == "null" ) {
264     _impl = new FGNullFDM( dt );
265   }
266     else if ( model == "larcsim" ) {
267 #ifdef ENABLE_LARCSIM
268         _impl = new FGLaRCsim( dt );
269 #else
270         fdmUnavailable = true;
271 #endif
272     }
273     else if ( model == "jsb" ) {
274 #ifdef ENABLE_JSBSIM
275         _impl = new FGJSBsim( dt );
276 #else
277         fdmUnavailable = true;
278 #endif
279     }
280 #ifdef ENABLE_SP_FDM
281     else if ( model == "ada" ) {
282         _impl = new FGADA( dt );
283     } else if ( model == "acms" ) {
284         _impl = new FGACMS( dt );
285     } else if ( model == "balloon" ) {
286         _impl = new FGBalloonSim( dt );
287     } else if ( model == "magic" ) {
288         _impl = new FGMagicCarpet( dt );
289     }
290 #else
291     else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
292     {
293         fdmUnavailable = true;
294     }
295 #endif
296     else if ( model == "yasim" ) {
297 #ifdef ENABLE_YASIM
298         _impl = new YASim( dt );
299 #else
300         fdmUnavailable = true;
301 #endif
302     } else {
303         throw sg_exception(string("Unrecognized flight model '") + model
304                + "', cannot init flight dynamics model.");
305     }
306
307     if (fdmUnavailable)
308     {
309         // FDM type is known, but its support was disabled at compile-time.
310         throw sg_exception(string("Support for flight model '") + model
311                 + ("' is not available with this binary (deprecated/disabled).\n"
312                    "If you still need it, please rebuild FlightGear and enable its support."));
313     }
314 }