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