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