]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGJSBBase.h
Remove the deprecated warning for JSBSim's egt_degf
[flightgear.git] / src / FDM / JSBSim / 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 Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  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 with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  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
46 using std::min;
47 using std::max;
48
49 #include "input_output/string_utilities.h"
50
51 #ifndef M_PI
52 #  define M_PI 3.14159265358979323846
53 #endif
54
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 DEFINITIONS
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58
59 #define ID_JSBBASE "$Id: FGJSBBase.h,v 1.34 2011/10/22 14:38:30 bcoconni Exp $"
60
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 FORWARD DECLARATIONS
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64
65 namespace JSBSim {
66
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 CLASS DOCUMENTATION
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
71 /** JSBSim Base class.
72 *   This class provides universal constants, utility functions, messaging
73 *   functions, and enumerated constants to JSBSim.
74     @author Jon S. Berndt
75     @version $Id: FGJSBBase.h,v 1.34 2011/10/22 14:38:30 bcoconni Exp $
76 */
77
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 CLASS DECLARATION
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
81
82 class FGJSBBase {
83 public:
84   /// Constructor for FGJSBBase.
85   FGJSBBase() {};
86
87   /// Destructor for FGJSBBase.
88   virtual ~FGJSBBase() {};
89
90   /// JSBSim Message structure
91   class Message {
92   public:
93     unsigned int fdmId;
94     unsigned int messageId;
95     std::string text;
96     std::string subsystem;
97     enum mType {eText, eInteger, eDouble, eBool} type;
98     bool bVal;
99     int  iVal;
100     double dVal;
101   };
102
103   /// First order, (low pass / lag) filter
104   class Filter {
105     double prev_in;
106     double prev_out;
107     double ca;
108     double cb;
109     public: Filter(void) {}
110     public: Filter(double coeff, double dt) {
111       prev_in = prev_out = 0.0;
112       double denom = 2.0 + coeff*dt;
113       ca = coeff*dt/denom;
114       cb = (2.0 - coeff*dt)/denom;
115     }
116     public: double execute(double in) {
117       double out = (in + prev_in)*ca + prev_out*cb;
118       prev_in = in;
119       prev_out = out;
120       return out;
121     }
122   };
123
124   ///@name JSBSim console output highlighting terms.
125   //@{
126   /// highlights text
127   static char highint[5];
128   /// low intensity text
129   static char halfint[5];
130   /// normal intensity text
131   static char normint[6];
132   /// resets text properties
133   static char reset[5];
134   /// underlines text
135   static char underon[5];
136   /// underline off
137   static char underoff[6];
138   /// blue text
139   static char fgblue[6];
140   /// cyan text
141   static char fgcyan[6];
142   /// red text
143   static char fgred[6];
144   /// green text
145   static char fggreen[6];
146   /// default text
147   static char fgdef[6];
148   //@}
149
150   ///@name JSBSim Messaging functions
151   //@{
152   /** Places a Message structure on the Message queue.
153       @param msg pointer to a Message structure
154       @return pointer to a Message structure */
155   void PutMessage(const Message& msg);
156   /** Creates a message with the given text and places it on the queue.
157       @param text message text
158       @return pointer to a Message structure */
159   void PutMessage(const std::string& text);
160   /** Creates a message with the given text and boolean value and places it on the queue.
161       @param text message text
162       @param bVal boolean value associated with the message
163       @return pointer to a Message structure */
164   void PutMessage(const std::string& text, bool bVal);
165   /** Creates a message with the given text and integer value and places it on the queue.
166       @param text message text
167       @param iVal integer value associated with the message
168       @return pointer to a Message structure */
169   void PutMessage(const std::string& text, int iVal);
170   /** Creates a message with the given text and double value and places it on the queue.
171       @param text message text
172       @param dVal double value associated with the message
173       @return pointer to a Message structure */
174   void PutMessage(const std::string& text, double dVal);
175   /** Reads the message on the queue (but does not delete it).
176       @return 1 if some messages */
177   int SomeMessages(void);
178   /** Reads the message on the queue and removes it from the queue.
179       This function also prints out the message.*/
180   void ProcessMessage(void);
181   /** Reads the next message on the queue and removes it from the queue.
182       This function also prints out the message.
183       @return a pointer to the message, or NULL if there are no messages.*/
184   Message* ProcessNextMessage(void);
185   //@}
186
187   /** Returns the version number of JSBSim.
188   *   @return The version number of JSBSim. */
189   std::string GetVersion(void) {return JSBSim_version;}
190
191   /// Disables highlighting in the console output.
192   void disableHighLighting(void);
193
194   static short debug_lvl;
195
196   /** Converts from degrees Kelvin to degrees Fahrenheit.
197   *   @param kelvin The temperature in degrees Kelvin.
198   *   @return The temperature in Fahrenheit. */
199   static double KelvinToFahrenheit (double kelvin) {
200     return 1.8*kelvin - 459.4;
201   }
202
203   /** Converts from degrees Celsius to degrees Rankine.
204   *   @param celsius The temperature in degrees Celsius.
205   *   @return The temperature in Rankine. */
206   static double CelsiusToRankine (double celsius) {
207     return celsius * 1.8 + 491.67;
208   }
209
210   /** Converts from degrees Rankine to degrees Celsius.
211   *   @param rankine The temperature in degrees Rankine.
212   *   @return The temperature in Celsius. */
213   static double RankineToCelsius (double rankine) {
214     return (rankine - 491.67)/1.8;
215   }
216
217   /** Converts from degrees Kelvin to degrees Rankine.
218   *   @param kelvin The temperature in degrees Kelvin.
219   *   @return The temperature in Rankine. */
220   static double KelvinToRankine (double kelvin) {
221     return kelvin * 1.8;
222   }
223
224   /** Converts from degrees Rankine to degrees Kelvin.
225   *   @param rankine The temperature in degrees Rankine.
226   *   @return The temperature in Kelvin. */
227   static double RankineToKelvin (double rankine) {
228     return rankine/1.8;
229   }
230
231   /** Converts from degrees Fahrenheit to degrees Celsius.
232   *   @param fahrenheit The temperature in degrees Fahrenheit.
233   *   @return The temperature in Celsius. */
234   static double FahrenheitToCelsius (double fahrenheit) {
235     return (fahrenheit - 32.0)/1.8;
236   }
237
238   /** Converts from degrees Celsius to degrees Fahrenheit.
239   *   @param celsius The temperature in degrees Celsius.
240   *   @return The temperature in Fahrenheit. */
241   static double CelsiusToFahrenheit (double celsius) {
242     return celsius * 1.8 + 32.0;
243   }
244
245   /** Converts from degrees Celsius to degrees Kelvin
246   *   @param celsius The temperature in degrees Celsius.
247   *   @return The temperature in Kelvin. */
248   static double CelsiusToKelvin (double celsius) {
249     return celsius + 273.15;
250   }
251
252   /** Converts from degrees Kelvin to degrees Celsius
253   *   @param celsius The temperature in degrees Kelvin.
254   *   @return The temperature in Celsius. */
255   static double KelvinToCelsius (double kelvin) {
256     return kelvin - 273.15;
257   }
258
259   /** Calculate the calibrated airspeed from the Mach number. It uses the
260   *   Rayleigh formula for supersonic speeds (See "Introduction to Aerodynamics
261   *   of a Compressible Fluid - H.W. Liepmann, A.E. Puckett - Wiley & sons
262   *   (1947)" ยง5.4 pp 75-80)
263   *   @param mach  The Mach number
264   *   @param p     Pressure in psf
265   *   @param psl   Pressure at sea level in psf
266   *   @param rhosl Density at sea level in slugs/ft^3
267   *   @return The calibrated airspeed (CAS) in ft/s
268   * */
269   static double VcalibratedFromMach(double mach, double p, double psl, double rhosl);
270
271   /** Calculate the Mach number from the calibrated airspeed. For subsonic
272   * speeds, the reversed formula has a closed form. For supersonic speeds, the
273   * Rayleigh formula is reversed by the Newton-Raphson algorithm.
274   *   @param vcas  The calibrated airspeed (CAS) in ft/s
275   *   @param p     Pressure in psf
276   *   @param psl   Pressure at sea level in psf
277   *   @param rhosl Density at sea level in slugs/ft^3
278   *   @return The Mach number
279   * */
280   static double MachFromVcalibrated(double vcas, double p, double psl, double rhosl);
281
282   /** Finite precision comparison.
283       @param a first value to compare
284       @param b second value to compare
285       @return if the two values can be considered equal up to roundoff */
286   static bool EqualToRoundoff(double a, double b) {
287     double eps = 2.0*DBL_EPSILON;
288     return std::fabs(a - b) <= eps * max(std::fabs(a), std::fabs(b));
289   }
290
291   /** Finite precision comparison.
292       @param a first value to compare
293       @param b second value to compare
294       @return if the two values can be considered equal up to roundoff */
295   static bool EqualToRoundoff(float a, float b) {
296     float eps = 2.0*FLT_EPSILON;
297     return std::fabs(a - b) <= eps * max(std::fabs(a), std::fabs(b));
298   }
299
300   /** Finite precision comparison.
301       @param a first value to compare
302       @param b second value to compare
303       @return if the two values can be considered equal up to roundoff */
304   static bool EqualToRoundoff(float a, double b) {
305     return EqualToRoundoff(a, (float)b);
306   }
307
308   /** Finite precision comparison.
309       @param a first value to compare
310       @param b second value to compare
311       @return if the two values can be considered equal up to roundoff */
312   static bool EqualToRoundoff(double a, float b) {
313     return EqualToRoundoff((float)a, b);
314   }
315   
316   /** Constrain a value between a minimum and a maximum value.
317   */
318   static double Constrain(double min, double value, double max) {
319     return value<min?(min):(value>max?(max):(value));
320   }
321   
322   static double sign(double num) {return num>=0.0?1.0:-1.0;}
323
324 protected:
325   static Message localMsg;
326
327   static std::queue <Message> Messages;
328
329   void Debug(int) {};
330
331   static unsigned int messageId;
332
333   static const double radtodeg;
334   static const double degtorad;
335   static const double hptoftlbssec;
336   static const double psftoinhg;
337   static const double psftopa;
338   static const double fpstokts;
339   static const double ktstofps;
340   static const double inchtoft;
341   static const double in3tom3;
342   static const double m3toft3;
343   static const double inhgtopa;
344   static const double fttom;
345   static double Reng;         // Specific Gas Constant,ft^2/(sec^2*R)
346   static double Rstar;
347   static double Mair;
348   static const double SHRatio;
349   static const double lbtoslug;
350   static const double slugtolb;
351   static const double kgtolb;
352   static const double kgtoslug;
353   static const std::string needed_cfg_version;
354   static const std::string JSBSim_version;
355
356   static std::string CreateIndexedPropertyName(const std::string& Property, int index);
357
358   static double GaussianRandomNumber(void);
359
360 public:
361 /// Moments L, M, N
362 enum {eL     = 1, eM,     eN    };
363 /// Rates P, Q, R
364 enum {eP     = 1, eQ,     eR    };
365 /// Velocities U, V, W
366 enum {eU     = 1, eV,     eW    };
367 /// Positions X, Y, Z
368 enum {eX     = 1, eY,     eZ    };
369 /// Euler angles Phi, Theta, Psi
370 enum {ePhi   = 1, eTht,   ePsi  };
371 /// Stability axis forces, Drag, Side force, Lift
372 enum {eDrag  = 1, eSide,  eLift };
373 /// Local frame orientation Roll, Pitch, Yaw
374 enum {eRoll  = 1, ePitch, eYaw  };
375 /// Local frame position North, East, Down
376 enum {eNorth = 1, eEast,  eDown };
377 /// Locations Radius, Latitude, Longitude
378 enum {eLat = 1, eLong, eRad     };
379 /// Conversion specifiers
380 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
381
382 };
383
384 }
385 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 #endif
387