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