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