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