]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.h
Latest updates from the JSBSim project.
[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 "FGDefs.h"
62 #include "FGJSBBase.h"
63 #include "FGInitialCondition.h"
64 #include "FGMatrix33.h"
65 #include "FGColumnVector3.h"
66 #include "FGColumnVector4.h"
67
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 DEFINITIONS
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71
72 #define ID_STATE "$Id$"
73
74 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 FORWARD DECLARATIONS
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77
78 class FGAircraft;
79 class FGTranslation;
80 class FGRotation;
81 class FGAtmosphere;
82 class FGOutput;
83 class FGPosition;
84 class FGFDMExec;
85 class FGGroundReactions;
86 class FGPropulsion;
87
88 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
91
92 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 CLASS DOCUMENTATION
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
95
96 /** Encapsulates the calculation of aircraft state.
97     @author Jon S. Berndt
98     @version $Id$
99 */
100
101 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 CLASS DECLARATION
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
104
105 class FGState : public FGJSBBase
106 {
107 public:
108   /** Constructor
109       @param Executive a pointer to the parent executive object */
110   FGState(FGFDMExec*);
111   /// Destructor
112   ~FGState();
113
114   /** Specifies the Reset file to use.
115       The reset file normally resides in the same directory as an aircraft config file.
116       it includes the following information:
117       <ul>
118       <li>U, the body X-Axis velocity</li>
119       <li>V, the body Y-Axis velocity</li>
120       <li>W, the body Z-Axis velocity</li>
121       <li>Latitude measured in radians from the equator, negative values are south.</li>
122       <li>Longitude, measured in radians from the Greenwich meridian, negative values are west.</li>
123       <li>Phi, the roll angle in radians.</li>
124       <li>Theta, the pitch attitude in radians.</li>
125       <li>Psi, the heading angle in radians.</li>
126       <li>H, the altitude in feet</li>
127       <li>Wind Direction, the direction the wind is coming <u>from</u>.</li>
128       <li>Wind magnitude, the wind speed in fps.</li>
129       </ul>
130       @param path the path string leading to the specific aircraft file, i.e. "aircraft".
131       @param aircraft the name of the aircraft, i.e. "c172".
132       @param filename the name of the reset file without an extension, i.e. "reset00".
133       @return true if successful, false if the file could not be opened.
134       */
135   bool Reset(string path, string aircraft, string filename);
136
137   /** Initializes the simulation state based on the passed-in parameters.
138       @param U the body X-Axis velocity in fps.
139       @param V the body Y-Axis velocity in fps.
140       @param W the body Z-Axis velocity in fps.
141       @param lat latitude measured in radians from the equator, negative values are south.
142       @param lon longitude, measured in radians from the Greenwich meridian, negative values are west.
143       @param phi the roll angle in radians.
144       @param tht the pitch angle in radians.
145       @param psi the heading angle in radians measured clockwise from north.
146       @param h altitude in feet.
147       @param wnorth north velocity in feet per second
148       @param weast eastward velocity in feet per second
149       @param wdown downward velocity in feet per second
150       */
151   void Initialize(float U,
152                   float V,
153                   float W,
154                   float lat,
155                   float lon,
156                   float phi,
157                   float tht,
158                   float psi,
159                   float h,
160                   float wnorth,
161                   float weast,
162                   float wdown);
163
164   /** Initializes the simulation state based on parameters from an Initial Conditions object.
165       @param FGIC pointer to an initial conditions object.
166       @see FGInitialConditions.
167       */
168   void Initialize(FGInitialCondition *FGIC);
169
170   /** Stores state data in the supplied file name.
171       @param filename the file to store the data in.
172       @return true if successful.
173       */
174   bool StoreData(string filename);
175
176   /// returns the speed of sound in feet per second.
177   inline float Geta(void) { return a; }
178
179   /// Returns the simulation time in seconds.
180   inline float Getsim_time(void) { return sim_time; }
181   /// Returns the simulation delta T.
182   inline float Getdt(void) { return dt; }
183
184   /// Suspends the simulation and sets the delta T to zero.
185   inline void Suspend(void) {saved_dt = dt; dt = 0.0;}
186   /// Resumes the simulation by resetting delta T to the correct value.
187   inline void Resume(void)  {dt = saved_dt;}
188
189   /** Retrieves a parameter.
190       The parameters that can be retrieved are enumerated in FGDefs.h.
191       @param val_idx one of the enumerated JSBSim parameters.
192       @return the value of the parameter.
193       */
194   float GetParameter(eParam val_idx);
195
196   /** Retrieves a parameter.
197       The parameters that can be retrieved are enumerated in FGDefs.h.
198       @param val_string a string representing one of the enumerated JSBSim parameters,
199              i.e. "FG_QBAR".
200       @return the value of the parameter.
201       */
202   float GetParameter(string val_string);
203
204   /** Retrieves the JSBSim parameter enumerated item given the text string.
205       @param val_string the parameter string, i.e. "FG_QBAR".
206       @return the JSBSim parameter index (an enumerated type) for the supplied string.
207       */
208   eParam GetParameterIndex(string val_string);
209
210   /** Sets the speed of sound.
211       @param speed the speed of sound in feet per second.
212       */
213   inline void Seta(float speed) { a = speed; }
214
215   /** Sets the current sim time.
216       @param cur_time the current time
217       @return the current time.
218       */
219   inline float Setsim_time(float cur_time) {
220     sim_time = cur_time;
221     return sim_time;
222   }
223   
224   /** Sets the integration time step for the simulation executive.
225       @param delta_t the time step in seconds.
226       */
227   inline void  Setdt(float delta_t) { dt = delta_t; }
228
229   /** Sets the JSBSim parameter to the supplied value.
230       @param prm the JSBSim parameter to set, i.e. FG_RUDDER_POS.
231       @param val the value to give the parameter.
232       */
233   void SetParameter(eParam prm, float val);
234
235   /** Increments the simulation time.
236       @return the new simulation time.
237       */
238   inline float IncrTime(void) {
239     sim_time+=dt;
240     return sim_time;
241   }
242
243   /** Initializes the transformation matrices.
244       @param phi the roll angle in radians.
245       @param tht the pitch angle in radians.
246       @param psi the heading angle in radians
247       */
248   void InitMatrices(float phi, float tht, float psi);
249
250   /** Calculates the local-to-body and body-to-local conversion matrices.
251       */
252   void CalcMatrices(void);
253
254   /** Integrates the quaternion.
255       Given the supplied rotational rate vector and integration rate, the quaternion
256       is integrated. The quaternion is later used to update the transformation
257       matrices.
258       @param vPQR the body rotational rate column vector.
259       @param rate the integration rate in seconds.
260       */
261   void IntegrateQuat(FGColumnVector3 vPQR, int rate);
262
263   /** Calculates Euler angles from the local-to-body matrix.
264       @return a reference to the vEuler column vector.
265       */
266   FGColumnVector3& CalcEuler(void);
267
268   /** Calculates and returns the stability-to-body axis transformation matrix.
269       @param alpha angle of attack in radians.
270       @param beta angle of sideslip in radians.
271       @return a reference to the stability-to-body transformation matrix.
272       */
273   FGMatrix33& GetTs2b(float alpha, float beta);
274
275   /** Retrieves the local-to-body transformation matrix.
276       @return a reference to the local-to-body transformation matrix.
277       */
278   FGMatrix33& GetTl2b(void) { return mTl2b; }
279
280   /** Retrieves a specific local-to-body matrix element.
281       @param r matrix row index.
282       @param c matrix column index.
283       @return the matrix element described by the row and column supplied.
284       */
285   float GetTl2b(int r, int c) { return mTl2b(r,c);}
286
287   /** Retrieves the body-to-local transformation matrix.
288       @return a reference to the body-to-local matrix.
289       */
290   FGMatrix33& GetTb2l(void) { return mTb2l; }
291
292   /** Retrieves a specific body-to-local matrix element.
293       @param r matrix row index.
294       @param c matrix column index.
295       @return the matrix element described by the row and column supplied.
296       */
297   float GetTb2l(int i, int j) { return mTb2l(i,j);}
298   
299   /** Prints a summary of simulator state (speed, altitude, 
300       configuration, etc.)
301   */
302   void ReportState(void);
303
304
305   typedef map<eParam, string> ParamMap;
306   ParamMap paramdef;
307
308 private:
309   float a;                          // speed of sound
310   float sim_time, dt;
311   float saved_dt;
312
313   FGFDMExec* FDMExec;
314   FGMatrix33 mTb2l;
315   FGMatrix33 mTl2b;
316   FGMatrix33 mTs2b;
317   FGColumnVector4 vQtrn;
318   FGColumnVector4 vlastQdot;
319   FGColumnVector3 vUVW;
320   FGColumnVector3 vLocalVelNED;
321   FGColumnVector3 vLocalEuler;
322   FGColumnVector4 vQdot;
323   FGColumnVector4 vTmp;
324   FGColumnVector3 vEuler;
325
326   FGAircraft* Aircraft;
327   FGPosition* Position;
328   FGTranslation* Translation;
329   FGRotation* Rotation;
330   FGOutput* Output;
331   FGAtmosphere* Atmosphere;
332   FGFCS* FCS;
333   FGAerodynamics* Aerodynamics;
334   FGGroundReactions* GroundReactions;
335   FGPropulsion* Propulsion;
336
337   typedef map<string, eParam> CoeffMap;
338   CoeffMap coeffdef;
339   void Debug(void);
340   int ActiveEngine;
341 };
342
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
345 #include "FGFDMExec.h"
346 #include "FGAtmosphere.h"
347 #include "FGFCS.h"
348 #include "FGTranslation.h"
349 #include "FGRotation.h"
350 #include "FGPosition.h"
351 #include "FGAerodynamics.h"
352 #include "FGOutput.h"
353 #include "FGAircraft.h"
354 #include "FGGroundReactions.h"
355 #include "FGPropulsion.h"
356
357 #endif
358