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