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