]> git.mxchange.org Git - flightgear.git/blob - src/FDM/fdm_shell.cxx
commradio: improvements for atis speech
[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 #include <simgear/props/props_io.hxx>
30
31 #include <FDM/fdm_shell.hxx>
32 #include <FDM/flight.hxx>
33 #include <Aircraft/replay.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Scenery/scenery.hxx>
37
38 // all the FDMs, since we are the factory method
39 #ifdef ENABLE_SP_FDM
40 #include <FDM/SP/ADA.hxx>
41 #include <FDM/SP/ACMS.hxx>
42 #include <FDM/SP/MagicCarpet.hxx>
43 #include <FDM/SP/Balloon.h>
44 #endif
45 #include <FDM/ExternalNet/ExternalNet.hxx>
46 #include <FDM/ExternalPipe/ExternalPipe.hxx>
47
48 #ifdef ENABLE_JSBSIM
49 #include <FDM/JSBSim/JSBSim.hxx>
50 #endif
51
52 #ifdef ENABLE_LARCSIM
53 #include <FDM/LaRCsim/LaRCsim.hxx>
54 #endif
55
56 #include <FDM/UFO.hxx>
57 #include <FDM/NullFDM.hxx>
58
59 #ifdef ENABLE_YASIM
60 #include <FDM/YASim/YASim.hxx>
61 #endif
62
63 using std::string;
64
65 FDMShell::FDMShell() :
66   _tankProperties( fgGetNode("/consumables/fuel", true) ),
67   _dataLogging(false)
68 {
69 }
70
71 FDMShell::~FDMShell()
72 {
73     SG_LOG(SG_GENERAL, SG_INFO, "destorying FDM shell");
74 }
75
76 void FDMShell::init()
77 {
78   _props = globals->get_props();
79   fgSetBool("/sim/fdm-initialized", false);
80
81   _wind_north       = _props->getNode("environment/wind-from-north-fps",    true);
82   _wind_east        = _props->getNode("environment/wind-from-east-fps",     true);
83   _wind_down        = _props->getNode("environment/wind-from-down-fps",     true);
84   _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
85   _temp_degc        = _props->getNode("environment/temperature-degc",       true);
86   _pressure_inhg    = _props->getNode("environment/pressure-inhg",          true);
87   _density_slugft   = _props->getNode("environment/density-slugft3",        true);
88   _data_logging     = _props->getNode("/sim/temp/fdm-data-logging",         true);
89   _replay_master    = _props->getNode("/sim/freeze/replay-state",           true);
90
91   createImplementation();
92 }
93
94 void FDMShell::postinit()
95 {
96     _initialFdmProperties = new SGPropertyNode;
97     
98     if (!copyProperties(_props->getNode("fdm", true),
99                                      _initialFdmProperties))
100     {
101         SG_LOG(SG_FLIGHT, SG_ALERT, "Failed to save initial FDM property state");
102     }
103 }
104
105 void FDMShell::shutdown()
106 {
107     if (_impl) {
108         fgSetBool("/sim/fdm-initialized", false);
109         _impl->unbind();
110         _impl.clear();
111     }
112     
113     _props.clear();
114     _wind_north.clear();
115     _wind_east.clear();
116     _wind_down.clear();
117     _control_fdm_atmo.clear();
118     _temp_degc.clear();
119     _pressure_inhg.clear();
120     _density_slugft .clear();
121     _data_logging.clear();
122     _replay_master.clear();
123 }
124
125 void FDMShell::reinit()
126 {
127   shutdown();
128   
129     if ( copyProperties(_initialFdmProperties, fgGetNode("/fdm", true)) ) {
130         SG_LOG( SG_FLIGHT, SG_INFO, "Preserved state restored successfully" );
131     } else {
132         SG_LOG( SG_FLIGHT, SG_WARN,
133                "FDM: Some errors restoring preserved state" );
134     }
135
136     
137   init();
138 }
139
140 void FDMShell::bind()
141 {
142   _tankProperties.bind();
143   if (_impl && _impl->get_inited()) {
144     if (_impl->get_bound()) {
145       throw sg_exception("FDMShell::bind of bound FGInterface impl");
146     }
147     _impl->bind();
148   }
149 }
150
151 void FDMShell::unbind()
152 {
153   if( _impl ) _impl->unbind();
154   _tankProperties.unbind();
155 }
156
157 void FDMShell::update(double dt)
158 {
159   if (!_impl) {
160     return;
161   }
162   
163   if (!_impl->get_inited()) {
164     // Check for scenery around the aircraft.
165     double lon = fgGetDouble("/sim/presets/longitude-deg");
166     double lat = fgGetDouble("/sim/presets/latitude-deg");
167         
168     double range = 1000.0; // in meters
169     SGGeod geod = SGGeod::fromDeg(lon, lat);
170     if (globals->get_scenery()->scenery_available(geod, range)) {
171         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
172         _impl->init();
173         if (_impl->get_bound()) {
174           _impl->unbind();
175         }
176         _impl->bind();
177         
178         fgSetBool("/sim/fdm-initialized", true);
179         fgSetBool("/sim/signals/fdm-initialized", true);
180     }
181   }
182
183   if (!_impl->get_inited()) {
184     return; // still waiting
185   }
186
187 // pull environmental data in, since the FDMs are lazy
188   _impl->set_Velocities_Local_Airmass(
189           _wind_north->getDoubleValue(),
190           _wind_east->getDoubleValue(),
191           _wind_down->getDoubleValue());
192
193   if (_control_fdm_atmo->getBoolValue()) {
194     // convert from Rankine to Celsius
195     double tempDegC = _temp_degc->getDoubleValue();
196     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
197     
198     // convert from inHG to PSF
199     double pressureInHg = _pressure_inhg->getDoubleValue();
200     _impl->set_Static_pressure(pressureInHg * 70.726566);
201     // keep in slugs/ft^3
202     _impl->set_Density(_density_slugft->getDoubleValue());
203   }
204
205   bool doLog = _data_logging->getBoolValue();
206   if (doLog != _dataLogging) {
207     _dataLogging = doLog;
208     _impl->ToggleDataLogging(doLog);
209   }
210
211   switch(_replay_master->getIntValue())
212   {
213       case 0:
214           // normal FDM operation
215           _impl->update(dt);
216           break;
217       case 3:
218           // resume FDM operation at current replay position
219           _impl->reinit();
220           break;
221       default:
222           // replay is active
223           break;
224   }
225 }
226
227 FGInterface* FDMShell::getInterface() const
228 {
229     return _impl;
230 }
231
232 void FDMShell::createImplementation()
233 {
234   assert(!_impl);
235   
236   double dt = 1.0 / fgGetInt("/sim/model-hz");
237   string model = fgGetString("/sim/flight-model");
238
239   bool fdmUnavailable = false;
240
241   if ( model == "ufo" ) {
242     _impl = new FGUFO( dt );
243   } else if ( model == "external" ) {
244     // external is a synonym for "--fdm=null" and is
245     // maintained here for backwards compatibility
246     _impl = new FGNullFDM( dt );
247   } else if ( model.find("network") == 0 ) {
248     string host = "localhost";
249     int port1 = 5501;
250     int port2 = 5502;
251     int port3 = 5503;
252     string net_options = model.substr(8);
253     string::size_type begin, end;
254     begin = 0;
255     // host
256     end = net_options.find( ",", begin );
257     if ( end != string::npos ) {
258       host = net_options.substr(begin, end - begin);
259       begin = end + 1;
260     }
261     // port1
262     end = net_options.find( ",", begin );
263     if ( end != string::npos ) {
264       port1 = atoi( net_options.substr(begin, end - begin).c_str() );
265       begin = end + 1;
266     }
267     // port2
268     end = net_options.find( ",", begin );
269     if ( end != string::npos ) {
270       port2 = atoi( net_options.substr(begin, end - begin).c_str() );
271       begin = end + 1;
272     }
273     // port3
274     end = net_options.find( ",", begin );
275     if ( end != string::npos ) {
276       port3 = atoi( net_options.substr(begin, end - begin).c_str() );
277       begin = end + 1;
278     }
279     _impl = new FGExternalNet( dt, host, port1, port2, port3 );
280   } else if ( model.find("pipe") == 0 ) {
281     // /* old */ string pipe_path = model.substr(5);
282     // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
283     string pipe_path = "";
284     string pipe_protocol = "";
285     string pipe_options = model.substr(5);
286     string::size_type begin, end;
287     begin = 0;
288     // pipe file path
289     end = pipe_options.find( ",", begin );
290     if ( end != string::npos ) {
291       pipe_path = pipe_options.substr(begin, end - begin);
292       begin = end + 1;
293     }
294     // protocol (last option)
295     pipe_protocol = pipe_options.substr(begin);
296     _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
297   } else if ( model == "null" ) {
298     _impl = new FGNullFDM( dt );
299   }
300     else if ( model == "larcsim" ) {
301 #ifdef ENABLE_LARCSIM
302         _impl = new FGLaRCsim( dt );
303 #else
304         fdmUnavailable = true;
305 #endif
306     }
307     else if ( model == "jsb" ) {
308 #ifdef ENABLE_JSBSIM
309         _impl = new FGJSBsim( dt );
310 #else
311         fdmUnavailable = true;
312 #endif
313     }
314 #ifdef ENABLE_SP_FDM
315     else if ( model == "ada" ) {
316         _impl = new FGADA( dt );
317     } else if ( model == "acms" ) {
318         _impl = new FGACMS( dt );
319     } else if ( model == "balloon" ) {
320         _impl = new FGBalloonSim( dt );
321     } else if ( model == "magic" ) {
322         _impl = new FGMagicCarpet( dt );
323     }
324 #else
325     else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
326     {
327         fdmUnavailable = true;
328     }
329 #endif
330     else if ( model == "yasim" ) {
331 #ifdef ENABLE_YASIM
332         _impl = new YASim( dt );
333 #else
334         fdmUnavailable = true;
335 #endif
336     } else {
337         throw sg_exception(string("Unrecognized flight model '") + model
338                + "', cannot init flight dynamics model.");
339     }
340
341     if (fdmUnavailable)
342     {
343         // FDM type is known, but its support was disabled at compile-time.
344         throw sg_exception(string("Support for flight model '") + model
345                 + ("' is not available with this binary (deprecated/disabled).\n"
346                    "If you still need it, please rebuild FlightGear and enable its support."));
347     }
348 }