FlightGear next
FGJSBBase.h
Go to the documentation of this file.
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 <queue>
43#include <string>
44#include <cmath>
45#include <stdexcept>
46
48
49#ifndef M_PI
50# define M_PI 3.14159265358979323846
51#endif
52
53/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54FORWARD DECLARATIONS
55%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57namespace JSBSim {
58
59class BaseException : public std::runtime_error {
60 public:
61 BaseException(const std::string& msg) : std::runtime_error(msg) {}
62};
63
64/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65CLASS DOCUMENTATION
66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67
73
74/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75CLASS DECLARATION
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77
78class FGJSBBase {
79public:
82
84 virtual ~FGJSBBase() {};
85
87 struct Message {
88 unsigned int fdmId;
89 unsigned int messageId;
90 std::string text;
91 std::string subsystem;
93 bool bVal;
94 int iVal;
95 double dVal;
96 };
97
99 class Filter {
100 double prev_in;
101 double prev_out;
102 double ca;
103 double cb;
104 public:
105 Filter(void) {}
106 Filter(double coeff, double dt) {
107 prev_in = prev_out = 0.0;
108 double denom = 2.0 + coeff*dt;
109 ca = coeff*dt/denom;
110 cb = (2.0 - coeff*dt)/denom;
111 }
112 double execute(double in) {
113 double out = (in + prev_in)*ca + prev_out*cb;
114 prev_in = in;
115 prev_out = out;
116 return out;
117 }
118 };
119
121
122
123 static char highint[5];
125 static char halfint[5];
127 static char normint[6];
129 static char reset[5];
131 static char underon[5];
133 static char underoff[6];
135 static char fgblue[6];
137 static char fgcyan[6];
139 static char fgred[6];
141 static char fggreen[6];
143 static char fgdef[6];
145
147
148
151 void PutMessage(const Message& msg);
155 void PutMessage(const std::string& text);
160 void PutMessage(const std::string& text, bool bVal);
165 void PutMessage(const std::string& text, int iVal);
170 void PutMessage(const std::string& text, double dVal);
173 int SomeMessages(void) const { return !Messages.empty(); }
176 void ProcessMessage(void);
180 Message* ProcessNextMessage(void);
182
185 static const std::string& GetVersion(void) {return JSBSim_version;}
186
188 void disableHighLighting(void);
189
190 static short debug_lvl;
191
195 static constexpr double KelvinToFahrenheit (double kelvin) {
196 return 1.8*kelvin - 459.4;
197 }
198
202 static constexpr double CelsiusToRankine (double celsius) {
203 return celsius * 1.8 + 491.67;
204 }
205
209 static constexpr double RankineToCelsius (double rankine) {
210 return (rankine - 491.67)/1.8;
211 }
212
216 static constexpr double KelvinToRankine (double kelvin) {
217 return kelvin * 1.8;
218 }
219
223 static constexpr double RankineToKelvin (double rankine) {
224 return rankine/1.8;
225 }
226
230 static constexpr double FahrenheitToCelsius (double fahrenheit) {
231 return (fahrenheit - 32.0)/1.8;
232 }
233
237 static constexpr double CelsiusToFahrenheit (double celsius) {
238 return celsius * 1.8 + 32.0;
239 }
240
244 static constexpr double CelsiusToKelvin (double celsius) {
245 return celsius + 273.15;
246 }
247
251 static constexpr double KelvinToCelsius (double kelvin) {
252 return kelvin - 273.15;
253 }
254
258 static constexpr double FeetToMeters (double measure) {
259 return measure*0.3048;
260 }
261
269 static double PitotTotalPressure(double mach, double p);
270
277 static double MachFromImpactPressure(double qc, double p);
278
286 static double VcalibratedFromMach(double mach, double p);
287
295 static double MachFromVcalibrated(double vcas, double p);
296
301 static bool EqualToRoundoff(double a, double b) {
302 double eps = 2.0*DBL_EPSILON;
303 return std::fabs(a - b) <= eps * std::max<double>(std::fabs(a), std::fabs(b));
304 }
305
310 static bool EqualToRoundoff(float a, float b) {
311 float eps = 2.0*FLT_EPSILON;
312 return std::fabs(a - b) <= eps * std::max<double>(std::fabs(a), std::fabs(b));
313 }
314
319 static bool EqualToRoundoff(float a, double b) {
320 return EqualToRoundoff(a, (float)b);
321 }
322
327 static bool EqualToRoundoff(double a, float b) {
328 return EqualToRoundoff((float)a, b);
329 }
330
333 static constexpr double Constrain(double min, double value, double max) {
334 return value<min?(min):(value>max?(max):(value));
335 }
336
337 static constexpr double sign(double num) {return num>=0.0?1.0:-1.0;}
338
339 static double GaussianRandomNumber(void);
340
341protected:
343
344 static std::queue <Message> Messages;
345
346 static unsigned int messageId;
347
348 static constexpr double radtodeg = 180. / M_PI;
349 static constexpr double degtorad = M_PI / 180.;
350 static constexpr double hptoftlbssec = 550.0;
351 static constexpr double psftoinhg = 0.014138;
352 static constexpr double psftopa = 47.88;
353 static constexpr double ktstofps = 1.68781;
354 static constexpr double fpstokts = 1.0 / ktstofps;
355 static constexpr double inchtoft = 1.0/12.0;
356 static constexpr double fttom = 0.3048;
357 static constexpr double m3toft3 = 1.0/(fttom*fttom*fttom);
358 static constexpr double in3tom3 = inchtoft*inchtoft*inchtoft/m3toft3;
359 static constexpr double inhgtopa = 3386.38;
366 static constexpr double slugtolb = 32.174049;
367 static constexpr double lbtoslug = 1.0/slugtolb;
368 static constexpr double kgtolb = 2.20462;
369 static constexpr double kgtoslug = 0.06852168;
370 static const std::string needed_cfg_version;
371 static const std::string JSBSim_version;
372
373 static std::string CreateIndexedPropertyName(const std::string& Property, int index);
374
376
377public:
379enum {eL = 1, eM, eN };
381enum {eP = 1, eQ, eR };
383enum {eU = 1, eV, eW };
385enum {eX = 1, eY, eZ };
387enum {ePhi = 1, eTht, ePsi };
389enum {eDrag = 1, eSide, eLift };
391enum {eRoll = 1, ePitch, eYaw };
393enum {eNorth = 1, eEast, eDown };
395enum {eLat = 1, eLong, eRad };
398
399};
400
401}
402//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403#endif
#define p(x)
#define M_PI
Definition FGJSBBase.h:50
#define min(X, Y)
BaseException(const std::string &msg)
Definition FGJSBBase.h:61
Filter(double coeff, double dt)
Definition FGJSBBase.h:106
double execute(double in)
Definition FGJSBBase.h:112
void PutMessage(const Message &msg)
Places a Message structure on the Message queue.
Definition FGJSBBase.cpp:90
static unsigned int messageId
Definition FGJSBBase.h:346
static constexpr double radtodeg
Definition FGJSBBase.h:348
static double PitotTotalPressure(double mach, double p)
Compute the total pressure in front of the Pitot tube.
static char fgdef[6]
default text
Definition FGJSBBase.h:143
static double MachFromImpactPressure(double qc, double p)
Compute the Mach number from the differential pressure (qc) and the static pressure.
static char halfint[5]
low intensity text
Definition FGJSBBase.h:125
static constexpr double FahrenheitToCelsius(double fahrenheit)
Converts from degrees Fahrenheit to degrees Celsius.
Definition FGJSBBase.h:230
static constexpr double ktstofps
Definition FGJSBBase.h:353
static std::queue< Message > Messages
Definition FGJSBBase.h:344
static constexpr double CelsiusToRankine(double celsius)
Converts from degrees Celsius to degrees Rankine.
Definition FGJSBBase.h:202
FGJSBBase()
Constructor for FGJSBBase.
Definition FGJSBBase.h:81
static constexpr double inchtoft
Definition FGJSBBase.h:355
static constexpr double Constrain(double min, double value, double max)
Constrain a value between a minimum and a maximum value.
Definition FGJSBBase.h:333
static double GaussianRandomNumber(void)
static constexpr double psftopa
Definition FGJSBBase.h:352
static constexpr double slugtolb
Note that definition of lbtoslug by the inverse of slugtolb and not to a different constant you can a...
Definition FGJSBBase.h:366
static constexpr double m3toft3
Definition FGJSBBase.h:357
static char highint[5]
highlights text
Definition FGJSBBase.h:123
static constexpr double inhgtopa
Definition FGJSBBase.h:359
static constexpr double in3tom3
Definition FGJSBBase.h:358
int SomeMessages(void) const
Reads the message on the queue (but does not delete it).
Definition FGJSBBase.h:173
static char reset[5]
resets text properties
Definition FGJSBBase.h:129
static constexpr double KelvinToRankine(double kelvin)
Converts from degrees Kelvin to degrees Rankine.
Definition FGJSBBase.h:216
static constexpr double sign(double num)
Definition FGJSBBase.h:337
static constexpr double psftoinhg
Definition FGJSBBase.h:351
static constexpr double kgtoslug
Definition FGJSBBase.h:369
static char fgblue[6]
blue text
Definition FGJSBBase.h:135
static const std::string & GetVersion(void)
Returns the version number of JSBSim.
Definition FGJSBBase.h:185
static char normint[6]
normal intensity text
Definition FGJSBBase.h:127
static int gaussian_random_number_phase
Definition FGJSBBase.h:375
static constexpr double RankineToKelvin(double rankine)
Converts from degrees Rankine to degrees Kelvin.
Definition FGJSBBase.h:223
static constexpr double kgtolb
Definition FGJSBBase.h:368
static char fgred[6]
red text
Definition FGJSBBase.h:139
static constexpr double CelsiusToKelvin(double celsius)
Converts from degrees Celsius to degrees Kelvin.
Definition FGJSBBase.h:244
static constexpr double KelvinToCelsius(double kelvin)
Converts from degrees Kelvin to degrees Celsius.
Definition FGJSBBase.h:251
static bool EqualToRoundoff(double a, double b)
Finite precision comparison.
Definition FGJSBBase.h:301
static constexpr double fpstokts
Definition FGJSBBase.h:354
void PutMessage(const std::string &text)
Creates a message with the given text and places it on the queue.
void PutMessage(const std::string &text, int iVal)
Creates a message with the given text and integer value and places it on the queue.
static bool EqualToRoundoff(float a, double b)
Finite precision comparison.
Definition FGJSBBase.h:319
static constexpr double CelsiusToFahrenheit(double celsius)
Converts from degrees Celsius to degrees Fahrenheit.
Definition FGJSBBase.h:237
static constexpr double FeetToMeters(double measure)
Converts from feet to meters.
Definition FGJSBBase.h:258
static char fggreen[6]
green text
Definition FGJSBBase.h:141
static constexpr double hptoftlbssec
Definition FGJSBBase.h:350
Message * ProcessNextMessage(void)
Reads the next message on the queue and removes it from the queue.
virtual ~FGJSBBase()
Destructor for FGJSBBase.
Definition FGJSBBase.h:84
void PutMessage(const std::string &text, double dVal)
Creates a message with the given text and double value and places it on the queue.
void disableHighLighting(void)
Disables highlighting in the console output.
static double VcalibratedFromMach(double mach, double p)
Calculate the calibrated airspeed from the Mach number.
static constexpr double degtorad
Definition FGJSBBase.h:349
void ProcessMessage(void)
Reads the message on the queue and removes it from the queue.
static const std::string JSBSim_version
Definition FGJSBBase.h:371
static char underoff[6]
underline off
Definition FGJSBBase.h:133
static constexpr double lbtoslug
Definition FGJSBBase.h:367
static short debug_lvl
Definition FGJSBBase.h:190
static Message localMsg
Definition FGJSBBase.h:342
static double MachFromVcalibrated(double vcas, double p)
Calculate the Mach number from the calibrated airspeed.Based on the formulas in the US Air Force Airc...
static char underon[5]
underlines text
Definition FGJSBBase.h:131
static constexpr double KelvinToFahrenheit(double kelvin)
Converts from degrees Kelvin to degrees Fahrenheit.
Definition FGJSBBase.h:195
static bool EqualToRoundoff(double a, float b)
Finite precision comparison.
Definition FGJSBBase.h:327
static const std::string needed_cfg_version
Definition FGJSBBase.h:370
void PutMessage(const std::string &text, bool bVal)
Creates a message with the given text and boolean value and places it on the queue.
static bool EqualToRoundoff(float a, float b)
Finite precision comparison.
Definition FGJSBBase.h:310
static constexpr double fttom
Definition FGJSBBase.h:356
static char fgcyan[6]
cyan text
Definition FGJSBBase.h:137
static std::string CreateIndexedPropertyName(const std::string &Property, int index)
static constexpr double RankineToCelsius(double rankine)
Converts from degrees Rankine to degrees Celsius.
Definition FGJSBBase.h:209
JSBSim Message structure.
Definition FGJSBBase.h:87
enum JSBSim::FGJSBBase::Message::mType type