]> git.mxchange.org Git - flightgear.git/blob - JSBsim/FGRotation.cpp
3b49f2e82376e9fad6b4218f25a8eb3efdda7448
[flightgear.git] / JSBsim / FGRotation.cpp
1 /*******************************************************************************
2
3  Module:       FGRotation.cpp
4  Author:       Jon Berndt
5  Date started: 12/02/98
6  Purpose:      Integrates the rotational EOM
7  Called by:    FGFDMExec
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  details.
20
21  You should have received a copy of the GNU General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class integrates the rotational EOM.
31
32 ARGUMENTS
33 --------------------------------------------------------------------------------
34
35
36 HISTORY
37 --------------------------------------------------------------------------------
38 12/02/98   JSB   Created
39
40 ********************************************************************************
41 COMMENTS, REFERENCES,  and NOTES
42 ********************************************************************************
43 [1] Cooke, Zyda, Pratt, and McGhee, "NPSNET: Flight Simulation Dynamic Modeling
44     Using Quaternions", Presence, Vol. 1, No. 4, pp. 404-420  Naval Postgraduate
45     School, January 1994
46 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
47     JSC 12960, July 1977
48 [3] Richard E. McFarland, "A Standard Kinematic Model for Flight Simulation at
49     NASA-Ames", NASA CR-2497, January 1975
50 [4] Barnes W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics",
51     Wiley & Sons, 1979 ISBN 0-471-03032-5
52 [5] Bernard Etkin, "Dynamics of Flight, Stability and Control", Wiley & Sons,
53     1982 ISBN 0-471-08936-2
54
55   The order of rotations used in this class corresponds to a 3-2-1 sequence,
56   or Y-P-R, or Z-Y-X, if you prefer.
57
58 ********************************************************************************
59 INCLUDES
60 *******************************************************************************/
61
62 #include "FGRotation.h"
63
64 /*******************************************************************************
65 ************************************ CODE **************************************
66 *******************************************************************************/
67
68
69 FGRotation::FGRotation(void) : FGModel()
70 {
71   strcpy(Name, "FGRotation");
72   Q0dot = Q1dot = Q2dot = Q3dot = 0.0;
73 }
74
75
76 FGRotation::~FGRotation(void)
77 {
78 }
79
80
81 bool FGRotation::Run(void)
82 {
83   float L2, N1, iQtot, sum;
84
85   if (!FGModel::Run()) {
86     GetState();
87
88     lastPdot = Pdot;
89     lastQdot = Qdot;
90     lastRdot = Rdot;
91
92     L2 = L + Ixz*P*Q - (Izz-Iyy)*R*Q;
93     N1 = N - (Iyy-Ixx)*P*Q - Ixz*R*Q;
94
95     Pdot = (L2*Izz - N1*Ixz) / (Ixx*Izz - Ixz*Ixz);
96     Qdot = (M - (Ixx-Izz)*P*R - Ixz*(P*P - R*R))/Iyy;
97     Rdot = (N1*Ixx + L2*Ixz) / (Ixx*Izz - Ixz*Ixz);
98
99     P += dt*(lastPdot + Pdot)/2.0;
100     Q += dt*(lastQdot + Qdot)/2.0;
101     R += dt*(lastRdot + Rdot)/2.0;
102
103     lastQ0dot = Q0dot;
104     lastQ1dot = Q1dot;
105     lastQ2dot = Q2dot;
106     lastQ3dot = Q3dot;
107
108     Q0dot = -0.5*(Q1*P + Q2*Q + Q3*R);
109     Q1dot =  0.5*(Q0*P + Q2*R - Q3*Q);
110     Q2dot =  0.5*(Q0*Q + Q3*P - Q1*R);
111     Q3dot =  0.5*(Q0*R + Q1*Q - Q2*P);
112
113     Q0 += 0.5*dt*(lastQ0dot + Q0dot);
114     Q1 += 0.5*dt*(lastQ1dot + Q1dot);
115     Q2 += 0.5*dt*(lastQ2dot + Q2dot);
116     Q3 += 0.5*dt*(lastQ3dot + Q3dot);
117
118     sum = Q0*Q0 + Q1*Q1 + Q2*Q2 + Q3*Q3;
119
120     iQtot = 1.0 / sqrt(sum);
121
122     Q0 *= iQtot;
123     Q1 *= iQtot;
124     Q2 *= iQtot;
125     Q3 *= iQtot;
126
127     if (T[3][3] == 0)
128       phi = 0.0;
129     else
130       phi = atan2(T[2][3], T[3][3]);
131
132     tht = asin(-T[1][3]);
133
134     if (T[1][1] == 0.0)
135       psi = 0.0;
136     else
137       psi = atan2(T[1][2], T[1][1]);
138
139     if (psi < 0.0) psi += 2*M_PI;
140
141     PutState();
142   } else {
143   }
144   return false;
145 }
146
147
148 void FGRotation::GetState(void)
149 {
150   dt = State->Getdt();
151   P = State->GetP();
152   Q = State->GetQ();
153   R = State->GetR();
154
155   Pdot = State->GetPdot();
156   Qdot = State->GetQdot();
157   Rdot = State->GetRdot();
158
159   L = State->GetL();
160   M = State->GetM();
161   N = State->GetN();
162
163   Ixx = State->GetIxx();
164   Iyy = State->GetIyy();
165   Izz = State->GetIzz();
166   Ixz = State->GetIxz();
167
168   Q0 = State->GetQ0();
169   Q1 = State->GetQ1();
170   Q2 = State->GetQ2();
171   Q3 = State->GetQ3();
172
173   for (int r=1;r<=3;r++)
174     for (int c=1;c<=3;c++)
175       T[r][c] = State->GetT(r,c);
176 }
177
178
179 void FGRotation::PutState(void)
180 {
181    State->SetP(P);
182    State->SetQ(Q);
183    State->SetR(R);
184
185    State->Setphi(phi);
186    State->Settht(tht);
187    State->Setpsi(psi);
188
189    State->SetQ0123(Q0, Q1, Q2, Q3);
190 }
191