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