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