]> git.mxchange.org Git - flightgear.git/blob - src/FDM/fdm_shell.cxx
MapWidget: silence compiler warning
[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   _dataLogging(false)
67 {
68 }
69
70 FDMShell::~FDMShell()
71 {
72     SG_LOG(SG_GENERAL, SG_INFO, "destorying FDM shell");
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::shutdown()
94 {
95     if (_impl) {
96         fgSetBool("/sim/fdm-initialized", false);
97         _impl->unbind();
98         _impl.clear();
99     }
100     
101     _props.clear();
102     _wind_north.clear();
103     _wind_east.clear();
104     _wind_down.clear();
105     _control_fdm_atmo.clear();
106     _temp_degc.clear();
107     _pressure_inhg.clear();
108     _density_slugft .clear();
109     _data_logging.clear();
110     _replay_master.clear();
111 }
112
113 void FDMShell::reinit()
114 {
115   shutdown();
116   init();
117 }
118
119 void FDMShell::bind()
120 {
121   _tankProperties.bind();
122   if (_impl && _impl->get_inited()) {
123     if (_impl->get_bound()) {
124       throw sg_exception("FDMShell::bind of bound FGInterface impl");
125     }
126     _impl->bind();
127   }
128 }
129
130 void FDMShell::unbind()
131 {
132   if( _impl ) _impl->unbind();
133   _tankProperties.unbind();
134 }
135
136 void FDMShell::update(double dt)
137 {
138   if (!_impl) {
139     return;
140   }
141   
142   if (!_impl->get_inited()) {
143     // Check for scenery around the aircraft.
144     double lon = fgGetDouble("/sim/presets/longitude-deg");
145     double lat = fgGetDouble("/sim/presets/latitude-deg");
146         
147     double range = 1000.0; // in meters
148     SGGeod geod = SGGeod::fromDeg(lon, lat);
149     if (globals->get_scenery()->scenery_available(geod, range)) {
150         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
151         _impl->init();
152         if (_impl->get_bound()) {
153           _impl->unbind();
154         }
155         _impl->bind();
156         
157         fgSetBool("/sim/fdm-initialized", true);
158         fgSetBool("/sim/signals/fdm-initialized", true);
159     }
160   }
161
162   if (!_impl->get_inited()) {
163     return; // still waiting
164   }
165
166 // pull environmental data in, since the FDMs are lazy
167   _impl->set_Velocities_Local_Airmass(
168           _wind_north->getDoubleValue(),
169           _wind_east->getDoubleValue(),
170           _wind_down->getDoubleValue());
171
172   if (_control_fdm_atmo->getBoolValue()) {
173     // convert from Rankine to Celsius
174     double tempDegC = _temp_degc->getDoubleValue();
175     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
176     
177     // convert from inHG to PSF
178     double pressureInHg = _pressure_inhg->getDoubleValue();
179     _impl->set_Static_pressure(pressureInHg * 70.726566);
180     // keep in slugs/ft^3
181     _impl->set_Density(_density_slugft->getDoubleValue());
182   }
183
184   bool doLog = _data_logging->getBoolValue();
185   if (doLog != _dataLogging) {
186     _dataLogging = doLog;
187     _impl->ToggleDataLogging(doLog);
188   }
189
190   switch(_replay_master->getIntValue())
191   {
192       case 0:
193           // normal FDM operation
194           _impl->update(dt);
195           break;
196       case 3:
197           // resume FDM operation at current replay position
198           _impl->reinit();
199           break;
200       default:
201           // replay is active
202           break;
203   }
204 }
205
206 FGInterface* FDMShell::getInterface() const
207 {
208     return _impl;
209 }
210
211 void FDMShell::createImplementation()
212 {
213   assert(!_impl);
214   
215   double dt = 1.0 / fgGetInt("/sim/model-hz");
216   string model = fgGetString("/sim/flight-model");
217
218   bool fdmUnavailable = false;
219
220   if ( model == "ufo" ) {
221     _impl = new FGUFO( dt );
222   } else if ( model == "external" ) {
223     // external is a synonym for "--fdm=null" and is
224     // maintained here for backwards compatibility
225     _impl = new FGNullFDM( dt );
226   } else if ( model.find("network") == 0 ) {
227     string host = "localhost";
228     int port1 = 5501;
229     int port2 = 5502;
230     int port3 = 5503;
231     string net_options = model.substr(8);
232     string::size_type begin, end;
233     begin = 0;
234     // host
235     end = net_options.find( ",", begin );
236     if ( end != string::npos ) {
237       host = net_options.substr(begin, end - begin);
238       begin = end + 1;
239     }
240     // port1
241     end = net_options.find( ",", begin );
242     if ( end != string::npos ) {
243       port1 = atoi( net_options.substr(begin, end - begin).c_str() );
244       begin = end + 1;
245     }
246     // port2
247     end = net_options.find( ",", begin );
248     if ( end != string::npos ) {
249       port2 = atoi( net_options.substr(begin, end - begin).c_str() );
250       begin = end + 1;
251     }
252     // port3
253     end = net_options.find( ",", begin );
254     if ( end != string::npos ) {
255       port3 = atoi( net_options.substr(begin, end - begin).c_str() );
256       begin = end + 1;
257     }
258     _impl = new FGExternalNet( dt, host, port1, port2, port3 );
259   } else if ( model.find("pipe") == 0 ) {
260     // /* old */ string pipe_path = model.substr(5);
261     // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
262     string pipe_path = "";
263     string pipe_protocol = "";
264     string pipe_options = model.substr(5);
265     string::size_type begin, end;
266     begin = 0;
267     // pipe file path
268     end = pipe_options.find( ",", begin );
269     if ( end != string::npos ) {
270       pipe_path = pipe_options.substr(begin, end - begin);
271       begin = end + 1;
272     }
273     // protocol (last option)
274     pipe_protocol = pipe_options.substr(begin);
275     _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
276   } else if ( model == "null" ) {
277     _impl = new FGNullFDM( dt );
278   }
279     else if ( model == "larcsim" ) {
280 #ifdef ENABLE_LARCSIM
281         _impl = new FGLaRCsim( dt );
282 #else
283         fdmUnavailable = true;
284 #endif
285     }
286     else if ( model == "jsb" ) {
287 #ifdef ENABLE_JSBSIM
288         _impl = new FGJSBsim( dt );
289 #else
290         fdmUnavailable = true;
291 #endif
292     }
293 #ifdef ENABLE_SP_FDM
294     else if ( model == "ada" ) {
295         _impl = new FGADA( dt );
296     } else if ( model == "acms" ) {
297         _impl = new FGACMS( dt );
298     } else if ( model == "balloon" ) {
299         _impl = new FGBalloonSim( dt );
300     } else if ( model == "magic" ) {
301         _impl = new FGMagicCarpet( dt );
302     }
303 #else
304     else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
305     {
306         fdmUnavailable = true;
307     }
308 #endif
309     else if ( model == "yasim" ) {
310 #ifdef ENABLE_YASIM
311         _impl = new YASim( dt );
312 #else
313         fdmUnavailable = true;
314 #endif
315     } else {
316         throw sg_exception(string("Unrecognized flight model '") + model
317                + "', cannot init flight dynamics model.");
318     }
319
320     if (fdmUnavailable)
321     {
322         // FDM type is known, but its support was disabled at compile-time.
323         throw sg_exception(string("Support for flight model '") + model
324                 + ("' is not available with this binary (deprecated/disabled).\n"
325                    "If you still need it, please rebuild FlightGear and enable its support."));
326     }
327 }