1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Called by: FGFDMExec and accessed by all models.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 # include <simgear/compiler.h>
43 # if defined(sgi) && !defined(__GNUC__)
51 #define snprintf _snprintf
56 static const char *IdSrc = "$Id$";
57 static const char *IdHdr = ID_STATE;
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 // For every term registered here there must be a corresponding handler in
72 // GetParameter() below that retrieves that parameter. Also, there must be an
73 // entry in the enum eParam definition in FGJSBBase.h. The ID is what must be used
74 // in any config file entry which references that item.
76 FGState::FGState(FGFDMExec* fdex)
85 Aircraft = FDMExec->GetAircraft();
86 Translation = FDMExec->GetTranslation();
87 Rotation = FDMExec->GetRotation();
88 Position = FDMExec->GetPosition();
89 FCS = FDMExec->GetFCS();
90 Output = FDMExec->GetOutput();
91 Atmosphere = FDMExec->GetAtmosphere();
92 Aerodynamics = FDMExec->GetAerodynamics();
93 GroundReactions = FDMExec->GetGroundReactions();
94 Propulsion = FDMExec->GetPropulsion();
96 RegisterVariable(FG_TIME, " time " );
97 RegisterVariable(FG_QBAR, " qbar " );
98 RegisterVariable(FG_WINGAREA, " wing_area " );
99 RegisterVariable(FG_WINGSPAN, " wingspan " );
100 RegisterVariable(FG_CBAR, " cbar " );
101 RegisterVariable(FG_ALPHA, " alpha " );
102 RegisterVariable(FG_ALPHADOT, " alphadot " );
103 RegisterVariable(FG_BETA, " beta " );
104 RegisterVariable(FG_ABETA, " |beta| " );
105 RegisterVariable(FG_BETADOT, " betadot " );
106 RegisterVariable(FG_PHI, " roll_angle " );
107 RegisterVariable(FG_THT, " pitch_angle " );
108 RegisterVariable(FG_PSI, " heading_angle " );
109 RegisterVariable(FG_PITCHRATE, " pitch_rate " );
110 RegisterVariable(FG_ROLLRATE, " roll_rate " );
111 RegisterVariable(FG_YAWRATE, " yaw_rate " );
112 RegisterVariable(FG_AEROQ, " aero_pitch_rate ");
113 RegisterVariable(FG_AEROP, " aero_roll_rate " );
114 RegisterVariable(FG_AEROR, " aero_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_AELEVATOR_POS, " |elevator_pos| " );
122 RegisterVariable(FG_AILERON_POS, " aileron_pos " );
123 RegisterVariable(FG_AAILERON_POS, " |aileron_pos| " );
124 RegisterVariable(FG_RUDDER_POS, " rudder_pos " );
125 RegisterVariable(FG_ARUDDER_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_GEAR_POS, " gear_pos " );
130 RegisterVariable(FG_ELEVATOR_CMD, " elevator_cmd " );
131 RegisterVariable(FG_AILERON_CMD, " aileron_cmd " );
132 RegisterVariable(FG_RUDDER_CMD, " rudder_cmd " );
133 RegisterVariable(FG_SPDBRAKE_CMD, " speedbrake_cmd " );
134 RegisterVariable(FG_SPOILERS_CMD, " spoiler_cmd " );
135 RegisterVariable(FG_FLAPS_CMD, " flaps_cmd " );
136 RegisterVariable(FG_THROTTLE_CMD, " throttle_cmd " );
137 RegisterVariable(FG_GEAR_CMD, " gear_cmd " );
138 RegisterVariable(FG_THROTTLE_POS, " throttle_pos " );
139 RegisterVariable(FG_MIXTURE_CMD, " mixture_cmd " );
140 RegisterVariable(FG_MIXTURE_POS, " mixture_pos " );
141 RegisterVariable(FG_MAGNETO_CMD, " magneto_cmd " );
142 RegisterVariable(FG_STARTER_CMD, " starter_cmd " );
143 RegisterVariable(FG_ACTIVE_ENGINE, " active_engine " );
144 RegisterVariable(FG_HOVERB, " height/span " );
145 RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
146 RegisterVariable(FG_YAW_TRIM_CMD, " yaw_trim_cmd " );
147 RegisterVariable(FG_ROLL_TRIM_CMD, " roll_trim_cmd " );
148 RegisterVariable(FG_LEFT_BRAKE_CMD, " left_brake_cmd " );
149 RegisterVariable(FG_RIGHT_BRAKE_CMD," right_brake_cmd ");
150 RegisterVariable(FG_CENTER_BRAKE_CMD," center_brake_cmd ");
151 RegisterVariable(FG_ALPHAH, " h-tail alpha " );
152 RegisterVariable(FG_ALPHAW, " wing alpha " );
153 RegisterVariable(FG_LBARH, " h-tail arm " );
154 RegisterVariable(FG_LBARV, " v-tail arm " );
155 RegisterVariable(FG_HTAILAREA, " h-tail area " );
156 RegisterVariable(FG_VTAILAREA, " v-tail area " );
157 RegisterVariable(FG_VBARH, " h-tail volume " );
158 RegisterVariable(FG_VBARV, " v-tail volume " );
159 RegisterVariable(FG_SET_LOGGING, " data_logging " );
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 double FGState::GetParameter(eParam val_idx) {
180 return Translation->Getqbar();
182 return Aircraft->GetWingArea();
184 return Aircraft->GetWingSpan();
186 return Aircraft->Getcbar();
188 return Aircraft->Getlbarh();
190 return Aircraft->Getvbarh();
192 return Aircraft->GetHTailArea();
194 return Aircraft->GetVTailArea();
196 return Aircraft->Getvbarh();
198 return Aircraft->Getvbarv();
200 return Translation->Getalpha();
202 return Translation->Getalpha() + Aircraft->GetWingIncidence();
204 return Translation->Getadot();
206 return Translation->Getbeta();
208 return fabs(Translation->Getbeta());
210 return Translation->Getbdot();
212 return Rotation->Getphi();
214 return Rotation->Gettht();
216 return Rotation->Getpsi();
218 return Rotation->GetPQR(eQ);
220 return Rotation->GetPQR(eP);
222 return Rotation->GetPQR(eR);
224 return Rotation->GetAeroPQR(eP);
226 return Rotation->GetAeroPQR(eQ);
228 return Rotation->GetAeroPQR(eR);
230 if (Translation->Getqbar() > 0.00)
231 scratch = Aerodynamics->GetvLastFs(eLift)/(Aircraft->GetWingArea()*Translation->Getqbar());
234 return scratch*scratch;
235 case FG_ELEVATOR_POS:
236 return FCS->GetDePos();
237 case FG_AELEVATOR_POS:
238 return fabs(FCS->GetDePos());
240 return FCS->GetDaPos();
241 case FG_AAILERON_POS:
242 return fabs(FCS->GetDaPos());
244 return FCS->GetDrPos();
246 return fabs(FCS->GetDrPos());
247 case FG_SPDBRAKE_POS:
248 return FCS->GetDsbPos();
249 case FG_SPOILERS_POS:
250 return FCS->GetDspPos();
252 return FCS->GetDfPos();
253 case FG_ELEVATOR_CMD:
254 return FCS->GetDeCmd();
256 return FCS->GetDaCmd();
258 return FCS->GetDrCmd();
259 case FG_SPDBRAKE_CMD:
260 return FCS->GetDsbCmd();
261 case FG_SPOILERS_CMD:
262 return FCS->GetDspCmd();
264 return FCS->GetDfCmd();
266 return Translation->GetMach();
268 return Position->Geth();
270 if(Translation->GetVt() > 0)
271 return Aircraft->GetWingSpan()/(2.0 * Translation->GetVt());
275 if(Translation->GetVt() > 0)
276 return Aircraft->Getcbar()/(2.0 * Translation->GetVt());
279 case FG_THROTTLE_CMD:
280 if (ActiveEngine < 0) return FCS->GetThrottleCmd(0);
281 else return FCS->GetThrottleCmd(ActiveEngine);
282 case FG_THROTTLE_POS:
283 if (ActiveEngine < 0) return FCS->GetThrottlePos(0);
284 else return FCS->GetThrottlePos(ActiveEngine);
286 if (ActiveEngine < 0) return Propulsion->GetEngine(0)->GetMagnetos();
287 else return Propulsion->GetEngine(ActiveEngine)->GetMagnetos();
289 if (ActiveEngine < 0) {
290 if (Propulsion->GetEngine(0)->GetStarter()) return 1.0;
293 if (Propulsion->GetEngine(ActiveEngine)->GetStarter()) return 1.0;
297 if (ActiveEngine < 0) return FCS->GetMixtureCmd(0);
298 else return FCS->GetMixtureCmd(ActiveEngine);
300 if (ActiveEngine < 0) return FCS->GetMixturePos(0);
301 else return FCS->GetMixturePos(ActiveEngine);
303 return Position->GetHOverBMAC();
304 case FG_PITCH_TRIM_CMD:
305 return FCS->GetPitchTrimCmd();
306 case FG_YAW_TRIM_CMD:
307 return FCS->GetYawTrimCmd();
308 case FG_ROLL_TRIM_CMD:
309 return FCS->GetRollTrimCmd();
311 return FCS->GetGearCmd();
313 return FCS->GetGearPos();
315 cerr << "FGState::GetParameter() - No handler for parameter " << paramdef[val_idx] << endl;
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 double FGState::GetParameter(string val_string) {
324 return GetParameter(coeffdef[val_string]);
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 eParam FGState::GetParameterIndex(string val_string)
331 return coeffdef[val_string];
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 void FGState::SetParameter(eParam val_idx, double val)
341 case FG_ELEVATOR_POS:
350 case FG_SPDBRAKE_POS:
353 case FG_SPOILERS_POS:
359 case FG_THROTTLE_POS:
360 if (ActiveEngine == -1) {
361 for (i=0; i<Propulsion->GetNumEngines(); i++) {
362 FCS->SetThrottlePos(i,val);
365 FCS->SetThrottlePos(ActiveEngine,val);
369 if (ActiveEngine == -1) {
370 for (i=0; i<Propulsion->GetNumEngines(); i++) {
371 FCS->SetMixturePos(i,val);
374 FCS->SetMixturePos(ActiveEngine,val);
378 case FG_ELEVATOR_CMD:
387 case FG_SPDBRAKE_CMD:
390 case FG_SPOILERS_CMD:
396 case FG_THROTTLE_CMD:
397 if (ActiveEngine == -1) {
398 for (i=0; i<Propulsion->GetNumEngines(); i++) {
399 FCS->SetThrottleCmd(i,val);
402 FCS->SetThrottleCmd(ActiveEngine,val);
406 if (ActiveEngine == -1) {
407 for (i=0; i<Propulsion->GetNumEngines(); i++) {
408 FCS->SetMixtureCmd(i,val);
411 FCS->SetMixtureCmd(ActiveEngine,val);
415 if (ActiveEngine == -1) {
416 for (i=0; i<Propulsion->GetNumEngines(); i++) {
417 Propulsion->GetEngine(i)->SetMagnetos((int)val);
420 Propulsion->GetEngine(ActiveEngine)->SetMagnetos((int)val);
424 if (ActiveEngine == -1) {
425 for (i=0; i<Propulsion->GetNumEngines(); i++) {
427 Propulsion->GetEngine(i)->SetStarter(false);
428 else if (val >= 0.001)
429 Propulsion->GetEngine(i)->SetStarter(true);
432 Propulsion->GetEngine(ActiveEngine)->SetStarter(true);
435 case FG_ACTIVE_ENGINE:
436 ActiveEngine = (int)val;
439 case FG_LEFT_BRAKE_CMD:
442 case FG_CENTER_BRAKE_CMD:
445 case FG_RIGHT_BRAKE_CMD:
449 FCS->SetGearCmd(val);
452 FCS->SetGearPos(val);
455 if (val < -0.01) Output->Disable();
456 else if (val > 0.01) Output->Enable();
457 else Output->Toggle();
461 cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
465 //***************************************************************************
467 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
470 bool FGState::Reset(string path, string acname, string fname)
476 double phi, tht, psi;
477 double latitude, longitude, h;
478 double wdir, wmag, wnorth, weast;
481 resetDef = path + "/" + acname + "/" + fname + ".xml";
483 resetDef = path + ";" + acname + ";" + fname + ".xml";
486 FGConfigFile resetfile(resetDef);
487 if (!resetfile.IsOpen()) return false;
489 resetfile.GetNextConfigLine();
490 token = resetfile.GetValue();
491 if (token != string("initialize")) {
492 cerr << "The reset file " << resetDef
493 << " does not appear to be a reset file" << endl;
496 resetfile.GetNextConfigLine();
498 cout << "Resetting using: " << token << endl << endl;
501 while (token != string("/initialize") && token != string("EOF")) {
502 if (token == "UBODY") resetfile >> U;
503 if (token == "VBODY") resetfile >> V;
504 if (token == "WBODY") resetfile >> W;
505 if (token == "LATITUDE") resetfile >> latitude;
506 if (token == "LONGITUDE") resetfile >> longitude;
507 if (token == "PHI") resetfile >> phi;
508 if (token == "THETA") resetfile >> tht;
509 if (token == "PSI") resetfile >> psi;
510 if (token == "ALTITUDE") resetfile >> h;
511 if (token == "WINDDIR") resetfile >> wdir;
512 if (token == "VWIND") resetfile >> wmag;
518 Position->SetLatitude(latitude*degtorad);
519 Position->SetLongitude(longitude*degtorad);
522 wnorth = wmag*ktstofps*cos(wdir*degtorad);
523 weast = wmag*ktstofps*sin(wdir*degtorad);
525 Initialize(U, V, W, phi*degtorad, tht*degtorad, psi*degtorad,
526 latitude*degtorad, longitude*degtorad, h, wnorth, weast, 0.0);
531 //***************************************************************************
533 // Initialize: Assume all angles GIVEN IN RADIANS !!
536 void FGState::Initialize(double U, double V, double W,
537 double phi, double tht, double psi,
538 double Latitude, double Longitude, double H,
539 double wnorth, double weast, double wdown)
543 FGColumnVector3 vAeroUVW;
545 Position->SetLatitude(Latitude);
546 Position->SetLongitude(Longitude);
551 vLocalEuler << phi << tht << psi;
552 Rotation->SetEuler(vLocalEuler);
554 InitMatrices(phi, tht, psi);
557 Translation->SetUVW(vUVW);
559 Atmosphere->SetWindNED(wnorth, weast, wdown);
561 vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
563 if (vAeroUVW(eW) != 0.0)
564 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
567 if (vAeroUVW(eV) != 0.0)
568 beta = vAeroUVW(eU)*vAeroUVW(eU)+vAeroUVW(eW)*vAeroUVW(eW) > 0.0 ? atan2(vAeroUVW(eV), (fabs(vAeroUVW(eU))/vAeroUVW(eU))*sqrt(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW))) : 0.0;
572 Translation->SetAB(alpha, beta);
574 Vt = sqrt(U*U + V*V + W*W);
575 Translation->SetVt(Vt);
577 Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
579 qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
580 Translation->Setqbar(qbar);
582 vLocalVelNED = mTb2l*vUVW;
583 Position->SetvVel(vLocalVelNED);
586 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 void FGState::Initialize(FGInitialCondition *FGIC) {
592 double latitude, longitude;
593 double wnorth,weast, wdown;
595 latitude = FGIC->GetLatitudeRadIC();
596 longitude = FGIC->GetLongitudeRadIC();
597 h = FGIC->GetAltitudeFtIC();
598 U = FGIC->GetUBodyFpsIC();
599 V = FGIC->GetVBodyFpsIC();
600 W = FGIC->GetWBodyFpsIC();
601 tht = FGIC->GetThetaRadIC();
602 phi = FGIC->GetPhiRadIC();
603 psi = FGIC->GetPsiRadIC();
604 wnorth = FGIC->GetWindNFpsIC();
605 weast = FGIC->GetWindEFpsIC();
606 wdown = FGIC->GetWindDFpsIC();
608 Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
609 Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
610 FGIC->GetTerrainAltitudeFtIC() );
612 // need to fix the wind speed args, here.
613 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
616 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618 bool FGState::StoreData(string fname) {
619 ofstream datafile(fname.c_str());
622 datafile << Translation->GetUVW(eU);
623 datafile << Translation->GetUVW(eV);
624 datafile << Translation->GetUVW(eW);
625 datafile << Position->GetLatitude();
626 datafile << Position->GetLongitude();
627 datafile << Rotation->GetEuler(ePhi);
628 datafile << Rotation->GetEuler(eTht);
629 datafile << Rotation->GetEuler(ePsi);
630 datafile << Position->Geth();
634 cerr << "Could not open dump file " << fname << endl;
639 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 void FGState::InitMatrices(double phi, double tht, double psi) {
642 double thtd2, psid2, phid2;
643 double Sthtd2, Spsid2, Sphid2;
644 double Cthtd2, Cpsid2, Cphid2;
662 Cphid2Cthtd2 = Cphid2*Cthtd2;
663 Cphid2Sthtd2 = Cphid2*Sthtd2;
664 Sphid2Sthtd2 = Sphid2*Sthtd2;
665 Sphid2Cthtd2 = Sphid2*Cthtd2;
667 vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
668 vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
669 vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
670 vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
675 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 void FGState::CalcMatrices(void) {
678 double Q0Q0, Q1Q1, Q2Q2, Q3Q3;
679 double Q0Q1, Q0Q2, Q0Q3, Q1Q2;
682 Q0Q0 = vQtrn(1)*vQtrn(1);
683 Q1Q1 = vQtrn(2)*vQtrn(2);
684 Q2Q2 = vQtrn(3)*vQtrn(3);
685 Q3Q3 = vQtrn(4)*vQtrn(4);
686 Q0Q1 = vQtrn(1)*vQtrn(2);
687 Q0Q2 = vQtrn(1)*vQtrn(3);
688 Q0Q3 = vQtrn(1)*vQtrn(4);
689 Q1Q2 = vQtrn(2)*vQtrn(3);
690 Q1Q3 = vQtrn(2)*vQtrn(4);
691 Q2Q3 = vQtrn(3)*vQtrn(4);
693 mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
694 mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
695 mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
696 mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
697 mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
698 mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
699 mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
700 mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
701 mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
707 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate) {
710 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
711 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
712 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
713 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
714 vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
721 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 FGColumnVector3& FGState::CalcEuler(void) {
724 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
725 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
727 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
728 vEuler(eTht) = asin(-mTl2b(1,3));
729 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
731 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
736 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738 FGMatrix33& FGState::GetTs2b(void)
740 double ca, cb, sa, sb;
742 double alpha = Translation->Getalpha();
743 double beta = Translation->Getbeta();
763 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 FGMatrix33& FGState::GetTb2s(void)
768 float ca, cb, sa, sb;
770 alpha = Translation->Getalpha();
771 beta = Translation->Getbeta();
791 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 void FGState::ReportState(void) {
794 char out[80], flap[10], gear[12];
796 cout << endl << " JSBSim State" << endl;
797 snprintf(out,80," Weight: %7.0f lbs. CG: %5.1f, %5.1f, %5.1f inches\n",
798 FDMExec->GetMassBalance()->GetWeight(),
799 FDMExec->GetMassBalance()->GetXYZcg(1),
800 FDMExec->GetMassBalance()->GetXYZcg(2),
801 FDMExec->GetMassBalance()->GetXYZcg(3));
803 if( FCS->GetDfPos() <= 0.01)
804 snprintf(flap,10,"Up");
806 snprintf(flap,10,"%2.0f",FCS->GetDfPos());
807 if(FCS->GetGearPos() < 0.01)
808 snprintf(gear,12,"Up");
809 else if(FCS->GetGearPos() > 0.99)
810 snprintf(gear,12,"Down");
812 snprintf(gear,12,"In Transit");
813 snprintf(out,80, " Flaps: %3s Gear: %12s\n",flap,gear);
815 snprintf(out,80, " Speed: %4.0f KCAS Mach: %5.2f\n",
816 FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
817 GetParameter(FG_MACH) );
819 snprintf(out,80, " Altitude: %7.0f ft. AGL Altitude: %7.0f ft.\n",
821 Position->GetDistanceAGL() );
823 snprintf(out,80, " Angle of Attack: %6.2f deg Pitch Angle: %6.2f deg\n",
824 GetParameter(FG_ALPHA)*radtodeg,
825 Rotation->Gettht()*radtodeg );
827 snprintf(out,80, " Flight Path Angle: %6.2f deg Climb Rate: %5.0f ft/min\n",
828 Position->GetGamma()*radtodeg,
829 Position->Gethdot()*60 );
831 snprintf(out,80, " Normal Load Factor: %4.2f g's Pitch Rate: %5.2f deg/s\n",
833 GetParameter(FG_PITCHRATE)*radtodeg );
835 snprintf(out,80, " Heading: %3.0f deg true Sideslip: %5.2f deg\n",
836 Rotation->Getpsi()*radtodeg,
837 GetParameter(FG_BETA)*radtodeg );
839 snprintf(out,80, " Bank Angle: %5.2f deg\n",
840 Rotation->Getphi()*radtodeg );
842 snprintf(out,80, " Elevator: %5.2f deg Left Aileron: %5.2f deg Rudder: %5.2f deg\n",
843 GetParameter(FG_ELEVATOR_POS)*radtodeg,
844 GetParameter(FG_AILERON_POS)*radtodeg,
845 GetParameter(FG_RUDDER_POS)*radtodeg );
847 snprintf(out,80, " Throttle: %5.2f%c\n",
848 FCS->GetThrottlePos(0)*100,'%' );
851 snprintf(out,80, " Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
852 FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
853 FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
856 snprintf(out,80, " Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
857 Position->GetVground()*fpstokts,
858 Position->GetGroundTrack()*radtodeg );
862 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 // The bitmasked value choices are as follows:
864 // unset: In this case (the default) JSBSim would only print
865 // out the normally expected messages, essentially echoing
866 // the config files as they are read. If the environment
867 // variable is not set, debug_lvl is set to 1 internally
868 // 0: This requests JSBSim not to output any messages
870 // 1: This value explicity requests the normal JSBSim
872 // 2: This value asks for a message to be printed out when
873 // a class is instantiated
874 // 4: When this value is set, a message is displayed when a
875 // FGModel object executes its Run() method
876 // 8: When this value is set, various runtime state variables
877 // are printed out periodically
878 // 16: When set various parameters are sanity checked and
879 // a message is printed out when they go out of bounds
881 void FGState::Debug(int from)
883 if (debug_lvl <= 0) return;
885 if (debug_lvl & 1) { // Standard console startup message output
886 if (from == 0) { // Constructor
890 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
891 if (from == 0) cout << "Instantiated: FGState" << endl;
892 if (from == 1) cout << "Destroyed: FGState" << endl;
894 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
896 if (debug_lvl & 8 ) { // Runtime state variables
898 if (debug_lvl & 16) { // Sanity checking
900 if (debug_lvl & 64) {
901 if (from == 0) { // Constructor
902 cout << IdSrc << endl;
903 cout << IdHdr << endl;