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_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 " );
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 double FGState::GetParameter(eParam val_idx) {
175 return Translation->Getqbar();
177 return Aircraft->GetWingArea();
179 return Aircraft->GetWingSpan();
181 return Aircraft->Getcbar();
183 return Aircraft->Getlbarh();
185 return Aircraft->Getvbarh();
187 return Aircraft->GetHTailArea();
189 return Aircraft->GetVTailArea();
191 return Aircraft->Getvbarh();
193 return Aircraft->Getvbarv();
195 return Translation->Getalpha();
197 return Translation->Getalpha() + Aircraft->GetWingIncidence();
199 return Translation->Getadot();
201 return Translation->Getbeta();
203 return fabs(Translation->Getbeta());
205 return Translation->Getbdot();
207 return Rotation->Getphi();
209 return Rotation->Gettht();
211 return Rotation->Getpsi();
213 return Rotation->GetPQR(eQ);
215 return Rotation->GetPQR(eP);
217 return Rotation->GetPQR(eR);
219 return Rotation->GetAeroPQR(eP);
221 return Rotation->GetAeroPQR(eQ);
223 return Rotation->GetAeroPQR(eR);
225 if (Translation->Getqbar() > 0.00)
226 scratch = Aerodynamics->GetvLastFs(eLift)/(Aircraft->GetWingArea()*Translation->Getqbar());
229 return scratch*scratch;
230 case FG_ELEVATOR_POS:
231 return FCS->GetDePos();
233 return FCS->GetDaPos();
235 return FCS->GetDrPos();
236 case FG_SPDBRAKE_POS:
237 return FCS->GetDsbPos();
238 case FG_SPOILERS_POS:
239 return FCS->GetDspPos();
241 return FCS->GetDfPos();
242 case FG_ELEVATOR_CMD:
243 return FCS->GetDeCmd();
245 return FCS->GetDaCmd();
247 return FCS->GetDrCmd();
248 case FG_SPDBRAKE_CMD:
249 return FCS->GetDsbCmd();
250 case FG_SPOILERS_CMD:
251 return FCS->GetDspCmd();
253 return FCS->GetDfCmd();
255 return Translation->GetMach();
257 return Position->Geth();
259 if(Translation->GetVt() > 0)
260 return Aircraft->GetWingSpan()/(2.0 * Translation->GetVt());
264 if(Translation->GetVt() > 0)
265 return Aircraft->Getcbar()/(2.0 * Translation->GetVt());
268 case FG_THROTTLE_CMD:
269 if (ActiveEngine < 0) return FCS->GetThrottleCmd(0);
270 else return FCS->GetThrottleCmd(ActiveEngine);
271 case FG_THROTTLE_POS:
272 if (ActiveEngine < 0) return FCS->GetThrottlePos(0);
273 else return FCS->GetThrottlePos(ActiveEngine);
275 if (ActiveEngine < 0) return Propulsion->GetEngine(0)->GetMagnetos();
276 else return Propulsion->GetEngine(ActiveEngine)->GetMagnetos();
278 if (ActiveEngine < 0) {
279 if (Propulsion->GetEngine(0)->GetStarter()) return 1.0;
282 if (Propulsion->GetEngine(ActiveEngine)->GetStarter()) return 1.0;
286 if (ActiveEngine < 0) return FCS->GetMixtureCmd(0);
287 else return FCS->GetMixtureCmd(ActiveEngine);
289 if (ActiveEngine < 0) return FCS->GetMixturePos(0);
290 else return FCS->GetMixturePos(ActiveEngine);
292 return Position->GetHOverBMAC();
293 case FG_PITCH_TRIM_CMD:
294 return FCS->GetPitchTrimCmd();
296 return FCS->GetGearCmd();
298 return FCS->GetGearPos();
300 cerr << "FGState::GetParameter() - No handler for parameter " << paramdef[val_idx] << endl;
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 double FGState::GetParameter(string val_string) {
309 return GetParameter(coeffdef[val_string]);
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 eParam FGState::GetParameterIndex(string val_string)
316 return coeffdef[val_string];
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 void FGState::SetParameter(eParam val_idx, double val)
326 case FG_ELEVATOR_POS:
335 case FG_SPDBRAKE_POS:
338 case FG_SPOILERS_POS:
344 case FG_THROTTLE_POS:
345 if (ActiveEngine == -1) {
346 for (i=0; i<Propulsion->GetNumEngines(); i++) {
347 FCS->SetThrottlePos(i,val);
350 FCS->SetThrottlePos(ActiveEngine,val);
354 if (ActiveEngine == -1) {
355 for (i=0; i<Propulsion->GetNumEngines(); i++) {
356 FCS->SetMixturePos(i,val);
359 FCS->SetMixturePos(ActiveEngine,val);
363 case FG_ELEVATOR_CMD:
372 case FG_SPDBRAKE_CMD:
375 case FG_SPOILERS_CMD:
381 case FG_THROTTLE_CMD:
382 if (ActiveEngine == -1) {
383 for (i=0; i<Propulsion->GetNumEngines(); i++) {
384 FCS->SetThrottleCmd(i,val);
387 FCS->SetThrottleCmd(ActiveEngine,val);
391 if (ActiveEngine == -1) {
392 for (i=0; i<Propulsion->GetNumEngines(); i++) {
393 FCS->SetMixtureCmd(i,val);
396 FCS->SetMixtureCmd(ActiveEngine,val);
400 if (ActiveEngine == -1) {
401 for (i=0; i<Propulsion->GetNumEngines(); i++) {
402 Propulsion->GetEngine(i)->SetMagnetos((int)val);
405 Propulsion->GetEngine(ActiveEngine)->SetMagnetos((int)val);
409 if (ActiveEngine == -1) {
410 for (i=0; i<Propulsion->GetNumEngines(); i++) {
412 Propulsion->GetEngine(i)->SetStarter(false);
413 else if (val >= 0.001)
414 Propulsion->GetEngine(i)->SetStarter(true);
417 Propulsion->GetEngine(ActiveEngine)->SetStarter(true);
420 case FG_ACTIVE_ENGINE:
421 ActiveEngine = (int)val;
424 case FG_LEFT_BRAKE_CMD:
427 case FG_CENTER_BRAKE_CMD:
430 case FG_RIGHT_BRAKE_CMD:
434 FCS->SetGearCmd(val);
437 FCS->SetGearPos(val);
440 if (val < -0.01) Output->Disable();
441 else if (val > 0.01) Output->Enable();
442 else Output->Toggle();
446 cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
450 //***************************************************************************
452 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
455 bool FGState::Reset(string path, string acname, string fname)
461 double phi, tht, psi;
462 double latitude, longitude, h;
463 double wdir, wmag, wnorth, weast;
466 resetDef = path + "/" + acname + "/" + fname + ".xml";
468 resetDef = path + ";" + acname + ";" + fname + ".xml";
471 FGConfigFile resetfile(resetDef);
472 if (!resetfile.IsOpen()) return false;
474 resetfile.GetNextConfigLine();
475 token = resetfile.GetValue();
476 if (token != string("initialize")) {
477 cerr << "The reset file " << resetDef
478 << " does not appear to be a reset file" << endl;
482 resetfile.GetNextConfigLine();
484 while (token != string("/initialize") && token != string("EOF")) {
485 if (token == "UBODY") resetfile >> U;
486 if (token == "VBODY") resetfile >> V;
487 if (token == "WBODY") resetfile >> W;
488 if (token == "LATITUDE") resetfile >> latitude;
489 if (token == "LONGITUDE") resetfile >> longitude;
490 if (token == "PHI") resetfile >> phi;
491 if (token == "THETA") resetfile >> tht;
492 if (token == "PSI") resetfile >> psi;
493 if (token == "ALTITUDE") resetfile >> h;
494 if (token == "WINDDIR") resetfile >> wdir;
495 if (token == "VWIND") resetfile >> wmag;
501 Position->SetLatitude(latitude*degtorad);
502 Position->SetLongitude(longitude*degtorad);
505 wnorth = wmag*ktstofps*cos(wdir*degtorad);
506 weast = wmag*ktstofps*sin(wdir*degtorad);
508 Initialize(U, V, W, phi*degtorad, tht*degtorad, psi*degtorad,
509 latitude*degtorad, longitude*degtorad, h, wnorth, weast, 0.0);
514 //***************************************************************************
516 // Initialize: Assume all angles GIVEN IN RADIANS !!
519 void FGState::Initialize(double U, double V, double W,
520 double phi, double tht, double psi,
521 double Latitude, double Longitude, double H,
522 double wnorth, double weast, double wdown)
526 FGColumnVector3 vAeroUVW;
528 Position->SetLatitude(Latitude);
529 Position->SetLongitude(Longitude);
534 vLocalEuler << phi << tht << psi;
535 Rotation->SetEuler(vLocalEuler);
537 InitMatrices(phi, tht, psi);
540 Translation->SetUVW(vUVW);
542 Atmosphere->SetWindNED(wnorth, weast, wdown);
544 vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
546 if (vAeroUVW(eW) != 0.0)
547 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
550 if (vAeroUVW(eV) != 0.0)
551 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;
555 Translation->SetAB(alpha, beta);
557 Vt = sqrt(U*U + V*V + W*W);
558 Translation->SetVt(Vt);
560 Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
562 qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
563 Translation->Setqbar(qbar);
565 vLocalVelNED = mTb2l*vUVW;
566 Position->SetvVel(vLocalVelNED);
569 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 void FGState::Initialize(FGInitialCondition *FGIC) {
575 double latitude, longitude;
576 double wnorth,weast, wdown;
578 latitude = FGIC->GetLatitudeRadIC();
579 longitude = FGIC->GetLongitudeRadIC();
580 h = FGIC->GetAltitudeFtIC();
581 U = FGIC->GetUBodyFpsIC();
582 V = FGIC->GetVBodyFpsIC();
583 W = FGIC->GetWBodyFpsIC();
584 tht = FGIC->GetThetaRadIC();
585 phi = FGIC->GetPhiRadIC();
586 psi = FGIC->GetPsiRadIC();
587 wnorth = FGIC->GetWindNFpsIC();
588 weast = FGIC->GetWindEFpsIC();
589 wdown = FGIC->GetWindDFpsIC();
591 Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
592 Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
593 FGIC->GetTerrainAltitudeFtIC() );
595 // need to fix the wind speed args, here.
596 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
599 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 bool FGState::StoreData(string fname) {
602 ofstream datafile(fname.c_str());
605 datafile << Translation->GetUVW(eU);
606 datafile << Translation->GetUVW(eV);
607 datafile << Translation->GetUVW(eW);
608 datafile << Position->GetLatitude();
609 datafile << Position->GetLongitude();
610 datafile << Rotation->GetEuler(ePhi);
611 datafile << Rotation->GetEuler(eTht);
612 datafile << Rotation->GetEuler(ePsi);
613 datafile << Position->Geth();
617 cerr << "Could not open dump file " << fname << endl;
622 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 void FGState::InitMatrices(double phi, double tht, double psi) {
625 double thtd2, psid2, phid2;
626 double Sthtd2, Spsid2, Sphid2;
627 double Cthtd2, Cpsid2, Cphid2;
645 Cphid2Cthtd2 = Cphid2*Cthtd2;
646 Cphid2Sthtd2 = Cphid2*Sthtd2;
647 Sphid2Sthtd2 = Sphid2*Sthtd2;
648 Sphid2Cthtd2 = Sphid2*Cthtd2;
650 vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
651 vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
652 vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
653 vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
658 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660 void FGState::CalcMatrices(void) {
661 double Q0Q0, Q1Q1, Q2Q2, Q3Q3;
662 double Q0Q1, Q0Q2, Q0Q3, Q1Q2;
665 Q0Q0 = vQtrn(1)*vQtrn(1);
666 Q1Q1 = vQtrn(2)*vQtrn(2);
667 Q2Q2 = vQtrn(3)*vQtrn(3);
668 Q3Q3 = vQtrn(4)*vQtrn(4);
669 Q0Q1 = vQtrn(1)*vQtrn(2);
670 Q0Q2 = vQtrn(1)*vQtrn(3);
671 Q0Q3 = vQtrn(1)*vQtrn(4);
672 Q1Q2 = vQtrn(2)*vQtrn(3);
673 Q1Q3 = vQtrn(2)*vQtrn(4);
674 Q2Q3 = vQtrn(3)*vQtrn(4);
676 mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
677 mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
678 mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
679 mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
680 mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
681 mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
682 mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
683 mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
684 mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
690 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate) {
693 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
694 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
695 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
696 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
697 vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
704 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 FGColumnVector3& FGState::CalcEuler(void) {
707 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
708 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
710 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
711 vEuler(eTht) = asin(-mTl2b(1,3));
712 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
714 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
719 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 FGMatrix33& FGState::GetTs2b(void)
723 double ca, cb, sa, sb;
725 double alpha = Translation->Getalpha();
726 double beta = Translation->Getbeta();
746 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748 FGMatrix33& FGState::GetTb2s(void)
751 float ca, cb, sa, sb;
753 alpha = Translation->Getalpha();
754 beta = Translation->Getbeta();
774 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 void FGState::ReportState(void) {
777 char out[80], flap[10], gear[12];
779 cout << endl << " JSBSim State" << endl;
780 snprintf(out,80," Weight: %7.0f lbs. CG: %5.1f, %5.1f, %5.1f inches\n",
781 FDMExec->GetMassBalance()->GetWeight(),
782 FDMExec->GetMassBalance()->GetXYZcg(1),
783 FDMExec->GetMassBalance()->GetXYZcg(2),
784 FDMExec->GetMassBalance()->GetXYZcg(3));
786 if( FCS->GetDfPos() <= 0.01)
787 snprintf(flap,10,"Up");
789 snprintf(flap,10,"%2.0f",FCS->GetDfPos());
790 if(FCS->GetGearPos() < 0.01)
791 snprintf(gear,12,"Up");
792 else if(FCS->GetGearPos() > 0.99)
793 snprintf(gear,12,"Down");
795 snprintf(gear,12,"In Transit");
796 snprintf(out,80, " Flaps: %3s Gear: %12s\n",flap,gear);
798 snprintf(out,80, " Speed: %4.0f KCAS Mach: %5.2f\n",
799 FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
800 GetParameter(FG_MACH) );
802 snprintf(out,80, " Altitude: %7.0f ft. AGL Altitude: %7.0f ft.\n",
804 Position->GetDistanceAGL() );
806 snprintf(out,80, " Angle of Attack: %6.2f deg Pitch Angle: %6.2f deg\n",
807 GetParameter(FG_ALPHA)*radtodeg,
808 Rotation->Gettht()*radtodeg );
810 snprintf(out,80, " Flight Path Angle: %6.2f deg Climb Rate: %5.0f ft/min\n",
811 Position->GetGamma()*radtodeg,
812 Position->Gethdot()*60 );
814 snprintf(out,80, " Normal Load Factor: %4.2f g's Pitch Rate: %5.2f deg/s\n",
816 GetParameter(FG_PITCHRATE)*radtodeg );
818 snprintf(out,80, " Heading: %3.0f deg true Sideslip: %5.2f deg\n",
819 Rotation->Getpsi()*radtodeg,
820 GetParameter(FG_BETA)*radtodeg );
822 snprintf(out,80, " Bank Angle: %5.2f deg\n",
823 Rotation->Getphi()*radtodeg );
825 snprintf(out,80, " Elevator: %5.2f deg Left Aileron: %5.2f deg Rudder: %5.2f deg\n",
826 GetParameter(FG_ELEVATOR_POS)*radtodeg,
827 GetParameter(FG_AILERON_POS)*radtodeg,
828 GetParameter(FG_RUDDER_POS)*radtodeg );
830 snprintf(out,80, " Throttle: %5.2f%c\n",
831 FCS->GetThrottlePos(0)*100,'%' );
834 snprintf(out,80, " Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
835 FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
836 FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
839 snprintf(out,80, " Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
840 Position->GetVground()*fpstokts,
841 Position->GetGroundTrack()*radtodeg );
845 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846 // The bitmasked value choices are as follows:
847 // unset: In this case (the default) JSBSim would only print
848 // out the normally expected messages, essentially echoing
849 // the config files as they are read. If the environment
850 // variable is not set, debug_lvl is set to 1 internally
851 // 0: This requests JSBSim not to output any messages
853 // 1: This value explicity requests the normal JSBSim
855 // 2: This value asks for a message to be printed out when
856 // a class is instantiated
857 // 4: When this value is set, a message is displayed when a
858 // FGModel object executes its Run() method
859 // 8: When this value is set, various runtime state variables
860 // are printed out periodically
861 // 16: When set various parameters are sanity checked and
862 // a message is printed out when they go out of bounds
864 void FGState::Debug(int from)
866 if (debug_lvl <= 0) return;
868 if (debug_lvl & 1) { // Standard console startup message output
869 if (from == 0) { // Constructor
873 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
874 if (from == 0) cout << "Instantiated: FGState" << endl;
875 if (from == 1) cout << "Destroyed: FGState" << endl;
877 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
879 if (debug_lvl & 8 ) { // Runtime state variables
881 if (debug_lvl & 16) { // Sanity checking