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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
46 #include "input_output/string_utilities.h"
49 # define M_PI 3.14159265358979323846
52 #if defined(_MSC_VER) && (_MSC_VER < 1300)
55 template <class T> inline T max(const T& a, const T& b)
57 return (a > b) ? a : b;
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66 #define ID_JSBBASE "$Id$"
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
78 /** JSBSim Base class.
79 * This class provides universal constants, utility functions, messaging
80 * functions, and enumerated constants to JSBSim.
85 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
91 /// Constructor for FGJSBBase.
94 /// Destructor for FGJSBBase.
97 /// JSBSim Message structure
101 unsigned int messageId;
103 std::string subsystem;
104 enum mType {eText, eInteger, eDouble, eBool} type;
110 /// First order, (low pass / lag) filter
116 public: Filter(void) {}
117 public: Filter(double coeff, double dt) {
118 prev_in = prev_out = 0.0;
119 double denom = 2.0 + coeff*dt;
121 cb = (2.0 - coeff*dt)/denom;
123 public: double execute(double in) {
124 double out = (in + prev_in)*ca + prev_out*cb;
131 ///@name JSBSim console output highlighting terms.
134 static char highint[5];
135 /// low intensity text
136 static char halfint[5];
137 /// normal intensity text
138 static char normint[6];
139 /// resets text properties
140 static char reset[5];
142 static char underon[5];
144 static char underoff[6];
146 static char fgblue[6];
148 static char fgcyan[6];
150 static char fgred[6];
152 static char fggreen[6];
154 static char fgdef[6];
157 ///@name JSBSim Messaging functions
159 /** Places a Message structure on the Message queue.
160 @param msg pointer to a Message structure
161 @return pointer to a Message structure */
162 void PutMessage(const Message& msg);
163 /** Creates a message with the given text and places it on the queue.
164 @param text message text
165 @return pointer to a Message structure */
166 void PutMessage(const std::string& text);
167 /** Creates a message with the given text and boolean value and places it on the queue.
168 @param text message text
169 @param bVal boolean value associated with the message
170 @return pointer to a Message structure */
171 void PutMessage(const std::string& text, bool bVal);
172 /** Creates a message with the given text and integer value and places it on the queue.
173 @param text message text
174 @param iVal integer value associated with the message
175 @return pointer to a Message structure */
176 void PutMessage(const std::string& text, int iVal);
177 /** Creates a message with the given text and double value and places it on the queue.
178 @param text message text
179 @param dVal double value associated with the message
180 @return pointer to a Message structure */
181 void PutMessage(const std::string& text, double dVal);
182 /** Reads the message on the queue (but does not delete it).
183 @return 1 if some messages */
184 int SomeMessages(void);
185 /** Reads the message on the queue and removes it from the queue.
186 This function also prints out the message.*/
187 void ProcessMessage(void);
188 /** Reads the next message on the queue and removes it from the queue.
189 This function also prints out the message.
190 @return a pointer to the message, or NULL if there are no messages.*/
191 Message* ProcessNextMessage(void);
194 /** Returns the version number of JSBSim.
195 * @return The version number of JSBSim. */
196 std::string GetVersion(void) {return JSBSim_version;}
198 /// Disables highlighting in the console output.
199 void disableHighLighting(void);
201 static short debug_lvl;
203 /** Converts from degrees Kelvin to degrees Fahrenheit.
204 * @param kelvin The temperature in degrees Kelvin.
205 * @return The temperature in Fahrenheit. */
206 static double KelvinToFahrenheit (double kelvin) {
207 return 1.8*kelvin - 459.4;
210 /** Converts from degrees Celsius to degrees Rankine.
211 * @param celsius The temperature in degrees Celsius.
212 * @return The temperature in Rankine. */
213 static double CelsiusToRankine (double celsius) {
214 return celsius * 1.8 + 491.67;
217 /** Converts from degrees Rankine to degrees Celsius.
218 * @param rankine The temperature in degrees Rankine.
219 * @return The temperature in Celsius. */
220 static double RankineToCelsius (double rankine) {
221 return (rankine - 491.67)/1.8;
224 /** Converts from degrees Kelvin to degrees Rankine.
225 * @param kelvin The temperature in degrees Kelvin.
226 * @return The temperature in Rankine. */
227 static double KelvinToRankine (double kelvin) {
231 /** Converts from degrees Rankine to degrees Kelvin.
232 * @param rankine The temperature in degrees Rankine.
233 * @return The temperature in Kelvin. */
234 static double RankineToKelvin (double rankine) {
238 /** Converts from degrees Fahrenheit to degrees Celsius.
239 * @param fahrenheit The temperature in degrees Fahrenheit.
240 * @return The temperature in Celsius. */
241 static double FahrenheitToCelsius (double fahrenheit) {
242 return (fahrenheit - 32.0)/1.8;
245 /** Converts from degrees Celsius to degrees Fahrenheit.
246 * @param celsius The temperature in degrees Celsius.
247 * @return The temperature in Fahrenheit. */
248 static double CelsiusToFahrenheit (double celsius) {
249 return celsius * 1.8 + 32.0;
252 /** Converts from degrees Celsius to degrees Kelvin
253 * @param celsius The temperature in degrees Celsius.
254 * @return The temperature in Kelvin. */
255 static double CelsiusToKelvin (double celsius) {
256 return celsius + 273.15;
259 /** Converts from degrees Kelvin to degrees Celsius
260 * @param celsius The temperature in degrees Kelvin.
261 * @return The temperature in Celsius. */
262 static double KelvinToCelsius (double kelvin) {
263 return kelvin - 273.15;
266 /** Finite precision comparison.
267 @param a first value to compare
268 @param b second value to compare
269 @return if the two values can be considered equal up to roundoff */
270 static bool EqualToRoundoff(double a, double b) {
271 double eps = 2.0*DBL_EPSILON;
272 return std::fabs(a - b) <= eps*std::max(std::fabs(a), std::fabs(b));
275 /** Finite precision comparison.
276 @param a first value to compare
277 @param b second value to compare
278 @return if the two values can be considered equal up to roundoff */
279 static bool EqualToRoundoff(float a, float b) {
280 float eps = 2.0*FLT_EPSILON;
281 return std::fabs(a - b) <= eps*std::max(std::fabs(a), std::fabs(b));
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(float a, double b) {
289 return EqualToRoundoff(a, (float)b);
292 /** Finite precision comparison.
293 @param a first value to compare
294 @param b second value to compare
295 @return if the two values can be considered equal up to roundoff */
296 static bool EqualToRoundoff(double a, float b) {
297 return EqualToRoundoff((float)a, b);
300 /** Constrain a value between a minimum and a maximum value.
302 static double Constrain(double min, double value, double max) {
303 return value<min?(min):(value>max?(max):(value));
306 static double sign(double num) {return num>=0.0?1.0:-1.0;}
309 static Message localMsg;
311 static std::queue <Message> Messages;
313 void Debug(int from) {};
315 static unsigned int messageId;
317 static const double radtodeg;
318 static const double degtorad;
319 static const double hptoftlbssec;
320 static const double psftoinhg;
321 static const double psftopa;
322 static const double fpstokts;
323 static const double ktstofps;
324 static const double inchtoft;
325 static const double in3tom3;
326 static const double m3toft3;
327 static const double inhgtopa;
328 static const double fttom;
329 static double Reng; // Specific Gas Constant,ft^2/(sec^2*R)
330 static const double SHRatio;
331 static const double lbtoslug;
332 static const double slugtolb;
333 static const double kgtolb;
334 static const double kgtoslug;
335 static const std::string needed_cfg_version;
336 static const std::string JSBSim_version;
338 static std::string CreateIndexedPropertyName(const std::string& Property, int index);
340 static double GaussianRandomNumber(void);
344 enum {eL = 1, eM, eN };
346 enum {eP = 1, eQ, eR };
347 /// Velocities U, V, W
348 enum {eU = 1, eV, eW };
349 /// Positions X, Y, Z
350 enum {eX = 1, eY, eZ };
351 /// Euler angles Phi, Theta, Psi
352 enum {ePhi = 1, eTht, ePsi };
353 /// Stability axis forces, Drag, Side force, Lift
354 enum {eDrag = 1, eSide, eLift };
355 /// Local frame orientation Roll, Pitch, Yaw
356 enum {eRoll = 1, ePitch, eYaw };
357 /// Local frame position North, East, Down
358 enum {eNorth = 1, eEast, eDown };
359 /// Locations Radius, Latitude, Longitude
360 enum {eLat = 1, eLong, eRad };
361 /// Conversion specifiers
362 enum {inNone = 0, inDegrees, inRadians, inMeters, inFeet };
367 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%