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