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