1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 1999 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 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 General Public License for more
19 You should have received a copy of the GNU 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 General Public License can also be found on
24 the world wide web at http://www.gnu.org.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 # include <simgear/compiler.h>
46 # ifdef SG_HAVE_STD_INCLUDES
52 # if defined(sgi) && !defined(__GNUC__)
61 #include "FGJSBBase.h"
62 #include "FGInitialCondition.h"
63 #include "FGMatrix33.h"
64 #include "FGColumnVector3.h"
65 #include "FGColumnVector4.h"
67 #include "FGFDMExec.h"
68 #include "FGAtmosphere.h"
70 #include "FGTranslation.h"
71 #include "FGRotation.h"
72 #include "FGPosition.h"
73 #include "FGAerodynamics.h"
75 #include "FGAircraft.h"
76 #include "FGGroundReactions.h"
77 #include "FGPropulsion.h"
80 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
84 #define ID_STATE "$Id$"
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
92 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
96 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
100 /** Encapsulates the calculation of aircraft state.
101 @author Jon S. Berndt
103 @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGState.h?rev=HEAD&content-type=text/vnd.viewcvs-markup">
105 @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGState.cpp?rev=HEAD&content-type=text/vnd.viewcvs-markup">
109 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113 class FGState : public FGJSBBase
117 @param Executive a pointer to the parent executive object */
122 /** Initializes the simulation state based on the passed-in parameters.
123 @param U the body X-Axis velocity in fps.
124 @param V the body Y-Axis velocity in fps.
125 @param W the body Z-Axis velocity in fps.
126 @param lat latitude measured in radians from the equator, negative values are south.
127 @param lon longitude, measured in radians from the Greenwich meridian, negative values are west.
128 @param phi the roll angle in radians.
129 @param tht the pitch angle in radians.
130 @param psi the heading angle in radians measured clockwise from north.
131 @param h altitude in feet.
132 @param wnorth north velocity in feet per second
133 @param weast eastward velocity in feet per second
134 @param wdown downward velocity in feet per second
136 void Initialize(double U,
149 /** Initializes the simulation state based on parameters from an Initial Conditions object.
150 @param FGIC pointer to an initial conditions object.
151 @see FGInitialConditions.
153 void Initialize(FGInitialCondition *FGIC);
155 /// returns the speed of sound in feet per second.
156 inline double Geta(void) { return a; }
158 /// Returns the simulation time in seconds.
159 inline double Getsim_time(void) const { return sim_time; }
160 /// Returns the simulation delta T.
161 inline double Getdt(void) { return dt; }
163 /// Suspends the simulation and sets the delta T to zero.
164 inline void Suspend(void) {saved_dt = dt; dt = 0.0;}
165 /// Resumes the simulation by resetting delta T to the correct value.
166 inline void Resume(void) {dt = saved_dt;}
168 /** Sets the speed of sound.
169 @param speed the speed of sound in feet per second.
171 inline void Seta(double speed) { a = speed; }
173 /** Sets the current sim time.
174 @param cur_time the current time
175 @return the current time.
177 inline double Setsim_time(double cur_time) {
182 /** Sets the integration time step for the simulation executive.
183 @param delta_t the time step in seconds.
185 inline void Setdt(double delta_t) { dt = delta_t; }
187 /** Increments the simulation time.
188 @return the new simulation time.
190 inline double IncrTime(void) {
195 /** Initializes the transformation matrices.
196 @param phi the roll angle in radians.
197 @param tht the pitch angle in radians.
198 @param psi the heading angle in radians
200 void InitMatrices(double phi, double tht, double psi);
202 /** Calculates the local-to-body and body-to-local conversion matrices.
204 void CalcMatrices(void);
206 /** Integrates the quaternion.
207 Given the supplied rotational rate vector and integration rate, the quaternion
208 is integrated. The quaternion is later used to update the transformation
210 @param vPQR the body rotational rate column vector.
211 @param rate the integration rate in seconds.
213 void IntegrateQuat(FGColumnVector3 vPQR, int rate);
215 // ======================================= General Purpose INTEGRATOR
217 enum iType {AB4, AB3, AB2, AM3, EULER, TRAPZ};
219 /** Multi-method integrator.
220 @param type Type of intergation scheme to use. Can be one of:
222 <li>AB4 - Adams-Bashforth, fourth order</li>
223 <li>AB3 - Adams-Bashforth, third order</li>
224 <li>AB2 - Adams-Bashforth, second order</li>
225 <li>AM3 - Adams Moulton, third order</li>
226 <li>EULER - Euler</li>
227 <li>TRAPZ - Trapezoidal</li>
229 @param delta_t the integration time step in seconds
230 @param vTDeriv a reference to the current value of the time derivative of
231 the quantity being integrated (i.e. if vUVW is being integrated
232 vTDeriv is the current value of vUVWdot)
233 @param vLastArray an array of previously calculated and saved values of
234 the quantity being integrated (i.e. if vUVW is being integrated
235 vLastArray[0] is the past value of vUVWdot, vLastArray[1] is the value of
236 vUVWdot prior to that, etc.)
237 @return the current, incremental value of the item integrated to add to the
240 template <class T> T Integrate(iType type, double delta_t, T& vTDeriv, T *vLastArray)
246 vResult = (delta_t/24.0)*( 55.0 * vTDeriv
247 - 59.0 * vLastArray[0]
248 + 37.0 * vLastArray[1]
249 - 9.0 * vLastArray[2] );
250 vLastArray[2] = vLastArray[1];
251 vLastArray[1] = vLastArray[0];
252 vLastArray[0] = vTDeriv;
255 vResult = (delta_t/12.0)*( 23.0 * vTDeriv
256 - 16.0 * vLastArray[0]
257 + 5.0 * vLastArray[1] );
258 vLastArray[1] = vLastArray[0];
259 vLastArray[0] = vTDeriv;
262 vResult = (delta_t/2.0)*( 3.0 * vTDeriv - vLastArray[0] );
263 vLastArray[0] = vTDeriv;
266 vResult = (delta_t/12.0)*( 5.0 * vTDeriv
267 + 8.0 * vLastArray[0]
268 - 1.0 * vLastArray[1] );
269 vLastArray[1] = vLastArray[0];
270 vLastArray[0] = vTDeriv;
273 vResult = delta_t * vTDeriv;
276 vResult = (delta_t*0.5) * (vTDeriv + vLastArray[0]);
277 vLastArray[0] = vTDeriv;
284 // =======================================
286 /** Calculates Euler angles from the local-to-body matrix.
287 @return a reference to the vEuler column vector.
289 FGColumnVector3& CalcEuler(void);
291 /** Calculates and returns the stability-to-body axis transformation matrix.
292 @return a reference to the stability-to-body transformation matrix.
294 FGMatrix33& GetTs2b(void);
296 /** Calculates and returns the body-to-stability axis transformation matrix.
297 @return a reference to the stability-to-body transformation matrix.
299 FGMatrix33& GetTb2s(void);
301 /** Retrieves the local-to-body transformation matrix.
302 @return a reference to the local-to-body transformation matrix.
304 FGMatrix33& GetTl2b(void) { return mTl2b; }
306 /** Retrieves a specific local-to-body matrix element.
307 @param r matrix row index.
308 @param c matrix column index.
309 @return the matrix element described by the row and column supplied.
311 double GetTl2b(int r, int c) { return mTl2b(r,c);}
313 /** Retrieves the body-to-local transformation matrix.
314 @return a reference to the body-to-local matrix.
316 FGMatrix33& GetTb2l(void) { return mTb2l; }
318 /** Retrieves a specific body-to-local matrix element.
319 @param r matrix row index.
320 @param c matrix column index.
321 @return the matrix element described by the row and column supplied.
323 double GetTb2l(int i, int j) { return mTb2l(i,j);}
325 /** Prints a summary of simulator state (speed, altitude,
328 void ReportState(void);
334 double a; // speed of sound
343 FGColumnVector4 vQtrn;
344 FGColumnVector4 vQdot_prev[3];
345 FGColumnVector4 vQdot;
346 FGColumnVector3 vUVW;
347 FGColumnVector3 vLocalVelNED;
348 FGColumnVector3 vLocalEuler;
350 FGColumnVector4 vTmp;
351 FGColumnVector3 vEuler;
353 FGAircraft* Aircraft;
354 FGPosition* Position;
355 FGTranslation* Translation;
356 FGRotation* Rotation;
358 FGAtmosphere* Atmosphere;
360 FGAerodynamics* Aerodynamics;
361 FGGroundReactions* GroundReactions;
362 FGPropulsion* Propulsion;
363 FGPropertyManager* PropertyManager;
365 void Debug(int from);
368 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%