]> git.mxchange.org Git - flightgear.git/blob - src/FDM/fdm_shell.cxx
Minor FDM shell performance improvement
[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 #include <FDM/JSBSim/JSBSim.hxx>
47 #include <FDM/LaRCsim/LaRCsim.hxx>
48 #include <FDM/UFO.hxx>
49 #include <FDM/NullFDM.hxx>
50 #include <FDM/YASim/YASim.hxx>
51
52
53 /*
54  * Evil global variable required by Network/FGNative,
55  * see that class for more information
56  */
57 FGInterface* evil_global_fdm_state = NULL;
58
59 FDMShell::FDMShell() :
60   _tankProperties( fgGetNode("/consumables/fuel", true) ),
61   _impl(NULL),
62   _dataLogging(false)
63 {
64 }
65
66 FDMShell::~FDMShell()
67 {
68   delete _impl;
69 }
70
71 void FDMShell::init()
72 {
73   _props = globals->get_props();
74   fgSetBool("/sim/fdm-initialized", false);
75
76   _wind_north       = _props->getNode("environment/wind-from-north-fps",    true);
77   _wind_east        = _props->getNode("environment/wind-from-east-fps",     true);
78   _wind_down        = _props->getNode("environment/wind-from-down-fps",     true);
79   _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
80   _temp_degc        = _props->getNode("environment/temperature-degc",       true);
81   _pressure_inhg    = _props->getNode("environment/pressure-inhg",          true);
82   _density_slugft   = _props->getNode("environment/density-slugft3",        true);
83   _data_logging     = _props->getNode("/sim/temp/fdm-data-logging",         true);
84
85   createImplementation();
86 }
87
88 void FDMShell::reinit()
89 {
90   if (_impl) {
91     fgSetBool("/sim/fdm-initialized", false);
92     evil_global_fdm_state = NULL;
93     _impl->unbind();
94     delete _impl;
95     _impl = NULL;
96   }
97   
98   init();
99 }
100
101 void FDMShell::bind()
102 {
103   _tankProperties.bind();
104   if (_impl && _impl->get_inited()) {
105     if (_impl->get_bound()) {
106       throw sg_exception("FDMShell::bind of bound FGInterface impl");
107     }
108     _impl->bind();
109   }
110 }
111
112 void FDMShell::unbind()
113 {
114   if( _impl ) _impl->unbind();
115   _tankProperties.unbind();
116 }
117
118 void FDMShell::update(double dt)
119 {
120   if (!_impl) {
121     return;
122   }
123   
124   if (!_impl->get_inited()) {
125     // Check for scenery around the aircraft.
126     double lon = fgGetDouble("/sim/presets/longitude-deg");
127     double lat = fgGetDouble("/sim/presets/latitude-deg");
128         
129     double range = 1000.0; // in meters
130     SGGeod geod = SGGeod::fromDeg(lon, lat);
131     if (globals->get_scenery()->scenery_available(geod, range)) {
132         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
133         _impl->init();
134         if (_impl->get_bound()) {
135           _impl->unbind();
136         }
137         _impl->bind();
138         
139         evil_global_fdm_state = _impl;
140         fgSetBool("/sim/fdm-initialized", true);
141         fgSetBool("/sim/signals/fdm-initialized", true);
142     }
143   }
144
145   if (!_impl->get_inited()) {
146     return; // still waiting
147   }
148
149 // pull environmental data in, since the FDMs are lazy
150   _impl->set_Velocities_Local_Airmass(
151           _wind_north->getDoubleValue(),
152           _wind_east->getDoubleValue(),
153           _wind_down->getDoubleValue());
154
155   if (_control_fdm_atmo->getBoolValue()) {
156     // convert from Rankine to Celsius
157     double tempDegC = _temp_degc->getDoubleValue();
158     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
159     
160     // convert from inHG to PSF
161     double pressureInHg = _pressure_inhg->getDoubleValue();
162     _impl->set_Static_pressure(pressureInHg * 70.726566);
163     // keep in slugs/ft^3
164     _impl->set_Density(_density_slugft->getDoubleValue());
165   }
166
167   bool doLog = _data_logging->getBoolValue();
168   if (doLog != _dataLogging) {
169     _dataLogging = doLog;
170     _impl->ToggleDataLogging(doLog);
171   }
172
173   if (!_impl->is_suspended())
174       _impl->update(dt);
175 }
176
177 void FDMShell::createImplementation()
178 {
179   assert(!_impl);
180   
181   double dt = 1.0 / fgGetInt("/sim/model-hz");
182   string model = fgGetString("/sim/flight-model");
183
184     if ( model == "larcsim" ) {
185         _impl = new FGLaRCsim( dt );
186     } else if ( model == "jsb" ) {
187         _impl = new FGJSBsim( dt );
188 #ifdef ENABLE_SP_FDM
189     } else if ( model == "ada" ) {
190         _impl = new FGADA( dt );
191     } else if ( model == "acms" ) {
192         _impl = new FGACMS( dt );
193     } else if ( model == "balloon" ) {
194         _impl = new FGBalloonSim( dt );
195     } else if ( model == "magic" ) {
196         _impl = new FGMagicCarpet( dt );
197 #endif
198     } else if ( model == "ufo" ) {
199         _impl = new FGUFO( dt );
200     } else if ( model == "external" ) {
201         // external is a synonym for "--fdm=null" and is
202         // maintained here for backwards compatibility
203         _impl = new FGNullFDM( dt );
204     } else if ( model.find("network") == 0 ) {
205         string host = "localhost";
206         int port1 = 5501;
207         int port2 = 5502;
208         int port3 = 5503;
209         string net_options = model.substr(8);
210         string::size_type begin, end;
211         begin = 0;
212         // host
213         end = net_options.find( ",", begin );
214         if ( end != string::npos ) {
215             host = net_options.substr(begin, end - begin);
216             begin = end + 1;
217         }
218         // port1
219         end = net_options.find( ",", begin );
220         if ( end != string::npos ) {
221             port1 = atoi( net_options.substr(begin, end - begin).c_str() );
222             begin = end + 1;
223         }
224         // port2
225         end = net_options.find( ",", begin );
226         if ( end != string::npos ) {
227             port2 = atoi( net_options.substr(begin, end - begin).c_str() );
228             begin = end + 1;
229         }
230         // port3
231         end = net_options.find( ",", begin );
232         if ( end != string::npos ) {
233             port3 = atoi( net_options.substr(begin, end - begin).c_str() );
234             begin = end + 1;
235         }
236         _impl = new FGExternalNet( dt, host, port1, port2, port3 );
237     } else if ( model.find("pipe") == 0 ) {
238         // /* old */ string pipe_path = model.substr(5);
239         // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
240         string pipe_path = "";
241         string pipe_protocol = "";
242         string pipe_options = model.substr(5);
243         string::size_type begin, end;
244         begin = 0;
245         // pipe file path
246         end = pipe_options.find( ",", begin );
247         if ( end != string::npos ) {
248             pipe_path = pipe_options.substr(begin, end - begin);
249             begin = end + 1;
250         }
251         // protocol (last option)
252         pipe_protocol = pipe_options.substr(begin);
253         _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
254     } else if ( model == "null" ) {
255         _impl = new FGNullFDM( dt );
256     } else if ( model == "yasim" ) {
257         _impl = new YASim( dt );
258     } else {
259         throw sg_exception(string("Unrecognized flight model '") + model
260                + "', cannot init flight dynamics model.");
261     }
262
263 }
264
265 /*
266  * Return FDM subsystem.
267  */
268
269 SGSubsystem* FDMShell::getFDM()
270 {
271     /* FIXME we could drop/replace this method, when _impl was a added
272      * to the global subsystem manager - like other proper subsystems... */
273     return _impl;
274 }