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 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 FGState::FGState(FGFDMExec* fdex)
75 Aircraft = FDMExec->GetAircraft();
76 Translation = FDMExec->GetTranslation();
77 Rotation = FDMExec->GetRotation();
78 Position = FDMExec->GetPosition();
79 FCS = FDMExec->GetFCS();
80 Output = FDMExec->GetOutput();
81 Atmosphere = FDMExec->GetAtmosphere();
82 Aerodynamics = FDMExec->GetAerodynamics();
83 GroundReactions = FDMExec->GetGroundReactions();
84 Propulsion = FDMExec->GetPropulsion();
85 PropertyManager = FDMExec->GetPropertyManager();
87 for(int i=0;i<3;i++) vQdot_prev[i].InitMatrix();
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 //***************************************************************************
104 // Initialize: Assume all angles GIVEN IN RADIANS !!
107 void FGState::Initialize(double U, double V, double W,
108 double phi, double tht, double psi,
109 double Latitude, double Longitude, double H,
110 double wnorth, double weast, double wdown)
114 FGColumnVector3 vAeroUVW;
116 Position->SetLatitude(Latitude);
117 Position->SetLongitude(Longitude);
122 vLocalEuler << phi << tht << psi;
123 Rotation->SetEuler(vLocalEuler);
125 InitMatrices(phi, tht, psi);
128 Translation->SetUVW(vUVW);
130 Atmosphere->SetWindNED(wnorth, weast, wdown);
132 vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
134 if (vAeroUVW(eW) != 0.0)
135 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
138 if (vAeroUVW(eV) != 0.0)
139 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;
143 Translation->SetAB(alpha, beta);
145 Vt = sqrt(U*U + V*V + W*W);
146 Translation->SetVt(Vt);
148 Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
150 qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
151 Translation->Setqbar(qbar);
153 vLocalVelNED = mTb2l*vUVW;
154 Position->SetvVel(vLocalVelNED);
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 void FGState::Initialize(FGInitialCondition *FGIC)
163 double latitude, longitude;
164 double wnorth,weast, wdown;
166 latitude = FGIC->GetLatitudeRadIC();
167 longitude = FGIC->GetLongitudeRadIC();
168 h = FGIC->GetAltitudeFtIC();
169 U = FGIC->GetUBodyFpsIC();
170 V = FGIC->GetVBodyFpsIC();
171 W = FGIC->GetWBodyFpsIC();
172 tht = FGIC->GetThetaRadIC();
173 phi = FGIC->GetPhiRadIC();
174 psi = FGIC->GetPsiRadIC();
175 wnorth = FGIC->GetWindNFpsIC();
176 weast = FGIC->GetWindEFpsIC();
177 wdown = FGIC->GetWindDFpsIC();
179 Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
180 Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
181 FGIC->GetTerrainAltitudeFtIC() );
183 // need to fix the wind speed args, here.
184 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 void FGState::InitMatrices(double phi, double tht, double psi)
191 double thtd2, psid2, phid2;
192 double Sthtd2, Spsid2, Sphid2;
193 double Cthtd2, Cpsid2, Cphid2;
211 Cphid2Cthtd2 = Cphid2*Cthtd2;
212 Cphid2Sthtd2 = Cphid2*Sthtd2;
213 Sphid2Sthtd2 = Sphid2*Stht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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate)
261 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
262 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
263 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
264 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
266 vQtrn += Integrate(TRAPZ, dt*rate, vQdot, vQdot_prev);
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 FGColumnVector3& FGState::CalcEuler(void)
275 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
276 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
278 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
279 vEuler(eTht) = asin(-mTl2b(1,3));
280 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
282 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 FGMatrix33& FGState::GetTs2b(void)
291 double ca, cb, sa, sb;
293 double alpha = Translation->Getalpha();
294 double beta = Translation->Getbeta();
314 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 FGMatrix33& FGState::GetTb2s(void)
319 float ca, cb, sa, sb;
321 alpha = Translation->Getalpha();
322 beta = Translation->Getbeta();
342 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 void FGState::ReportState(void)
346 #if !defined(__BORLANDCPP__)
347 char out[80], flap[10], gear[12];
349 cout << endl << " JSBSim State" << endl;
350 snprintf(out,80," Weight: %7.0f lbs. CG: %5.1f, %5.1f, %5.1f inches\n",
351 FDMExec->GetMassBalance()->GetWeight(),
352 FDMExec->GetMassBalance()->GetXYZcg(1),
353 FDMExec->GetMassBalance()->GetXYZcg(2),
354 FDMExec->GetMassBalance()->GetXYZcg(3));
356 if ( FCS->GetDfPos() <= 0.01)
357 snprintf(flap,10,"Up");
359 snprintf(flap,10,"%2.0f",FCS->GetDfPos());
361 if (FCS->GetGearPos() < 0.01)
362 snprintf(gear,12,"Up");
363 else if (FCS->GetGearPos() > 0.99)
364 snprintf(gear,12,"Down");
366 snprintf(gear,12,"In Transit");
368 snprintf(out,80, " Flaps: %3s Gear: %12s\n",flap,gear);
370 snprintf(out,80, " Speed: %4.0f KCAS Mach: %5.2f\n",
371 FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
372 Translation->GetMach() );
374 snprintf(out,80, " Altitude: %7.0f ft. AGL Altitude: %7.0f ft.\n",
376 Position->GetDistanceAGL() );
378 snprintf(out,80, " Angle of Attack: %6.2f deg Pitch Angle: %6.2f deg\n",
379 Translation->Getalpha()*radtodeg,
380 Rotation->Gettht()*radtodeg );
382 snprintf(out,80, " Flight Path Angle: %6.2f deg Climb Rate: %5.0f ft/min\n",
383 Position->GetGamma()*radtodeg,
384 Position->Gethdot()*60 );
386 snprintf(out,80, " Normal Load Factor: %4.2f g's Pitch Rate: %5.2f deg/s\n",
388 Rotation->GetPQR(2)*radtodeg );
390 snprintf(out,80, " Heading: %3.0f deg true Sideslip: %5.2f deg Yaw Rate: %5.2f deg/s\n",
391 Rotation->Getpsi()*radtodeg,
392 Translation->Getbeta()*radtodeg,
393 Rotation->GetPQR(3)*radtodeg );
395 snprintf(out,80, " Bank Angle: %5.2f deg Roll Rate: %5.2f deg/s\n",
396 Rotation->Getphi()*radtodeg,
397 Rotation->GetPQR(1)*radtodeg );
399 snprintf(out,80, " Elevator: %5.2f deg Left Aileron: %5.2f deg Rudder: %5.2f deg\n",
400 FCS->GetDePos(ofRad)*radtodeg,
401 FCS->GetDaLPos(ofRad)*radtodeg,
402 FCS->GetDrPos(ofRad)*radtodeg );
404 snprintf(out,80, " Throttle: %5.2f%c\n",
405 FCS->GetThrottlePos(0)*100,'%' );
408 snprintf(out,80, " Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
409 FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
410 FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
413 snprintf(out,80, " Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
414 Position->GetVground()*fpstokts,
415 Position->GetGroundTrack()*radtodeg );
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 void FGState::bind(void)
424 PropertyManager->Tie("sim-time-sec",this,
425 &FGState::Getsim_time);
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 void FGState::unbind(void)
432 PropertyManager->Untie("sim-time-sec");
435 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 // The bitmasked value choices are as follows:
437 // unset: In this case (the default) JSBSim would only print
438 // out the normally expected messages, essentially echoing
439 // the config files as they are read. If the environment
440 // variable is not set, debug_lvl is set to 1 internally
441 // 0: This requests JSBSim not to output any messages
443 // 1: This value explicity requests the normal JSBSim
445 // 2: This value asks for a message to be printed out when
446 // a class is instantiated
447 // 4: When this value is set, a message is displayed when a
448 // FGModel object executes its Run() method
449 // 8: When this value is set, various runtime state variables
450 // are printed out periodically
451 // 16: When set various parameters are sanity checked and
452 // a message is printed out when they go out of bounds
454 void FGState::Debug(int from)
456 if (debug_lvl <= 0) return;
458 if (debug_lvl & 1) { // Standard console startup message output
459 if (from == 0) { // Constructor
463 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
464 if (from == 0) cout << "Instantiated: FGState" << endl;
465 if (from == 1) cout << "Destroyed: FGState" << endl;
467 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
469 if (debug_lvl & 8 ) { // Runtime state variables
471 if (debug_lvl & 16) { // Sanity checking
473 if (debug_lvl & 64) {
474 if (from == 0) { // Constructor
475 cout << IdSrc << endl;
476 cout << IdHdr << endl;