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>
41 # ifdef FG_HAVE_STD_INCLUDES
51 # include <simgear/constants.h>
56 #include "FGFDMExec.h"
57 #include "FGAtmosphere.h"
59 #include "FGAircraft.h"
60 #include "FGTranslation.h"
61 #include "FGRotation.h"
62 #include "FGPosition.h"
63 #include "FGAuxiliary.h"
66 static const char *IdSrc = "$Id$";
67 static const char *IdHdr = ID_STATE;
69 extern short debug_lvl;
71 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
77 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
81 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 // For every term registered here there must be a corresponding handler in
84 // GetParameter() below that retrieves that parameter. Also, there must be an
85 // entry in the enum eParam definition in FGDefs.h. The ID is what must be used
86 // in any config file entry which references that item.
88 FGState::FGState(FGFDMExec* fdex) : mTb2l(3,3),
101 RegisterVariable(FG_TIME, " time " );
102 RegisterVariable(FG_QBAR, " qbar " );
103 RegisterVariable(FG_WINGAREA, " wing_area " );
104 RegisterVariable(FG_WINGSPAN, " wingspan " );
105 RegisterVariable(FG_CBAR, " cbar " );
106 RegisterVariable(FG_ALPHA, " alpha " );
107 RegisterVariable(FG_ALPHADOT, " alphadot " );
108 RegisterVariable(FG_BETA, " beta " );
109 RegisterVariable(FG_BETADOT, " betadot " );
110 RegisterVariable(FG_PITCHRATE, " pitch_rate " );
111 RegisterVariable(FG_ROLLRATE, " roll_rate " );
112 RegisterVariable(FG_YAWRATE, " yaw_rate " );
113 RegisterVariable(FG_MACH, " mach " );
114 RegisterVariable(FG_ALTITUDE, " altitude " );
115 RegisterVariable(FG_BI2VEL, " BI2Vel " );
116 RegisterVariable(FG_CI2VEL, " CI2Vel " );
117 RegisterVariable(FG_ELEVATOR_POS, " elevator_pos " );
118 RegisterVariable(FG_AILERON_POS, " aileron_pos " );
119 RegisterVariable(FG_RUDDER_POS, " rudder_pos " );
120 RegisterVariable(FG_SPDBRAKE_POS, " speedbrake_pos " );
121 RegisterVariable(FG_SPOILERS_POS, " spoiler_pos " );
122 RegisterVariable(FG_FLAPS_POS, " flaps_pos " );
123 RegisterVariable(FG_ELEVATOR_CMD, " elevator_cmd " );
124 RegisterVariable(FG_AILERON_CMD, " aileron_cmd " );
125 RegisterVariable(FG_RUDDER_CMD, " rudder_cmd " );
126 RegisterVariable(FG_SPDBRAKE_CMD, " speedbrake_cmd " );
127 RegisterVariable(FG_SPOILERS_CMD, " spoiler_cmd " );
128 RegisterVariable(FG_FLAPS_CMD, " flaps_cmd " );
129 RegisterVariable(FG_THROTTLE_CMD, " throttle_cmd " );
130 RegisterVariable(FG_THROTTLE_POS, " throttle_pos " );
131 RegisterVariable(FG_ACTIVE_ENGINE, " active_engine " );
132 RegisterVariable(FG_HOVERB, " height/span " );
133 RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
134 RegisterVariable(FG_LEFT_BRAKE_CMD, " left_brake_cmd " );
135 RegisterVariable(FG_RIGHT_BRAKE_CMD," right_brake_cmd ");
136 RegisterVariable(FG_CENTER_BRAKE_CMD," center_brake_cmd ");
137 RegisterVariable(FG_SET_LOGGING, " data_logging " );
139 if (debug_lvl & 2) cout << "Instantiated: FGState" << endl;
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 if (debug_lvl & 2) cout << "Destroyed: FGState" << endl;
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 float FGState::GetParameter(eParam val_idx) {
156 return FDMExec->GetTranslation()->Getqbar();
158 return FDMExec->GetAircraft()->GetWingArea();
160 return FDMExec->GetAircraft()->GetWingSpan();
162 return FDMExec->GetAircraft()->Getcbar();
164 return FDMExec->GetTranslation()->Getalpha();
166 return FDMExec->GetTranslation()->Getadot();
168 return FDMExec->GetTranslation()->Getbeta();
170 return FDMExec->GetTranslation()->Getbdot();
172 return (FDMExec->GetRotation()->GetPQR())(2);
174 return (FDMExec->GetRotation()->GetPQR())(1);
176 return (FDMExec->GetRotation()->GetPQR())(3);
177 case FG_ELEVATOR_POS:
178 return FDMExec->GetFCS()->GetDePos();
180 return FDMExec->GetFCS()->GetDaPos();
182 return FDMExec->GetFCS()->GetDrPos();
183 case FG_SPDBRAKE_POS:
184 return FDMExec->GetFCS()->GetDsbPos();
185 case FG_SPOILERS_POS:
186 return FDMExec->GetFCS()->GetDspPos();
188 return FDMExec->GetFCS()->GetDfPos();
189 case FG_ELEVATOR_CMD:
190 return FDMExec->GetFCS()->GetDeCmd();
192 return FDMExec->GetFCS()->GetDaCmd();
194 return FDMExec->GetFCS()->GetDrCmd();
195 case FG_SPDBRAKE_CMD:
196 return FDMExec->GetFCS()->GetDsbCmd();
197 case FG_SPOILERS_CMD:
198 return FDMExec->GetFCS()->GetDspCmd();
200 return FDMExec->GetFCS()->GetDfCmd();
202 return FDMExec->GetTranslation()->GetMach();
204 return FDMExec->GetPosition()->Geth();
206 if(FDMExec->GetTranslation()->GetVt() > 0)
207 return FDMExec->GetAircraft()->GetWingSpan()/(2.0 * FDMExec->GetTranslation()->GetVt());
211 if(FDMExec->GetTranslation()->GetVt() > 0)
212 return FDMExec->GetAircraft()->Getcbar()/(2.0 * FDMExec->GetTranslation()->GetVt());
215 case FG_THROTTLE_CMD:
216 return FDMExec->GetFCS()->GetThrottleCmd(0);
217 case FG_THROTTLE_POS:
218 return FDMExec->GetFCS()->GetThrottlePos(0);
220 return FDMExec->GetPosition()->GetHOverB();
221 case FG_PITCH_TRIM_CMD:
222 return FDMExec->GetFCS()->GetPitchTrimCmd();
224 cerr << "FGState::GetParameter() - No handler for parameter " << val_idx << endl;
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 float FGState::GetParameter(string val_string) {
233 return GetParameter(coeffdef[val_string]);
236 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 eParam FGState::GetParameterIndex(string val_string) {
239 return coeffdef[val_string];
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 void FGState::SetParameter(eParam val_idx, float val) {
246 case FG_ELEVATOR_POS:
247 FDMExec->GetFCS()->SetDePos(val);
250 FDMExec->GetFCS()->SetDaPos(val);
253 FDMExec->GetFCS()->SetDrPos(val);
255 case FG_SPDBRAKE_POS:
256 FDMExec->GetFCS()->SetDsbPos(val);
258 case FG_SPOILERS_POS:
259 FDMExec->GetFCS()->SetDspPos(val);
262 FDMExec->GetFCS()->SetDfPos(val);
264 case FG_THROTTLE_POS:
265 FDMExec->GetFCS()->SetThrottlePos(ActiveEngine,val);
268 case FG_ELEVATOR_CMD:
269 FDMExec->GetFCS()->SetDeCmd(val);
272 FDMExec->GetFCS()->SetDaCmd(val);
275 FDMExec->GetFCS()->SetDrCmd(val);
277 case FG_SPDBRAKE_CMD:
278 FDMExec->GetFCS()->SetDsbCmd(val);
280 case FG_SPOILERS_CMD:
281 FDMExec->GetFCS()->SetDspCmd(val);
284 FDMExec->GetFCS()->SetDfCmd(val);
286 case FG_THROTTLE_CMD:
287 FDMExec->GetFCS()->SetThrottleCmd(ActiveEngine,val);
290 case FG_ACTIVE_ENGINE:
291 ActiveEngine = (int)val;
294 case FG_LEFT_BRAKE_CMD:
295 FDMExec->GetFCS()->SetLBrake(val);
297 case FG_CENTER_BRAKE_CMD:
298 FDMExec->GetFCS()->SetCBrake(val);
300 case FG_RIGHT_BRAKE_CMD:
301 FDMExec->GetFCS()->SetRBrake(val);
305 if (val < -0.01) FDMExec->GetOutput()->Disable();
306 else if (val > 0.01) FDMExec->GetOutput()->Enable();
307 else FDMExec->GetOutput()->Toggle();
311 cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
315 //***************************************************************************
317 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
320 bool FGState::Reset(string path, string acname, string fname) {
324 float latitude, longitude, h;
326 resetDef = path + "/" + acname + "/" + fname + ".xml";
328 ifstream resetfile(resetDef.c_str());
334 resetfile >> latitude;
335 resetfile >> longitude;
342 FDMExec->GetPosition()->SetLatitude(latitude*DEGTORAD);
343 FDMExec->GetPosition()->SetLongitude(longitude*DEGTORAD);
344 FDMExec->GetPosition()->Seth(h);
346 Initialize(U, V, W, phi*DEGTORAD, tht*DEGTORAD, psi*DEGTORAD,
347 latitude*DEGTORAD, longitude*DEGTORAD, h);
351 cerr << "Unable to load reset file " << fname << endl;
356 //***************************************************************************
358 // Initialize: Assume all angles GIVEN IN RADIANS !!
361 void FGState::Initialize(float U, float V, float W,
362 float phi, float tht, float psi,
363 float Latitude, float Longitude, float H) {
364 FGColumnVector vUVW(3);
365 FGColumnVector vLocalVelNED(3);
366 FGColumnVector vEuler(3);
370 FDMExec->GetPosition()->SetLatitude(Latitude);
371 FDMExec->GetPosition()->SetLongitude(Longitude);
372 FDMExec->GetPosition()->Seth(H);
374 FDMExec->GetAtmosphere()->Run();
377 alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
381 beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
386 FDMExec->GetTranslation()->SetUVW(vUVW);
388 vEuler << phi << tht << psi;
389 FDMExec->GetRotation()->SetEuler(vEuler);
391 FDMExec->GetTranslation()->SetAB(alpha, beta);
393 Vt = sqrt(U*U + V*V + W*W);
394 FDMExec->GetTranslation()->SetVt(Vt);
396 FDMExec->GetTranslation()->SetMach(Vt/FDMExec->GetAtmosphere()->GetSoundSpeed());
398 qbar = 0.5*(U*U + V*V + W*W)*FDMExec->GetAtmosphere()->GetDensity();
399 FDMExec->GetTranslation()->Setqbar(qbar);
401 InitMatrices(phi, tht, psi);
403 vLocalVelNED = mTb2l*vUVW;
404 FDMExec->GetPosition()->SetvVel(vLocalVelNED);
407 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 void FGState::Initialize(FGInitialCondition *FGIC) {
413 float latitude, longitude;
415 latitude = FGIC->GetLatitudeRadIC();
416 longitude = FGIC->GetLongitudeRadIC();
417 h = FGIC->GetAltitudeFtIC();
418 U = FGIC->GetUBodyFpsIC();
419 V = FGIC->GetVBodyFpsIC();
420 W = FGIC->GetWBodyFpsIC();
421 tht = FGIC->GetThetaRadIC();
422 phi = FGIC->GetPhiRadIC();
423 psi = FGIC->GetPsiRadIC();
425 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h);
427 FDMExec->GetPosition()->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
428 FDMExec->GetPosition()->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
429 FGIC->GetTerrainAltitudeFtIC() );
433 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 bool FGState::StoreData(string fname) {
436 ofstream datafile(fname.c_str());
439 datafile << (FDMExec->GetTranslation()->GetUVW())(1);
440 datafile << (FDMExec->GetTranslation()->GetUVW())(2);
441 datafile << (FDMExec->GetTranslation()->GetUVW())(3);
442 datafile << FDMExec->GetPosition()->GetLatitude();
443 datafile << FDMExec->GetPosition()->GetLongitude();
444 datafile << (FDMExec->GetRotation()->GetEuler())(1);
445 datafile << (FDMExec->GetRotation()->GetEuler())(2);
446 datafile << (FDMExec->GetRotation()->GetEuler())(3);
447 datafile << FDMExec->GetPosition()->Geth();
451 cerr << "Could not open dump file " << fname << endl;
456 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 void FGState::InitMatrices(float phi, float tht, float psi) {
459 float thtd2, psid2, phid2;
460 float Sthtd2, Spsid2, Sphid2;
461 float Cthtd2, Cpsid2, Cphid2;
479 Cphid2Cthtd2 = Cphid2*Cthtd2;
480 Cphid2Sthtd2 = Cphid2*Sthtd2;
481 Sphid2Sthtd2 = Sphid2*Sthtd2;
482 Sphid2Cthtd2 = Sphid2*Cthtd2;
484 vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
485 vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
486 vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
487 vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
492 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 void FGState::CalcMatrices(void) {
495 float Q0Q0, Q1Q1, Q2Q2, Q3Q3;
496 float Q0Q1, Q0Q2, Q0Q3, Q1Q2;
499 Q0Q0 = vQtrn(1)*vQtrn(1);
500 Q1Q1 = vQtrn(2)*vQtrn(2);
501 Q2Q2 = vQtrn(3)*vQtrn(3);
502 Q3Q3 = vQtrn(4)*vQtrn(4);
503 Q0Q1 = vQtrn(1)*vQtrn(2);
504 Q0Q2 = vQtrn(1)*vQtrn(3);
505 Q0Q3 = vQtrn(1)*vQtrn(4);
506 Q1Q2 = vQtrn(2)*vQtrn(3);
507 Q1Q3 = vQtrn(2)*vQtrn(4);
508 Q2Q3 = vQtrn(3)*vQtrn(4);
510 mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
511 mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
512 mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
513 mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
514 mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
515 mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
516 mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
517 mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
518 mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
524 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526 void FGState::IntegrateQuat(FGColumnVector vPQR, int rate) {
527 static FGColumnVector vQdot(4);
528 static FGColumnVector vTmp(4);
530 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
531 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
532 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
533 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
534 vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
541 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 FGColumnVector FGState::CalcEuler(void) {
544 static FGColumnVector vEuler(3);
546 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
547 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
549 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
550 vEuler(eTht) = asin(-mTl2b(1,3));
551 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
553 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
558 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 FGMatrix FGState::GetTs2b(float alpha, float beta) {
561 float ca, cb, sa, sb;
581 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 void FGState::Debug(void)
585 //TODO: Add your source code here