]> git.mxchange.org Git - flightgear.git/blob - JSBsim/FGState.cpp
Added initial support for native SGI compilers.
[flightgear.git] / JSBsim / FGState.cpp
1 /*******************************************************************************
2                                                                        
3  Module:       FGState.cpp
4  Author:       Jon Berndt
5  Date started: 11/17/98
6  Called by:    FGFDMExec and accessed by all models.
7
8  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
9
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
13  version.
14
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
18  details.
19
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.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 See header file.
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 11/17/98   JSB   Created
34
35 ********************************************************************************
36 INCLUDES
37 *******************************************************************************/
38
39 #ifdef FGFS
40 #  include <Include/compiler.h>
41 #  ifdef FG_HAVE_STD_INCLUDES
42 #    include <cmath>
43 #  else
44 #    include <math.h>
45 #  endif
46 #else
47 #  include <cmath>
48 #endif
49
50 #include "FGState.h"
51 #include "FGFDMExec.h"
52 #include "FGAtmosphere.h"
53 #include "FGFCS.h"
54 #include "FGAircraft.h"
55 #include "FGTranslation.h"
56 #include "FGRotation.h"
57 #include "FGPosition.h"
58 #include "FGAuxiliary.h"
59 #include "FGOutput.h"
60
61 /*******************************************************************************
62 ************************************ CODE **************************************
63 *******************************************************************************/
64
65
66 FGState::FGState(FGFDMExec* fdex)
67 {
68   FDMExec = fdex;
69
70   Vt = 0.0;
71   latitude = longitude = 0.0;
72   adot = bdot = 0.0;
73   h = 0.0;
74   a = 1000.0;
75   qbar = 0.0;
76   sim_time = dt = 0.1;
77 }
78
79
80 FGState::~FGState(void)
81 {
82 }
83
84
85 bool FGState::Reset(string path, string fname)
86 {
87   string resetDef;
88   float U, V, W;
89   float phi, tht, psi;
90   float alpha, beta, gamma;
91   float Q0, Q1, Q2, Q3;
92   float T[4][4];
93
94   resetDef = path + "/" + FDMExec->GetAircraft()->GetAircraftName() + 
95       "/" + fname;
96
97   ifstream resetfile(resetDef.c_str());
98
99   if (resetfile) {
100     resetfile >> U;
101     resetfile >> V;
102     resetfile >> W;
103     resetfile >> latitude;
104     resetfile >> longitude;
105     resetfile >> phi;
106     resetfile >> tht;
107     resetfile >> psi;
108     resetfile >> h;
109     resetfile.close();
110
111 // Change all angular measurements from degrees (as in config file) to radians
112
113     gamma = 0.0;
114     if (W != 0.0)
115       alpha = U*U > 0.0 ? atan2(W, U) : 0.0;
116     else
117       alpha = 0.0;
118     if (V != 0.0)
119       beta = U*U+W*W > 0.0 ? atan2(V, (fabs(U)/U)*sqrt(U*U + W*W)) : 0.0;
120     else
121       beta = 0.0;
122
123     latitude  *= M_PI / 180.0;
124     longitude *= M_PI / 180.0;
125     phi       *= M_PI / 180.0;
126     tht       *= M_PI / 180.0;
127     psi       *= M_PI / 180.0;
128
129     FDMExec->GetTranslation()->SetUVW(U, V, W);
130     FDMExec->GetRotation()->SetEuler(phi, tht, psi);
131     FDMExec->GetTranslation()->SetABG(alpha, beta, gamma);
132
133     Vt = sqrt(U*U + V*V + W*W);
134     qbar = sqrt(U*U + V*V + W*W);
135
136     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);
137     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);
138     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);
139     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);
140
141     FDMExec->GetRotation()->SetQ0123(Q0, Q1, Q2, Q3);
142
143     T[1][1] = Q0*Q0 + Q1*Q1 - Q2*Q2 - Q3*Q3;
144     T[1][2] = 2*(Q1*Q2 + Q0*Q3);
145     T[1][3] = 2*(Q1*Q3 - Q0*Q2);
146     T[2][1] = 2*(Q1*Q2 - Q0*Q3);
147     T[2][2] = Q0*Q0 - Q1*Q1 + Q2*Q2 - Q3*Q3;
148     T[2][3] = 2*(Q2*Q3 + Q0*Q1);
149     T[3][1] = 2*(Q1*Q3 + Q0*Q2);
150     T[3][2] = 2*(Q2*Q3 - Q0*Q1);
151     T[3][3] = Q0*Q0 - Q1*Q1 - Q2*Q2 + Q3*Q3;
152
153     FDMExec->GetPosition()->SetT(T[1][1], T[1][2], T[1][3],
154                                  T[2][1], T[2][2], T[2][3],
155                                  T[3][1], T[3][2], T[3][3]);
156
157     return true;
158   } else {
159     cerr << "Unable to load reset file " << fname << endl;
160     return false;
161   }
162 }
163
164
165 bool FGState::StoreData(string fname)
166 {
167   ofstream datafile(fname.c_str());
168
169   if (datafile) {
170     datafile << FDMExec->GetTranslation()->GetU();
171     datafile << FDMExec->GetTranslation()->GetV();
172     datafile << FDMExec->GetTranslation()->GetW();
173     datafile << latitude;
174     datafile << longitude;
175     datafile << FDMExec->GetRotation()->Getphi();
176     datafile << FDMExec->GetRotation()->Gettht();
177     datafile << FDMExec->GetRotation()->Getpsi();
178     datafile << h;
179     datafile.close();
180     return true;
181   } else {
182     cerr << "Could not open dump file " << fname << endl;
183     return false;
184   }
185 }
186
187
188 bool FGState::DumpData(string fname)
189 {
190   ofstream datafile(fname.c_str());
191
192   if (datafile) {
193     datafile << "U: " << FDMExec->GetTranslation()->GetU() << endl;
194     datafile << "V: " << FDMExec->GetTranslation()->GetV() << endl;
195     datafile << "W: " << FDMExec->GetTranslation()->GetW() << endl;
196     datafile << "P: " << FDMExec->GetRotation()->GetP() << endl;
197     datafile << "Q: " << FDMExec->GetRotation()->GetQ() << endl;
198     datafile << "R: " << FDMExec->GetRotation()->GetR() << endl;
199     datafile << "L: " << FDMExec->GetAircraft()->GetL() << endl;
200     datafile << "M: " << FDMExec->GetAircraft()->GetM() << endl;
201     datafile << "N: " << FDMExec->GetAircraft()->GetN() << endl;
202     datafile << "latitude: " << latitude << endl;
203     datafile << "longitude: " << longitude << endl;
204     datafile << "alpha: " << FDMExec->GetTranslation()->Getalpha() << endl;
205     datafile << "beta: " << FDMExec->GetTranslation()->Getbeta() << endl;
206     datafile << "gamma: " << FDMExec->GetTranslation()->Getgamma() << endl;
207     datafile << "phi: " << FDMExec->GetRotation()->Getphi() << endl;
208     datafile << "tht: " << FDMExec->GetRotation()->Gettht() << endl;
209     datafile << "psi: " << FDMExec->GetRotation()->Getpsi() << endl;
210     datafile << "Pdot: " << FDMExec->GetRotation()->GetPdot() << endl;
211     datafile << "Qdot: " << FDMExec->GetRotation()->GetQdot() << endl;
212     datafile << "Rdot: " << FDMExec->GetRotation()->GetRdot() << endl;
213     datafile << "h: " << h << endl;
214     datafile << "a: " << a << endl;
215     datafile << "rho: " << FDMExec->GetAtmosphere()->Getrho() << endl;
216     datafile << "qbar: " << qbar << endl;
217     datafile << "sim_time: " << sim_time << endl;
218     datafile << "dt: " << dt << endl;
219     datafile << "m: " << FDMExec->GetAircraft()->GetMass() << endl;
220     datafile.close();
221     return true;
222   } else {
223     return false;
224   }
225 }
226
227
228 bool FGState::DisplayData(void)
229 {
230   cout << "U: " << FDMExec->GetTranslation()->GetU() << endl;
231   cout << "V: " << FDMExec->GetTranslation()->GetV() << endl;
232   cout << "W: " << FDMExec->GetTranslation()->GetW() << endl;
233   cout << "P: " << FDMExec->GetRotation()->GetP() << endl;
234   cout << "Q: " << FDMExec->GetRotation()->GetQ() << endl;
235   cout << "R: " << FDMExec->GetRotation()->GetR() << endl;
236   cout << "L: " << FDMExec->GetAircraft()->GetL() << endl;
237   cout << "M: " << FDMExec->GetAircraft()->GetM() << endl;
238   cout << "N: " << FDMExec->GetAircraft()->GetN() << endl;
239   cout << "Vt: " << Vt << endl;
240   cout << "latitude: " << latitude << endl;
241   cout << "longitude: " << longitude << endl;
242   cout << "alpha: " << FDMExec->GetTranslation()->Getalpha() << endl;
243   cout << "beta: " << FDMExec->GetTranslation()->Getbeta() << endl;
244   cout << "gamma: " << FDMExec->GetTranslation()->Getgamma() << endl;
245   cout << "phi: " << FDMExec->GetRotation()->Getphi() << endl;
246   cout << "tht: " << FDMExec->GetRotation()->Gettht() << endl;
247   cout << "psi: " << FDMExec->GetRotation()->Getpsi() << endl;
248   cout << "Pdot: " << FDMExec->GetRotation()->GetPdot() << endl;
249   cout << "Qdot: " << FDMExec->GetRotation()->GetQdot() << endl;
250   cout << "Rdot: " << FDMExec->GetRotation()->GetRdot() << endl;
251   cout << "h: " << h << endl;
252   cout << "a: " << a << endl;
253   cout << "rho: " << FDMExec->GetAtmosphere()->Getrho() << endl;
254   cout << "qbar: " << qbar << endl;
255   cout << "sim_time: " << sim_time << endl;
256   cout << "dt: " << dt << endl;
257   cout << "m: " << FDMExec->GetAircraft()->GetMass() << endl;
258
259   return true;
260 }