1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 --------------------------------------------------------------------------------
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 #include "input_output/string_utilities.h"
52 # define M_PI 3.14159265358979323846
54 #define IDENT(a,b) static const char* const (a)[] = {b,(a)[0]}
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 #define ID_JSBBASE "$Id: FGJSBBase.h,v 1.41 2014/09/03 17:35:04 bcoconni Exp $"
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 /** JSBSim Base class.
73 * This class provides universal constants, utility functions, messaging
74 * functions, and enumerated constants to JSBSim.
76 @version $Id: FGJSBBase.h,v 1.41 2014/09/03 17:35:04 bcoconni Exp $
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85 /// Constructor for FGJSBBase.
88 /// Destructor for FGJSBBase.
89 virtual ~FGJSBBase() {};
91 /// JSBSim Message structure
95 unsigned int messageId;
97 std::string subsystem;
98 enum mType {eText, eInteger, eDouble, eBool} type;
104 /// First order, (low pass / lag) filter
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;
115 cb = (2.0 - coeff*dt)/denom;
117 public: double execute(double in) {
118 double out = (in + prev_in)*ca + prev_out*cb;
125 ///@name JSBSim console output highlighting terms.
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];
136 static char underon[5];
138 static char underoff[6];
140 static char fgblue[6];
142 static char fgcyan[6];
144 static char fgred[6];
146 static char fggreen[6];
148 static char fgdef[6];
151 ///@name JSBSim Messaging functions
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);
188 /** Returns the version number of JSBSim.
189 * @return The version number of JSBSim. */
190 std::string GetVersion(void) {return JSBSim_version;}
192 /// Disables highlighting in the console output.
193 void disableHighLighting(void);
195 static short debug_lvl;
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;
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;
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;
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) {
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) {
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;
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;
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;
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;
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;
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
277 static double VcalibratedFromMach(double mach, double p, double psl, double rhosl);
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
288 static double MachFromVcalibrated(double vcas, double p, double psl, double rhosl);
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));
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));
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);
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);
324 /** Constrain a value between a minimum and a maximum value.
326 static double Constrain(double min, double value, double max) {
327 return value<min?(min):(value>max?(max):(value));
330 static double sign(double num) {return num>=0.0?1.0:-1.0;}
332 static double GaussianRandomNumber(void);
335 static Message localMsg;
337 static std::queue <Message> Messages;
341 static unsigned int messageId;
343 static const double radtodeg;
344 static const double degtorad;
345 static const double hptoftlbssec;
346 static const double psftoinhg;
347 static const double psftopa;
348 static const double fpstokts;
349 static const double ktstofps;
350 static const double inchtoft;
351 static const double in3tom3;
352 static const double m3toft3;
353 static const double inhgtopa;
354 static const double fttom;
355 static double Reng; // Specific Gas Constant,ft^2/(sec^2*R)
358 static const double SHRatio;
359 static const double lbtoslug;
360 static const double slugtolb;
361 static const double kgtolb;
362 static const double kgtoslug;
363 static const std::string needed_cfg_version;
364 static const std::string JSBSim_version;
366 static std::string CreateIndexedPropertyName(const std::string& Property, int index);
368 static int gaussian_random_number_phase;
372 enum {eL = 1, eM, eN };
374 enum {eP = 1, eQ, eR };
375 /// Velocities U, V, W
376 enum {eU = 1, eV, eW };
377 /// Positions X, Y, Z
378 enum {eX = 1, eY, eZ };
379 /// Euler angles Phi, Theta, Psi
380 enum {ePhi = 1, eTht, ePsi };
381 /// Stability axis forces, Drag, Side force, Lift
382 enum {eDrag = 1, eSide, eLift };
383 /// Local frame orientation Roll, Pitch, Yaw
384 enum {eRoll = 1, ePitch, eYaw };
385 /// Local frame position North, East, Down
386 enum {eNorth = 1, eEast, eDown };
387 /// Locations Radius, Latitude, Longitude
388 enum {eLat = 1, eLong, eRad };
389 /// Conversion specifiers
390 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
395 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%