1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 2001 Jon S. Berndt (jsb@hal-pc.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 #include "input_output/string_utilities.h"
54 # define M_PI 3.14159265358979323846
57 #if !defined(WIN32) || defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 1300))
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65 #define ID_JSBBASE "$Id$"
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77 /** JSBSim Base class.
78 * This class provides universal constants, utility functions, messaging
79 * functions, and enumerated constants to JSBSim.
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
90 /// Constructor for FGJSBBase.
93 /// Destructor for FGJSBBase.
96 /// JSBSim Message structure
100 unsigned int messageId;
103 enum mType {eText, eInteger, eDouble, eBool} type;
109 /// First order, (low pass / lag) filter
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;
120 cb = (2.0 - coeff*dt)/denom;
122 public: double execute(double in) {
123 double out = (in + prev_in)*ca + prev_out*cb;
130 ///@name JSBSim console output highlighting terms.
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];
141 static char underon[5];
143 static char underoff[6];
145 static char fgblue[6];
147 static char fgcyan[6];
149 static char fgred[6];
151 static char fggreen[6];
153 static char fgdef[6];
156 ///@name JSBSim Messaging functions
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);
189 /** Returns the version number of JSBSim.
190 * @return The version number of JSBSim. */
191 string GetVersion(void) {return JSBSim_version;}
193 /// Disables highlighting in the console output.
194 void disableHighLighting(void);
196 static short debug_lvl;
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;
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;
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;
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) {
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) {
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;
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;
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;
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;
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));
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));
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);
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);
295 /** Constrain a value between a minimum and a maximum value.
297 static double Constrain(double min, double value, double max) {
298 return value<min?(min):(value>max?(max):(value));
301 static double sign(double num) {return num>=0.0?1.0:-1.0;}
304 static Message localMsg;
306 static std::queue <Message> Messages;
308 void Debug(int from) {};
310 static unsigned int messageId;
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;
333 static string CreateIndexedPropertyName(string Property, int index)
335 std::stringstream str;
339 return Property + "[" + tmp + "]";
342 static double GaussianRandomNumber(void)
344 static double V1, V2, S;
345 static int phase = 0;
348 V1 = V2 = S = X = 0.0;
352 double U1 = (double)rand() / RAND_MAX;
353 double U2 = (double)rand() / RAND_MAX;
357 S = V1 * V1 + V2 * V2;
358 } while(S >= 1 || S == 0);
360 X = V1 * sqrt(-2 * log(S) / S);
362 X = V2 * sqrt(-2 * log(S) / S);
371 enum {eL = 1, eM, eN };
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 };
394 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%