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