]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
Syncing with the very latest JSBSim development code.
[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 #  include <math.h>
42 #else
43 #  if defined(sgi) && !defined(__GNUC__)
44 #    include <math.h>
45 #  else
46 #    include <cmath>
47 #  endif
48 #endif
49
50 #include "FGState.h"
51
52 static const char *IdSrc = "$Id$";
53 static const char *IdHdr = ID_STATE;
54
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 MACROS
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58
59 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
60
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 CLASS IMPLEMENTATION
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64
65 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 //
67 // For every term registered here there must be a corresponding handler in
68 // GetParameter() below that retrieves that parameter. Also, there must be an
69 // entry in the enum eParam definition in FGDefs.h. The ID is what must be used
70 // in any config file entry which references that item.
71
72 FGState::FGState(FGFDMExec* fdex) : mTb2l(3,3),
73     mTl2b(3,3),
74     mTs2b(3,3),
75     vQtrn(4),
76     vlastQdot(4),
77     vQdot(4),
78     vTmp(4),
79     vEuler(3),
80     vUVW(3),
81     vLocalVelNED(3),
82     vLocalEuler(3)
83 {
84   FDMExec = fdex;
85
86   a = 1000.0;
87   sim_time = 0.0;
88   dt = 1.0/120.0;
89   ActiveEngine = -1;
90
91   Aircraft     = FDMExec->GetAircraft();
92   Translation  = FDMExec->GetTranslation();
93   Rotation     = FDMExec->GetRotation();
94   Position     = FDMExec->GetPosition();
95   FCS          = FDMExec->GetFCS();
96   Output       = FDMExec->GetOutput();
97   Atmosphere   = FDMExec->GetAtmosphere();
98   Aerodynamics = FDMExec->GetAerodynamics();
99
100   RegisterVariable(FG_TIME,           " time "           );
101   RegisterVariable(FG_QBAR,           " qbar "           );
102   RegisterVariable(FG_WINGAREA,       " wing_area "      );
103   RegisterVariable(FG_WINGSPAN,       " wingspan "       );
104   RegisterVariable(FG_CBAR,           " cbar "           );
105   RegisterVariable(FG_ALPHA,          " alpha "          );
106   RegisterVariable(FG_ALPHADOT,       " alphadot "       );
107   RegisterVariable(FG_BETA,           " beta "           );
108   RegisterVariable(FG_BETADOT,        " betadot "        );
109   RegisterVariable(FG_PHI,            " roll_angle "     );
110   RegisterVariable(FG_THT,            " pitch_angle "    );
111   RegisterVariable(FG_PSI,            " heading_angle "  );
112   RegisterVariable(FG_PITCHRATE,      " pitch_rate "     );
113   RegisterVariable(FG_ROLLRATE,       " roll_rate "      );
114   RegisterVariable(FG_YAWRATE,        " yaw_rate "       );
115   RegisterVariable(FG_CL_SQRD,        " Clift_sqrd "     );
116   RegisterVariable(FG_MACH,           " mach "           );
117   RegisterVariable(FG_ALTITUDE,       " altitude "       );
118   RegisterVariable(FG_BI2VEL,         " BI2Vel "         );
119   RegisterVariable(FG_CI2VEL,         " CI2Vel "         );
120   RegisterVariable(FG_ELEVATOR_POS,   " elevator_pos "   );
121   RegisterVariable(FG_AILERON_POS,    " aileron_pos "    );
122   RegisterVariable(FG_RUDDER_POS,     " rudder_pos "     );
123   RegisterVariable(FG_SPDBRAKE_POS,   " speedbrake_pos " );
124   RegisterVariable(FG_SPOILERS_POS,   " spoiler_pos "    );
125   RegisterVariable(FG_FLAPS_POS,      " flaps_pos "      );
126   RegisterVariable(FG_ELEVATOR_CMD,   " elevator_cmd "   );
127   RegisterVariable(FG_AILERON_CMD,    " aileron_cmd "    );
128   RegisterVariable(FG_RUDDER_CMD,     " rudder_cmd "     );
129   RegisterVariable(FG_SPDBRAKE_CMD,   " speedbrake_cmd " );
130   RegisterVariable(FG_SPOILERS_CMD,   " spoiler_cmd "    );
131   RegisterVariable(FG_FLAPS_CMD,      " flaps_cmd "      );
132   RegisterVariable(FG_THROTTLE_CMD,   " throttle_cmd "   );
133   RegisterVariable(FG_THROTTLE_POS,   " throttle_pos "   );
134   RegisterVariable(FG_ACTIVE_ENGINE,  " active_engine "  );
135   RegisterVariable(FG_HOVERB,         " height/span "    );
136   RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
137   RegisterVariable(FG_LEFT_BRAKE_CMD, " left_brake_cmd " );
138   RegisterVariable(FG_RIGHT_BRAKE_CMD," right_brake_cmd ");
139   RegisterVariable(FG_CENTER_BRAKE_CMD," center_brake_cmd ");
140   RegisterVariable(FG_ALPHAH,          " h-tail alpha " );
141   RegisterVariable(FG_ALPHAW,          " wing alpha " );
142   RegisterVariable(FG_LBARH,           " h-tail arm " );
143   RegisterVariable(FG_LBARV,           " v-tail arm " );
144   RegisterVariable(FG_HTAILAREA,       " h-tail area " );
145   RegisterVariable(FG_VTAILAREA,       " v-tail area " );
146   RegisterVariable(FG_VBARH,           " h-tail volume " );
147   RegisterVariable(FG_VBARV,           " v-tail volume " );
148   RegisterVariable(FG_SET_LOGGING,    " data_logging "   );
149
150   if (debug_lvl & 2) cout << "Instantiated: FGState" << endl;
151 }
152
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154
155 FGState::~FGState()
156 {
157   if (debug_lvl & 2) cout << "Destroyed:    FGState" << endl;
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162 float FGState::GetParameter(eParam val_idx) {
163   float scratch;
164   
165   switch(val_idx) {
166   case FG_TIME:
167     return sim_time;
168   case FG_QBAR:
169     return Translation->Getqbar();
170   case FG_WINGAREA:
171     return Aircraft->GetWingArea();
172   case FG_WINGSPAN:
173     return Aircraft->GetWingSpan();
174   case FG_CBAR:
175     return Aircraft->Getcbar();
176   case FG_LBARH:
177     return Aircraft->Getlbarh();
178   case FG_LBARV:
179     return Aircraft->Getvbarh();
180   case FG_HTAILAREA:
181     return Aircraft->GetHTailArea();
182   case FG_VTAILAREA:
183     return Aircraft->GetVTailArea();
184   case FG_VBARH:
185     return Aircraft->Getvbarh();
186   case FG_VBARV:
187     return Aircraft->Getvbarv();
188   case FG_ALPHA:
189     return Translation->Getalpha();
190   case FG_ALPHAW:
191     return  Translation->Getalpha() + Aircraft->GetWingIncidence();
192   case FG_ALPHADOT:
193     return Translation->Getadot();
194   case FG_BETA:
195     return Translation->Getbeta();
196   case FG_BETADOT:
197     return Translation->Getbdot();
198   case FG_PHI:
199     return Rotation->Getphi();
200   case FG_THT:
201     return Rotation->Gettht();
202   case FG_PSI:
203     return Rotation->Getpsi();
204   case FG_PITCHRATE:
205     return Rotation->GetPQR(eQ);
206   case FG_ROLLRATE:
207     return Rotation->GetPQR(eP);
208   case FG_YAWRATE:
209     return Rotation->GetPQR(eR);
210   case FG_CL_SQRD:
211     if (Translation->Getqbar() > 0.00)
212       scratch = Aerodynamics->GetvLastFs(eLift)/(Aircraft->GetWingArea()*Translation->Getqbar());
213     else
214       scratch = 0.0;
215     return scratch*scratch;                                        
216   case FG_ELEVATOR_POS:
217     return FCS->GetDePos();
218   case FG_AILERON_POS:
219     return FCS->GetDaPos();
220   case FG_RUDDER_POS:
221     return FCS->GetDrPos();
222   case FG_SPDBRAKE_POS:
223     return FCS->GetDsbPos();
224   case FG_SPOILERS_POS:
225     return FCS->GetDspPos();
226   case FG_FLAPS_POS:
227     return FCS->GetDfPos();
228   case FG_ELEVATOR_CMD:
229     return FCS->GetDeCmd();
230   case FG_AILERON_CMD:
231     return FCS->GetDaCmd();
232   case FG_RUDDER_CMD:
233     return FCS->GetDrCmd();
234   case FG_SPDBRAKE_CMD:
235     return FCS->GetDsbCmd();
236   case FG_SPOILERS_CMD:
237     return FCS->GetDspCmd();
238   case FG_FLAPS_CMD:
239     return FCS->GetDfCmd();
240   case FG_MACH:
241     return Translation->GetMach();
242   case FG_ALTITUDE:
243     return Position->Geth();
244   case FG_BI2VEL:
245     if(Translation->GetVt() > 0)
246         return Aircraft->GetWingSpan()/(2.0 * Translation->GetVt());
247     else
248         return 0;
249   case FG_CI2VEL:
250     if(Translation->GetVt() > 0)
251         return Aircraft->Getcbar()/(2.0 * Translation->GetVt());
252     else
253         return 0;
254   case FG_THROTTLE_CMD:
255     if (ActiveEngine < 0) return FCS->GetThrottleCmd(0);
256     else return FCS->GetThrottleCmd(ActiveEngine);
257   case FG_THROTTLE_POS:
258     if (ActiveEngine < 0) return FCS->GetThrottlePos(0);
259     else return FCS->GetThrottlePos(ActiveEngine);
260   case FG_HOVERB:
261     return Position->GetHOverB();
262   case FG_PITCH_TRIM_CMD:
263     return FCS->GetPitchTrimCmd();
264   default:
265     cerr << "FGState::GetParameter() - No handler for parameter " << val_idx << endl;
266     return 0.0;
267   }
268   return 0;
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 float FGState::GetParameter(string val_string) {
274   return GetParameter(coeffdef[val_string]);
275 }
276
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278
279 eParam FGState::GetParameterIndex(string val_string) {
280   return coeffdef[val_string];
281 }
282
283 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284
285 void FGState::SetParameter(eParam val_idx, float val) {
286   switch(val_idx) {
287   case FG_ELEVATOR_POS:
288     FCS->SetDePos(val);
289     break;
290   case FG_AILERON_POS:
291     FCS->SetDaPos(val);
292     break;
293   case FG_RUDDER_POS:
294     FCS->SetDrPos(val);
295     break;
296   case FG_SPDBRAKE_POS:
297     FCS->SetDsbPos(val);
298     break;
299   case FG_SPOILERS_POS:
300     FCS->SetDspPos(val);
301     break;
302   case FG_FLAPS_POS:
303     FCS->SetDfPos(val);
304     break;
305   case FG_THROTTLE_POS:
306     FCS->SetThrottlePos(ActiveEngine,val);
307     break;
308
309   case FG_ELEVATOR_CMD:
310     FCS->SetDeCmd(val);
311     break;
312   case FG_AILERON_CMD:
313     FCS->SetDaCmd(val);
314     break;
315   case FG_RUDDER_CMD:
316     FCS->SetDrCmd(val);
317     break;
318   case FG_SPDBRAKE_CMD:
319     FCS->SetDsbCmd(val);
320     break;
321   case FG_SPOILERS_CMD:
322     FCS->SetDspCmd(val);
323     break;
324   case FG_FLAPS_CMD:
325     FCS->SetDfCmd(val);
326     break;
327   case FG_THROTTLE_CMD:
328     FCS->SetThrottleCmd(ActiveEngine,val);
329     break;
330
331   case FG_ACTIVE_ENGINE:
332     ActiveEngine = (int)val;
333     break;
334
335   case FG_LEFT_BRAKE_CMD:
336     FCS->SetLBrake(val);
337     break;
338   case FG_CENTER_BRAKE_CMD:
339     FCS->SetCBrake(val);
340     break;
341   case FG_RIGHT_BRAKE_CMD:
342     FCS->SetRBrake(val);
343     break;
344
345   case FG_SET_LOGGING:
346     if      (val < -0.01) Output->Disable();
347     else if (val >  0.01) Output->Enable();
348     else                  Output->Toggle();
349     break;
350
351   default:
352     cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
353   }
354 }
355
356 //***************************************************************************
357 //
358 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
359 //
360
361 bool FGState::Reset(string path, string acname, string fname)
362 {
363   string resetDef;
364   string token="";
365
366   float U, V, W;
367   float phi, tht, psi;
368   float latitude, longitude, h;
369   float wdir, wmag, wnorth, weast;
370
371 # ifndef macintosh
372   resetDef = path + "/" + acname + "/" + fname + ".xml";
373 # else
374   resetDef = path + ";" + acname + ";" + fname + ".xml";
375 # endif
376
377   FGConfigFile resetfile(resetDef);
378   if (!resetfile.IsOpen()) return false;
379
380   resetfile.GetNextConfigLine();
381   token = resetfile.GetValue();
382   if (token != "initialize") {
383     cerr << "The reset file " << resetDef
384          << " does not appear to be a reset file" << endl;
385     return false;
386   }
387   
388   resetfile.GetNextConfigLine();
389   resetfile >> token;
390   while (token != "/initialize" && token != "EOF") {
391     if (token == "UBODY") resetfile >> U;
392     if (token == "VBODY") resetfile >> V;
393     if (token == "WBODY") resetfile >> W;
394     if (token == "LATITUDE") resetfile >> latitude;
395     if (token == "LONGITUDE") resetfile >> longitude;
396     if (token == "PHI") resetfile >> phi;
397     if (token == "THETA") resetfile >> tht;
398     if (token == "PSI") resetfile >> psi;
399     if (token == "ALTITUDE") resetfile >> h;
400     if (token == "WINDDIR") resetfile >> wdir;
401     if (token == "VWIND") resetfile >> wmag;
402
403     resetfile >> token;
404   }
405   
406   
407   Position->SetLatitude(latitude*DEGTORAD);
408   Position->SetLongitude(longitude*DEGTORAD);
409   Position->Seth(h);
410
411   wnorth = wmag*KTSTOFPS*cos(wdir*DEGTORAD);
412   weast = wmag*KTSTOFPS*sin(wdir*DEGTORAD);
413   
414   Initialize(U, V, W, phi*DEGTORAD, tht*DEGTORAD, psi*DEGTORAD,
415                latitude*DEGTORAD, longitude*DEGTORAD, h, wnorth, weast, 0.0);
416
417   return true;
418 }
419
420 //***************************************************************************
421 //
422 // Initialize: Assume all angles GIVEN IN RADIANS !!
423 //
424
425 void FGState::Initialize(float U, float V, float W,
426                          float phi, float tht, float psi,
427                          float Latitude, float Longitude, float H,
428                          float wnorth, float weast, float wdown)
429 {
430   float alpha, beta;
431   float qbar, Vt;
432   FGColumnVector3 vAero;
433
434   Position->SetLatitude(Latitude);
435   Position->SetLongitude(Longitude);
436   Position->Seth(H);
437
438   Atmosphere->Run();
439   
440   vLocalEuler << phi << tht << psi;
441   Rotation->SetEuler(vLocalEuler);
442
443   InitMatrices(phi, tht, psi);
444   
445   vUVW << U << V << W;
446   Translation->SetUVW(vUVW);
447   
448   Atmosphere->SetWindNED(wnorth, weast, wdown);
449   
450   vAero = vUVW + mTl2b*Atmosphere->GetWindNED();
451   
452   if (vAero(eW) != 0.0)
453     alpha = vAero(eU)*vAero(eU) > 0.0 ? atan2(vAero(eW), vAero(eU)) : 0.0;
454   else
455     alpha = 0.0;
456   if (vAero(eV) != 0.0)
457     beta = vAero(eU)*vAero(eU)+vAero(eW)*vAero(eW) > 0.0 ? atan2(vAero(eV), (fabs(vAero(eU))/vAero(eU))*sqrt(vAero(eU)*vAero(eU) + vAero(eW)*vAero(eW))) : 0.0;
458   else
459     beta = 0.0;
460
461   Translation->SetAB(alpha, beta);
462
463   Vt = sqrt(U*U + V*V + W*W);
464   Translation->SetVt(Vt);
465
466   Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
467
468   qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
469   Translation->Setqbar(qbar);
470
471   vLocalVelNED = mTb2l*vUVW;
472   Position->SetvVel(vLocalVelNED);
473 }
474
475 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476
477 void FGState::Initialize(FGInitialCondition *FGIC) {
478
479   float tht,psi,phi;
480   float U, V, W, h;
481   float latitude, longitude;
482   float wnorth,weast, wdown;
483   
484   latitude = FGIC->GetLatitudeRadIC();
485   longitude = FGIC->GetLongitudeRadIC();
486   h = FGIC->GetAltitudeFtIC();
487   U = FGIC->GetUBodyFpsIC();
488   V = FGIC->GetVBodyFpsIC();
489   W = FGIC->GetWBodyFpsIC();
490   tht = FGIC->GetThetaRadIC();
491   phi = FGIC->GetPhiRadIC();
492   psi = FGIC->GetPsiRadIC();
493   wnorth = FGIC->GetWindNFpsIC();
494   weast = FGIC->GetWindEFpsIC();
495   wdown = FGIC->GetWindDFpsIC();
496   
497   Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
498   Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() + 
499                                              FGIC->GetTerrainAltitudeFtIC() );
500
501   // need to fix the wind speed args, here.  
502   Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
503 }
504
505 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506
507 bool FGState::StoreData(string fname) {
508   ofstream datafile(fname.c_str());
509
510   if (datafile) {
511     datafile << Translation->GetUVW(eU);
512     datafile << Translation->GetUVW(eV);
513     datafile << Translation->GetUVW(eW);
514     datafile << Position->GetLatitude();
515     datafile << Position->GetLongitude();
516     datafile << Rotation->GetEuler(ePhi);
517     datafile << Rotation->GetEuler(eTht);
518     datafile << Rotation->GetEuler(ePsi);
519     datafile << Position->Geth();
520     datafile.close();
521     return true;
522   } else {
523     cerr << "Could not open dump file " << fname << endl;
524     return false;
525   }
526 }
527
528 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529
530 void FGState::InitMatrices(float phi, float tht, float psi) {
531   float thtd2, psid2, phid2;
532   float Sthtd2, Spsid2, Sphid2;
533   float Cthtd2, Cpsid2, Cphid2;
534   float Cphid2Cthtd2;
535   float Cphid2Sthtd2;
536   float Sphid2Sthtd2;
537   float Sphid2Cthtd2;
538
539   thtd2 = tht/2.0;
540   psid2 = psi/2.0;
541   phid2 = phi/2.0;
542
543   Sthtd2 = sin(thtd2);
544   Spsid2 = sin(psid2);
545   Sphid2 = sin(phid2);
546
547   Cthtd2 = cos(thtd2);
548   Cpsid2 = cos(psid2);
549   Cphid2 = cos(phid2);
550
551   Cphid2Cthtd2 = Cphid2*Cthtd2;
552   Cphid2Sthtd2 = Cphid2*Sthtd2;
553   Sphid2Sthtd2 = Sphid2*Sthtd2;
554   Sphid2Cthtd2 = Sphid2*Cthtd2;
555
556   vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
557   vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
558   vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
559   vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
560
561   CalcMatrices();
562 }
563
564 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565
566 void FGState::CalcMatrices(void) {
567   float Q0Q0, Q1Q1, Q2Q2, Q3Q3;
568   float Q0Q1, Q0Q2, Q0Q3, Q1Q2;
569   float Q1Q3, Q2Q3;
570
571   Q0Q0 = vQtrn(1)*vQtrn(1);
572   Q1Q1 = vQtrn(2)*vQtrn(2);
573   Q2Q2 = vQtrn(3)*vQtrn(3);
574   Q3Q3 = vQtrn(4)*vQtrn(4);
575   Q0Q1 = vQtrn(1)*vQtrn(2);
576   Q0Q2 = vQtrn(1)*vQtrn(3);
577   Q0Q3 = vQtrn(1)*vQtrn(4);
578   Q1Q2 = vQtrn(2)*vQtrn(3);
579   Q1Q3 = vQtrn(2)*vQtrn(4);
580   Q2Q3 = vQtrn(3)*vQtrn(4);
581
582   mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
583   mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
584   mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
585   mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
586   mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
587   mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
588   mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
589   mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
590   mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
591
592   mTb2l = mTl2b;
593   mTb2l.T();
594 }
595
596 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597
598 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate) {
599   vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
600   vQdot(2) =  0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
601   vQdot(3) =  0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
602   vQdot(4) =  0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
603   vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
604
605   vQtrn.Normalize();
606
607   vlastQdot = vQdot;
608 }
609
610 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611
612 FGColumnVector3& FGState::CalcEuler(void) {
613   if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
614   if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
615
616   vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
617   vEuler(eTht) = asin(-mTl2b(1,3));
618   vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
619
620   if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
621
622   return vEuler;
623 }
624
625 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626
627 FGMatrix33& FGState::GetTs2b(float alpha, float beta)
628 {
629   float ca, cb, sa, sb;
630
631   ca = cos(alpha);
632   sa = sin(alpha);
633   cb = cos(beta);
634   sb = sin(beta);
635
636   mTs2b(1,1) = -ca*cb;
637   mTs2b(1,2) = -ca*sb;
638   mTs2b(1,3) = sa;
639   mTs2b(2,1) = -sb;
640   mTs2b(2,2) = cb;
641   mTs2b(2,3) = 0.0;
642   mTs2b(3,1) = -sa*cb;
643   mTs2b(3,2) = -sa*sb;
644   mTs2b(3,3) = -ca;
645
646   return mTs2b;
647 }
648
649
650 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651
652 void FGState::ReportState(void) {
653   char out[80], flap[10], gear[10];
654   
655   cout << endl << "  JSBSim State" << endl;
656   snprintf(out,80,"    Weight: %7.0f lbs.  CG: %5.1f, %5.1f, %5.1f inches\n",
657                    FDMExec->GetMassBalance()->GetWeight(),
658                    FDMExec->GetMassBalance()->GetXYZcg(1),
659                    FDMExec->GetMassBalance()->GetXYZcg(2),
660                    FDMExec->GetMassBalance()->GetXYZcg(3));
661   cout << out;             
662   if( FCS->GetDfPos() <= 0.01)
663     snprintf(flap,10,"Up");
664   else
665     snprintf(flap,10,"%2.0f",FCS->GetDfPos());
666   if(Aircraft->GetGearUp() == true)
667     snprintf(gear,10,"Up");
668   else
669     snprintf(gear,10,"Down");
670   snprintf(out,80, "    Flaps: %3s  Gear: %4s\n",flap,gear);
671   cout << out;
672   snprintf(out,80, "    Speed: %4.0f KCAS  Mach: %5.2f\n",
673                     FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
674                     GetParameter(FG_MACH) );
675   cout << out;
676   snprintf(out,80, "    Altitude: %7.0f ft.  AGL Altitude: %7.0f ft.\n",
677                     Position->Geth(),
678                     Position->GetDistanceAGL() );
679   cout << out;
680   snprintf(out,80, "    Angle of Attack: %6.2f deg  Pitch Angle: %6.2f deg\n",
681                     GetParameter(FG_ALPHA)*RADTODEG,
682                     Rotation->Gettht()*RADTODEG );
683   cout << out;
684   snprintf(out,80, "    Flight Path Angle: %6.2f deg  Climb Rate: %5.0f ft/min\n",
685                     Position->GetGamma()*RADTODEG,
686                     Position->Gethdot()*60 );
687   cout << out;                  
688   snprintf(out,80, "    Normal Load Factor: %4.2f g's  Pitch Rate: %5.2f deg/s\n",
689                     Aerodynamics->GetNlf(),
690                     GetParameter(FG_PITCHRATE)*RADTODEG );
691   cout << out;
692   snprintf(out,80, "    Heading: %3.0f deg true  Sideslip: %5.2f deg\n",
693                     Rotation->Getpsi()*RADTODEG,
694                     GetParameter(FG_BETA)*RADTODEG );                  
695   cout << out;
696   snprintf(out,80, "    Bank Angle: %5.2f deg\n",
697                     Rotation->Getphi()*RADTODEG );
698   cout << out;
699   snprintf(out,80, "    Elevator: %5.2f deg  Left Aileron: %5.2f deg  Rudder: %5.2f deg\n",
700                     GetParameter(FG_ELEVATOR_POS)*RADTODEG,
701                     GetParameter(FG_AILERON_POS)*RADTODEG,
702                     GetParameter(FG_RUDDER_POS)*RADTODEG );
703   cout << out;                  
704   snprintf(out,80, "    Throttle: %5.2f%c\n",
705                     FCS->GetThrottlePos(0),'%' );
706   cout << out;
707   
708   snprintf(out,80, "    Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
709                     FDMExec->GetAuxiliary()->GetHeadWind()*jsbFPSTOKTS,
710                     FDMExec->GetAuxiliary()->GetCrossWind()*jsbFPSTOKTS );
711   cout << out; 
712   
713   snprintf(out,80, "    Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
714                     Position->GetVground()*jsbFPSTOKTS,
715                     Position->GetGroundTrack()*RADTODEG );
716   cout << out;                                   
717
718
719 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720
721 void FGState::Debug(void)
722 {
723     //TODO: Add your source code here
724 }
725