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
58 static const char *IdSrc = "$Id$";
59 static const char *IdHdr = ID_STATE;
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69 FGState::FGState(FGFDMExec* fdex)
77 Aircraft = FDMExec->GetAircraft();
78 Translation = FDMExec->GetTranslation();
79 Rotation = FDMExec->GetRotation();
80 Position = FDMExec->GetPosition();
81 FCS = FDMExec->GetFCS();
82 Output = FDMExec->GetOutput();
83 Atmosphere = FDMExec->GetAtmosphere();
84 Aerodynamics = FDMExec->GetAerodynamics();
85 GroundReactions = FDMExec->GetGroundReactions();
86 Propulsion = FDMExec->GetPropulsion();
87 PropertyManager = FDMExec->GetPropertyManager();
89 for(int i=0;i<3;i++) vQdot_prev[i].InitMatrix();
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 //***************************************************************************
106 // Initialize: Assume all angles GIVEN IN RADIANS !!
109 void FGState::Initialize(double U, double V, double W,
110 double phi, double tht, double psi,
111 double Latitude, double Longitude, double H,
112 double wnorth, double weast, double wdown)
116 FGColumnVector3 vAeroUVW;
118 Position->SetLatitude(Latitude);
119 Position->SetLongitude(Longitude);
124 vLocalEuler << phi << tht << psi;
125 Rotation->SetEuler(vLocalEuler);
127 InitMatrices(phi, tht, psi);
130 Translation->SetUVW(vUVW);
132 Atmosphere->SetWindNED(wnorth, weast, wdown);
134 vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
136 if (vAeroUVW(eW) != 0.0)
137 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
140 if (vAeroUVW(eV) != 0.0)
141 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;
145 Translation->SetAB(alpha, beta);
147 Vt = sqrt(U*U + V*V + W*W);
148 Translation->SetVt(Vt);
150 Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
152 qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
153 Translation->Setqbar(qbar);
155 vLocalVelNED = mTb2l*vUVW;
156 Position->SetvVel(vLocalVelNED);
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 void FGState::Initialize(FGInitialCondition *FGIC)
165 double latitude, longitude;
166 double wnorth,weast, wdown;
168 latitude = FGIC->GetLatitudeRadIC();
169 longitude = FGIC->GetLongitudeRadIC();
170 h = FGIC->GetAltitudeFtIC();
171 U = FGIC->GetUBodyFpsIC();
172 V = FGIC->GetVBodyFpsIC();
173 W = FGIC->GetWBodyFpsIC();
174 tht = FGIC->GetThetaRadIC();
175 phi = FGIC->GetPhiRadIC();
176 psi = FGIC->GetPsiRadIC();
177 wnorth = FGIC->GetWindNFpsIC();
178 weast = FGIC->GetWindEFpsIC();
179 wdown = FGIC->GetWindDFpsIC();
181 Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
182 Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
183 FGIC->GetTerrainAltitudeFtIC() );
185 // need to fix the wind speed args, here.
186 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 void FGState::InitMatrices(double phi, double tht, double psi)
193 double thtd2, psid2, phid2;
194 double Sthtd2, Spsid2, Sphid2;
195 double Cthtd2, Cpsid2, Cphid2;
213 Cphid2Cthtd2 = Cphid2*Cthtd2;
214 Cphid2Sthtd2 = Cphid2*Sthtd2;
215 Sphid2Sthtd2 = Sphid2*Stht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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate)
263 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
264 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
265 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
266 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
268 vQtrn += Integrate(TRAPZ, dt*rate, vQdot, vQdot_prev);
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 FGColumnVector3& FGState::CalcEuler(void)
277 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
278 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
280 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
281 vEuler(eTht) = asin(-mTl2b(1,3));
282 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
284 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
289 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 FGMatrix33& FGState::GetTs2b(void)
293 double ca, cb, sa, sb;
295 double alpha = Translation->Getalpha();
296 double beta = Translation->Getbeta();
316 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 FGMatrix33& FGState::GetTb2s(void)
321 float ca, cb, sa, sb;
323 alpha = Translation->Getalpha();
324 beta = Translation->Getbeta();
344 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 void FGState::ReportState(void)
348 #if !defined(__BORLANDCPP__)
349 char out[80], flap[10], gear[12];
351 cout << endl << " JSBSim State" << endl;
352 snprintf(out,80," Weight: %7.0f lbs. CG: %5.1f, %5.1f, %5.1f inches\n",
353 FDMExec->GetMassBalance()->GetWeight(),
354 FDMExec->GetMassBalance()->GetXYZcg(1),
355 FDMExec->GetMassBalance()->GetXYZcg(2),
356 FDMExec->GetMassBalance()->GetXYZcg(3));
358 if ( FCS->GetDfPos() <= 0.01)
359 snprintf(flap,10,"Up");
361 snprintf(flap,10,"%2.0f",FCS->GetDfPos());
363 if (FCS->GetGearPos() < 0.01)
364 snprintf(gear,12,"Up");
365 else if (FCS->GetGearPos() > 0.99)
366 snprintf(gear,12,"Down");
368 snprintf(gear,12,"In Transit");
370 snprintf(out,80, " Flaps: %3s Gear: %12s\n",flap,gear);
372 snprintf(out,80, " Speed: %4.0f KCAS Mach: %5.2f\n",
373 FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
374 Translation->GetMach() );
376 snprintf(out,80, " Altitude: %7.0f ft. AGL Altitude: %7.0f ft.\n",
378 Position->GetDistanceAGL() );
380 snprintf(out,80, " Angle of Attack: %6.2f deg Pitch Angle: %6.2f deg\n",
381 Translation->Getalpha()*radtodeg,
382 Rotation->Gettht()*radtodeg );
384 snprintf(out,80, " Flight Path Angle: %6.2f deg Climb Rate: %5.0f ft/min\n",
385 Position->GetGamma()*radtodeg,
386 Position->Gethdot()*60 );
388 snprintf(out,80, " Normal Load Factor: %4.2f g's Pitch Rate: %5.2f deg/s\n",
390 Rotation->GetPQR(2)*radtodeg );
392 snprintf(out,80, " Heading: %3.0f deg true Sideslip: %5.2f deg Yaw Rate: %5.2f deg/s\n",
393 Rotation->Getpsi()*radtodeg,
394 Translation->Getbeta()*radtodeg,
395 Rotation->GetPQR(3)*radtodeg );
397 snprintf(out,80, " Bank Angle: %5.2f deg Roll Rate: %5.2f deg/s\n",
398 Rotation->Getphi()*radtodeg,
399 Rotation->GetPQR(1)*radtodeg );
401 snprintf(out,80, " Elevator: %5.2f deg Left Aileron: %5.2f deg Rudder: %5.2f deg\n",
402 FCS->GetDePos(ofRad)*radtodeg,
403 FCS->GetDaLPos(ofRad)*radtodeg,
404 FCS->GetDrPos(ofRad)*radtodeg );
406 snprintf(out,80, " Throttle: %5.2f%c\n",
407 FCS->GetThrottlePos(0)*100,'%' );
410 snprintf(out,80, " Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
411 FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
412 FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
415 snprintf(out,80, " Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
416 Position->GetVground()*fpstokts,
417 Position->GetGroundTrack()*radtodeg );
422 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 void FGState::bind(void)
426 PropertyManager->Tie("sim-time-sec",this,
427 &FGState::Getsim_time);
430 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 void FGState::unbind(void)
434 PropertyManager->Untie("sim-time-sec");
437 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 // The bitmasked value choices are as follows:
439 // unset: In this case (the default) JSBSim would only print
440 // out the normally expected messages, essentially echoing
441 // the config files as they are read. If the environment
442 // variable is not set, debug_lvl is set to 1 internally
443 // 0: This requests JSBSim not to output any messages
445 // 1: This value explicity requests the normal JSBSim
447 // 2: This value asks for a message to be printed out when
448 // a class is instantiated
449 // 4: When this value is set, a message is displayed when a
450 // FGModel object executes its Run() method
451 // 8: When this value is set, various runtime state variables
452 // are printed out periodically
453 // 16: When set various parameters are sanity checked and
454 // a message is printed out when they go out of bounds
456 void FGState::Debug(int from)
458 if (debug_lvl <= 0) return;
460 if (debug_lvl & 1) { // Standard console startup message output
461 if (from == 0) { // Constructor
465 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
466 if (from == 0) cout << "Instantiated: FGState" << endl;
467 if (from == 1) cout << "Destroyed: FGState" << endl;
469 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
471 if (debug_lvl & 8 ) { // Runtime state variables
473 if (debug_lvl & 16) { // Sanity checking
475 if (debug_lvl & 64) {
476 if (from == 0) { // Constructor
477 cout << IdSrc << endl;
478 cout << IdHdr << endl;