JSBSim Flight Dynamics Model  1.2.0 (05 Nov 2023)
An Open Source Flight Dynamics and Control Software Library in C++
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 
26 HISTORY
27 --------------------------------------------------------------------------------
28 07/01/01 JSB Created
29 
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31 SENTRY
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 
34 #ifndef FGJSBBASE_H
35 #define FGJSBBASE_H
36 
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 
41 #include <float.h>
42 #include <queue>
43 #include <string>
44 #include <cmath>
45 #include <stdexcept>
46 #include <random>
47 #include <chrono>
48 
49 #include "JSBSim_API.h"
50 #include "input_output/string_utilities.h"
51 
52 #ifndef M_PI
53 # define M_PI 3.14159265358979323846
54 #endif
55 
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 FORWARD DECLARATIONS
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 
60 namespace JSBSim {
61 
62 class JSBSIM_API BaseException : public std::runtime_error {
63  public:
64  BaseException(const std::string& msg) : std::runtime_error(msg) {}
65 };
66 
75 class JSBSIM_API RandomNumberGenerator {
76  public:
78  RandomNumberGenerator(void) : uniform_random(-1.0, 1.0), normal_random(0.0, 1.0)
79  {
80  auto seed_value = std::chrono::system_clock::now().time_since_epoch().count();
81  generator.seed(static_cast<unsigned int>(seed_value));
82  }
84  RandomNumberGenerator(unsigned int seed)
85  : generator(seed), uniform_random(-1.0, 1.0), normal_random(0.0, 1.0) {}
87  void seed(unsigned int value) {
88  generator.seed(value);
89  uniform_random.reset();
90  normal_random.reset();
91  }
94  double GetUniformRandomNumber(void) { return uniform_random(generator); }
97  double GetNormalRandomNumber(void) { return normal_random(generator); }
98  private:
99  std::default_random_engine generator;
100  std::uniform_real_distribution<double> uniform_random;
101  std::normal_distribution<double> normal_random;
102 };
103 
104 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 CLASS DOCUMENTATION
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
107 
114 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 CLASS DECLARATION
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
117 
118 class JSBSIM_API FGJSBBase {
119 public:
121  FGJSBBase() {};
122 
124  virtual ~FGJSBBase() {};
125 
127  class Filter {
128  double prev_in;
129  double prev_out;
130  double ca;
131  double cb;
132  public:
133  Filter(void) {}
134  Filter(double coeff, double dt) {
135  prev_in = prev_out = 0.0;
136  double denom = 2.0 + coeff*dt;
137  ca = coeff*dt/denom;
138  cb = (2.0 - coeff*dt)/denom;
139  }
140  double execute(double in) {
141  double out = (in + prev_in)*ca + prev_out*cb;
142  prev_in = in;
143  prev_out = out;
144  return out;
145  }
146  };
147 
149 
150  static char highint[5];
153  static char halfint[5];
155  static char normint[6];
157  static char reset[5];
159  static char underon[5];
161  static char underoff[6];
163  static char fgblue[6];
165  static char fgcyan[6];
167  static char fgred[6];
169  static char fggreen[6];
171  static char fgdef[6];
173 
176  static const std::string& GetVersion(void) {return JSBSim_version;}
177 
179  void disableHighLighting(void);
180 
181  static short debug_lvl;
182 
186  static constexpr double KelvinToFahrenheit (double kelvin) {
187  return 1.8*kelvin - 459.4;
188  }
189 
193  static constexpr double CelsiusToRankine (double celsius) {
194  return celsius * 1.8 + 491.67;
195  }
196 
200  static constexpr double RankineToCelsius (double rankine) {
201  return (rankine - 491.67)/1.8;
202  }
203 
207  static constexpr double KelvinToRankine (double kelvin) {
208  return kelvin * 1.8;
209  }
210 
214  static constexpr double RankineToKelvin (double rankine) {
215  return rankine/1.8;
216  }
217 
221  static constexpr double FahrenheitToCelsius (double fahrenheit) {
222  return (fahrenheit - 32.0)/1.8;
223  }
224 
228  static constexpr double CelsiusToFahrenheit (double celsius) {
229  return celsius * 1.8 + 32.0;
230  }
231 
235  static constexpr double CelsiusToKelvin (double celsius) {
236  return celsius + 273.15;
237  }
238 
242  static constexpr double KelvinToCelsius (double kelvin) {
243  return kelvin - 273.15;
244  }
245 
249  static constexpr double FeetToMeters (double measure) {
250  return measure*0.3048;
251  }
252 
257  static bool EqualToRoundoff(double a, double b) {
258  double eps = 2.0*DBL_EPSILON;
259  return std::fabs(a - b) <= eps * std::max<double>(std::fabs(a), std::fabs(b));
260  }
261 
266  static bool EqualToRoundoff(float a, float b) {
267  float eps = 2.0*FLT_EPSILON;
268  return std::fabs(a - b) <= eps * std::max<double>(std::fabs(a), std::fabs(b));
269  }
270 
275  static bool EqualToRoundoff(float a, double b) {
276  return EqualToRoundoff(a, (float)b);
277  }
278 
283  static bool EqualToRoundoff(double a, float b) {
284  return EqualToRoundoff((float)a, b);
285  }
286 
289  static constexpr double Constrain(double min, double value, double max) {
290  return value<min?(min):(value>max?(max):(value));
291  }
292 
293  static constexpr double sign(double num) {return num>=0.0?1.0:-1.0;}
294 
295 protected:
296  static constexpr double radtodeg = 180. / M_PI;
297  static constexpr double degtorad = M_PI / 180.;
298  static constexpr double hptoftlbssec = 550.0;
299  static constexpr double psftoinhg = 0.014138;
300  static constexpr double psftopa = 47.88;
301  static constexpr double fttom = 0.3048;
302  static constexpr double ktstofps = 1852./(3600*fttom);
303  static constexpr double fpstokts = 1.0 / ktstofps;
304  static constexpr double inchtoft = 1.0/12.0;
305  static constexpr double m3toft3 = 1.0/(fttom*fttom*fttom);
306  static constexpr double in3tom3 = inchtoft*inchtoft*inchtoft/m3toft3;
307  static constexpr double inhgtopa = 3386.38;
314  static constexpr double slugtolb = 32.174049;
315  static constexpr double lbtoslug = 1.0/slugtolb;
316  static constexpr double kgtolb = 2.20462;
317  static constexpr double kgtoslug = 0.06852168;
318  static const std::string needed_cfg_version;
319  static const std::string JSBSim_version;
320 
321  static std::string CreateIndexedPropertyName(const std::string& Property, int index);
322 
323 public:
325 enum {eL = 1, eM, eN };
327 enum {eP = 1, eQ, eR };
329 enum {eU = 1, eV, eW };
331 enum {eX = 1, eY, eZ };
333 enum {ePhi = 1, eTht, ePsi };
335 enum {eDrag = 1, eSide, eLift };
337 enum {eRoll = 1, ePitch, eYaw };
339 enum {eNorth = 1, eEast, eDown };
341 enum {eLat = 1, eLong, eRad };
343 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
344 
345 };
346 
347 }
348 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 #endif
First order, (low pass / lag) filter.
Definition: FGJSBBase.h:127
JSBSim Base class.
Definition: FGJSBBase.h:118
static bool EqualToRoundoff(double a, double b)
Finite precision comparison.
Definition: FGJSBBase.h:257
static constexpr double KelvinToCelsius(double kelvin)
Converts from degrees Kelvin to degrees Celsius.
Definition: FGJSBBase.h:242
virtual ~FGJSBBase()
Destructor for FGJSBBase.
Definition: FGJSBBase.h:124
static bool EqualToRoundoff(float a, double b)
Finite precision comparison.
Definition: FGJSBBase.h:275
static bool EqualToRoundoff(double a, float b)
Finite precision comparison.
Definition: FGJSBBase.h:283
FGJSBBase()
Constructor for FGJSBBase.
Definition: FGJSBBase.h:121
static constexpr double Constrain(double min, double value, double max)
Constrain a value between a minimum and a maximum value.
Definition: FGJSBBase.h:289
static constexpr double KelvinToFahrenheit(double kelvin)
Converts from degrees Kelvin to degrees Fahrenheit.
Definition: FGJSBBase.h:186
static constexpr double RankineToKelvin(double rankine)
Converts from degrees Rankine to degrees Kelvin.
Definition: FGJSBBase.h:214
static constexpr double CelsiusToRankine(double celsius)
Converts from degrees Celsius to degrees Rankine.
Definition: FGJSBBase.h:193
static constexpr double FeetToMeters(double measure)
Converts from feet to meters.
Definition: FGJSBBase.h:249
static const std::string & GetVersion(void)
Returns the version number of JSBSim.
Definition: FGJSBBase.h:176
static constexpr double RankineToCelsius(double rankine)
Converts from degrees Rankine to degrees Celsius.
Definition: FGJSBBase.h:200
static constexpr double CelsiusToKelvin(double celsius)
Converts from degrees Celsius to degrees Kelvin.
Definition: FGJSBBase.h:235
static bool EqualToRoundoff(float a, float b)
Finite precision comparison.
Definition: FGJSBBase.h:266
static constexpr double KelvinToRankine(double kelvin)
Converts from degrees Kelvin to degrees Rankine.
Definition: FGJSBBase.h:207
static constexpr double FahrenheitToCelsius(double fahrenheit)
Converts from degrees Fahrenheit to degrees Celsius.
Definition: FGJSBBase.h:221
static constexpr double CelsiusToFahrenheit(double celsius)
Converts from degrees Celsius to degrees Fahrenheit.
Definition: FGJSBBase.h:228
Random number generator.
Definition: FGJSBBase.h:75
double GetNormalRandomNumber(void)
Get a random number which probability of occurrence is following Gauss normal distribution with a mea...
Definition: FGJSBBase.h:97
double GetUniformRandomNumber(void)
Get a random number which probability of occurrence is uniformly distributed over the segment [-1;1(.
Definition: FGJSBBase.h:94
RandomNumberGenerator(void)
Default constructor using a seed based on the system clock.
Definition: FGJSBBase.h:78
RandomNumberGenerator(unsigned int seed)
Constructor allowing to specify a seed.
Definition: FGJSBBase.h:84
void seed(unsigned int value)
Specify a new seed and reinitialize the random generation process.
Definition: FGJSBBase.h:87