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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 # define M_PI 3.14159265358979323846
54 #if !defined(WIN32) || defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 1300))
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 #define ID_JSBBASE "$Id$"
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74 /** JSBSim Base class.
75 * This class provides universal constants, utility functions, messaging
76 * functions, and enumerated constants to JSBSim.
81 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
87 /// Constructor for FGJSBBase.
90 /// Destructor for FGJSBBase.
93 /// JSBSim Message structure
97 unsigned int messageId;
100 enum mType {eText, eInteger, eDouble, eBool} type;
106 /// First order, (low pass / lag) filter
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;
117 cb = (2.0 - coeff*dt)/denom;
119 public: double execute(double in) {
120 double out = (in + prev_in)*ca + prev_out*cb;
127 ///@name JSBSim console output highlighting terms.
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];
138 static char underon[5];
140 static char underoff[6];
142 static char fgblue[6];
144 static char fgcyan[6];
146 static char fgred[6];
148 static char fggreen[6];
150 static char fgdef[6];
153 ///@name JSBSim Messaging functions
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);
186 /** Returns the version number of JSBSim.
187 * @return The version number of JSBSim. */
188 string GetVersion(void) {return JSBSim_version;}
190 /// Disables highlighting in the console output.
191 void disableHighLighting(void);
193 static short debug_lvl;
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;
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;
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;
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) {
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) {
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;
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;
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;
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;
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));
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));
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);
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);
292 /** Constrain a value between a minimum and a maximum value.
294 static double Constrain(double min, double value, double max) {
295 return value<min?(min):(value>max?(max):(value));
299 static Message localMsg;
301 static std::queue <Message> Messages;
303 void Debug(int from) {};
305 static unsigned int messageId;
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;
328 static string CreateIndexedPropertyName(string Property, int index)
330 std::stringstream str;
334 return Property + "[" + tmp + "]";
339 enum {eL = 1, eM, eN };
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 };
362 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%