]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGJSBBase.h
7e56d5f5f5079d1574d12b24c9c96004a78ea9ba
[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       @return pointer to a Message structure (or NULL if no mesage) */
186   Message* ProcessMessage(void);
187   //@}
188
189   /** Returns the version number of JSBSim.
190   *   @return The version number of JSBSim. */
191   string GetVersion(void) {return JSBSim_version;}
192
193   /// Disables highlighting in the console output.
194   void disableHighLighting(void);
195
196   static short debug_lvl;
197
198   /** Converts from degrees Kelvin to degrees Fahrenheit.
199   *   @param kelvin The temperature in degrees Kelvin.
200   *   @return The temperature in Fahrenheit. */
201   static double KelvinToFahrenheit (double kelvin) {
202     return 1.8*kelvin - 459.4;
203   }
204
205   /** Converts from degrees Celsius to degrees Rankine.
206   *   @param celsius The temperature in degrees Celsius.
207   *   @return The temperature in Rankine. */
208   static double CelsiusToRankine (double celsius) {
209     return celsius * 1.8 + 491.67;
210   }
211
212   /** Converts from degrees Rankine to degrees Celsius.
213   *   @param rankine The temperature in degrees Rankine.
214   *   @return The temperature in Celsius. */
215   static double RankineToCelsius (double rankine) {
216     return (rankine - 491.67)/1.8;
217   }
218
219   /** Converts from degrees Kelvin to degrees Rankine.
220   *   @param kelvin The temperature in degrees Kelvin.
221   *   @return The temperature in Rankine. */
222   static double KelvinToRankine (double kelvin) {
223     return kelvin * 1.8;
224   }
225
226   /** Converts from degrees Rankine to degrees Kelvin.
227   *   @param rankine The temperature in degrees Rankine.
228   *   @return The temperature in Kelvin. */
229   static double RankineToKelvin (double rankine) {
230     return rankine/1.8;
231   }
232
233   /** Converts from degrees Fahrenheit to degrees Celsius.
234   *   @param fahrenheit The temperature in degrees Fahrenheit.
235   *   @return The temperature in Celsius. */
236   static double FahrenheitToCelsius (double fahrenheit) {
237     return (fahrenheit - 32.0)/1.8;
238   }
239
240   /** Converts from degrees Celsius to degrees Fahrenheit.
241   *   @param celsius The temperature in degrees Celsius.
242   *   @return The temperature in Fahrenheit. */
243   static double CelsiusToFahrenheit (double celsius) {
244     return celsius * 1.8 + 32.0;
245   }
246
247   /** Converts from degrees Celsius to degrees Kelvin
248   *   @param celsius The temperature in degrees Celsius.
249   *   @return The temperature in Kelvin. */
250   static double CelsiusToKelvin (double celsius) {
251     return celsius + 273.15;
252   }
253
254   /** Converts from degrees Kelvin to degrees Celsius
255   *   @param celsius The temperature in degrees Kelvin.
256   *   @return The temperature in Celsius. */
257   static double KelvinToCelsius (double kelvin) {
258     return kelvin - 273.15;
259   }
260
261   /** Finite precision comparison.
262       @param a first value to compare
263       @param b second value to compare
264       @return if the two values can be considered equal up to roundoff */
265   static bool EqualToRoundoff(double a, double b) {
266     double eps = 2.0*DBL_EPSILON;
267     return fabs(a - b) <= eps*max(fabs(a), fabs(b));
268   }
269
270   /** Finite precision comparison.
271       @param a first value to compare
272       @param b second value to compare
273       @return if the two values can be considered equal up to roundoff */
274   static bool EqualToRoundoff(float a, float b) {
275     float eps = 2.0*FLT_EPSILON;
276     return fabs(a - b) <= eps*max(fabs(a), fabs(b));
277   }
278
279   /** Finite precision comparison.
280       @param a first value to compare
281       @param b second value to compare
282       @return if the two values can be considered equal up to roundoff */
283   static bool EqualToRoundoff(float a, double b) {
284     return EqualToRoundoff(a, (float)b);
285   }
286
287   /** Finite precision comparison.
288       @param a first value to compare
289       @param b second value to compare
290       @return if the two values can be considered equal up to roundoff */
291   static bool EqualToRoundoff(double a, float b) {
292     return EqualToRoundoff((float)a, b);
293   }
294   
295   /** Constrain a value between a minimum and a maximum value.
296   */
297   static double Constrain(double min, double value, double max) {
298     return value<min?(min):(value>max?(max):(value));
299   }
300   
301   static double sign(double num) {return num>=0.0?1.0:-1.0;}
302
303 protected:
304   static Message localMsg;
305
306   static std::queue <Message> Messages;
307
308   void Debug(int from) {};
309
310   static unsigned int messageId;
311
312   static const double radtodeg;
313   static const double degtorad;
314   static const double hptoftlbssec;
315   static const double psftoinhg;
316   static const double psftopa;
317   static const double fpstokts;
318   static const double ktstofps;
319   static const double inchtoft;
320   static const double in3tom3;
321   static const double m3toft3;
322   static const double inhgtopa;
323   static const double fttom;
324   static double Reng;         // Specific Gas Constant,ft^2/(sec^2*R)
325   static const double SHRatio;
326   static const double lbtoslug;
327   static const double slugtolb;
328   static const double kgtolb;
329   static const double kgtoslug;
330   static const string needed_cfg_version;
331   static const string JSBSim_version;
332
333   static string CreateIndexedPropertyName(string Property, int index)
334   {
335     std::stringstream str;
336     str << index;
337     string tmp;
338     str >> tmp;
339     return Property + "[" + tmp + "]";
340   }
341
342   static double GaussianRandomNumber(void)
343   {
344     static double V1, V2, S;
345     static int phase = 0;
346     double X;
347
348     V1 = V2 = S = X = 0.0;
349
350     if (phase == 0) {
351       do {
352         double U1 = (double)rand() / RAND_MAX;
353         double U2 = (double)rand() / RAND_MAX;
354
355         V1 = 2 * U1 - 1;
356         V2 = 2 * U2 - 1;
357         S = V1 * V1 + V2 * V2;
358       } while(S >= 1 || S == 0);
359
360       X = V1 * sqrt(-2 * log(S) / S);
361     } else
362       X = V2 * sqrt(-2 * log(S) / S);
363
364     phase = 1 - phase;
365
366     return X;
367   }
368
369 public:
370 /// Moments L, M, N
371 enum {eL     = 1, eM,     eN    };
372 /// Rates P, Q, R
373 enum {eP     = 1, eQ,     eR    };
374 /// Velocities U, V, W
375 enum {eU     = 1, eV,     eW    };
376 /// Positions X, Y, Z
377 enum {eX     = 1, eY,     eZ    };
378 /// Euler angles Phi, Theta, Psi
379 enum {ePhi   = 1, eTht,   ePsi  };
380 /// Stability axis forces, Drag, Side force, Lift
381 enum {eDrag  = 1, eSide,  eLift };
382 /// Local frame orientation Roll, Pitch, Yaw
383 enum {eRoll  = 1, ePitch, eYaw  };
384 /// Local frame position North, East, Down
385 enum {eNorth = 1, eEast,  eDown };
386 /// Locations Radius, Latitude, Longitude
387 enum {eLat = 1, eLong, eRad     };
388 /// Conversion specifiers
389 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
390
391 };
392
393 }
394 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 #endif
396