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 ");
138 if (debug_lvl & 2) cout << "Instantiated: FGState" << endl;
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 if (debug_lvl & 2) cout << "Destroyed: FGState" << endl;
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 float FGState::GetParameter(eParam val_idx) {
155 return FDMExec->GetTranslation()->Getqbar();
157 return FDMExec->GetAircraft()->GetWingArea();
159 return FDMExec->GetAircraft()->GetWingSpan();
161 return FDMExec->GetAircraft()->Getcbar();
163 return FDMExec->GetTranslation()->Getalpha();
165 return FDMExec->GetTranslation()->Getadot();
167 return FDMExec->GetTranslation()->Getbeta();
169 return FDMExec->GetTranslation()->Getbdot();
171 return (FDMExec->GetRotation()->GetPQR())(2);
173 return (FDMExec->GetRotation()->GetPQR())(1);
175 return (FDMExec->GetRotation()->GetPQR())(3);
176 case FG_ELEVATOR_POS:
177 return FDMExec->GetFCS()->GetDePos();
179 return FDMExec->GetFCS()->GetDaPos();
181 return FDMExec->GetFCS()->GetDrPos();
182 case FG_SPDBRAKE_POS:
183 return FDMExec->GetFCS()->GetDsbPos();
184 case FG_SPOILERS_POS:
185 return FDMExec->GetFCS()->GetDspPos();
187 return FDMExec->GetFCS()->GetDfPos();
188 case FG_ELEVATOR_CMD:
189 return FDMExec->GetFCS()->GetDeCmd();
191 return FDMExec->GetFCS()->GetDaCmd();
193 return FDMExec->GetFCS()->GetDrCmd();
194 case FG_SPDBRAKE_CMD:
195 return FDMExec->GetFCS()->GetDsbCmd();
196 case FG_SPOILERS_CMD:
197 return FDMExec->GetFCS()->GetDspCmd();
199 return FDMExec->GetFCS()->GetDfCmd();
201 return FDMExec->GetTranslation()->GetMach();
203 return FDMExec->GetPosition()->Geth();
205 if(FDMExec->GetTranslation()->GetVt() > 0)
206 return FDMExec->GetAircraft()->GetWingSpan()/(2.0 * FDMExec->GetTranslation()->GetVt());
210 if(FDMExec->GetTranslation()->GetVt() > 0)
211 return FDMExec->GetAircraft()->Getcbar()/(2.0 * FDMExec->GetTranslation()->GetVt());
214 case FG_THROTTLE_CMD:
215 return FDMExec->GetFCS()->GetThrottleCmd(0);
216 case FG_THROTTLE_POS:
217 return FDMExec->GetFCS()->GetThrottlePos(0);
219 return FDMExec->GetPosition()->GetHOverB();
220 case FG_PITCH_TRIM_CMD:
221 return FDMExec->GetFCS()->GetPitchTrimCmd();
223 cerr << "FGState::GetParameter() - No handler for parameter " << val_idx << endl;
229 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 float FGState::GetParameter(string val_string) {
232 return GetParameter(coeffdef[val_string]);
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 eParam FGState::GetParameterIndex(string val_string) {
238 return coeffdef[val_string];
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 void FGState::SetParameter(eParam val_idx, float val) {
245 case FG_ELEVATOR_POS:
246 FDMExec->GetFCS()->SetDePos(val);
249 FDMExec->GetFCS()->SetDaPos(val);
252 FDMExec->GetFCS()->SetDrPos(val);
254 case FG_SPDBRAKE_POS:
255 FDMExec->GetFCS()->SetDsbPos(val);
257 case FG_SPOILERS_POS:
258 FDMExec->GetFCS()->SetDspPos(val);
261 FDMExec->GetFCS()->SetDfPos(val);
263 case FG_THROTTLE_POS:
264 FDMExec->GetFCS()->SetThrottlePos(ActiveEngine,val);
267 case FG_ELEVATOR_CMD:
268 FDMExec->GetFCS()->SetDeCmd(val);
271 FDMExec->GetFCS()->SetDaCmd(val);
274 FDMExec->GetFCS()->SetDrCmd(val);
276 case FG_SPDBRAKE_CMD:
277 FDMExec->GetFCS()->SetDsbCmd(val);
279 case FG_SPOILERS_CMD:
280 FDMExec->GetFCS()->SetDspCmd(val);
283 FDMExec->GetFCS()->SetDfCmd(val);
285 case FG_THROTTLE_CMD:
286 FDMExec->GetFCS()->SetThrottleCmd(ActiveEngine,val);
289 case FG_ACTIVE_ENGINE:
290 ActiveEngine = (int)val;
293 case FG_LEFT_BRAKE_CMD:
294 FDMExec->GetFCS()->SetLBrake(val);
296 case FG_CENTER_BRAKE_CMD:
297 FDMExec->GetFCS()->SetCBrake(val);
299 case FG_RIGHT_BRAKE_CMD:
300 FDMExec->GetFCS()->SetRBrake(val);
304 cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
308 //***************************************************************************
310 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
313 bool FGState::Reset(string path, string acname, string fname) {
317 float latitude, longitude, h;
319 resetDef = path + "/" + acname + "/" + fname + ".xml";
321 ifstream resetfile(resetDef.c_str());
327 resetfile >> latitude;
328 resetfile >> longitude;
335 FDMExec->GetPosition()->SetLatitude(latitude*DEGTORAD);
336 FDMExec->GetPosition()->SetLongitude(longitude*DEGTORAD);
337 FDMExec->GetPosition()->Seth(h);
339 Initialize(U, V, W, phi*DEGTORAD, tht*DEGTORAD, psi*DEGTORAD,
340 latitude*DEGTORAD, longitude*DEGTORAD, h);
344 cerr << "Unable to load reset file " << fname << endl;
349 //***************************************************************************
351 // Initialize: Assume all angles GIVEN IN RADIANS !!
354 void FGState::Initialize(float U, float V, float W,
355 float phi, float tht, float psi,
356 float Latitude, float Longitude, float H) {
357 FGColumnVector vUVW(3);
358 FGColumnVector vLocalVelNED(3);
359 FGColumnVector vEuler(3);
363 FDMExec->GetPosition()->SetLatitude(Latitude);
364 FDMExec->GetPosition()->SetLongitude(Longitude);
365 FDMExec->GetPosition()->Seth(H);
367 FDMExec->GetAtmosphere()->Run();
370 alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
374 beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
379 FDMExec->GetTranslation()->SetUVW(vUVW);
381 vEuler << phi << tht << psi;
382 FDMExec->GetRotation()->SetEuler(vEuler);
384 FDMExec->GetTranslation()->SetAB(alpha, beta);
386 Vt = sqrt(U*U + V*V + W*W);
387 FDMExec->GetTranslation()->SetVt(Vt);
389 FDMExec->GetTranslation()->SetMach(Vt/FDMExec->GetAtmosphere()->GetSoundSpeed());
391 qbar = 0.5*(U*U + V*V + W*W)*FDMExec->GetAtmosphere()->GetDensity();
392 FDMExec->GetTranslation()->Setqbar(qbar);
394 InitMatrices(phi, tht, psi);
396 vLocalVelNED = mTb2l*vUVW;
397 FDMExec->GetPosition()->SetvVel(vLocalVelNED);
400 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 void FGState::Initialize(FGInitialCondition *FGIC) {
406 float latitude, longitude;
408 latitude = FGIC->GetLatitudeRadIC();
409 longitude = FGIC->GetLongitudeRadIC();
410 h = FGIC->GetAltitudeFtIC();
411 U = FGIC->GetUBodyFpsIC();
412 V = FGIC->GetVBodyFpsIC();
413 W = FGIC->GetWBodyFpsIC();
414 tht = FGIC->GetThetaRadIC();
415 phi = FGIC->GetPhiRadIC();
416 psi = FGIC->GetPsiRadIC();
418 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h);
420 FDMExec->GetPosition()->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
421 FDMExec->GetPosition()->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() +
422 FGIC->GetTerrainAltitudeFtIC() );
426 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 bool FGState::StoreData(string fname) {
429 ofstream datafile(fname.c_str());
432 datafile << (FDMExec->GetTranslation()->GetUVW())(1);
433 datafile << (FDMExec->GetTranslation()->GetUVW())(2);
434 datafile << (FDMExec->GetTranslation()->GetUVW())(3);
435 datafile << FDMExec->GetPosition()->GetLatitude();
436 datafile << FDMExec->GetPosition()->GetLongitude();
437 datafile << (FDMExec->GetRotation()->GetEuler())(1);
438 datafile << (FDMExec->GetRotation()->GetEuler())(2);
439 datafile << (FDMExec->GetRotation()->GetEuler())(3);
440 datafile << FDMExec->GetPosition()->Geth();
444 cerr << "Could not open dump file " << fname << endl;
449 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 void FGState::InitMatrices(float phi, float tht, float psi) {
452 float thtd2, psid2, phid2;
453 float Sthtd2, Spsid2, Sphid2;
454 float Cthtd2, Cpsid2, Cphid2;
472 Cphid2Cthtd2 = Cphid2*Cthtd2;
473 Cphid2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= Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
517 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 void FGState::IntegrateQuat(FGColumnVector vPQR, int rate) {
520 static FGColumnVector vQdot(4);
521 static FGColumnVector vTmp(4);
523 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
524 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
525 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
526 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
527 vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
534 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 FGColumnVector FGState::CalcEuler(void) {
537 static FGColumnVector vEuler(3);
539 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
540 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
542 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
543 vEuler(eTht) = asin(-mTl2b(1,3));
544 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
546 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
551 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 FGMatrix FGState::GetTs2b(float alpha, float beta) {
554 float ca, cb, sa, sb;
574 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 void FGState::Debug(void)
578 //TODO: Add your source code here