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 This function also prints out the message.*/
186 void ProcessMessage(void);
187 /** Reads the next message on the queue and removes it from the queue.
188 This function also prints out the message.
189 @return a pointer to the message, or NULL if there are no messages.*/
190 Message* ProcessNextMessage(void);
193 /** Returns the version number of JSBSim.
194 * @return The version number of JSBSim. */
195 string GetVersion(void) {return JSBSim_version;}
197 /// Disables highlighting in the console output.
198 void disableHighLighting(void);
200 static short debug_lvl;
202 /** Converts from degrees Kelvin to degrees Fahrenheit.
203 * @param kelvin The temperature in degrees Kelvin.
204 * @return The temperature in Fahrenheit. */
205 static double KelvinToFahrenheit (double kelvin) {
206 return 1.8*kelvin - 459.4;
209 /** Converts from degrees Celsius to degrees Rankine.
210 * @param celsius The temperature in degrees Celsius.
211 * @return The temperature in Rankine. */
212 static double CelsiusToRankine (double celsius) {
213 return celsius * 1.8 + 491.67;
216 /** Converts from degrees Rankine to degrees Celsius.
217 * @param rankine The temperature in degrees Rankine.
218 * @return The temperature in Celsius. */
219 static double RankineToCelsius (double rankine) {
220 return (rankine - 491.67)/1.8;
223 /** Converts from degrees Kelvin to degrees Rankine.
224 * @param kelvin The temperature in degrees Kelvin.
225 * @return The temperature in Rankine. */
226 static double KelvinToRankine (double kelvin) {
230 /** Converts from degrees Rankine to degrees Kelvin.
231 * @param rankine The temperature in degrees Rankine.
232 * @return The temperature in Kelvin. */
233 static double RankineToKelvin (double rankine) {
237 /** Converts from degrees Fahrenheit to degrees Celsius.
238 * @param fahrenheit The temperature in degrees Fahrenheit.
239 * @return The temperature in Celsius. */
240 static double FahrenheitToCelsius (double fahrenheit) {
241 return (fahrenheit - 32.0)/1.8;
244 /** Converts from degrees Celsius to degrees Fahrenheit.
245 * @param celsius The temperature in degrees Celsius.
246 * @return The temperature in Fahrenheit. */
247 static double CelsiusToFahrenheit (double celsius) {
248 return celsius * 1.8 + 32.0;
251 /** Converts from degrees Celsius to degrees Kelvin
252 * @param celsius The temperature in degrees Celsius.
253 * @return The temperature in Kelvin. */
254 static double CelsiusToKelvin (double celsius) {
255 return celsius + 273.15;
258 /** Converts from degrees Kelvin to degrees Celsius
259 * @param celsius The temperature in degrees Kelvin.
260 * @return The temperature in Celsius. */
261 static double KelvinToCelsius (double kelvin) {
262 return kelvin - 273.15;
265 /** Finite precision comparison.
266 @param a first value to compare
267 @param b second value to compare
268 @return if the two values can be considered equal up to roundoff */
269 static bool EqualToRoundoff(double a, double b) {
270 double eps = 2.0*DBL_EPSILON;
271 return fabs(a - b) <= eps*max(fabs(a), fabs(b));
274 /** Finite precision comparison.
275 @param a first value to compare
276 @param b second value to compare
277 @return if the two values can be considered equal up to roundoff */
278 static bool EqualToRoundoff(float a, float b) {
279 float eps = 2.0*FLT_EPSILON;
280 return fabs(a - b) <= eps*max(fabs(a), fabs(b));
283 /** Finite precision comparison.
284 @param a first value to compare
285 @param b second value to compare
286 @return if the two values can be considered equal up to roundoff */
287 static bool EqualToRoundoff(float a, double b) {
288 return EqualToRoundoff(a, (float)b);
291 /** Finite precision comparison.
292 @param a first value to compare
293 @param b second value to compare
294 @return if the two values can be considered equal up to roundoff */
295 static bool EqualToRoundoff(double a, float b) {
296 return EqualToRoundoff((float)a, b);
299 /** Constrain a value between a minimum and a maximum value.
301 static double Constrain(double min, double value, double max) {
302 return value<min?(min):(value>max?(max):(value));
305 static double sign(double num) {return num>=0.0?1.0:-1.0;}
308 static Message localMsg;
310 static std::queue <Message> Messages;
312 void Debug(int from) {};
314 static unsigned int messageId;
316 static const double radtodeg;
317 static const double degtorad;
318 static const double hptoftlbssec;
319 static const double psftoinhg;
320 static const double psftopa;
321 static const double fpstokts;
322 static const double ktstofps;
323 static const double inchtoft;
324 static const double in3tom3;
325 static const double m3toft3;
326 static const double inhgtopa;
327 static const double fttom;
328 static double Reng; // Specific Gas Constant,ft^2/(sec^2*R)
329 static const double SHRatio;
330 static const double lbtoslug;
331 static const double slugtolb;
332 static const double kgtolb;
333 static const double kgtoslug;
334 static const string needed_cfg_version;
335 static const string JSBSim_version;
337 static string CreateIndexedPropertyName(string Property, int index)
339 std::stringstream str;
343 return Property + "[" + tmp + "]";
346 static double GaussianRandomNumber(void)
348 static double V1, V2, S;
349 static int phase = 0;
352 V1 = V2 = S = X = 0.0;
356 double U1 = (double)rand() / RAND_MAX;
357 double U2 = (double)rand() / RAND_MAX;
361 S = V1 * V1 + V2 * V2;
362 } while(S >= 1 || S == 0);
364 X = V1 * sqrt(-2 * log(S) / S);
366 X = V2 * sqrt(-2 * log(S) / S);
375 enum {eL = 1, eM, eN };
377 enum {eP = 1, eQ, eR };
378 /// Velocities U, V, W
379 enum {eU = 1, eV, eW };
380 /// Positions X, Y, Z
381 enum {eX = 1, eY, eZ };
382 /// Euler angles Phi, Theta, Psi
383 enum {ePhi = 1, eTht, ePsi };
384 /// Stability axis forces, Drag, Side force, Lift
385 enum {eDrag = 1, eSide, eLift };
386 /// Local frame orientation Roll, Pitch, Yaw
387 enum {eRoll = 1, ePitch, eYaw };
388 /// Local frame position North, East, Down
389 enum {eNorth = 1, eEast, eDown };
390 /// Locations Radius, Latitude, Longitude
391 enum {eLat = 1, eLong, eRad };
392 /// Conversion specifiers
393 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
398 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%