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