JSBSim Flight Dynamics Model  1.1.11 (13 Feb 2022)
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 
47 #include "input_output/string_utilities.h"
48 
49 #ifndef M_PI
50 # define M_PI 3.14159265358979323846
51 #endif
52 
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 FORWARD DECLARATIONS
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 
57 namespace JSBSim {
58 
59 class BaseException : public std::runtime_error {
60  public:
61  BaseException(const std::string& msg) : std::runtime_error(msg) {}
62 };
63 
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 CLASS DOCUMENTATION
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 
74 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 CLASS DECLARATION
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77 
78 class FGJSBBase {
79 public:
81  FGJSBBase() {};
82 
84  virtual ~FGJSBBase() {};
85 
87  struct Message {
88  unsigned int fdmId;
89  unsigned int messageId;
90  std::string text;
91  std::string subsystem;
92  enum mType {eText, eInteger, eDouble, eBool} type;
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  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 
341 protected:
342  static Message localMsg;
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 
375  static int gaussian_random_number_phase;
376 
377 public:
379 enum {eL = 1, eM, eN };
381 enum {eP = 1, eQ, eR };
383 enum {eU = 1, eV, eW };
385 enum {eX = 1, eY, eZ };
387 enum {ePhi = 1, eTht, ePsi };
389 enum {eDrag = 1, eSide, eLift };
391 enum {eRoll = 1, ePitch, eYaw };
393 enum {eNorth = 1, eEast, eDown };
395 enum {eLat = 1, eLong, eRad };
397 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
398 
399 };
400 
401 }
402 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 #endif
JSBSim::FGJSBBase::CelsiusToRankine
static constexpr double CelsiusToRankine(double celsius)
Converts from degrees Celsius to degrees Rankine.
Definition: FGJSBBase.h:202
JSBSim::FGJSBBase::FahrenheitToCelsius
static constexpr double FahrenheitToCelsius(double fahrenheit)
Converts from degrees Fahrenheit to degrees Celsius.
Definition: FGJSBBase.h:230
JSBSim::FGJSBBase::EqualToRoundoff
static bool EqualToRoundoff(double a, double b)
Finite precision comparison.
Definition: FGJSBBase.h:301
JSBSim::FGJSBBase::fgdef
static char fgdef[6]
default text
Definition: FGJSBBase.h:143
JSBSim::FGJSBBase::ProcessNextMessage
Message * ProcessNextMessage(void)
Reads the next message on the queue and removes it from the queue.
Definition: FGJSBBase.cpp:180
JSBSim::FGJSBBase::VcalibratedFromMach
static double VcalibratedFromMach(double mach, double p)
Calculate the calibrated airspeed from the Mach number.
Definition: FGJSBBase.cpp:289
JSBSim::FGJSBBase::PitotTotalPressure
static double PitotTotalPressure(double mach, double p)
Compute the total pressure in front of the Pitot tube.
Definition: FGJSBBase.cpp:245
JSBSim::FGJSBBase::EqualToRoundoff
static bool EqualToRoundoff(float a, double b)
Finite precision comparison.
Definition: FGJSBBase.h:319
JSBSim::FGJSBBase::KelvinToRankine
static constexpr double KelvinToRankine(double kelvin)
Converts from degrees Kelvin to degrees Rankine.
Definition: FGJSBBase.h:216
JSBSim::FGJSBBase::PutMessage
void PutMessage(const Message &msg)
Places a Message structure on the Message queue.
Definition: FGJSBBase.cpp:90
JSBSim::FGJSBBase::ProcessMessage
void ProcessMessage(void)
Reads the message on the queue and removes it from the queue.
Definition: FGJSBBase.cpp:148
JSBSim::BaseException
Definition: FGJSBBase.h:59
JSBSim::FGJSBBase::KelvinToFahrenheit
static constexpr double KelvinToFahrenheit(double kelvin)
Converts from degrees Kelvin to degrees Fahrenheit.
Definition: FGJSBBase.h:195
JSBSim::FGJSBBase::CelsiusToKelvin
static constexpr double CelsiusToKelvin(double celsius)
Converts from degrees Celsius to degrees Kelvin.
Definition: FGJSBBase.h:244
JSBSim::FGJSBBase::Message
JSBSim Message structure.
Definition: FGJSBBase.h:87
JSBSim::FGJSBBase::slugtolb
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
JSBSim::FGJSBBase::GetVersion
static const std::string & GetVersion(void)
Returns the version number of JSBSim.
Definition: FGJSBBase.h:185
JSBSim::FGJSBBase::halfint
static char halfint[5]
low intensity text
Definition: FGJSBBase.h:125
JSBSim::FGJSBBase
JSBSim Base class.
Definition: FGJSBBase.h:78
JSBSim::FGJSBBase::MachFromVcalibrated
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...
Definition: FGJSBBase.cpp:300
JSBSim::FGJSBBase::EqualToRoundoff
static bool EqualToRoundoff(float a, float b)
Finite precision comparison.
Definition: FGJSBBase.h:310
JSBSim::FGJSBBase::fgred
static char fgred[6]
red text
Definition: FGJSBBase.h:139
JSBSim::FGJSBBase::FGJSBBase
FGJSBBase()
Constructor for FGJSBBase.
Definition: FGJSBBase.h:81
JSBSim::FGJSBBase::fggreen
static char fggreen[6]
green text
Definition: FGJSBBase.h:141
JSBSim::FGJSBBase::reset
static char reset[5]
resets text properties
Definition: FGJSBBase.h:129
JSBSim::FGJSBBase::fgcyan
static char fgcyan[6]
cyan text
Definition: FGJSBBase.h:137
JSBSim::FGJSBBase::RankineToCelsius
static constexpr double RankineToCelsius(double rankine)
Converts from degrees Rankine to degrees Celsius.
Definition: FGJSBBase.h:209
JSBSim::FGJSBBase::Constrain
static constexpr double Constrain(double min, double value, double max)
Constrain a value between a minimum and a maximum value.
Definition: FGJSBBase.h:333
JSBSim::FGJSBBase::CelsiusToFahrenheit
static constexpr double CelsiusToFahrenheit(double celsius)
Converts from degrees Celsius to degrees Fahrenheit.
Definition: FGJSBBase.h:237
JSBSim::FGJSBBase::Filter
First order, (low pass / lag) filter.
Definition: FGJSBBase.h:99
JSBSim::FGJSBBase::~FGJSBBase
virtual ~FGJSBBase()
Destructor for FGJSBBase.
Definition: FGJSBBase.h:84
JSBSim::FGJSBBase::EqualToRoundoff
static bool EqualToRoundoff(double a, float b)
Finite precision comparison.
Definition: FGJSBBase.h:327
JSBSim::FGJSBBase::underon
static char underon[5]
underlines text
Definition: FGJSBBase.h:131
JSBSim::FGJSBBase::normint
static char normint[6]
normal intensity text
Definition: FGJSBBase.h:127
JSBSim::FGJSBBase::FeetToMeters
static constexpr double FeetToMeters(double measure)
Converts from feet to meters.
Definition: FGJSBBase.h:258
JSBSim::FGJSBBase::RankineToKelvin
static constexpr double RankineToKelvin(double rankine)
Converts from degrees Rankine to degrees Kelvin.
Definition: FGJSBBase.h:223
JSBSim::FGJSBBase::highint
static char highint[5]
highlights text
Definition: FGJSBBase.h:123
JSBSim::FGJSBBase::KelvinToCelsius
static constexpr double KelvinToCelsius(double kelvin)
Converts from degrees Kelvin to degrees Celsius.
Definition: FGJSBBase.h:251
JSBSim::FGJSBBase::disableHighLighting
void disableHighLighting(void)
Disables highlighting in the console output.
Definition: FGJSBBase.cpp:191
JSBSim::FGJSBBase::fgblue
static char fgblue[6]
blue text
Definition: FGJSBBase.h:135
JSBSim::FGJSBBase::underoff
static char underoff[6]
underline off
Definition: FGJSBBase.h:133
JSBSim::FGJSBBase::SomeMessages
int SomeMessages(void) const
Reads the message on the queue (but does not delete it).
Definition: FGJSBBase.h:173
JSBSim::FGJSBBase::MachFromImpactPressure
static double MachFromImpactPressure(double qc, double p)
Compute the Mach number from the differential pressure (qc) and the static pressure.
Definition: FGJSBBase.cpp:275