Debian Codegraph @

src/FDM/JSBSim/FGJSBBase.h

raw symbols SCIP
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 ([email protected]) -------------
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
67 /**
68 * @brief Random number generator.
69 * This class encapsulates the C++11 random number generation classes for
70 * uniform and gaussian (aka normal) distributions as well as their seed.
71 * This class guarantees that whenever its seed is reset so are its uniform
72 * and normal random number generators.
73 */
74
75 class JSBSIM_API RandomNumberGenerator {
76 public:
77 /// Default constructor using a seed based on the system clock.
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 }
83 /// Constructor allowing to specify a seed.
84 RandomNumberGenerator(unsigned int seed)
85 : generator(seed), uniform_random(-1.0, 1.0), normal_random(0.0, 1.0) {}
86 /// Specify a new seed and reinitialize the random generation process.
87 void seed(unsigned int value) {
88 generator.seed(value);
89 uniform_random.reset();
90 normal_random.reset();
91 }
92 /** Get a random number which probability of occurrence is uniformly
93 * distributed over the segment [-1;1( */
94 double GetUniformRandomNumber(void) { return uniform_random(generator); }
95 /** Get a random number which probability of occurrence is following Gauss
96 * normal distribution with a mean of 0.0 and a standard deviation of 1.0 */
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
108 /** JSBSim Base class.
109 * This class provides universal constants, utility functions, messaging
110 * functions, and enumerated constants to JSBSim.
111 @author Jon S. Berndt
112 */
113
114 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 CLASS DECLARATION
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
117
118 class JSBSIM_API FGJSBBase {
119 public:
120 /// Constructor for FGJSBBase.
121 FGJSBBase() {};
122
123 /// Destructor for FGJSBBase.
124 virtual ~FGJSBBase() {};
125
126 /// First order, (low pass / lag) filter
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
148 ///@name JSBSim console output highlighting terms.
149 //@{
150 /// highlights text
151 static char highint[5];
152 /// low intensity text
153 static char halfint[5];
154 /// normal intensity text
155 static char normint[6];
156 /// resets text properties
157 static char reset[5];
158 /// underlines text
159 static char underon[5];
160 /// underline off
161 static char underoff[6];
162 /// blue text
163 static char fgblue[6];
164 /// cyan text
165 static char fgcyan[6];
166 /// red text
167 static char fgred[6];
168 /// green text
169 static char fggreen[6];
170 /// default text
171 static char fgdef[6];
172 //@}
173
174 /** Returns the version number of JSBSim.
175 * @return The version number of JSBSim. */
176 static const std::string& GetVersion(void) {return JSBSim_version;}
177
178 /// Disables highlighting in the console output.
179 void disableHighLighting(void);
180
181 static short debug_lvl;
182
183 /** Converts from degrees Kelvin to degrees Fahrenheit.
184 * @param kelvin The temperature in degrees Kelvin.
185 * @return The temperature in Fahrenheit. */
186 static constexpr double KelvinToFahrenheit (double kelvin) {
187 return 1.8*kelvin - 459.4;
188 }
189
190 /** Converts from degrees Celsius to degrees Rankine.
191 * @param celsius The temperature in degrees Celsius.
192 * @return The temperature in Rankine. */
193 static constexpr double CelsiusToRankine (double celsius) {
194 return celsius * 1.8 + 491.67;
195 }
196
197 /** Converts from degrees Rankine to degrees Celsius.
198 * @param rankine The temperature in degrees Rankine.
199 * @return The temperature in Celsius. */
200 static constexpr double RankineToCelsius (double rankine) {
201 return (rankine - 491.67)/1.8;
202 }
203
204 /** Converts from degrees Kelvin to degrees Rankine.
205 * @param kelvin The temperature in degrees Kelvin.
206 * @return The temperature in Rankine. */
207 static constexpr double KelvinToRankine (double kelvin) {
208 return kelvin * 1.8;
209 }
210
211 /** Converts from degrees Rankine to degrees Kelvin.
212 * @param rankine The temperature in degrees Rankine.
213 * @return The temperature in Kelvin. */
214 static constexpr double RankineToKelvin (double rankine) {
215 return rankine/1.8;
216 }
217
218 /** Converts from degrees Fahrenheit to degrees Celsius.
219 * @param fahrenheit The temperature in degrees Fahrenheit.
220 * @return The temperature in Celsius. */
221 static constexpr double FahrenheitToCelsius (double fahrenheit) {
222 return (fahrenheit - 32.0)/1.8;
223 }
224
225 /** Converts from degrees Celsius to degrees Fahrenheit.
226 * @param celsius The temperature in degrees Celsius.
227 * @return The temperature in Fahrenheit. */
228 static constexpr double CelsiusToFahrenheit (double celsius) {
229 return celsius * 1.8 + 32.0;
230 }
231
232 /** Converts from degrees Celsius to degrees Kelvin
233 * @param celsius The temperature in degrees Celsius.
234 * @return The temperature in Kelvin. */
235 static constexpr double CelsiusToKelvin (double celsius) {
236 return celsius + 273.15;
237 }
238
239 /** Converts from degrees Kelvin to degrees Celsius
240 * @param celsius The temperature in degrees Kelvin.
241 * @return The temperature in Celsius. */
242 static constexpr double KelvinToCelsius (double kelvin) {
243 return kelvin - 273.15;
244 }
245
246 /** Converts from feet to meters
247 * @param measure The length in feet.
248 * @return The length in meters. */
249 static constexpr double FeetToMeters (double measure) {
250 return measure*0.3048;
251 }
252
253 /** Finite precision comparison.
254 @param a first value to compare
255 @param b second value to compare
256 @return if the two values can be considered equal up to roundoff */
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
262 /** Finite precision comparison.
263 @param a first value to compare
264 @param b second value to compare
265 @return if the two values can be considered equal up to roundoff */
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
271 /** Finite precision comparison.
272 @param a first value to compare
273 @param b second value to compare
274 @return if the two values can be considered equal up to roundoff */
275 static bool EqualToRoundoff(float a, double b) {
276 return EqualToRoundoff(a, (float)b);
277 }
278
279 /** Finite precision comparison.
280 @param a first value to compare
281 @param b second value to compare
282 @return if the two values can be considered equal up to roundoff */
283 static bool EqualToRoundoff(double a, float b) {
284 return EqualToRoundoff((float)a, b);
285 }
286
287 /** Constrain a value between a minimum and a maximum value.
288 */
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;
308 /** Note that definition of lbtoslug by the inverse of slugtolb and not to a
309 different constant you can also get from some tables will make
310 lbtoslug*slugtolb == 1 up to the magnitude of roundoff. So converting from
311 slug to lb and back will yield to the original value you started with up
312 to the magnitude of roundoff.
313 Taken from units gnu commandline tool */
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:
324 /// Moments L, M, N
325 enum {eL = 1, eM, eN };
326 /// Rates P, Q, R
327 enum {eP = 1, eQ, eR };
328 /// Velocities U, V, W
329 enum {eU = 1, eV, eW };
330 /// Positions X, Y, Z
331 enum {eX = 1, eY, eZ };
332 /// Euler angles Phi, Theta, Psi
333 enum {ePhi = 1, eTht, ePsi };
334 /// Stability axis forces, Drag, Side force, Lift
335 enum {eDrag = 1, eSide, eLift };
336 /// Local frame orientation Roll, Pitch, Yaw
337 enum {eRoll = 1, ePitch, eYaw };
338 /// Local frame position North, East, Down
339 enum {eNorth = 1, eEast, eDown };
340 /// Locations Radius, Latitude, Longitude
341 enum {eLat = 1, eLong, eRad };
342 /// Conversion specifiers
343 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
344
345 };
346
347 }
348 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 #endif