]> git.mxchange.org Git - flightgear.git/blob - src/FDM/fdm_shell.cxx
Fix bug 141, by ensuring certain subsystems are assigned to the 'post FDM' group...
[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 <simgear/structure/exception.hxx>
28
29 #include <FDM/fdm_shell.hxx>
30 #include <FDM/flight.hxx>
31 #include <Aircraft/replay.hxx>
32 #include <Main/globals.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Scenery/scenery.hxx>
35
36 // all the FDMs, since we are the factory method
37 #if ENABLE_SP_FDM
38 #include <FDM/SP/ADA.hxx>
39 #include <FDM/SP/ACMS.hxx>
40 #include <FDM/SP/MagicCarpet.hxx>
41 #include <FDM/SP/Balloon.h>
42 #endif
43 #include <FDM/ExternalNet/ExternalNet.hxx>
44 #include <FDM/ExternalPipe/ExternalPipe.hxx>
45 #include <FDM/JSBSim/JSBSim.hxx>
46 #include <FDM/LaRCsim/LaRCsim.hxx>
47 #include <FDM/UFO.hxx>
48 #include <FDM/NullFDM.hxx>
49 #include <FDM/YASim/YASim.hxx>
50
51 /*
52  * Evil global variable required by Network/FGNative,
53  * see that class for more information
54  */
55 FGInterface* evil_global_fdm_state = NULL;
56
57 FDMShell::FDMShell() :
58   _impl(NULL),
59   _dataLogging(false)
60 {
61 }
62
63 FDMShell::~FDMShell()
64 {
65   delete _impl;
66 }
67
68 void FDMShell::init()
69 {
70   _props = globals->get_props();
71   createImplementation();
72 }
73
74 void FDMShell::reinit()
75 {
76   if (_impl) {
77     fgSetBool("/sim/signals/fdm-initialized", false);
78     evil_global_fdm_state = NULL;
79     _impl->unbind();
80     delete _impl;
81     _impl = NULL;
82   }
83   
84   init();
85 }
86
87 void FDMShell::bind()
88 {
89   if (_impl && _impl->get_inited()) {
90     if (_impl->get_bound()) {
91       throw sg_exception("FDMShell::bind of bound FGInterface impl");
92     }
93     
94     _impl->bind();
95   }
96 }
97
98 void FDMShell::unbind()
99 {
100   _impl->unbind();
101 }
102
103 void FDMShell::update(double dt)
104 {
105   if (!_impl) {
106     return;
107   }
108   
109   if (!_impl->get_inited()) {
110     // Check for scenery around the aircraft.
111     double lon = fgGetDouble("/sim/presets/longitude-deg");
112     double lat = fgGetDouble("/sim/presets/latitude-deg");
113         
114     double range = 1000.0; // in metres
115     SGGeod geod = SGGeod::fromDeg(lon, lat);
116     if (globals->get_scenery()->scenery_available(geod, range)) {
117         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
118         _impl->init();
119         if (_impl->get_bound()) {
120           _impl->unbind();
121         }
122         _impl->bind();
123         
124         evil_global_fdm_state = _impl;
125         fgSetBool("/sim/signals/fdm-initialized", true);
126     }
127   }
128
129   if (!_impl->get_inited()) {
130     return; // still waiting
131   }
132
133 // pull environmental data in, since the FDMs are lazy
134   _impl->set_Velocities_Local_Airmass(
135       _props->getDoubleValue("environment/wind-from-north-fps", 0.0),
136                         _props->getDoubleValue("environment/wind-from-east-fps", 0.0),
137                         _props->getDoubleValue("environment/wind-from-down-fps", 0.0));
138
139   if (_props->getBoolValue("environment/params/control-fdm-atmosphere")) {
140     // convert from Rankine to Celsius
141     double tempDegC = _props->getDoubleValue("environment/temperature-degc");
142     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
143     
144     // convert from inHG to PSF
145     double pressureInHg = _props->getDoubleValue("environment/pressure-inhg");
146     _impl->set_Static_pressure(pressureInHg * 70.726566);
147     // keep in slugs/ft^3
148     _impl->set_Density(_props->getDoubleValue("environment/density-slugft3"));
149   }
150
151   bool doLog = _props->getBoolValue("/sim/temp/fdm-data-logging", false);
152   if (doLog != _dataLogging) {
153     _dataLogging = doLog;
154     _impl->ToggleDataLogging(doLog);
155   }
156
157 // FIXME - replay manager should handle most of this           
158   int replayState = fgGetInt("/sim/freeze/replay-state", 0);
159   if (replayState == 0) {
160     _impl->update(dt); // normal code path
161   } else if (replayState == 1) {
162     // should be inside FGReplay!
163     SGPropertyNode* replay_time = fgGetNode("/sim/replay/time", true);
164     FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
165     r->replay( replay_time->getDoubleValue() );
166     replay_time->setDoubleValue( replay_time->getDoubleValue()
167                                              + ( dt
168                                                  * fgGetInt("/sim/speed-up") ) );
169   
170   } else if (replayState == 2) {
171     // paused replay, no-op
172   } else {
173     throw sg_range_exception("unknown FGReplay state");
174   }  
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 #if 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