JSBSim Flight Dynamics Model 1.3.3 (16 Oct 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGJSBBase.h
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Header: FGJSBBase.h
4 Author: Jon S. Berndt
5 Date started: 07/01/01
6
7 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 details.
18
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on the world wide web at http://www.gnu.org.
25
26HISTORY
27--------------------------------------------------------------------------------
2807/01/01 JSB Created
29
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31SENTRY
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33
34#ifndef FGJSBBASE_H
35#define FGJSBBASE_H
36
37/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38INCLUDES
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41#include <float.h>
42#include <stdexcept>
43#include <random>
44#include <chrono>
45
46#include "JSBSim_API.h"
47
48#ifndef M_PI
49# define M_PI 3.14159265358979323846
50#endif
51
52/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53FORWARD DECLARATIONS
54%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55
57namespace JSBSim {
58
59class JSBSIM_API BaseException : public std::runtime_error {
60 public:
61 using std::runtime_error::runtime_error;
62};
63
72class JSBSIM_API RandomNumberGenerator {
73 public:
75 RandomNumberGenerator(void) : uniform_random(-1.0, 1.0), normal_random(0.0, 1.0)
76 {
77 auto seed_value = std::chrono::system_clock::now().time_since_epoch().count();
78 generator.seed(static_cast<unsigned int>(seed_value));
79 }
81 RandomNumberGenerator(unsigned int seed)
82 : generator(seed), uniform_random(-1.0, 1.0), normal_random(0.0, 1.0) {}
84 void seed(unsigned int value) {
85 generator.seed(value);
86 uniform_random.reset();
87 normal_random.reset();
88 }
91 double GetUniformRandomNumber(void) { return uniform_random(generator); }
94 double GetNormalRandomNumber(void) { return normal_random(generator); }
95 private:
96 std::default_random_engine generator;
97 std::uniform_real_distribution<double> uniform_random;
98 std::normal_distribution<double> normal_random;
99};
100
101/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102CLASS DOCUMENTATION
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
104
111/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112CLASS DECLARATION
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
114
115class JSBSIM_API FGJSBBase {
116public:
119
121 virtual ~FGJSBBase() {};
122
124 class Filter {
125 double prev_in;
126 double prev_out;
127 double ca;
128 double cb;
129 public:
130 Filter(void) {}
131 Filter(double coeff, double dt) {
132 prev_in = prev_out = 0.0;
133 double denom = 2.0 + coeff*dt;
134 ca = coeff*dt/denom;
135 cb = (2.0 - coeff*dt)/denom;
136 }
137 double execute(double in) {
138 double out = (in + prev_in)*ca + prev_out*cb;
139 prev_in = in;
140 prev_out = out;
141 return out;
142 }
143 void reset(void) { prev_in = prev_out = 0.0; }
144 };
145
148 static const std::string& GetVersion(void) {return JSBSim_version;}
149
150 static short debug_lvl;
151
155 static constexpr double KelvinToFahrenheit (double kelvin) {
156 return 1.8*kelvin - 459.4;
157 }
158
162 static constexpr double CelsiusToRankine (double celsius) {
163 return celsius * 1.8 + 491.67;
164 }
165
169 static constexpr double RankineToCelsius (double rankine) {
170 return (rankine - 491.67)/1.8;
171 }
172
176 static constexpr double KelvinToRankine (double kelvin) {
177 return kelvin * 1.8;
178 }
179
183 static constexpr double RankineToKelvin (double rankine) {
184 return rankine/1.8;
185 }
186
190 static constexpr double FahrenheitToCelsius (double fahrenheit) {
191 return (fahrenheit - 32.0)/1.8;
192 }
193
197 static constexpr double CelsiusToFahrenheit (double celsius) {
198 return celsius * 1.8 + 32.0;
199 }
200
204 static constexpr double CelsiusToKelvin (double celsius) {
205 return celsius + 273.15;
206 }
207
211 static constexpr double KelvinToCelsius (double kelvin) {
212 return kelvin - 273.15;
213 }
214
218 static constexpr double FeetToMeters (double measure) {
219 return measure*0.3048;
220 }
221
226 static bool EqualToRoundoff(double a, double b) {
227 double eps = 2.0*DBL_EPSILON;
228 return std::fabs(a - b) <= eps * std::max<double>(std::fabs(a), std::fabs(b));
229 }
230
235 static bool EqualToRoundoff(float a, float b) {
236 float eps = 2.0*FLT_EPSILON;
237 return std::fabs(a - b) <= eps * std::max<double>(std::fabs(a), std::fabs(b));
238 }
239
244 static bool EqualToRoundoff(float a, double b) {
245 return EqualToRoundoff(a, (float)b);
246 }
247
252 static bool EqualToRoundoff(double a, float b) {
253 return EqualToRoundoff((float)a, b);
254 }
255
258 static constexpr double Constrain(double min, double value, double max) {
259 return value<min?(min):(value>max?(max):(value));
260 }
261
262 static constexpr double sign(double num) {return num>=0.0?1.0:-1.0;}
263
264protected:
265 static constexpr double radtodeg = 180. / M_PI;
266 static constexpr double degtorad = M_PI / 180.;
267 static constexpr double hptoftlbssec = 550.0;
268 static constexpr double psftoinhg = 0.014138;
269 static constexpr double psftopa = 47.88;
270 static constexpr double fttom = 0.3048;
271 static constexpr double ktstofps = 1852./(3600*fttom);
272 static constexpr double fpstokts = 1.0 / ktstofps;
273 static constexpr double inchtoft = 1.0/12.0;
274 static constexpr double m3toft3 = 1.0/(fttom*fttom*fttom);
275 static constexpr double in3tom3 = inchtoft*inchtoft*inchtoft/m3toft3;
276 static constexpr double inhgtopa = 3386.38;
283 static constexpr double slugtolb = 32.174049;
284 static constexpr double lbtoslug = 1.0/slugtolb;
285 static constexpr double kgtolb = 2.20462;
286 static constexpr double kgtoslug = 0.06852168;
287 static const std::string needed_cfg_version;
288 static const std::string JSBSim_version;
289
290 static std::string CreateIndexedPropertyName(const std::string& Property, int index);
291
292public:
294enum {eL = 1, eM, eN };
296enum {eP = 1, eQ, eR };
298enum {eU = 1, eV, eW };
300enum {eX = 1, eY, eZ };
302enum {ePhi = 1, eTht, ePsi };
304enum {eDrag = 1, eSide, eLift };
306enum {eRoll = 1, ePitch, eYaw };
308enum {eNorth = 1, eEast, eDown };
310enum {eLat = 1, eLong, eRad };
312enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
313
314};
315
316}
317//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318#endif
First order, (low pass / lag) filter.
Definition FGJSBBase.h:124
JSBSim Base class.
Definition FGJSBBase.h:115
static bool EqualToRoundoff(double a, double b)
Finite precision comparison.
Definition FGJSBBase.h:226
static const std::string & GetVersion(void)
Returns the version number of JSBSim.
Definition FGJSBBase.h:148
static constexpr double KelvinToCelsius(double kelvin)
Converts from degrees Kelvin to degrees Celsius.
Definition FGJSBBase.h:211
virtual ~FGJSBBase()
Destructor for FGJSBBase.
Definition FGJSBBase.h:121
static bool EqualToRoundoff(float a, double b)
Finite precision comparison.
Definition FGJSBBase.h:244
static bool EqualToRoundoff(double a, float b)
Finite precision comparison.
Definition FGJSBBase.h:252
FGJSBBase()
Constructor for FGJSBBase.
Definition FGJSBBase.h:118
static constexpr double Constrain(double min, double value, double max)
Constrain a value between a minimum and a maximum value.
Definition FGJSBBase.h:258
static constexpr double KelvinToFahrenheit(double kelvin)
Converts from degrees Kelvin to degrees Fahrenheit.
Definition FGJSBBase.h:155
static constexpr double RankineToKelvin(double rankine)
Converts from degrees Rankine to degrees Kelvin.
Definition FGJSBBase.h:183
static constexpr double CelsiusToRankine(double celsius)
Converts from degrees Celsius to degrees Rankine.
Definition FGJSBBase.h:162
static constexpr double FeetToMeters(double measure)
Converts from feet to meters.
Definition FGJSBBase.h:218
static constexpr double RankineToCelsius(double rankine)
Converts from degrees Rankine to degrees Celsius.
Definition FGJSBBase.h:169
static constexpr double CelsiusToKelvin(double celsius)
Converts from degrees Celsius to degrees Kelvin.
Definition FGJSBBase.h:204
static bool EqualToRoundoff(float a, float b)
Finite precision comparison.
Definition FGJSBBase.h:235
static constexpr double KelvinToRankine(double kelvin)
Converts from degrees Kelvin to degrees Rankine.
Definition FGJSBBase.h:176
static constexpr double FahrenheitToCelsius(double fahrenheit)
Converts from degrees Fahrenheit to degrees Celsius.
Definition FGJSBBase.h:190
static constexpr double CelsiusToFahrenheit(double celsius)
Converts from degrees Celsius to degrees Fahrenheit.
Definition FGJSBBase.h:197
Random number generator.
Definition FGJSBBase.h:72
double GetNormalRandomNumber(void)
Get a random number which probability of occurrence is following Gauss normal distribution with a mea...
Definition FGJSBBase.h:94
double GetUniformRandomNumber(void)
Get a random number which probability of occurrence is uniformly distributed over the segment [-1;1(.
Definition FGJSBBase.h:91
RandomNumberGenerator(void)
Default constructor using a seed based on the system clock.
Definition FGJSBBase.h:75
RandomNumberGenerator(unsigned int seed)
Constructor allowing to specify a seed.
Definition FGJSBBase.h:81
void seed(unsigned int value)
Specify a new seed and reinitialize the random generation process.
Definition FGJSBBase.h:84
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:73