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