]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
b7649a405a4b69d8737b4babc2f0f291dbd9f680
[flightgear.git] / src / FDM / JSBSim / FGState.cpp
1 /*******************************************************************************
2                                                                        
3  Module:       FGState.cpp
4  Author:       Jon Berndt
5  Date started: 11/17/98
6  Called by:    FGFDMExec and accessed by all models.
7  
8  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
9  
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14  
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19  
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23  
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26  
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 See header file.
30  
31 HISTORY
32 --------------------------------------------------------------------------------
33 11/17/98   JSB   Created
34  
35 ********************************************************************************
36 INCLUDES
37 *******************************************************************************/
38
39 #ifdef FGFS
40 #  include <simgear/compiler.h>
41 #  ifdef FG_HAVE_STD_INCLUDES
42 #    include <cmath>
43 #  else
44 #    include <math.h>
45 #  endif
46 #else
47 #  include <cmath>
48 #endif
49
50 #ifndef M_PI 
51 #  include <simgear/constants.h>
52 #  define M_PI FG_PI
53 #endif
54
55 #include "FGState.h"
56 #include "FGFDMExec.h"
57 #include "FGAtmosphere.h"
58 #include "FGFCS.h"
59 #include "FGAircraft.h"
60 #include "FGTranslation.h"
61 #include "FGRotation.h"
62 #include "FGPosition.h"
63 #include "FGAuxiliary.h"
64 #include "FGOutput.h"
65
66 /*******************************************************************************
67 MACROS
68 *******************************************************************************/
69
70 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
71
72 /*******************************************************************************
73 ************************************ CODE **************************************
74 *******************************************************************************/
75
76 /******************************************************************************/
77 //
78 // For every term registered here there must be a corresponding handler in
79 // GetParameter() below that retrieves that parameter. Also, there must be an
80 // entry in the enum eParam definition in FGDefs.h. The ID is what must be used
81 // in any config file entry which references that item.
82
83 FGState::FGState(FGFDMExec* fdex) : mTb2l(3,3),
84     mTl2b(3,3),
85     mTs2b(3,3),
86     vQtrn(4)
87 {
88   FDMExec = fdex;
89
90   a = 1000.0;
91   sim_time = 0.0;
92   dt = 1.0/120.0;
93
94   RegisterVariable(FG_QBAR,           " qbar "           );
95   RegisterVariable(FG_WINGAREA,       " wing_area "      );
96   RegisterVariable(FG_WINGSPAN,       " wingspan "       );
97   RegisterVariable(FG_CBAR,           " cbar "           );
98   RegisterVariable(FG_ALPHA,          " alpha "          );
99   RegisterVariable(FG_ALPHADOT,       " alphadot "       );
100   RegisterVariable(FG_BETA,           " beta "           );
101   RegisterVariable(FG_BETADOT,        " betadot "        );
102   RegisterVariable(FG_PITCHRATE,      " pitch_rate "     );
103   RegisterVariable(FG_ROLLRATE,       " roll_rate "      );
104   RegisterVariable(FG_YAWRATE,        " yaw_rate "       );
105   RegisterVariable(FG_MACH,           " mach "           );
106   RegisterVariable(FG_ALTITUDE,       " altitude "       );
107   RegisterVariable(FG_BI2VEL,         " BI2Vel "         );
108   RegisterVariable(FG_CI2VEL,         " CI2Vel "         );
109   RegisterVariable(FG_ELEVATOR_POS,   " elevator_pos "   );
110   RegisterVariable(FG_AILERON_POS,    " aileron_pos "    );
111   RegisterVariable(FG_RUDDER_POS,     " rudder_pos "     );
112   RegisterVariable(FG_SPDBRAKE_POS,   " speedbrake_pos " );
113   RegisterVariable(FG_SPOILERS_POS,   " spoiler_pos "    );
114   RegisterVariable(FG_FLAPS_POS,      " flaps_pos "      );
115   RegisterVariable(FG_ELEVATOR_CMD,   " elevator_cmd "   );
116   RegisterVariable(FG_AILERON_CMD,    " aileron_cmd "    );
117   RegisterVariable(FG_RUDDER_CMD,     " rudder_cmd "     );
118   RegisterVariable(FG_SPDBRAKE_CMD,   " speedbrake_cmd " );
119   RegisterVariable(FG_SPOILERS_CMD,   " spoiler_cmd "    );
120   RegisterVariable(FG_FLAPS_CMD,      " flaps_cmd "      );
121   RegisterVariable(FG_THROTTLE_CMD,   " throttle_cmd "   );
122   RegisterVariable(FG_THROTTLE_POS,   " throttle_pos "   );
123   RegisterVariable(FG_HOVERB,         " height/span "    );
124   RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
125 }
126
127 /******************************************************************************/
128
129 FGState::~FGState(void) {}
130
131 /******************************************************************************/
132
133 float FGState::GetParameter(eParam val_idx) {
134   switch(val_idx) {
135   case FG_QBAR:
136     return FDMExec->GetTranslation()->Getqbar();
137   case FG_WINGAREA:
138     return FDMExec->GetAircraft()->GetWingArea();
139   case FG_WINGSPAN:
140     return FDMExec->GetAircraft()->GetWingSpan();
141   case FG_CBAR:
142     return FDMExec->GetAircraft()->Getcbar();
143   case FG_ALPHA:
144     return FDMExec->GetTranslation()->Getalpha();
145   case FG_ALPHADOT:
146     return FDMExec->GetTranslation()->Getadot();
147   case FG_BETA:
148     return FDMExec->GetTranslation()->Getbeta();
149   case FG_BETADOT:
150     return FDMExec->GetTranslation()->Getbdot();
151   case FG_PITCHRATE:
152     return (FDMExec->GetRotation()->GetPQR())(2);
153   case FG_ROLLRATE:
154     return (FDMExec->GetRotation()->GetPQR())(1);
155   case FG_YAWRATE:
156     return (FDMExec->GetRotation()->GetPQR())(3);
157   case FG_ELEVATOR_POS:
158     return FDMExec->GetFCS()->GetDePos();
159   case FG_AILERON_POS:
160     return FDMExec->GetFCS()->GetDaPos();
161   case FG_RUDDER_POS:
162     return FDMExec->GetFCS()->GetDrPos();
163   case FG_SPDBRAKE_POS:
164     return FDMExec->GetFCS()->GetDsbPos();
165   case FG_SPOILERS_POS:
166     return FDMExec->GetFCS()->GetDspPos();
167   case FG_FLAPS_POS:
168     return FDMExec->GetFCS()->GetDfPos();
169   case FG_ELEVATOR_CMD:
170     return FDMExec->GetFCS()->GetDeCmd();
171   case FG_AILERON_CMD:
172     return FDMExec->GetFCS()->GetDaCmd();
173   case FG_RUDDER_CMD:
174     return FDMExec->GetFCS()->GetDrCmd();
175   case FG_SPDBRAKE_CMD:
176     return FDMExec->GetFCS()->GetDsbCmd();
177   case FG_SPOILERS_CMD:
178     return FDMExec->GetFCS()->GetDspCmd();
179   case FG_FLAPS_CMD:
180     return FDMExec->GetFCS()->GetDfCmd();
181   case FG_MACH:
182     return FDMExec->GetTranslation()->GetMach();
183   case FG_ALTITUDE:
184     return FDMExec->GetPosition()->Geth();
185   case FG_BI2VEL:
186     if(FDMExec->GetTranslation()->GetVt() > 0)
187         return FDMExec->GetAircraft()->GetWingSpan()/(2.0 * FDMExec->GetTranslation()->GetVt());
188     else
189         return 0;
190   case FG_CI2VEL:
191     if(FDMExec->GetTranslation()->GetVt() > 0)
192         return FDMExec->GetAircraft()->Getcbar()/(2.0 * FDMExec->GetTranslation()->GetVt());
193     else
194         return 0;
195   case FG_THROTTLE_CMD:
196     return FDMExec->GetFCS()->GetThrottleCmd(0);
197   case FG_THROTTLE_POS:
198     return FDMExec->GetFCS()->GetThrottlePos(0);
199   case FG_HOVERB:
200     return FDMExec->GetPosition()->GetHOverB();
201   case FG_PITCH_TRIM_CMD:
202     return FDMExec->GetFCS()->GetPitchTrimCmd();
203   default:
204     cerr << "FGState::GetParameter() - No handler for parameter " << val_idx << endl;
205     return 0.0;
206   }
207   return 0;
208 }
209
210 /******************************************************************************/
211
212 float FGState::GetParameter(string val_string) {
213   return GetParameter(coeffdef[val_string]);
214 }
215
216 /******************************************************************************/
217
218 eParam FGState::GetParameterIndex(string val_string) {
219   return coeffdef[val_string];
220 }
221
222 /******************************************************************************/
223
224 void FGState::SetParameter(eParam val_idx, float val) {
225   switch(val_idx) {
226   case FG_ELEVATOR_POS:
227     FDMExec->GetFCS()->SetDePos(val);
228     break;
229   case FG_AILERON_POS:
230     FDMExec->GetFCS()->SetDaPos(val);
231     break;
232   case FG_RUDDER_POS:
233     FDMExec->GetFCS()->SetDrPos(val);
234     break;
235   case FG_SPDBRAKE_POS:
236     FDMExec->GetFCS()->SetDsbPos(val);
237     break;
238   case FG_SPOILERS_POS:
239     FDMExec->GetFCS()->SetDspPos(val);
240     break;
241   case FG_FLAPS_POS:
242     FDMExec->GetFCS()->SetDfPos(val);
243     break;
244   case FG_THROTTLE_POS:
245     FDMExec->GetFCS()->SetThrottlePos(-1,val);
246   }
247 }
248
249 //***************************************************************************
250 //
251 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
252 //
253
254 bool FGState::Reset(string path, string acname, string fname) {
255   string resetDef;
256   float U, V, W;
257   float phi, tht, psi;
258   float latitude, longitude, h;
259
260   resetDef = path + "/" + acname + "/" + fname;
261
262   ifstream resetfile(resetDef.c_str());
263
264   if (resetfile) {
265     resetfile >> U;
266     resetfile >> V;
267     resetfile >> W;
268     resetfile >> latitude;
269     resetfile >> longitude;
270     resetfile >> phi;
271     resetfile >> tht;
272     resetfile >> psi;
273     resetfile >> h;
274     resetfile.close();
275
276     FDMExec->GetPosition()->SetLatitude(latitude*DEGTORAD);
277     FDMExec->GetPosition()->SetLongitude(longitude*DEGTORAD);
278     FDMExec->GetPosition()->Seth(h);
279
280     Initialize(U, V, W, phi*DEGTORAD, tht*DEGTORAD, psi*DEGTORAD,
281                latitude*DEGTORAD, longitude*DEGTORAD, h);
282
283     return true;
284   } else {
285     cerr << "Unable to load reset file " << fname << endl;
286     return false;
287   }
288 }
289
290 //***************************************************************************
291 //
292 // Initialize: Assume all angles GIVEN IN RADIANS !!
293 //
294
295 void FGState::Initialize(float U, float V, float W,
296                          float phi, float tht, float psi,
297                          float Latitude, float Longitude, float H) {
298   FGColumnVector vUVW(3);
299   FGColumnVector vLocalVelNED(3);
300   FGColumnVector vEuler(3);
301   float alpha, beta;
302   float qbar, Vt;
303
304   FDMExec->GetPosition()->SetLatitude(Latitude);
305   FDMExec->GetPosition()->SetLongitude(Longitude);
306   FDMExec->GetPosition()->Seth(H);
307
308   FDMExec->GetAtmosphere()->Run();
309
310   if (W != 0.0)
311     alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
312   else
313     alpha = 0.0;
314   if (V != 0.0)
315     beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
316   else
317     beta = 0.0;
318
319   vUVW << U << V << W;
320   FDMExec->GetTranslation()->SetUVW(vUVW);
321
322   vEuler << phi << tht << psi;
323   FDMExec->GetRotation()->SetEuler(vEuler);
324
325   FDMExec->GetTranslation()->SetAB(alpha, beta);
326
327   Vt = sqrt(U*U + V*V + W*W);
328   FDMExec->GetTranslation()->SetVt(Vt);
329
330   FDMExec->GetTranslation()->SetMach(Vt/FDMExec->GetAtmosphere()->GetSoundSpeed());
331
332   qbar = 0.5*(U*U + V*V + W*W)*FDMExec->GetAtmosphere()->GetDensity();
333   FDMExec->GetTranslation()->Setqbar(qbar);
334
335   InitMatrices(phi, tht, psi);
336
337   vLocalVelNED = mTb2l*vUVW;
338   FDMExec->GetPosition()->SetvVel(vLocalVelNED);
339 }
340
341 /******************************************************************************/
342
343 void FGState::Initialize(FGInitialCondition *FGIC) {
344
345   float tht,psi,phi;
346   float U, V, W, h;
347   float latitude, longitude;
348
349   latitude = FGIC->GetLatitudeRadIC();
350   longitude = FGIC->GetLongitudeRadIC();
351   h = FGIC->GetAltitudeFtIC();
352   U = FGIC->GetUBodyFpsIC();
353   V = FGIC->GetVBodyFpsIC();
354   W = FGIC->GetWBodyFpsIC();
355   tht = FGIC->GetThetaRadIC();
356   phi = FGIC->GetPhiRadIC();
357   psi = FGIC->GetPsiRadIC();
358
359   Initialize(U, V, W, phi, tht, psi, latitude, longitude, h);
360 }
361
362 /******************************************************************************/
363
364 bool FGState::StoreData(string fname) {
365   ofstream datafile(fname.c_str());
366
367   if (datafile) {
368     datafile << (FDMExec->GetTranslation()->GetUVW())(1);
369     datafile << (FDMExec->GetTranslation()->GetUVW())(2);
370     datafile << (FDMExec->GetTranslation()->GetUVW())(3);
371     datafile << FDMExec->GetPosition()->GetLatitude();
372     datafile << FDMExec->GetPosition()->GetLongitude();
373     datafile << (FDMExec->GetRotation()->GetEuler())(1);
374     datafile << (FDMExec->GetRotation()->GetEuler())(2);
375     datafile << (FDMExec->GetRotation()->GetEuler())(3);
376     datafile << FDMExec->GetPosition()->Geth();
377     datafile.close();
378     return true;
379   } else {
380     cerr << "Could not open dump file " << fname << endl;
381     return false;
382   }
383 }
384
385 /******************************************************************************/
386
387 void FGState::InitMatrices(float phi, float tht, float psi) {
388   float thtd2, psid2, phid2;
389   float Sthtd2, Spsid2, Sphid2;
390   float Cthtd2, Cpsid2, Cphid2;
391   float Cphid2Cthtd2;
392   float Cphid2Sthtd2;
393   float Sphid2Sthtd2;
394   float Sphid2Cthtd2;
395
396   thtd2 = tht/2.0;
397   psid2 = psi/2.0;
398   phid2 = phi/2.0;
399
400   Sthtd2 = sin(thtd2);
401   Spsid2 = sin(psid2);
402   Sphid2 = sin(phid2);
403
404   Cthtd2 = cos(thtd2);
405   Cpsid2 = cos(psid2);
406   Cphid2 = cos(phid2);
407
408   Cphid2Cthtd2 = Cphid2*Cthtd2;
409   Cphid2Sthtd2 = Cphid2*Sthtd2;
410   Sphid2Sthtd2 = Sphid2*Sthtd2;
411   Sphid2Cthtd2 = Sphid2*Cthtd2;
412
413   vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
414   vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
415   vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
416   vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
417
418   CalcMatrices();
419 }
420
421 /******************************************************************************/
422
423 void FGState::CalcMatrices(void) {
424   float Q0Q0, Q1Q1, Q2Q2, Q3Q3;
425   float Q0Q1, Q0Q2, Q0Q3, Q1Q2;
426   float Q1Q3, Q2Q3;
427
428   Q0Q0 = vQtrn(1)*vQtrn(1);
429   Q1Q1 = vQtrn(2)*vQtrn(2);
430   Q2Q2 = vQtrn(3)*vQtrn(3);
431   Q3Q3 = vQtrn(4)*vQtrn(4);
432   Q0Q1 = vQtrn(1)*vQtrn(2);
433   Q0Q2 = vQtrn(1)*vQtrn(3);
434   Q0Q3 = vQtrn(1)*vQtrn(4);
435   Q1Q2 = vQtrn(2)*vQtrn(3);
436   Q1Q3 = vQtrn(2)*vQtrn(4);
437   Q2Q3 = vQtrn(3)*vQtrn(4);
438
439   mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
440   mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
441   mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
442   mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
443   mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
444   mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
445   mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
446   mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
447   mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
448
449   mTb2l = mTl2b;
450   mTb2l.T();
451 }
452
453 /******************************************************************************/
454
455 void FGState::IntegrateQuat(FGColumnVector vPQR, int rate) {
456   static FGColumnVector vlastQdot(4);
457   static FGColumnVector vQdot(4);
458
459   vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
460   vQdot(2) =  0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
461   vQdot(3) =  0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
462   vQdot(4) =  0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
463
464   vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
465
466   vQtrn.Normalize();
467
468   vlastQdot = vQdot;
469 }
470
471 /******************************************************************************/
472
473 FGColumnVector FGState::CalcEuler(void) {
474   static FGColumnVector vEuler(3);
475
476   if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
477   if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
478
479   vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
480   vEuler(eTht) = asin(-mTl2b(1,3));
481   vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
482
483   if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
484
485   return vEuler;
486 }
487
488 /******************************************************************************/
489
490 FGMatrix FGState::GetTs2b(float alpha, float beta) {
491   float ca, cb, sa, sb;
492
493   ca = cos(alpha);
494   sa = sin(alpha);
495   cb = cos(beta);
496   sb = sin(beta);
497
498   mTs2b(1,1) = -ca*cb;
499   mTs2b(1,2) = -ca*sb;
500   mTs2b(1,3) = sa;
501   mTs2b(2,1) = -sb;
502   mTs2b(2,2) = cb;
503   mTs2b(2,3) = 0.0;
504   mTs2b(3,1) = -sa*cb;
505   mTs2b(3,2) = -sa*sb;
506   mTs2b(3,3) = -ca;
507
508   return mTs2b;
509 }
510
511 /******************************************************************************/
512