1999-02-05 21:26:01 +00:00
|
|
|
/*******************************************************************************
|
1999-02-11 21:05:34 +00:00
|
|
|
|
1999-02-05 21:26:01 +00:00
|
|
|
Module: FGState.cpp
|
|
|
|
Author: Jon Berndt
|
|
|
|
Date started: 11/17/98
|
|
|
|
Called by: FGFDMExec and accessed by all models.
|
|
|
|
|
|
|
|
------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
Further information about the GNU General Public License can also be found on
|
|
|
|
the world wide web at http://www.gnu.org.
|
|
|
|
|
|
|
|
FUNCTIONAL DESCRIPTION
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
See header file.
|
|
|
|
|
|
|
|
HISTORY
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
11/17/98 JSB Created
|
|
|
|
|
|
|
|
********************************************************************************
|
|
|
|
INCLUDES
|
|
|
|
*******************************************************************************/
|
|
|
|
|
1999-02-26 22:09:10 +00:00
|
|
|
#ifdef FGFS
|
|
|
|
# include <Include/compiler.h>
|
|
|
|
# ifdef FG_HAVE_STD_INCLUDES
|
|
|
|
# include <cmath>
|
|
|
|
# else
|
|
|
|
# include <math.h>
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# include <cmath>
|
|
|
|
#endif
|
1999-02-13 01:12:03 +00:00
|
|
|
|
1999-02-05 21:26:01 +00:00
|
|
|
#include "FGState.h"
|
1999-02-11 21:05:34 +00:00
|
|
|
#include "FGFDMExec.h"
|
|
|
|
#include "FGAtmosphere.h"
|
|
|
|
#include "FGFCS.h"
|
1999-02-05 21:26:01 +00:00
|
|
|
#include "FGAircraft.h"
|
1999-02-11 21:05:34 +00:00
|
|
|
#include "FGTranslation.h"
|
|
|
|
#include "FGRotation.h"
|
|
|
|
#include "FGPosition.h"
|
|
|
|
#include "FGAuxiliary.h"
|
|
|
|
#include "FGOutput.h"
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
************************************ CODE **************************************
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
1999-02-11 21:05:34 +00:00
|
|
|
FGState::FGState(FGFDMExec* fdex)
|
1999-02-05 21:26:01 +00:00
|
|
|
{
|
1999-02-11 21:05:34 +00:00
|
|
|
FDMExec = fdex;
|
|
|
|
|
1999-02-05 21:26:01 +00:00
|
|
|
Vt = 0.0;
|
|
|
|
latitude = longitude = 0.0;
|
|
|
|
adot = bdot = 0.0;
|
|
|
|
h = 0.0;
|
|
|
|
a = 1000.0;
|
1999-02-11 21:05:34 +00:00
|
|
|
qbar = 0.0;
|
1999-02-05 21:26:01 +00:00
|
|
|
sim_time = dt = 0.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FGState::~FGState(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-26 22:09:10 +00:00
|
|
|
bool FGState::Reset(string path, string fname)
|
1999-02-05 21:26:01 +00:00
|
|
|
{
|
1999-02-13 01:12:03 +00:00
|
|
|
string resetDef;
|
1999-02-11 21:05:34 +00:00
|
|
|
float U, V, W;
|
|
|
|
float phi, tht, psi;
|
|
|
|
float alpha, beta, gamma;
|
|
|
|
float Q0, Q1, Q2, Q3;
|
|
|
|
float T[4][4];
|
1999-02-05 21:26:01 +00:00
|
|
|
|
1999-03-02 01:02:51 +00:00
|
|
|
resetDef = path + "/" + FDMExec->GetAircraft()->GetAircraftName() + "/" + fname;
|
1999-02-05 21:26:01 +00:00
|
|
|
|
1999-02-26 22:09:10 +00:00
|
|
|
ifstream resetfile(resetDef.c_str());
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
if (resetfile) {
|
|
|
|
resetfile >> U;
|
|
|
|
resetfile >> V;
|
|
|
|
resetfile >> W;
|
|
|
|
resetfile >> latitude;
|
|
|
|
resetfile >> longitude;
|
|
|
|
resetfile >> phi;
|
|
|
|
resetfile >> tht;
|
|
|
|
resetfile >> psi;
|
|
|
|
resetfile >> h;
|
|
|
|
resetfile.close();
|
|
|
|
|
|
|
|
// Change all angular measurements from degrees (as in config file) to radians
|
|
|
|
|
1999-02-11 21:05:34 +00:00
|
|
|
gamma = 0.0;
|
1999-02-05 21:26:01 +00:00
|
|
|
if (W != 0.0)
|
|
|
|
alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
|
1999-02-11 21:05:34 +00:00
|
|
|
else
|
|
|
|
alpha = 0.0;
|
1999-02-05 21:26:01 +00:00
|
|
|
if (V != 0.0)
|
|
|
|
beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
|
1999-02-11 21:05:34 +00:00
|
|
|
else
|
|
|
|
beta = 0.0;
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
latitude *= M_PI / 180.0;
|
|
|
|
longitude *= M_PI / 180.0;
|
|
|
|
phi *= M_PI / 180.0;
|
|
|
|
tht *= M_PI / 180.0;
|
|
|
|
psi *= M_PI / 180.0;
|
|
|
|
|
1999-02-11 21:05:34 +00:00
|
|
|
FDMExec->GetTranslation()->SetUVW(U, V, W);
|
|
|
|
FDMExec->GetRotation()->SetEuler(phi, tht, psi);
|
|
|
|
FDMExec->GetTranslation()->SetABG(alpha, beta, gamma);
|
|
|
|
|
1999-02-05 21:26:01 +00:00
|
|
|
Vt = sqrt(U*U + V*V + W*W);
|
1999-06-17 20:07:19 +00:00
|
|
|
qbar = sqrt(U*U + V*V + W*W);
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
Q0 = sin(psi*0.5)*sin(tht*0.5)*sin(phi*0.5) + cos(psi*0.5)*cos(tht*0.5)*cos(phi*0.5);
|
|
|
|
Q1 = -sin(psi*0.5)*sin(tht*0.5)*cos(phi*0.5) + cos(psi*0.5)*cos(tht*0.5)*sin(phi*0.5);
|
|
|
|
Q2 = sin(psi*0.5)*cos(tht*0.5)*sin(phi*0.5) + cos(psi*0.5)*sin(tht*0.5)*cos(phi*0.5);
|
|
|
|
Q3 = sin(psi*0.5)*cos(tht*0.5)*cos(phi*0.5) - cos(psi*0.5)*sin(tht*0.5)*sin(phi*0.5);
|
|
|
|
|
1999-02-11 21:05:34 +00:00
|
|
|
FDMExec->GetRotation()->SetQ0123(Q0, Q1, Q2, Q3);
|
|
|
|
|
1999-02-05 21:26:01 +00:00
|
|
|
T[1][1] = Q0*Q0 + Q1*Q1 - Q2*Q2 - Q3*Q3;
|
|
|
|
T[1][2] = 2*(Q1*Q2 + Q0*Q3);
|
|
|
|
T[1][3] = 2*(Q1*Q3 - Q0*Q2);
|
|
|
|
T[2][1] = 2*(Q1*Q2 - Q0*Q3);
|
|
|
|
T[2][2] = Q0*Q0 - Q1*Q1 + Q2*Q2 - Q3*Q3;
|
|
|
|
T[2][3] = 2*(Q2*Q3 + Q0*Q1);
|
|
|
|
T[3][1] = 2*(Q1*Q3 + Q0*Q2);
|
|
|
|
T[3][2] = 2*(Q2*Q3 - Q0*Q1);
|
|
|
|
T[3][3] = Q0*Q0 - Q1*Q1 - Q2*Q2 + Q3*Q3;
|
|
|
|
|
1999-02-11 21:05:34 +00:00
|
|
|
FDMExec->GetPosition()->SetT(T[1][1], T[1][2], T[1][3],
|
|
|
|
T[2][1], T[2][2], T[2][3],
|
|
|
|
T[3][1], T[3][2], T[3][3]);
|
|
|
|
|
1999-02-05 21:26:01 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
cerr << "Unable to load reset file " << fname << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-26 22:09:10 +00:00
|
|
|
bool FGState::StoreData(string fname)
|
1999-02-05 21:26:01 +00:00
|
|
|
{
|
1999-02-26 22:09:10 +00:00
|
|
|
ofstream datafile(fname.c_str());
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
if (datafile) {
|
1999-02-11 21:05:34 +00:00
|
|
|
datafile << FDMExec->GetTranslation()->GetU();
|
|
|
|
datafile << FDMExec->GetTranslation()->GetV();
|
|
|
|
datafile << FDMExec->GetTranslation()->GetW();
|
1999-02-05 21:26:01 +00:00
|
|
|
datafile << latitude;
|
|
|
|
datafile << longitude;
|
1999-02-11 21:05:34 +00:00
|
|
|
datafile << FDMExec->GetRotation()->Getphi();
|
|
|
|
datafile << FDMExec->GetRotation()->Gettht();
|
|
|
|
datafile << FDMExec->GetRotation()->Getpsi();
|
1999-02-05 21:26:01 +00:00
|
|
|
datafile << h;
|
|
|
|
datafile.close();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
cerr << "Could not open dump file " << fname << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-26 22:09:10 +00:00
|
|
|
bool FGState::DumpData(string fname)
|
1999-02-05 21:26:01 +00:00
|
|
|
{
|
1999-02-26 22:09:10 +00:00
|
|
|
ofstream datafile(fname.c_str());
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
if (datafile) {
|
1999-02-11 21:05:34 +00:00
|
|
|
datafile << "U: " << FDMExec->GetTranslation()->GetU() << endl;
|
|
|
|
datafile << "V: " << FDMExec->GetTranslation()->GetV() << endl;
|
|
|
|
datafile << "W: " << FDMExec->GetTranslation()->GetW() << endl;
|
|
|
|
datafile << "P: " << FDMExec->GetRotation()->GetP() << endl;
|
|
|
|
datafile << "Q: " << FDMExec->GetRotation()->GetQ() << endl;
|
|
|
|
datafile << "R: " << FDMExec->GetRotation()->GetR() << endl;
|
|
|
|
datafile << "L: " << FDMExec->GetAircraft()->GetL() << endl;
|
|
|
|
datafile << "M: " << FDMExec->GetAircraft()->GetM() << endl;
|
|
|
|
datafile << "N: " << FDMExec->GetAircraft()->GetN() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
datafile << "latitude: " << latitude << endl;
|
|
|
|
datafile << "longitude: " << longitude << endl;
|
1999-02-11 21:05:34 +00:00
|
|
|
datafile << "alpha: " << FDMExec->GetTranslation()->Getalpha() << endl;
|
|
|
|
datafile << "beta: " << FDMExec->GetTranslation()->Getbeta() << endl;
|
|
|
|
datafile << "gamma: " << FDMExec->GetTranslation()->Getgamma() << endl;
|
|
|
|
datafile << "phi: " << FDMExec->GetRotation()->Getphi() << endl;
|
|
|
|
datafile << "tht: " << FDMExec->GetRotation()->Gettht() << endl;
|
|
|
|
datafile << "psi: " << FDMExec->GetRotation()->Getpsi() << endl;
|
|
|
|
datafile << "Pdot: " << FDMExec->GetRotation()->GetPdot() << endl;
|
|
|
|
datafile << "Qdot: " << FDMExec->GetRotation()->GetQdot() << endl;
|
|
|
|
datafile << "Rdot: " << FDMExec->GetRotation()->GetRdot() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
datafile << "h: " << h << endl;
|
|
|
|
datafile << "a: " << a << endl;
|
1999-02-11 21:05:34 +00:00
|
|
|
datafile << "rho: " << FDMExec->GetAtmosphere()->Getrho() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
datafile << "qbar: " << qbar << endl;
|
|
|
|
datafile << "sim_time: " << sim_time << endl;
|
|
|
|
datafile << "dt: " << dt << endl;
|
1999-02-11 21:05:34 +00:00
|
|
|
datafile << "m: " << FDMExec->GetAircraft()->GetMass() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
datafile.close();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FGState::DisplayData(void)
|
|
|
|
{
|
1999-02-11 21:05:34 +00:00
|
|
|
cout << "U: " << FDMExec->GetTranslation()->GetU() << endl;
|
|
|
|
cout << "V: " << FDMExec->GetTranslation()->GetV() << endl;
|
|
|
|
cout << "W: " << FDMExec->GetTranslation()->GetW() << endl;
|
|
|
|
cout << "P: " << FDMExec->GetRotation()->GetP() << endl;
|
|
|
|
cout << "Q: " << FDMExec->GetRotation()->GetQ() << endl;
|
|
|
|
cout << "R: " << FDMExec->GetRotation()->GetR() << endl;
|
|
|
|
cout << "L: " << FDMExec->GetAircraft()->GetL() << endl;
|
|
|
|
cout << "M: " << FDMExec->GetAircraft()->GetM() << endl;
|
|
|
|
cout << "N: " << FDMExec->GetAircraft()->GetN() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
cout << "Vt: " << Vt << endl;
|
|
|
|
cout << "latitude: " << latitude << endl;
|
|
|
|
cout << "longitude: " << longitude << endl;
|
1999-02-11 21:05:34 +00:00
|
|
|
cout << "alpha: " << FDMExec->GetTranslation()->Getalpha() << endl;
|
|
|
|
cout << "beta: " << FDMExec->GetTranslation()->Getbeta() << endl;
|
|
|
|
cout << "gamma: " << FDMExec->GetTranslation()->Getgamma() << endl;
|
|
|
|
cout << "phi: " << FDMExec->GetRotation()->Getphi() << endl;
|
|
|
|
cout << "tht: " << FDMExec->GetRotation()->Gettht() << endl;
|
|
|
|
cout << "psi: " << FDMExec->GetRotation()->Getpsi() << endl;
|
|
|
|
cout << "Pdot: " << FDMExec->GetRotation()->GetPdot() << endl;
|
|
|
|
cout << "Qdot: " << FDMExec->GetRotation()->GetQdot() << endl;
|
|
|
|
cout << "Rdot: " << FDMExec->GetRotation()->GetRdot() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
cout << "h: " << h << endl;
|
|
|
|
cout << "a: " << a << endl;
|
1999-02-11 21:05:34 +00:00
|
|
|
cout << "rho: " << FDMExec->GetAtmosphere()->Getrho() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
cout << "qbar: " << qbar << endl;
|
|
|
|
cout << "sim_time: " << sim_time << endl;
|
|
|
|
cout << "dt: " << dt << endl;
|
1999-02-11 21:05:34 +00:00
|
|
|
cout << "m: " << FDMExec->GetAircraft()->GetMass() << endl;
|
1999-02-05 21:26:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|