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 /*******************************************************************************
68 *******************************************************************************/
70 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
72 /*******************************************************************************
73 ************************************ CODE **************************************
74 *******************************************************************************/
76 /******************************************************************************/
78 // For every term registered here there must be a corresponding handler in
79 // GetParameter() below that retrieves that parameter. Also, there must be an
80 // entry in the enum eParam definition in FGDefs.h. The ID is what must be used
81 // in any config file entry which references that item.
83 FGState::FGState(FGFDMExec* fdex) : mTb2l(3,3),
95 RegisterVariable(FG_QBAR, " qbar " );
96 RegisterVariable(FG_WINGAREA, " wing_area " );
97 RegisterVariable(FG_WINGSPAN, " wingspan " );
98 RegisterVariable(FG_CBAR, " cbar " );
99 RegisterVariable(FG_ALPHA, " alpha " );
100 RegisterVariable(FG_ALPHADOT, " alphadot " );
101 RegisterVariable(FG_BETA, " beta " );
102 RegisterVariable(FG_BETADOT, " betadot " );
103 RegisterVariable(FG_PITCHRATE, " pitch_rate " );
104 RegisterVariable(FG_ROLLRATE, " roll_rate " );
105 RegisterVariable(FG_YAWRATE, " yaw_rate " );
106 RegisterVariable(FG_MACH, " mach " );
107 RegisterVariable(FG_ALTITUDE, " altitude " );
108 RegisterVariable(FG_BI2VEL, " BI2Vel " );
109 RegisterVariable(FG_CI2VEL, " CI2Vel " );
110 RegisterVariable(FG_ELEVATOR_POS, " elevator_pos " );
111 RegisterVariable(FG_AILERON_POS, " aileron_pos " );
112 RegisterVariable(FG_RUDDER_POS, " rudder_pos " );
113 RegisterVariable(FG_SPDBRAKE_POS, " speedbrake_pos " );
114 RegisterVariable(FG_SPOILERS_POS, " spoiler_pos " );
115 RegisterVariable(FG_FLAPS_POS, " flaps_pos " );
116 RegisterVariable(FG_ELEVATOR_CMD, " elevator_cmd " );
117 RegisterVariable(FG_AILERON_CMD, " aileron_cmd " );
118 RegisterVariable(FG_RUDDER_CMD, " rudder_cmd " );
119 RegisterVariable(FG_SPDBRAKE_CMD, " speedbrake_cmd " );
120 RegisterVariable(FG_SPOILERS_CMD, " spoiler_cmd " );
121 RegisterVariable(FG_FLAPS_CMD, " flaps_cmd " );
122 RegisterVariable(FG_THROTTLE_CMD, " throttle_cmd " );
123 RegisterVariable(FG_THROTTLE_POS, " throttle_pos " );
124 RegisterVariable(FG_HOVERB, " height/span " );
125 RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
128 /******************************************************************************/
130 FGState::~FGState(void) {}
132 /******************************************************************************/
134 float FGState::GetParameter(eParam val_idx) {
137 return FDMExec->GetTranslation()->Getqbar();
139 return FDMExec->GetAircraft()->GetWingArea();
141 return FDMExec->GetAircraft()->GetWingSpan();
143 return FDMExec->GetAircraft()->Getcbar();
145 return FDMExec->GetTranslation()->Getalpha();
149 return FDMExec->GetTranslation()->Getbeta();
153 return (FDMExec->GetRotation()->GetPQR())(2);
155 return (FDMExec->GetRotation()->GetPQR())(1);
157 return (FDMExec->GetRotation()->GetPQR())(3);
158 case FG_ELEVATOR_POS:
159 return FDMExec->GetFCS()->GetDePos();
161 return FDMExec->GetFCS()->GetDaPos();
163 return FDMExec->GetFCS()->GetDrPos();
164 case FG_SPDBRAKE_POS:
165 return FDMExec->GetFCS()->GetDsbPos();
166 case FG_SPOILERS_POS:
167 return FDMExec->GetFCS()->GetDspPos();
169 return FDMExec->GetFCS()->GetDfPos();
170 case FG_ELEVATOR_CMD:
171 return FDMExec->GetFCS()->GetDeCmd();
173 return FDMExec->GetFCS()->GetDaCmd();
175 return FDMExec->GetFCS()->GetDrCmd();
176 case FG_SPDBRAKE_CMD:
177 return FDMExec->GetFCS()->GetDsbCmd();
178 case FG_SPOILERS_CMD:
179 return FDMExec->GetFCS()->GetDspCmd();
181 return FDMExec->GetFCS()->GetDfCmd();
183 return FDMExec->GetTranslation()->GetMach();
185 return FDMExec->GetPosition()->Geth();
187 if(FDMExec->GetTranslation()->GetVt() > 0)
188 return FDMExec->GetAircraft()->GetWingSpan()/(2.0 * FDMExec->GetTranslation()->GetVt());
192 if(FDMExec->GetTranslation()->GetVt() > 0)
193 return FDMExec->GetAircraft()->Getcbar()/(2.0 * FDMExec->GetTranslation()->GetVt());
196 case FG_THROTTLE_CMD:
197 return FDMExec->GetFCS()->GetThrottleCmd(0);
198 case FG_THROTTLE_POS:
199 return FDMExec->GetFCS()->GetThrottlePos(0);
201 return FDMExec->GetPosition()->GetHOverB();
202 case FG_PITCH_TRIM_CMD:
203 return FDMExec->GetFCS()->GetPitchTrimCmd();
205 cerr << "FGState::GetParameter() - No handler for parameter " << val_idx << endl;
211 /******************************************************************************/
213 float FGState::GetParameter(string val_string) {
214 return GetParameter(coeffdef[val_string]);
217 /******************************************************************************/
219 eParam FGState::GetParameterIndex(string val_string) {
220 return coeffdef[val_string];
223 /******************************************************************************/
225 void FGState::SetParameter(eParam val_idx, float val) {
227 case FG_ELEVATOR_POS:
228 FDMExec->GetFCS()->SetDePos(val);
231 FDMExec->GetFCS()->SetDaPos(val);
234 FDMExec->GetFCS()->SetDrPos(val);
236 case FG_SPDBRAKE_POS:
237 FDMExec->GetFCS()->SetDsbPos(val);
239 case FG_SPOILERS_POS:
240 FDMExec->GetFCS()->SetDspPos(val);
243 FDMExec->GetFCS()->SetDfPos(val);
245 case FG_THROTTLE_POS:
246 FDMExec->GetFCS()->SetThrottlePos(-1,val);
250 //***************************************************************************
252 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
255 bool FGState::Reset(string path, string acname, string fname) {
259 float latitude, longitude, h;
261 resetDef = path + "/" + acname + "/" + fname;
263 ifstream resetfile(resetDef.c_str());
269 resetfile >> latitude;
270 resetfile >> longitude;
277 FDMExec->GetPosition()->SetLatitude(latitude*DEGTORAD);
278 FDMExec->GetPosition()->SetLongitude(longitude*DEGTORAD);
279 FDMExec->GetPosition()->Seth(h);
281 Initialize(U, V, W, phi*DEGTORAD, tht*DEGTORAD, psi*DEGTORAD,
282 latitude*DEGTORAD, longitude*DEGTORAD, h);
286 cerr << "Unable to load reset file " << fname << endl;
291 //***************************************************************************
293 // Initialize: Assume all angles GIVEN IN RADIANS !!
296 void FGState::Initialize(float U, float V, float W,
297 float phi, float tht, float psi,
298 float Latitude, float Longitude, float H) {
299 FGColumnVector vUVW(3);
300 FGColumnVector vLocalVelNED(3);
301 FGColumnVector vEuler(3);
305 FDMExec->GetPosition()->SetLatitude(Latitude);
306 FDMExec->GetPosition()->SetLongitude(Longitude);
307 FDMExec->GetPosition()->Seth(H);
309 FDMExec->GetAtmosphere()->Run();
312 alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
316 beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
321 FDMExec->GetTranslation()->SetUVW(vUVW);
323 vEuler << phi << tht << psi;
324 FDMExec->GetRotation()->SetEuler(vEuler);
326 FDMExec->GetTranslation()->SetAB(alpha, beta);
328 Vt = sqrt(U*U + V*V + W*W);
329 FDMExec->GetTranslation()->SetVt(Vt);
331 FDMExec->GetTranslation()->SetMach(Vt/FDMExec->GetAtmosphere()->GetSoundSpeed());
333 qbar = 0.5*(U*U + V*V + W*W)*FDMExec->GetAtmosphere()->GetDensity();
334 FDMExec->GetTranslation()->Setqbar(qbar);
336 InitMatrices(phi, tht, psi);
338 vLocalVelNED = mTb2l*vUVW;
339 FDMExec->GetPosition()->SetvVel(vLocalVelNED);
342 /******************************************************************************/
344 void FGState::Initialize(FGInitialCondition *FGIC) {
348 float latitude, longitude;
350 latitude = FGIC->GetLatitudeRadIC();
351 longitude = FGIC->GetLongitudeRadIC();
352 h = FGIC->GetAltitudeFtIC();
353 U = FGIC->GetUBodyFpsIC();
354 V = FGIC->GetVBodyFpsIC();
355 W = FGIC->GetWBodyFpsIC();
356 tht = FGIC->GetThetaRadIC();
357 phi = FGIC->GetPhiRadIC();
358 psi = FGIC->GetPsiRadIC();
360 Initialize(U, V, W, phi, tht, psi, latitude, longitude, h);
363 /******************************************************************************/
365 bool FGState::StoreData(string fname) {
366 ofstream datafile(fname.c_str());
369 datafile << (FDMExec->GetTranslation()->GetUVW())(1);
370 datafile << (FDMExec->GetTranslation()->GetUVW())(2);
371 datafile << (FDMExec->GetTranslation()->GetUVW())(3);
372 datafile << FDMExec->GetPosition()->GetLatitude();
373 datafile << FDMExec->GetPosition()->GetLongitude();
374 datafile << (FDMExec->GetRotation()->GetEuler())(1);
375 datafile << (FDMExec->GetRotation()->GetEuler())(2);
376 datafile << (FDMExec->GetRotation()->GetEuler())(3);
377 datafile << FDMExec->GetPosition()->Geth();
381 cerr << "Could not open dump file " << fname << endl;
386 /******************************************************************************/
388 void FGState::InitMatrices(float phi, float tht, float psi) {
389 float thtd2, psid2, phid2;
390 float Sthtd2, Spsid2, Sphid2;
391 float Cthtd2, Cpsid2, Cphid2;
409 Cphid2Cthtd2 = Cphid2*Cthtd2;
410 Cphid2Sthtd2 = Cphid2*Sthtd2;
411 Sphid2Sthtd2 = Sphid2*Sthtd2;
412 Sphid2Cthtd2 = Sphid2*Cthtd2;
414 vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
415 vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
416 vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
417 vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
422 /******************************************************************************/
424 void FGState::CalcMatrices(void) {
425 float Q0Q0, Q1Q1, Q2Q2, Q3Q3;
426 float Q0Q1, Q0Q2, Q0Q3, Q1Q2;
429 Q0Q0 = vQtrn(1)*vQtrn(1);
430 Q1Q1 = vQtrn(2)*vQtrn(2);
431 Q2Q2 = vQtrn(3)*vQtrn(3);
432 Q3Q3 = vQtrn(4)*vQtrn(4);
433 Q0Q1 = vQtrn(1)*vQtrn(2);
434 Q0Q2 = vQtrn(1)*vQtrn(3);
435 Q0Q3 = vQtrn(1)*vQtrn(4);
436 Q1Q2 = vQtrn(2)*vQtrn(3);
437 Q1Q3 = vQtrn(2)*vQtrn(4);
438 Q2Q3 = vQtrn(3)*vQtrn(4);
440 mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
441 mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
442 mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
443 mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
444 mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
445 mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
446 mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
447 mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
448 mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
454 /******************************************************************************/
456 void FGState::IntegrateQuat(FGColumnVector vPQR, int rate) {
457 static FGColumnVector vlastQdot(4);
458 static FGColumnVector vQdot(4);
460 vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
461 vQdot(2) = 0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
462 vQdot(3) = 0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
463 vQdot(4) = 0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
465 vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
472 /******************************************************************************/
474 FGColumnVector FGState::CalcEuler(void) {
475 static FGColumnVector vEuler(3);
477 if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
478 if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
480 vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
481 vEuler(eTht) = asin(-mTl2b(1,3));
482 vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
484 if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
489 /******************************************************************************/
491 FGMatrix FGState::GetTs2b(float alpha, float beta) {
492 float ca, cb, sa, sb;
512 /******************************************************************************/