FlightGear next
fdm_shell.cxx
Go to the documentation of this file.
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#include <config.h>
24
25#include <cassert>
26#include <simgear/structure/exception.hxx>
27#include <simgear/props/props_io.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#include "AIModel/AIManager.hxx"
37#include "simgear/debug/debug_types.h"
38
39// all the FDMs, since we are the factory method
40#ifdef ENABLE_SP_FDM
41#include <FDM/SP/ADA.hxx>
42#include <FDM/SP/ACMS.hxx>
44#include <FDM/SP/Balloon.h>
45#include <FDM/SP/AISim.hpp>
46#endif
49
50#ifdef ENABLE_JSBSIM
51#include <FDM/JSBSim/JSBSim.hxx>
52#endif
53
54#include <FDM/UFO.hxx>
55#include <FDM/NullFDM.hxx>
56
57#ifdef ENABLE_YASIM
58#include <FDM/YASim/YASim.hxx>
59#endif
60
61#include <GUI/MessageBox.hxx>
63
64using std::string;
65
66FDMShell::FDMShell() : _tankProperties(fgGetNode("/consumables/fuel", true))
67{
68}
69
73
75{
76 _props = globals->get_props();
77 fgSetBool("/sim/fdm-initialized", false);
78
79 _wind_north = _props->getNode("environment/wind-from-north-fps", true);
80 _wind_east = _props->getNode("environment/wind-from-east-fps", true);
81 _wind_down = _props->getNode("environment/wind-from-down-fps", true);
82 _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
83 _temp_degc = _props->getNode("environment/temperature-degc", true);
84 _pressure_inhg = _props->getNode("environment/pressure-inhg", true);
85 _density_slugft = _props->getNode("environment/density-slugft3", true);
86 _data_logging = _props->getNode("/sim/temp/fdm-data-logging", true);
87 _replay_master = _props->getNode("/sim/freeze/replay-state", true);
88
89 // AI aerodynamic wake interaction
90 _max_radius_nm = _props->getNode("fdm/ai-wake/max-radius-nm", true);
91 _ai_wake_enabled = _props->getNode("fdm/ai-wake/enabled", true);
92
93 _nanCheckFailed = false;
94 fgSetBool("/sim/fdm-nan-failure", false);
95 _lastValidPos = SGGeod::invalid();
96
97 createImplementation();
98}
99
101{
102 _initialFdmProperties = new SGPropertyNode;
103
104 if (!copyProperties(_props->getNode("fdm", true),
105 _initialFdmProperties))
106 {
107 SG_LOG(SG_FLIGHT, SG_ALERT, "Failed to save initial FDM property state");
108 }
109
110 _ai_mgr = globals->get_subsystem<FGAIManager>();
111 if (!_ai_mgr) {
112 SG_LOG(SG_FLIGHT, SG_DEV_ALERT, "FDM connection to the AI manager: FAILED");
113 }
114}
115
117{
118 if (_impl) {
119 fgSetBool("/sim/fdm-initialized", false);
120 _impl->unbind();
121 _impl.clear();
122 }
123
124 _props.clear();
125 _wind_north.clear();
126 _wind_east.clear();
127 _wind_down.clear();
128 _control_fdm_atmo.clear();
129 _temp_degc.clear();
130 _pressure_inhg.clear();
131 _density_slugft .clear();
132 _data_logging.clear();
133 _replay_master.clear();
134}
135
137{
138 shutdown();
139
140 if ( copyProperties(_initialFdmProperties, fgGetNode("/fdm", true)) ) {
141 SG_LOG( SG_FLIGHT, SG_INFO, "Preserved state restored successfully" );
142 } else {
143 SG_LOG( SG_FLIGHT, SG_WARN,
144 "FDM: Some errors restoring preserved state" );
145 }
146
147
148 init();
149}
150
152{
153 _tankProperties.bind();
154 if (_impl && _impl->get_inited()) {
155 if (_impl->get_bound()) {
156 throw sg_exception("FDMShell::bind of bound FGInterface impl");
157 }
158 _impl->bind();
159 }
160}
161
163{
164 if( _impl ) _impl->unbind();
165 _tankProperties.unbind();
166}
167
168void FDMShell::doInitAndBind()
169{
170 SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
171 try {
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 } catch (std::exception& e) {
181 flightgear::fatalMessageBoxThenExit("Aircraft FDM initialization error",
182 string{"The aircraft flight dynamics model contains errors and cannot be used. ("} + e.what() + ")");
183 }
184
185 if (!copyProperties(_props->getNode("fdm", true),
186 _initialFdmProperties)) {
187 SG_LOG(SG_FLIGHT, SG_ALERT, "Failed to save initial FDM property state");
188 }
189}
190
191void FDMShell::update(double dt)
192{
193 if (!_impl) {
194 return;
195 }
196
197 if (_nanCheckFailed) {
198 return;
199 }
200
201 if (!_impl->get_inited()) {
202 // Check for scenery around the aircraft.
203 double lon = fgGetDouble("/sim/presets/longitude-deg");
204 double lat = fgGetDouble("/sim/presets/latitude-deg");
205
206 double range = 1000.0; // in meters
207 SGGeod geod = SGGeod::fromDeg(lon, lat);
208 const auto startUpPositionFialized = fgGetBool("/sim/position-finalized", false);
209 if (startUpPositionFialized && globals->get_scenery()->scenery_available(geod, range)) {
210 doInitAndBind();
211 }
212 }
213
214 if (!_impl->get_inited()) {
215 return; // still waiting
216 }
217
218 // AI aerodynamic wake interaction
219 if (_ai_wake_enabled->getBoolValue()) {
220 for (FGAIBase* base : _ai_mgr->get_ai_list()) {
221 try {
222 if (base->isa(FGAIBase::object_type::otAircraft) ) {
223 SGVec3d pos = _impl->getCartPosition();
224 const SGSharedPtr<FGAIAircraft> aircraft = dynamic_cast<FGAIAircraft*>(base);
225 double range = _ai_mgr->calcRangeFt(pos, aircraft)*SG_FEET_TO_METER;
226
227 if (!aircraft->onGround() && aircraft->getSpeed() > 0.0
228 && range < _max_radius_nm->getDoubleValue()*SG_NM_TO_METER) {
229 _impl->add_ai_wake(aircraft);
230 }
231 }
232 } catch (sg_exception& e) {
233 SG_LOG(SG_FLIGHT, SG_WARN, "caught exception updating AI model:"
234 << base->_getName()<< ", which will be killed."
235 "\n\tError:" << e.getFormattedMessage());
236 base->setDie(true);
237 }
238 }
239 }
240
241 // pull environmental data in, since the FDMs are lazy
242 _impl->set_Velocities_Local_Airmass(
243 _wind_north->getDoubleValue(),
244 _wind_east->getDoubleValue(),
245 _wind_down->getDoubleValue());
246
247 if (_control_fdm_atmo->getBoolValue()) {
248 // convert from Rankine to Celsius
249 double tempDegC = _temp_degc->getDoubleValue();
250 _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
251
252 // convert from inHG to PSF
253 double pressureInHg = _pressure_inhg->getDoubleValue();
254 _impl->set_Static_pressure(pressureInHg * 70.726566);
255 // keep in slugs/ft^3
256 _impl->set_Density(_density_slugft->getDoubleValue());
257 }
258
259 bool doLog = _data_logging->getBoolValue();
260 if (doLog != _dataLogging) {
261 _dataLogging = doLog;
262 _impl->ToggleDataLogging(doLog);
263 }
264
265 switch(_replay_master->getIntValue())
266 {
267 case 0:
268 // normal FDM operation
269 _impl->update(dt);
270 break;
271 case 3:
272 // resume FDM operation at current replay position
273 _impl->reinit();
274 break;
275 default:
276 // replay is active
277 break;
278 }
279
280 validateOutputProperties();
281}
282
284{
285 return _impl;
286}
287
288void FDMShell::createImplementation()
289{
290 assert(!_impl);
291
292 double dt = 1.0 / fgGetInt("/sim/model-hz");
293 string model = fgGetString("/sim/flight-model");
294
295 bool fdmUnavailable = false;
296
297 if ( model == "ufo" ) {
298 _impl = new FGUFO( dt );
299 } else if ( model == "external" ) {
300 // external is a synonym for "--fdm=null" and is
301 // maintained here for backwards compatibility
302 _impl = new FGNullFDM( dt );
303 } else if ( model.find("network") == 0 ) {
304 string host = "localhost";
305 int port1 = 5501;
306 int port2 = 5502;
307 int port3 = 5503;
308 string net_options = model.substr(8);
309 string::size_type begin, end;
310 begin = 0;
311 // host
312 end = net_options.find( ",", begin );
313 if ( end != string::npos ) {
314 host = net_options.substr(begin, end - begin);
315 begin = end + 1;
316 }
317 // port1
318 end = net_options.find( ",", begin );
319 if ( end != string::npos ) {
320 port1 = atoi( net_options.substr(begin, end - begin).c_str() );
321 begin = end + 1;
322 }
323 // port2
324 end = net_options.find( ",", begin );
325 if ( end != string::npos ) {
326 port2 = atoi( net_options.substr(begin, end - begin).c_str() );
327 begin = end + 1;
328 }
329 // port3
330 end = net_options.find( ",", begin );
331 if ( end != string::npos ) {
332 port3 = atoi( net_options.substr(begin, end - begin).c_str() );
333 begin = end + 1;
334 }
335 _impl = new FGExternalNet( dt, host, port1, port2, port3 );
336 } else if ( model.find("pipe") == 0 ) {
337 // /* old */ string pipe_path = model.substr(5);
338 // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
339 string pipe_path = "";
340 string pipe_protocol = "";
341 string pipe_options = model.substr(5);
342 string::size_type begin, end;
343 begin = 0;
344 // pipe file path
345 end = pipe_options.find( ",", begin );
346 if ( end != string::npos ) {
347 pipe_path = pipe_options.substr(begin, end - begin);
348 begin = end + 1;
349 }
350 // protocol (last option)
351 pipe_protocol = pipe_options.substr(begin);
352 _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
353 } else if ( model == "null" ) {
354 _impl = new FGNullFDM( dt );
355 }
356 else if ( model == "larcsim" ) {
357 throw sg_exception(string("LaRCsim/UIUC support is removed from this version of FlightGear.\n"
358 "If you still need it, please use the 2024.1 release instead."));
359 }
360 else if ( model == "jsb" ) {
361#ifdef ENABLE_JSBSIM
362 _impl = new FGJSBsim( dt );
363#else
364 fdmUnavailable = true;
365#endif
366 }
367#ifdef ENABLE_SP_FDM
368 else if ( model == "ada" ) {
369 _impl = new FGADA( dt );
370 } else if ( model == "acms" ) {
371 _impl = new FGACMS( dt );
372 } else if ( model == "balloon" ) {
373 _impl = new FGBalloonSim( dt );
374 } else if ( model == "magic" ) {
375 _impl = new FGMagicCarpet( dt );
376 } else if ( model == "aisim" ) {
377 _impl = new FGAISim( dt );
378 }
379#else
380 else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" )||( model == "aisim" ))
381 {
382 fdmUnavailable = true;
383 }
384#endif
385 else if ( model == "yasim" ) {
386#ifdef ENABLE_YASIM
387 _impl = new YASim( dt );
388#else
389 fdmUnavailable = true;
390#endif
391 } else {
392 throw sg_exception(string("Unrecognized flight model '") + model
393 + "', cannot init flight dynamics model.");
394 }
395
396 if (fdmUnavailable)
397 {
398 // FDM type is known, but its support was disabled at compile-time.
399 throw sg_exception(string("Support for flight model '") + model
400 + ("' is not available with this binary (deprecated/disabled).\n"
401 "If you still need it, please rebuild FlightGear and enable its support."));
402 }
403}
404
405void FDMShell::validateOutputProperties()
406{
407 if (_nanCheckFailed)
408 return;
409
410 const auto p = globals->get_aircraft_position();
411 if (SGMisc<double>::isNaN(p.getLatitudeDeg()) ||
412 SGMisc<double>::isNaN(p.getLongitudeDeg()) ||
413 SGMisc<double>::isNaN(p.getElevationFt())) {
414 SG_LOG(SG_FLIGHT, SG_ALERT, "FDM position became invalid. Last valid position was:" << _lastValidPos);
415 _nanCheckFailed = true;
416 } else {
417 _lastValidPos = p;
418 }
419
420 if (SGMisc<double>::isNaN(_impl->get_V_true_kts())) {
421 SG_LOG(SG_FLIGHT, SG_ALERT, "FDM velocity became invalid");
422 _nanCheckFailed = true;
423 }
424
425 if (_nanCheckFailed) {
426 flightgear::sentryReportException("FDM NaN check failed");
427
428 flightgear::modalMessageBox("Flight dynamics error",
429 "The flight dynamics model (FDM) has become invalid. The simulation will be stopped, so you can restart at a new location.");
430 fgSetBool("/sim/fdm-nan-failure", true);
431 fgSetBool("/sim/freeze/master", true);
432 fgSetBool("/sim/freeze/clock", true);
433 }
434}
435
436// Register the subsystem.
437SGSubsystemMgr::Registrant<FDMShell> registrantFDMShell(
438 SGSubsystemMgr::FDM);
#define p(x)
void reinit() override
void shutdown() override
void update(double dt) override
~FDMShell() override
Definition fdm_shell.cxx:70
FGInterface * getInterface() const
void unbind() override
void postinit() override
void bind() override
void init() override
Definition fdm_shell.cxx:74
SGGeod get_aircraft_position() const
Definition globals.cxx:611
Definition UFO.hxx:31
SGSubsystemMgr::Registrant< FDMShell > registrantFDMShell(SGSubsystemMgr::FDM)
int fgGetInt(const char *name, int defaultValue)
Get an int value for a property.
Definition fg_props.cxx:532
std::string fgGetString(const char *name, const char *defaultValue)
Get a string value for a property.
Definition fg_props.cxx:556
FGGlobals * globals
Definition globals.cxx:142
MessageBoxResult modalMessageBox(const std::string &caption, const std::string &msg, const std::string &moreText)
void sentryReportException(const std::string &, const std::string &)
void fatalMessageBoxThenExit(const std::string &caption, const std::string &msg, const std::string &moreText, int exitStatus, bool reportToSentry)
static int atoi(const string &str)
Definition options.cxx:113
bool fgGetBool(char const *name, bool def)
Get a bool value for a property.
Definition proptest.cpp:25
bool fgSetBool(char const *name, bool val)
Set a bool value for a property.
Definition proptest.cpp:24
double fgGetDouble(const char *name, double defaultValue)
Get a double value for a property.
Definition proptest.cpp:30
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27