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_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_GEAR_POS, " gear_pos " );
127 RegisterVariable(FG_ELEVATOR_CMD, " elevator_cmd " );
128 RegisterVariable(FG_AILERON_CMD, " aileron_cmd " );
129 RegisterVariable(FG_RUDDER_CMD, " rudder_cmd " );
130 RegisterVariable(FG_SPDBRAKE_CMD, " speedbrake_cmd " );
131 RegisterVariable(FG_SPOILERS_CMD, " spoiler_cmd " );
132 RegisterVariable(FG_FLAPS_CMD, " flaps_cmd " );
133 RegisterVariable(FG_THROTTLE_CMD, " throttle_cmd " );
134 RegisterVariable(FG_GEAR_CMD, " gear_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_YAW_TRIM_CMD, " yaw_trim_cmd " );
144 RegisterVariable(FG_ROLL_TRIM_CMD, " roll_trim_cmd " );
145 RegisterVariable(FG_LEFT_BRAKE_CMD, " left_brake_cmd " );
146 RegisterVariable(FG_RIGHT_BRAKE_CMD," right_brake_cmd ");
147 RegisterVariable(FG_CENTER_BRAKE_CMD," center_brake_cmd ");
148 RegisterVariable(FG_ALPHAH, " h-tail alpha " );
149 RegisterVariable(FG_ALPHAW, " wing alpha " );
150 RegisterVariable(FG_LBARH, " h-tail arm " );
151 RegisterVariable(FG_LBARV, " v-tail arm " );
152 RegisterVariable(FG_HTAILAREA, " h-tail area " );
153 RegisterVariable(FG_VTAILAREA, " v-tail area " );
154 RegisterVariable(FG_VBARH, " h-tail volume " );
155 RegisterVariable(FG_VBARV, " v-tail volume " );
156 RegisterVariable(FG_SET_LOGGING, " data_logging " );
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 double FGState::GetParameter(eParam val_idx) {
177 return Translation->Getqbar();
179 return Aircraft->GetWingArea();
181 return Aircraft->GetWingSpan();
183 return Aircraft->Getcbar();
185 return Aircraft->Getlbarh();
187 return Aircraft->Getvbarh();
189 return Aircraft->GetHTailArea();
191 return Aircraft->GetVTailArea();
193 return Aircraft->Getvbarh();
195 return Aircraft->Getvbarv();
197 return Translation->Getalpha();
199 return Translation->Getalpha() + Aircraft->GetWingIncidence();
201 return Translation->Getadot();
203 return Translation->Getbeta();
205 return fabs(Translation->Getbeta());
207 return Translation->Getbdot();
209 return Rotation->Getphi();
211 return Rotation->Gettht();
213 return Rotation->Getpsi();
215 return Rotation->GetPQR(eQ);
217 return Rotation->GetPQR(eP);
219 return Rotation->GetPQR(eR);
221 return Rotation->GetAeroPQR(eP);
223 return Rotation->GetAeroPQR(eQ);
225 return Rotation->GetAeroPQR(eR);
227 if (Translation->Getqbar() > 0.00)
228 scratch = Aerodynamics->GetvLastFs(eLift)/(Aircraft->GetWingArea()*Translation->Getqbar());
231 return scratch*scratch;
232 case FG_ELEVATOR_POS:
233 return FCS->GetDePos();
235 return FCS->GetDaPos();
237 return FCS->GetDrPos();
238 case FG_SPDBRAKE_POS:
239 return FCS->GetDsbPos();
240 case FG_SPOILERS_POS:
241 return FCS->GetDspPos();
243 return FCS->GetDfPos();
244 case FG_ELEVATOR_CMD:
245 return FCS->GetDeCmd();
247 return FCS->GetDaCmd();
249 return FCS->GetDrCmd();
250 case FG_SPDBRAKE_CMD:
251 return FCS->GetDsbCmd();
252 case FG_SPOILERS_CMD:
253 return FCS->GetDspCmd();
255 return FCS->GetDfCmd();
257 return Translation->GetMach();
259 return Position->Geth();
261 if(Translation->GetVt() > 0)
262 return Aircraft->GetWingSpan()/(2.0 * Translation->GetVt());
266 if(Translation->GetVt() > 0)
267 return Aircraft->Getcbar()/(2.0 * Translation->GetVt());
270 case FG_THROTTLE_CMD:
271 if (ActiveEngine < 0) return FCS->GetThrottleCmd(0);
272 else return FCS->GetThrottleCmd(ActiveEngine);
273 case FG_THROTTLE_POS:
274 if (ActiveEngine < 0) return FCS->GetThrottlePos(0);
275 else return FCS->GetThrottlePos(ActiveEngine);
277 if (ActiveEngine < 0) return Propulsion->GetEngine(0)->GetMagnetos();
278 else return Propulsion->GetEngine(ActiveEngine)->GetMagnetos();
280 if (ActiveEngine < 0) {
281 if (Propulsion->GetEngine(0)->GetStarter()) return 1.0;
284 if (Propulsion->GetEngine(ActiveEngine)->GetStarter()) return 1.0;
288 if (ActiveEngine < 0) return FCS->GetMixtureCmd(0);
289 else return FCS->GetMixtureCmd(ActiveEngine);
291 if (ActiveEngine < 0) return FCS->GetMixturePos(0);
292 else return FCS->GetMixturePos(ActiveEngine);
294 return Position->GetHOverBMAC();
295 case FG_PITCH_TRIM_CMD:
296 return FCS->GetPitchTrimCmd();
297 case FG_YAW_TRIM_CMD:
298 return FCS->GetYawTrimCmd();
299 case FG_ROLL_TRIM_CMD:
300 return FCS->GetRollTrimCmd();
302 return FCS->GetGearCmd();
304 return FCS->GetGearPos();
306 cerr << "FGState::GetParameter() - No handler for parameter " << paramdef[val_idx] << endl;
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 double FGState::GetParameter(string val_string) {
315 return GetParameter(coeffdef[val_string]);
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 eParam FGState::GetParameterIndex(string val_string)
322 return coeffdef[val_string];
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 void FGState::SetParameter(eParam val_idx, double val)
332 case FG_ELEVATOR_POS:
341 case FG_SPDBRAKE_POS:
344 case FG_SPOILERS_POS:
350 case FG_THROTTLE_POS:
351 if (ActiveEngine == -1) {
352 for (i=0; i<Propulsion->GetNumEngines(); i++) {
353 FCS->SetThrottlePos(i,val);
356 FCS->SetThrottlePos(ActiveEngine,val);
360 if (ActiveEngine == -1) {
361 for (i=0; i<Propulsion->GetNumEngines(); i++) {
362 FCS->SetMixturePos(i,val);
365 FCS->SetMixturePos(ActiveEngine,val);
369 case FG_ELEVATOR_CMD:
378 case FG_SPDBRAKE_CMD:
381 case FG_SPOILERS_CMD:
387 case FG_THROTTLE_CMD:
388 if (ActiveEngine == -1) {
389 for (i=0; i<Propulsion->GetNumEngines(); i++) {
390 FCS->SetThrottleCmd(i,val);
393 FCS->SetThrottleCmd(ActiveEngine,val);
397 if (ActiveEngine == -1) {
398 for (i=0; i<Propulsion->GetNumEngines(); i++) {
399 FCS->SetMixtureCmd(i,val);
402 FCS->SetMixtureCmd(ActiveEngine,val);
406 if (ActiveEngine == -1) {
407 for (i=0; i<Propulsion->GetNumEngines(); i++) {
408 Propulsion->GetEngine(i)->SetMagnetos((int)val);
411 Propulsion->GetEngine(ActiveEngine)->SetMagnetos((int)val);
415 if (ActiveEngine == -1) {
416 for (i=0; i<Propulsion->GetNumEngines(); i++) {
418 Propulsion->GetEngine(i)->SetStarter(false);
419 else if (val >= 0.001)
420 Propulsion->GetEngine(i)->SetStarter(true);
423 Propulsion->GetEngine(ActiveEngine)->SetStarter(true);
426 case FG_ACTIVE_ENGINE:
427 ActiveEngine = (int)val;
430 case FG_LEFT_BRAKE_CMD:
433 case FG_CENTER_BRAKE_CMD:
436 case FG_RIGHT_BRAKE_CMD:
440 FCS->SetGearCmd(val);
443 FCS->SetGearPos(val);
446 if (val < -0.01) Output->Disable();
447 else if (val > 0.01) Output->Enable();
448 else Output->Toggle();
452 cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
456 //***************************************************************************
458 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
461 bool FGState::Reset(string path, string acname, string fname)
467 double phi, tht, psi;
468 double latitude, longitude, h;
469 double wdir, wmag, wnorth, weast;
472 resetDef = path + "/" + acname + "/" + fname + ".xml";
474 resetDef = path + ";" + acname + ";" + fname + ".xml";
477 FGConfigFile resetfile(resetDef);
478 if (!resetfile.IsOpen()) return false;
480 resetfile.GetNextConfigLine();
481 token = resetfile.GetValue();
482 if (token != string("initialize")) {
483 cerr << "The reset file " << resetDef
484 << " does not appear to be a reset file" << endl;
487 resetfile.GetNextConfigLine();
489 cout << "Resetting using: " << token << endl << endl;
492 while (token != string("/initialize") && token != string("EOF")) {
493 if (token == "UBODY") resetfile >> U;
494 if (token == "VBODY") resetfile >> V;
495 if (token == "WBODY") resetfile >> W;
496 if (token == "LATITUDE") resetfile >> latitude;
497 if (token == "LONGITUDE") resetfile >> longitude;
498 if (token == "PHI") resetfile >> phi;
499 if (token == "THETA") resetfile >> tht;
500 if (token == "PSI") resetfile >> psi;
501 if (token == "ALTITUDE") resetfile >> h;
502 if (token == "WINDDIR") resetfile >> wdir;
503 if (token == "VWIND") resetfile >> wmag;
509 Position->SetLatitude(latitude*degtorad);
510 Position->SetLongitude(longitude*degtorad);
513 wnorth = wmag*ktstofps*cos(wdir*degtorad);
514 weast = wmag*ktstofps*sin(wdir*degtorad);
516 Initialize(U, V, W, phi*degtorad, tht*degtorad, psi*degtorad,
517 latitude*degtorad, longitude*degtorad, h, wnorth, weast, 0.0);
522 //***************************************************************************
524 // Initialize: Assume all angles GIVEN IN RADIANS !!
527 void FGState::Initialize(double U, double V, double W,
528 double phi, double tht, double psi,
529 double Latitude, double Longitude, double H,
530 double wnorth, double weast, double wdown)
534 FGColumnVector3 vAeroUVW;
536 Position->SetLatitude(Latitude);
537 Position->SetLongitude(Longitude);
542 vLocalEuler << phi << tht << psi;
543 Rotation->SetEuler(vLocalEuler);
545 InitMatrices(phi, tht, psi);
548 Translation->SetUVW(vUVW);
550 Atmosphere->SetWindNED(wnorth, weast, wdown);
552 vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
554 if (vAeroUVW(eW) != 0.0)
555 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
558 if (vAeroUVW(eV) != 0.0)
559 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;
563 Translation->SetAB(alpha, beta);
565 Vt = sqrt(U*U + V*V + W*W);
566 Translation->SetVt(Vt);
568 Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
570 qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
571 Translation->Setqbar(qbar);
573 vLocalVelNED = mTb2l*vUVW;
574 Position->SetvVel(vLocalVelNED);
577 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 void FGState::Initialize(FGInitialCondition *FGIC) {
583 double latitude, longitude;
584 double wnorth,weast, wdown;
586 latitude = FGIC->GetLatitudeRadIC();
587 longitude = FGIC->GetLongitudeRadIC();
588 h = FGIC->GetAltitudeFtIC();
589 U = FGIC->GetUBodyFpsIC();
590 V = FGIC->GetVBodyFpsIC();
591 W = FGIC->GetWBodyFpsIC();
592 tht = FGIC->GetThetaRadIC();
593 phi = FGIC->GetPhiRadIC();
594 psi = FGIC->GetPsiRadIC();
595 wnorth = FGIC->GetWindNFpsIC();
596 weast = FGIC->GetWindEFpsIC();
597 wdown = FGIC->GetWindDFpsIC();
599 Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
600 Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
601 FGIC->GetTerrainAltitudeFtIC() );
603 // need to fix the wind speed args, here.
604 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
607 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609 bool FGState::StoreData(string fname) {
610 ofstream datafile(fname.c_str());
613 datafile << Translation->GetUVW(eU);
614 datafile << Translation->GetUVW(eV);
615 datafile << Translation->GetUVW(eW);
616 datafile << Position->GetLatitude();
617 datafile << Position->GetLongitude();
618 datafile << Rotation->GetEuler(ePhi);
619 datafile << Rotation->GetEuler(eTht);
620 datafile << Rotation->GetEuler(ePsi);
621 datafile << Position->Geth();
625 cerr << "Could not open dump file " << fname << endl;
630 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 void FGState::InitMatrices(double phi, double tht, double psi) {
633 double thtd2, psid2, phid2;
634 double Sthtd2, Spsid2, Sphid2;
635 double Cthtd2, Cpsid2, Cphid2;
653 Cphid2Cthtd2 = Cphid2*Cthtd2;
654 Cphid2Sthtd2 = Cphid2*Sthtd2;
655 Sphid2Sthtd2 = Sphid2*Sthtd2;
656 Sphid2Cthtd2 = Sphid2*Cthtd2;
658 vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
659 vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
660 vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
661 vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
666 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 void FGState::CalcMatrices(void) {
669 double Q0Q0, Q1Q1, Q2Q2, Q3Q3;
670 double Q0Q1, Q0Q2, Q0Q3, Q1Q2;
673 Q0Q0 = vQtrn(1)*vQtrn(1);
674 Q1Q1 = vQtrn(2)*vQtrn(2);
675 Q2Q2 = vQtrn(3)*vQtrn(3);
676 Q3Q3 = vQtrn(4)*vQtrn(4);
677 Q0Q1 = vQtrn(1)*vQtrn(2);
678 Q0Q2 = vQtrn(1)*vQtrn(3);
679 Q0Q3 = vQtrn(1)*vQtrn(4);
680 Q1Q2 = vQtrn(2)*vQtrn(3);
681 Q1Q3 = vQtrn(2)*vQtrn(4);
682 Q2Q3 = vQtrn(3)*vQtrn(4);
684 mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
685 mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
686 mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
687 mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
688 mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
689 mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
690 mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
691 mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
692 mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
698 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate) {
701 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
702 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
703 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
704 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
705 vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
712 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714 FGColumnVector3& FGState::CalcEuler(void) {
715 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
716 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
718 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
719 vEuler(eTht) = asin(-mTl2b(1,3));
720 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
722 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
727 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 FGMatrix33& FGState::GetTs2b(void)
731 double ca, cb, sa, sb;
733 double alpha = Translation->Getalpha();
734 double beta = Translation->Getbeta();
754 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 FGMatrix33& FGState::GetTb2s(void)
759 float ca, cb, sa, sb;
761 alpha = Translation->Getalpha();
762 beta = Translation->Getbeta();
782 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 void FGState::ReportState(void) {
785 char out[80], flap[10], gear[12];
787 cout << endl << " JSBSim State" << endl;
788 snprintf(out,80," Weight: %7.0f lbs. CG: %5.1f, %5.1f, %5.1f inches\n",
789 FDMExec->GetMassBalance()->GetWeight(),
790 FDMExec->GetMassBalance()->GetXYZcg(1),
791 FDMExec->GetMassBalance()->GetXYZcg(2),
792 FDMExec->GetMassBalance()->GetXYZcg(3));
794 if( FCS->GetDfPos() <= 0.01)
795 snprintf(flap,10,"Up");
797 snprintf(flap,10,"%2.0f",FCS->GetDfPos());
798 if(FCS->GetGearPos() < 0.01)
799 snprintf(gear,12,"Up");
800 else if(FCS->GetGearPos() > 0.99)
801 snprintf(gear,12,"Down");
803 snprintf(gear,12,"In Transit");
804 snprintf(out,80, " Flaps: %3s Gear: %12s\n",flap,gear);
806 snprintf(out,80, " Speed: %4.0f KCAS Mach: %5.2f\n",
807 FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
808 GetParameter(FG_MACH) );
810 snprintf(out,80, " Altitude: %7.0f ft. AGL Altitude: %7.0f ft.\n",
812 Position->GetDistanceAGL() );
814 snprintf(out,80, " Angle of Attack: %6.2f deg Pitch Angle: %6.2f deg\n",
815 GetParameter(FG_ALPHA)*radtodeg,
816 Rotation->Gettht()*radtodeg );
818 snprintf(out,80, " Flight Path Angle: %6.2f deg Climb Rate: %5.0f ft/min\n",
819 Position->GetGamma()*radtodeg,
820 Position->Gethdot()*60 );
822 snprintf(out,80, " Normal Load Factor: %4.2f g's Pitch Rate: %5.2f deg/s\n",
824 GetParameter(FG_PITCHRATE)*radtodeg );
826 snprintf(out,80, " Heading: %3.0f deg true Sideslip: %5.2f deg\n",
827 Rotation->Getpsi()*radtodeg,
828 GetParameter(FG_BETA)*radtodeg );
830 snprintf(out,80, " Bank Angle: %5.2f deg\n",
831 Rotation->Getphi()*radtodeg );
833 snprintf(out,80, " Elevator: %5.2f deg Left Aileron: %5.2f deg Rudder: %5.2f deg\n",
834 GetParameter(FG_ELEVATOR_POS)*radtodeg,
835 GetParameter(FG_AILERON_POS)*radtodeg,
836 GetParameter(FG_RUDDER_POS)*radtodeg );
838 snprintf(out,80, " Throttle: %5.2f%c\n",
839 FCS->GetThrottlePos(0)*100,'%' );
842 snprintf(out,80, " Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
843 FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
844 FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
847 snprintf(out,80, " Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
848 Position->GetVground()*fpstokts,
849 Position->GetGroundTrack()*radtodeg );
853 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 // The bitmasked value choices are as follows:
855 // unset: In this case (the default) JSBSim would only print
856 // out the normally expected messages, essentially echoing
857 // the config files as they are read. If the environment
858 // variable is not set, debug_lvl is set to 1 internally
859 // 0: This requests JSBSim not to output any messages
861 // 1: This value explicity requests the normal JSBSim
863 // 2: This value asks for a message to be printed out when
864 // a class is instantiated
865 // 4: When this value is set, a message is displayed when a
866 // FGModel object executes its Run() method
867 // 8: When this value is set, various runtime state variables
868 // are printed out periodically
869 // 16: When set various parameters are sanity checked and
870 // a message is printed out when they go out of bounds
872 void FGState::Debug(int from)
874 if (debug_lvl <= 0) return;
876 if (debug_lvl & 1) { // Standard console startup message output
877 if (from == 0) { // Constructor
881 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
882 if (from == 0) cout << "Instantiated: FGState" << endl;
883 if (from == 1) cout << "Destroyed: FGState" << endl;
885 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
887 if (debug_lvl & 8 ) { // Runtime state variables
889 if (debug_lvl & 16) { // Sanity checking