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