]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.h
Latest JSBSim updates.
[flightgear.git] / src / FDM / JSBSim / FGState.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Header:       FGState.h
4  Author:       Jon S. Berndt
5  Date started: 11/17/98
6  
7  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
8  
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
12  version.
13  
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
17  details.
18  
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.
22  
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25  
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28  
29 HISTORY
30 --------------------------------------------------------------------------------
31 11/17/98   JSB   Created
32  
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 SENTRY
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37 #ifndef FGSTATE_H
38 #define FGSTATE_H
39
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 INCLUDES
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
44 #ifdef FGFS
45 #  include <simgear/compiler.h>
46 #  ifdef SG_HAVE_STD_INCLUDES
47 #    include <fstream>
48 #  else
49 #    include <fstream.h>
50 #  endif
51 #else
52 #  if defined(sgi) && !defined(__GNUC__)
53 #    include <fstream.h>
54 #  else
55 #    include <fstream>
56 #  endif
57 #endif
58
59 #include <string>
60 #include <map>
61 #include "FGJSBBase.h"
62 #include "FGInitialCondition.h"
63 #include "FGMatrix33.h"
64 #include "FGColumnVector3.h"
65 #include "FGColumnVector4.h"
66
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 DEFINITIONS
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
71 #define ID_STATE "$Id$"
72
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 FORWARD DECLARATIONS
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76
77 class FGAircraft;
78 class FGTranslation;
79 class FGRotation;
80 class FGAtmosphere;
81 class FGOutput;
82 class FGPosition;
83 class FGFDMExec;
84 class FGGroundReactions;
85 class FGPropulsion;
86
87 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
90
91 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 CLASS DOCUMENTATION
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
94
95 /** Encapsulates the calculation of aircraft state.
96     @author Jon S. Berndt
97     @version $Id$
98     @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGState.h?rev=HEAD&content-type=text/vnd.viewcvs-markup">
99          Header File </a>
100     @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGState.cpp?rev=HEAD&content-type=text/vnd.viewcvs-markup">
101          Source File </a>
102 */
103
104 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 CLASS DECLARATION
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
107
108 class FGState : public FGJSBBase
109 {
110 public:
111   /** Constructor
112       @param Executive a pointer to the parent executive object */
113   FGState(FGFDMExec*);
114   /// Destructor
115   ~FGState();
116
117   /** Specifies the Reset file to use.
118       The reset file normally resides in the same directory as an aircraft config file.
119       it includes the following information:
120       <ul>
121       <li>U, the body X-Axis velocity</li>
122       <li>V, the body Y-Axis velocity</li>
123       <li>W, the body Z-Axis velocity</li>
124       <li>Latitude measured in radians from the equator, negative values are south.</li>
125       <li>Longitude, measured in radians from the Greenwich meridian, negative values are west.</li>
126       <li>Phi, the roll angle in radians.</li>
127       <li>Theta, the pitch attitude in radians.</li>
128       <li>Psi, the heading angle in radians.</li>
129       <li>H, the altitude in feet</li>
130       <li>Wind Direction, the direction the wind is coming <u>from</u>.</li>
131       <li>Wind magnitude, the wind speed in fps.</li>
132       </ul>
133       @param path the path string leading to the specific aircraft file, i.e. "aircraft".
134       @param aircraft the name of the aircraft, i.e. "c172".
135       @param filename the name of the reset file without an extension, i.e. "reset00".
136       @return true if successful, false if the file could not be opened.
137       */
138   bool Reset(string path, string aircraft, string filename);
139
140   /** Initializes the simulation state based on the passed-in parameters.
141       @param U the body X-Axis velocity in fps.
142       @param V the body Y-Axis velocity in fps.
143       @param W the body Z-Axis velocity in fps.
144       @param lat latitude measured in radians from the equator, negative values are south.
145       @param lon longitude, measured in radians from the Greenwich meridian, negative values are west.
146       @param phi the roll angle in radians.
147       @param tht the pitch angle in radians.
148       @param psi the heading angle in radians measured clockwise from north.
149       @param h altitude in feet.
150       @param wnorth north velocity in feet per second
151       @param weast eastward velocity in feet per second
152       @param wdown downward velocity in feet per second
153       */
154   void Initialize(double U,
155                   double V,
156                   double W,
157                   double lat,
158                   double lon,
159                   double phi,
160                   double tht,
161                   double psi,
162                   double h,
163                   double wnorth,
164                   double weast,
165                   double wdown);
166
167   /** Initializes the simulation state based on parameters from an Initial Conditions object.
168       @param FGIC pointer to an initial conditions object.
169       @see FGInitialConditions.
170       */
171   void Initialize(FGInitialCondition *FGIC);
172
173   /// returns the speed of sound in feet per second.
174   inline double Geta(void) { return a; }
175
176   /// Returns the simulation time in seconds.
177   inline double Getsim_time(void) const { return sim_time; }
178   /// Returns the simulation delta T.
179   inline double Getdt(void) { return dt; }
180
181   /// Suspends the simulation and sets the delta T to zero.
182   inline void Suspend(void) {saved_dt = dt; dt = 0.0;}
183   /// Resumes the simulation by resetting delta T to the correct value.
184   inline void Resume(void)  {dt = saved_dt;}
185
186   /** Sets the speed of sound.
187       @param speed the speed of sound in feet per second.
188       */
189   inline void Seta(double speed) { a = speed; }
190
191   /** Sets the current sim time.
192       @param cur_time the current time
193       @return the current time.
194       */
195   inline double Setsim_time(double cur_time) {
196     sim_time = cur_time;
197     return sim_time;
198   }
199   
200   /** Sets the integration time step for the simulation executive.
201       @param delta_t the time step in seconds.
202       */
203   inline void  Setdt(double delta_t) { dt = delta_t; }
204
205   /** Increments the simulation time.
206       @return the new simulation time.
207       */
208   inline double IncrTime(void) {
209     sim_time+=dt;
210     return sim_time;
211   }
212
213   /** Initializes the transformation matrices.
214       @param phi the roll angle in radians.
215       @param tht the pitch angle in radians.
216       @param psi the heading angle in radians
217       */
218   void InitMatrices(double phi, double tht, double psi);
219
220   /** Calculates the local-to-body and body-to-local conversion matrices.
221       */
222   void CalcMatrices(void);
223
224   /** Integrates the quaternion.
225       Given the supplied rotational rate vector and integration rate, the quaternion
226       is integrated. The quaternion is later used to update the transformation
227       matrices.
228       @param vPQR the body rotational rate column vector.
229       @param rate the integration rate in seconds.
230       */
231   void IntegrateQuat(FGColumnVector3 vPQR, int rate);
232   
233   // ======================================= General Purpose INTEGRATOR
234
235   enum iType {AB4, AB3, AB2, AM3, EULER, TRAPZ};
236   
237   /** Multi-method integrator.
238       @param type Type of intergation scheme to use. Can be one of:
239              <ul>
240              <li>AB4 - Adams-Bashforth, fourth order</li>
241              <li>AB3 - Adams-Bashforth, third order</li>
242              <li>AB2 - Adams-Bashforth, second order</li>
243              <li>AM3 - Adams Moulton, third order</li>
244              <li>EULER - Euler</li>
245              <li>TRAPZ - Trapezoidal</li>
246              </ul>
247       @param delta_t the integration time step in seconds
248       @param vTDeriv a reference to the current value of the time derivative of
249              the quantity being integrated (i.e. if vUVW is being integrated
250              vTDeriv is the current value of vUVWdot)
251       @param vLastArray an array of previously calculated and saved values of 
252              the quantity being integrated (i.e. if vUVW is being integrated
253              vLastArray[0] is the past value of vUVWdot, vLastArray[1] is the value of
254              vUVWdot prior to that, etc.)
255       @return the current, incremental value of the item integrated to add to the
256               previous value. */
257   
258   template <class T> T Integrate(iType type, double delta_t, T& vTDeriv, T *vLastArray)
259   {
260     T vResult;
261
262     switch (type) {
263     case AB4:
264       vResult = (delta_t/24.0)*(  55.0 * vTDeriv
265                                 - 59.0 * vLastArray[0]
266                                 + 37.0 * vLastArray[1]
267                                 -  9.0 * vLastArray[2] );
268       vLastArray[2] = vLastArray[1];
269       vLastArray[1] = vLastArray[0];
270       vLastArray[0] = vTDeriv;
271       break;
272     case AB3:
273       vResult = (delta_t/12.0)*(  23.0 * vTDeriv
274                                 - 16.0 * vLastArray[0]
275                                 +  5.0 * vLastArray[1] );
276       vLastArray[1] = vLastArray[0];
277       vLastArray[0] = vTDeriv;
278       break;
279     case AB2:
280       vResult = (delta_t/2.0)*( 3.0 * vTDeriv - vLastArray[0] );
281       vLastArray[0] = vTDeriv;
282       break;
283     case AM3:
284       vResult = (delta_t/12.0)*(  5.0 * vTDeriv
285                                 + 8.0 * vLastArray[0]
286                                 - 1.0 * vLastArray[1] );
287       vLastArray[1] = vLastArray[0];
288       vLastArray[0] = vTDeriv;
289       break;
290     case EULER:
291       vResult = delta_t * vTDeriv;
292       break;
293     case TRAPZ:
294       vResult = (delta_t*0.5) * (vTDeriv + vLastArray[0]);
295       vLastArray[0] = vTDeriv;
296       break;
297     }
298
299     return vResult;
300   }
301
302   // =======================================
303
304   /** Calculates Euler angles from the local-to-body matrix.
305       @return a reference to the vEuler column vector.
306       */
307   FGColumnVector3& CalcEuler(void);
308
309   /** Calculates and returns the stability-to-body axis transformation matrix.
310       @return a reference to the stability-to-body transformation matrix.
311       */
312   FGMatrix33& GetTs2b(void);
313   
314   /** Calculates and returns the body-to-stability axis transformation matrix.
315       @return a reference to the stability-to-body transformation matrix.
316       */
317   FGMatrix33& GetTb2s(void);
318
319   /** Retrieves the local-to-body transformation matrix.
320       @return a reference to the local-to-body transformation matrix.
321       */
322   FGMatrix33& GetTl2b(void) { return mTl2b; }
323
324   /** Retrieves a specific local-to-body matrix element.
325       @param r matrix row index.
326       @param c matrix column index.
327       @return the matrix element described by the row and column supplied.
328       */
329   double GetTl2b(int r, int c) { return mTl2b(r,c);}
330
331   /** Retrieves the body-to-local transformation matrix.
332       @return a reference to the body-to-local matrix.
333       */
334   FGMatrix33& GetTb2l(void) { return mTb2l; }
335
336   /** Retrieves a specific body-to-local matrix element.
337       @param r matrix row index.
338       @param c matrix column index.
339       @return the matrix element described by the row and column supplied.
340       */
341   double GetTb2l(int i, int j) { return mTb2l(i,j);}
342   
343   /** Prints a summary of simulator state (speed, altitude, 
344       configuration, etc.)
345   */
346   void ReportState(void);
347   
348   inline string GetPropertyName(string prm) { return ParamNameToProp[prm]; }
349   
350   void bind();
351   void unbind();
352
353 private:
354   double a;                          // speed of sound
355   double sim_time, dt;
356   double saved_dt;
357
358   FGFDMExec* FDMExec;
359   FGMatrix33 mTb2l;
360   FGMatrix33 mTl2b;
361   FGMatrix33 mTs2b;
362   FGMatrix33 mTb2s;
363   FGColumnVector4 vQtrn;
364   FGColumnVector4 vQdot_prev[3];
365   FGColumnVector4 vQdot;
366   FGColumnVector3 vUVW;
367   FGColumnVector3 vLocalVelNED;
368   FGColumnVector3 vLocalEuler;
369   
370   FGColumnVector4 vTmp;
371   FGColumnVector3 vEuler;
372
373   FGAircraft* Aircraft;
374   FGPosition* Position;
375   FGTranslation* Translation;
376   FGRotation* Rotation;
377   FGOutput* Output;
378   FGAtmosphere* Atmosphere;
379   FGFCS* FCS;
380   FGAerodynamics* Aerodynamics;
381   FGGroundReactions* GroundReactions;
382   FGPropulsion* Propulsion;
383   FGPropertyManager* PropertyManager;
384
385   typedef map<string,string> ParamNameMap;
386   ParamNameMap ParamNameToProp;
387   
388   void InitPropertyMaps(void);
389   
390   void Debug(int from);
391 };
392
393 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394
395 #include "FGFDMExec.h"
396 #include "FGAtmosphere.h"
397 #include "FGFCS.h"
398 #include "FGTranslation.h"
399 #include "FGRotation.h"
400 #include "FGPosition.h"
401 #include "FGAerodynamics.h"
402 #include "FGOutput.h"
403 #include "FGAircraft.h"
404 #include "FGGroundReactions.h"
405 #include "FGPropulsion.h"
406
407 #endif
408