]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.h
Moved JSBSim.hxx to src/FDM/JSBSim/
[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(double U,
151                   double V,
152                   double W,
153                   double lat,
154                   double lon,
155                   double phi,
156                   double tht,
157                   double psi,
158                   double h,
159                   double wnorth,
160                   double weast,
161                   double 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 double Geta(void) { return a; }
177
178   /// Returns the simulation time in seconds.
179   inline double Getsim_time(void) { return sim_time; }
180   /// Returns the simulation delta T.
181   inline double 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   double 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   double 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(double speed) { a = speed; }
213
214   /** Gets the name of the parameter given the index.
215       @param val_idx one of the enumerated JSBSim parameters.
216       @return the name of the parameter pointed to by the index.
217       */
218   string GetParameterName(eParam val_idx) {return paramdef[val_idx];}
219   
220   /** Sets the current sim time.
221       @param cur_time the current time
222       @return the current time.
223       */
224   inline double Setsim_time(double cur_time) {
225     sim_time = cur_time;
226     return sim_time;
227   }
228   
229   /** Sets the integration time step for the simulation executive.
230       @param delta_t the time step in seconds.
231       */
232   inline void  Setdt(double delta_t) { dt = delta_t; }
233
234   /** Sets the JSBSim parameter to the supplied value.
235       @param prm the JSBSim parameter to set, i.e. FG_RUDDER_POS.
236       @param val the value to give the parameter.
237       */
238   void SetParameter(eParam prm, double val);
239
240   /** Increments the simulation time.
241       @return the new simulation time.
242       */
243   inline double IncrTime(void) {
244     sim_time+=dt;
245     return sim_time;
246   }
247
248   /** Initializes the transformation matrices.
249       @param phi the roll angle in radians.
250       @param tht the pitch angle in radians.
251       @param psi the heading angle in radians
252       */
253   void InitMatrices(double phi, double tht, double psi);
254
255   /** Calculates the local-to-body and body-to-local conversion matrices.
256       */
257   void CalcMatrices(void);
258
259   /** Integrates the quaternion.
260       Given the supplied rotational rate vector and integration rate, the quaternion
261       is integrated. The quaternion is later used to update the transformation
262       matrices.
263       @param vPQR the body rotational rate column vector.
264       @param rate the integration rate in seconds.
265       */
266   void IntegrateQuat(FGColumnVector3 vPQR, int rate);
267
268   /** Calculates Euler angles from the local-to-body matrix.
269       @return a reference to the vEuler column vector.
270       */
271   FGColumnVector3& CalcEuler(void);
272
273   /** Calculates and returns the stability-to-body axis transformation matrix.
274       @return a reference to the stability-to-body transformation matrix.
275       */
276   FGMatrix33& GetTs2b(void);
277   
278   /** Calculates and returns the body-to-stability axis transformation matrix.
279       @return a reference to the stability-to-body transformation matrix.
280       */
281   FGMatrix33& GetTb2s(void);
282
283   /** Retrieves the local-to-body transformation matrix.
284       @return a reference to the local-to-body transformation matrix.
285       */
286   FGMatrix33& GetTl2b(void) { return mTl2b; }
287
288   /** Retrieves a specific local-to-body matrix element.
289       @param r matrix row index.
290       @param c matrix column index.
291       @return the matrix element described by the row and column supplied.
292       */
293   double GetTl2b(int r, int c) { return mTl2b(r,c);}
294
295   /** Retrieves the body-to-local transformation matrix.
296       @return a reference to the body-to-local matrix.
297       */
298   FGMatrix33& GetTb2l(void) { return mTb2l; }
299
300   /** Retrieves a specific body-to-local matrix element.
301       @param r matrix row index.
302       @param c matrix column index.
303       @return the matrix element described by the row and column supplied.
304       */
305   double GetTb2l(int i, int j) { return mTb2l(i,j);}
306   
307   /** Prints a summary of simulator state (speed, altitude, 
308       configuration, etc.)
309   */
310   void ReportState(void);
311
312 private:
313   double a;                          // speed of sound
314   double sim_time, dt;
315   double saved_dt;
316
317   FGFDMExec* FDMExec;
318   FGMatrix33 mTb2l;
319   FGMatrix33 mTl2b;
320   FGMatrix33 mTs2b;
321   FGMatrix33 mTb2s;
322   FGColumnVector4 vQtrn;
323   FGColumnVector4 vlastQdot;
324   FGColumnVector4 vQdot;
325   FGColumnVector3 vUVW;
326   FGColumnVector3 vLocalVelNED;
327   FGColumnVector3 vLocalEuler;
328   
329   FGColumnVector4 vTmp;
330   FGColumnVector3 vEuler;
331
332   FGAircraft* Aircraft;
333   FGPosition* Position;
334   FGTranslation* Translation;
335   FGRotation* Rotation;
336   FGOutput* Output;
337   FGAtmosphere* Atmosphere;
338   FGFCS* FCS;
339   FGAerodynamics* Aerodynamics;
340   FGGroundReactions* GroundReactions;
341   FGPropulsion* Propulsion;
342
343   typedef map<string, eParam> CoeffMap;
344   CoeffMap coeffdef;
345
346   typedef map<eParam, string> ParamMap;
347   ParamMap paramdef;
348
349   int ActiveEngine;
350   void Debug(int from);
351 };
352
353 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354
355 #include "FGFDMExec.h"
356 #include "FGAtmosphere.h"
357 #include "FGFCS.h"
358 #include "FGTranslation.h"
359 #include "FGRotation.h"
360 #include "FGPosition.h"
361 #include "FGAerodynamics.h"
362 #include "FGOutput.h"
363 #include "FGAircraft.h"
364 #include "FGGroundReactions.h"
365 #include "FGPropulsion.h"
366
367 #endif
368