]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
4d930ece693541a43f9ff3456b3e6f20c06cc4a8
[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 static const char *IdSrc = "$Id$";
67 static const char *IdHdr = ID_STATE;
68
69 extern short debug_lvl;
70
71 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 MACROS
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74
75 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
76
77 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 CLASS IMPLEMENTATION
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
80
81 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 //
83 // For every term registered here there must be a corresponding handler in
84 // GetParameter() below that retrieves that parameter. Also, there must be an
85 // entry in the enum eParam definition in FGDefs.h. The ID is what must be used
86 // in any config file entry which references that item.
87
88 FGState::FGState(FGFDMExec* fdex) : mTb2l(3,3),
89     mTl2b(3,3),
90     mTs2b(3,3),
91     vQtrn(4),
92     vlastQdot(4)
93 {
94   FDMExec = fdex;
95
96   a = 1000.0;
97   sim_time = 0.0;
98   dt = 1.0/120.0;
99   ActiveEngine = -1;
100   
101   RegisterVariable(FG_TIME,           " time "           );
102   RegisterVariable(FG_QBAR,           " qbar "           );
103   RegisterVariable(FG_WINGAREA,       " wing_area "      );
104   RegisterVariable(FG_WINGSPAN,       " wingspan "       );
105   RegisterVariable(FG_CBAR,           " cbar "           );
106   RegisterVariable(FG_ALPHA,          " alpha "          );
107   RegisterVariable(FG_ALPHADOT,       " alphadot "       );
108   RegisterVariable(FG_BETA,           " beta "           );
109   RegisterVariable(FG_BETADOT,        " betadot "        );
110   RegisterVariable(FG_PITCHRATE,      " pitch_rate "     );
111   RegisterVariable(FG_ROLLRATE,       " roll_rate "      );
112   RegisterVariable(FG_YAWRATE,        " yaw_rate "       );
113   RegisterVariable(FG_MACH,           " mach "           );
114   RegisterVariable(FG_ALTITUDE,       " altitude "       );
115   RegisterVariable(FG_BI2VEL,         " BI2Vel "         );
116   RegisterVariable(FG_CI2VEL,         " CI2Vel "         );
117   RegisterVariable(FG_ELEVATOR_POS,   " elevator_pos "   );
118   RegisterVariable(FG_AILERON_POS,    " aileron_pos "    );
119   RegisterVariable(FG_RUDDER_POS,     " rudder_pos "     );
120   RegisterVariable(FG_SPDBRAKE_POS,   " speedbrake_pos " );
121   RegisterVariable(FG_SPOILERS_POS,   " spoiler_pos "    );
122   RegisterVariable(FG_FLAPS_POS,      " flaps_pos "      );
123   RegisterVariable(FG_ELEVATOR_CMD,   " elevator_cmd "   );
124   RegisterVariable(FG_AILERON_CMD,    " aileron_cmd "    );
125   RegisterVariable(FG_RUDDER_CMD,     " rudder_cmd "     );
126   RegisterVariable(FG_SPDBRAKE_CMD,   " speedbrake_cmd " );
127   RegisterVariable(FG_SPOILERS_CMD,   " spoiler_cmd "    );
128   RegisterVariable(FG_FLAPS_CMD,      " flaps_cmd "      );
129   RegisterVariable(FG_THROTTLE_CMD,   " throttle_cmd "   );
130   RegisterVariable(FG_THROTTLE_POS,   " throttle_pos "   );
131   RegisterVariable(FG_ACTIVE_ENGINE,  " active_engine "  );
132   RegisterVariable(FG_HOVERB,         " height/span "    );
133   RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
134   RegisterVariable(FG_LEFT_BRAKE_CMD, " left_brake_cmd " );
135   RegisterVariable(FG_RIGHT_BRAKE_CMD," right_brake_cmd ");
136   RegisterVariable(FG_CENTER_BRAKE_CMD," center_brake_cmd ");
137   RegisterVariable(FG_SET_LOGGING,    " data_logging "   );
138
139   if (debug_lvl & 2) cout << "Instantiated: FGState" << endl;
140 }
141
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143
144 FGState::~FGState()
145 {
146   if (debug_lvl & 2) cout << "Destroyed:    FGState" << endl;
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 float FGState::GetParameter(eParam val_idx) {
152   switch(val_idx) {
153   case FG_TIME:
154     return sim_time;
155   case FG_QBAR:
156     return FDMExec->GetTranslation()->Getqbar();
157   case FG_WINGAREA:
158     return FDMExec->GetAircraft()->GetWingArea();
159   case FG_WINGSPAN:
160     return FDMExec->GetAircraft()->GetWingSpan();
161   case FG_CBAR:
162     return FDMExec->GetAircraft()->Getcbar();
163   case FG_ALPHA:
164     return FDMExec->GetTranslation()->Getalpha();
165   case FG_ALPHADOT:
166     return FDMExec->GetTranslation()->Getadot();
167   case FG_BETA:
168     return FDMExec->GetTranslation()->Getbeta();
169   case FG_BETADOT:
170     return FDMExec->GetTranslation()->Getbdot();
171   case FG_PITCHRATE:
172     return (FDMExec->GetRotation()->GetPQR())(2);
173   case FG_ROLLRATE:
174     return (FDMExec->GetRotation()->GetPQR())(1);
175   case FG_YAWRATE:
176     return (FDMExec->GetRotation()->GetPQR())(3);
177   case FG_ELEVATOR_POS:
178     return FDMExec->GetFCS()->GetDePos();
179   case FG_AILERON_POS:
180     return FDMExec->GetFCS()->GetDaPos();
181   case FG_RUDDER_POS:
182     return FDMExec->GetFCS()->GetDrPos();
183   case FG_SPDBRAKE_POS:
184     return FDMExec->GetFCS()->GetDsbPos();
185   case FG_SPOILERS_POS:
186     return FDMExec->GetFCS()->GetDspPos();
187   case FG_FLAPS_POS:
188     return FDMExec->GetFCS()->GetDfPos();
189   case FG_ELEVATOR_CMD:
190     return FDMExec->GetFCS()->GetDeCmd();
191   case FG_AILERON_CMD:
192     return FDMExec->GetFCS()->GetDaCmd();
193   case FG_RUDDER_CMD:
194     return FDMExec->GetFCS()->GetDrCmd();
195   case FG_SPDBRAKE_CMD:
196     return FDMExec->GetFCS()->GetDsbCmd();
197   case FG_SPOILERS_CMD:
198     return FDMExec->GetFCS()->GetDspCmd();
199   case FG_FLAPS_CMD:
200     return FDMExec->GetFCS()->GetDfCmd();
201   case FG_MACH:
202     return FDMExec->GetTranslation()->GetMach();
203   case FG_ALTITUDE:
204     return FDMExec->GetPosition()->Geth();
205   case FG_BI2VEL:
206     if(FDMExec->GetTranslation()->GetVt() > 0)
207         return FDMExec->GetAircraft()->GetWingSpan()/(2.0 * FDMExec->GetTranslation()->GetVt());
208     else
209         return 0;
210   case FG_CI2VEL:
211     if(FDMExec->GetTranslation()->GetVt() > 0)
212         return FDMExec->GetAircraft()->Getcbar()/(2.0 * FDMExec->GetTranslation()->GetVt());
213     else
214         return 0;
215   case FG_THROTTLE_CMD:
216     return FDMExec->GetFCS()->GetThrottleCmd(0);
217   case FG_THROTTLE_POS:
218     return FDMExec->GetFCS()->GetThrottlePos(0);
219   case FG_HOVERB:
220     return FDMExec->GetPosition()->GetHOverB();
221   case FG_PITCH_TRIM_CMD:
222     return FDMExec->GetFCS()->GetPitchTrimCmd();
223   default:
224     cerr << "FGState::GetParameter() - No handler for parameter " << val_idx << endl;
225     return 0.0;
226   }
227   return 0;
228 }
229
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231
232 float FGState::GetParameter(string val_string) {
233   return GetParameter(coeffdef[val_string]);
234 }
235
236 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237
238 eParam FGState::GetParameterIndex(string val_string) {
239   return coeffdef[val_string];
240 }
241
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243
244 void FGState::SetParameter(eParam val_idx, float val) {
245   switch(val_idx) {
246   case FG_ELEVATOR_POS:
247     FDMExec->GetFCS()->SetDePos(val);
248     break;
249   case FG_AILERON_POS:
250     FDMExec->GetFCS()->SetDaPos(val);
251     break;
252   case FG_RUDDER_POS:
253     FDMExec->GetFCS()->SetDrPos(val);
254     break;
255   case FG_SPDBRAKE_POS:
256     FDMExec->GetFCS()->SetDsbPos(val);
257     break;
258   case FG_SPOILERS_POS:
259     FDMExec->GetFCS()->SetDspPos(val);
260     break;
261   case FG_FLAPS_POS:
262     FDMExec->GetFCS()->SetDfPos(val);
263     break;
264   case FG_THROTTLE_POS:
265     FDMExec->GetFCS()->SetThrottlePos(ActiveEngine,val);
266     break;
267
268   case FG_ELEVATOR_CMD:
269     FDMExec->GetFCS()->SetDeCmd(val);
270     break;
271   case FG_AILERON_CMD:
272     FDMExec->GetFCS()->SetDaCmd(val);
273     break;
274   case FG_RUDDER_CMD:
275     FDMExec->GetFCS()->SetDrCmd(val);
276     break;
277   case FG_SPDBRAKE_CMD:
278     FDMExec->GetFCS()->SetDsbCmd(val);
279     break;
280   case FG_SPOILERS_CMD:
281     FDMExec->GetFCS()->SetDspCmd(val);
282     break;
283   case FG_FLAPS_CMD:
284     FDMExec->GetFCS()->SetDfCmd(val);
285     break;
286   case FG_THROTTLE_CMD:
287     FDMExec->GetFCS()->SetThrottleCmd(ActiveEngine,val);
288     break;
289
290   case FG_ACTIVE_ENGINE:
291     ActiveEngine = (int)val;
292     break;
293
294   case FG_LEFT_BRAKE_CMD:
295     FDMExec->GetFCS()->SetLBrake(val);
296     break;
297   case FG_CENTER_BRAKE_CMD:
298     FDMExec->GetFCS()->SetCBrake(val);
299     break;
300   case FG_RIGHT_BRAKE_CMD:
301     FDMExec->GetFCS()->SetRBrake(val);
302     break;
303
304   case FG_SET_LOGGING:
305     if      (val < -0.01) FDMExec->GetOutput()->Disable();
306     else if (val >  0.01) FDMExec->GetOutput()->Enable();
307     else                  FDMExec->GetOutput()->Toggle();
308     break;
309
310   default:
311     cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
312   }
313 }
314
315 //***************************************************************************
316 //
317 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
318 //
319
320 bool FGState::Reset(string path, string acname, string fname) {
321   string resetDef;
322   float U, V, W;
323   float phi, tht, psi;
324   float latitude, longitude, h;
325
326   resetDef = path + "/" + acname + "/" + fname + ".xml";
327
328   ifstream resetfile(resetDef.c_str());
329
330   if (resetfile) {
331     resetfile >> U;
332     resetfile >> V;
333     resetfile >> W;
334     resetfile >> latitude;
335     resetfile >> longitude;
336     resetfile >> phi;
337     resetfile >> tht;
338     resetfile >> psi;
339     resetfile >> h;
340     resetfile.close();
341
342     FDMExec->GetPosition()->SetLatitude(latitude*DEGTORAD);
343     FDMExec->GetPosition()->SetLongitude(longitude*DEGTORAD);
344     FDMExec->GetPosition()->Seth(h);
345
346     Initialize(U, V, W, phi*DEGTORAD, tht*DEGTORAD, psi*DEGTORAD,
347                latitude*DEGTORAD, longitude*DEGTORAD, h);
348
349     return true;
350   } else {
351     cerr << "Unable to load reset file " << fname << endl;
352     return false;
353   }
354 }
355
356 //***************************************************************************
357 //
358 // Initialize: Assume all angles GIVEN IN RADIANS !!
359 //
360
361 void FGState::Initialize(float U, float V, float W,
362                          float phi, float tht, float psi,
363                          float Latitude, float Longitude, float H) {
364   FGColumnVector vUVW(3);
365   FGColumnVector vLocalVelNED(3);
366   FGColumnVector vEuler(3);
367   float alpha, beta;
368   float qbar, Vt;
369
370   FDMExec->GetPosition()->SetLatitude(Latitude);
371   FDMExec->GetPosition()->SetLongitude(Longitude);
372   FDMExec->GetPosition()->Seth(H);
373
374   FDMExec->GetAtmosphere()->Run();
375
376   if (W != 0.0)
377     alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
378   else
379     alpha = 0.0;
380   if (V != 0.0)
381     beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
382   else
383     beta = 0.0;
384
385   vUVW << U << V << W;
386   FDMExec->GetTranslation()->SetUVW(vUVW);
387
388   vEuler << phi << tht << psi;
389   FDMExec->GetRotation()->SetEuler(vEuler);
390
391   FDMExec->GetTranslation()->SetAB(alpha, beta);
392
393   Vt = sqrt(U*U + V*V + W*W);
394   FDMExec->GetTranslation()->SetVt(Vt);
395
396   FDMExec->GetTranslation()->SetMach(Vt/FDMExec->GetAtmosphere()->GetSoundSpeed());
397
398   qbar = 0.5*(U*U + V*V + W*W)*FDMExec->GetAtmosphere()->GetDensity();
399   FDMExec->GetTranslation()->Setqbar(qbar);
400
401   InitMatrices(phi, tht, psi);
402
403   vLocalVelNED = mTb2l*vUVW;
404   FDMExec->GetPosition()->SetvVel(vLocalVelNED);
405 }
406
407 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408
409 void FGState::Initialize(FGInitialCondition *FGIC) {
410
411   float tht,psi,phi;
412   float U, V, W, h;
413   float latitude, longitude;
414
415   latitude = FGIC->GetLatitudeRadIC();
416   longitude = FGIC->GetLongitudeRadIC();
417   h = FGIC->GetAltitudeFtIC();
418   U = FGIC->GetUBodyFpsIC();
419   V = FGIC->GetVBodyFpsIC();
420   W = FGIC->GetWBodyFpsIC();
421   tht = FGIC->GetThetaRadIC();
422   phi = FGIC->GetPhiRadIC();
423   psi = FGIC->GetPsiRadIC();
424
425   Initialize(U, V, W, phi, tht, psi, latitude, longitude, h);
426   
427   FDMExec->GetPosition()->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
428   FDMExec->GetPosition()->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() + 
429                                              FGIC->GetTerrainAltitudeFtIC() );
430
431 }
432
433 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434
435 bool FGState::StoreData(string fname) {
436   ofstream datafile(fname.c_str());
437
438   if (datafile) {
439     datafile << (FDMExec->GetTranslation()->GetUVW())(1);
440     datafile << (FDMExec->GetTranslation()->GetUVW())(2);
441     datafile << (FDMExec->GetTranslation()->GetUVW())(3);
442     datafile << FDMExec->GetPosition()->GetLatitude();
443     datafile << FDMExec->GetPosition()->GetLongitude();
444     datafile << (FDMExec->GetRotation()->GetEuler())(1);
445     datafile << (FDMExec->GetRotation()->GetEuler())(2);
446     datafile << (FDMExec->GetRotation()->GetEuler())(3);
447     datafile << FDMExec->GetPosition()->Geth();
448     datafile.close();
449     return true;
450   } else {
451     cerr << "Could not open dump file " << fname << endl;
452     return false;
453   }
454 }
455
456 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457
458 void FGState::InitMatrices(float phi, float tht, float psi) {
459   float thtd2, psid2, phid2;
460   float Sthtd2, Spsid2, Sphid2;
461   float Cthtd2, Cpsid2, Cphid2;
462   float Cphid2Cthtd2;
463   float Cphid2Sthtd2;
464   float Sphid2Sthtd2;
465   float Sphid2Cthtd2;
466
467   thtd2 = tht/2.0;
468   psid2 = psi/2.0;
469   phid2 = phi/2.0;
470
471   Sthtd2 = sin(thtd2);
472   Spsid2 = sin(psid2);
473   Sphid2 = sin(phid2);
474
475   Cthtd2 = cos(thtd2);
476   Cpsid2 = cos(psid2);
477   Cphid2 = cos(phid2);
478
479   Cphid2Cthtd2 = Cphid2*Cthtd2;
480   Cphid2Sthtd2 = Cphid2*Sthtd2;
481   Sphid2Sthtd2 = Sphid2*Sthtd2;
482   Sphid2Cthtd2 = Sphid2*Cthtd2;
483
484   vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
485   vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
486   vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
487   vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
488
489   CalcMatrices();
490 }
491
492 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493
494 void FGState::CalcMatrices(void) {
495   float Q0Q0, Q1Q1, Q2Q2, Q3Q3;
496   float Q0Q1, Q0Q2, Q0Q3, Q1Q2;
497   float Q1Q3, Q2Q3;
498
499   Q0Q0 = vQtrn(1)*vQtrn(1);
500   Q1Q1 = vQtrn(2)*vQtrn(2);
501   Q2Q2 = vQtrn(3)*vQtrn(3);
502   Q3Q3 = vQtrn(4)*vQtrn(4);
503   Q0Q1 = vQtrn(1)*vQtrn(2);
504   Q0Q2 = vQtrn(1)*vQtrn(3);
505   Q0Q3 = vQtrn(1)*vQtrn(4);
506   Q1Q2 = vQtrn(2)*vQtrn(3);
507   Q1Q3 = vQtrn(2)*vQtrn(4);
508   Q2Q3 = vQtrn(3)*vQtrn(4);
509
510   mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
511   mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
512   mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
513   mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
514   mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
515   mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
516   mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
517   mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
518   mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
519
520   mTb2l = mTl2b;
521   mTb2l.T();
522 }
523
524 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525
526 void FGState::IntegrateQuat(FGColumnVector vPQR, int rate) {
527   static FGColumnVector vQdot(4);
528   static FGColumnVector vTmp(4);
529
530   vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
531   vQdot(2) =  0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
532   vQdot(3) =  0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
533   vQdot(4) =  0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
534   vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
535
536   vQtrn.Normalize();
537
538   vlastQdot = vQdot;
539 }
540
541 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542
543 FGColumnVector FGState::CalcEuler(void) {
544   static FGColumnVector vEuler(3);
545
546   if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
547   if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
548
549   vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
550   vEuler(eTht) = asin(-mTl2b(1,3));
551   vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
552
553   if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
554
555   return vEuler;
556 }
557
558 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559
560 FGMatrix FGState::GetTs2b(float alpha, float beta) {
561   float ca, cb, sa, sb;
562
563   ca = cos(alpha);
564   sa = sin(alpha);
565   cb = cos(beta);
566   sb = sin(beta);
567
568   mTs2b(1,1) = -ca*cb;
569   mTs2b(1,2) = -ca*sb;
570   mTs2b(1,3) = sa;
571   mTs2b(2,1) = -sb;
572   mTs2b(2,2) = cb;
573   mTs2b(2,3) = 0.0;
574   mTs2b(3,1) = -sa*cb;
575   mTs2b(3,2) = -sa*sb;
576   mTs2b(3,3) = -ca;
577
578   return mTs2b;
579 }
580
581 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582
583 void FGState::Debug(void)
584 {
585     //TODO: Add your source code here
586 }
587