]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGRotation.cpp
Syncing with latest JSBSim code with retractable landing gear support.
[flightgear.git] / src / FDM / 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 HISTORY
33 --------------------------------------------------------------------------------
34 12/02/98   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES,  and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 [1] Cooke, Zyda, Pratt, and McGhee, "NPSNET: Flight Simulation Dynamic Modeling
40     Using Quaternions", Presence, Vol. 1, No. 4, pp. 404-420  Naval Postgraduate
41     School, January 1994
42 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
43     JSC 12960, July 1977
44 [3] Richard E. McFarland, "A Standard Kinematic Model for Flight Simulation at
45     NASA-Ames", NASA CR-2497, January 1975
46 [4] Barnes W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics",
47     Wiley & Sons, 1979 ISBN 0-471-03032-5
48 [5] Bernard Etkin, "Dynamics of Flight, Stability and Control", Wiley & Sons,
49     1982 ISBN 0-471-08936-2
50
51   The order of rotations used in this class corresponds to a 3-2-1 sequence,
52   or Y-P-R, or Z-Y-X, if you prefer.
53
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 INCLUDES
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58 #include "FGRotation.h"
59 #include "FGAtmosphere.h"
60 #include "FGState.h"
61 #include "FGFDMExec.h"
62 #include "FGFCS.h"
63 #include "FGAircraft.h"
64 #include "FGMassBalance.h"
65 #include "FGTranslation.h"
66 #include "FGPosition.h"
67 #include "FGAuxiliary.h"
68 #include "FGOutput.h"
69
70 static const char *IdSrc = "$Id$";
71 static const char *IdHdr = ID_ROTATION;
72
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 CLASS IMPLEMENTATION
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76
77
78 FGRotation::FGRotation(FGFDMExec* fdmex) : FGModel(fdmex),
79         vPQR(3),
80         vAeroPQR(3),
81         vPQRdot(3),
82         vMoments(3),
83         vEuler(3),
84         vEulerRates(3),
85         vlastPQRdot(3)
86 {
87   Name = "FGRotation";
88   cTht=cPhi=cPsi=1.0;
89   sTht=sPhi=sPsi=0.0;
90
91   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 FGRotation::~FGRotation()
97 {
98   if (debug_lvl & 2) cout << "Destroyed:    FGRotation" << endl;
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 bool FGRotation::Run(void)
104 {
105   double L2, N1;
106   double tTheta;
107
108   if (!FGModel::Run()) {
109     GetState();
110
111     L2 = vMoments(eL) + Ixz*vPQR(eP)*vPQR(eQ) - (Izz-Iyy)*vPQR(eR)*vPQR(eQ);
112     N1 = vMoments(eN) - (Iyy-Ixx)*vPQR(eP)*vPQR(eQ) - Ixz*vPQR(eR)*vPQR(eQ);
113
114     vPQRdot(eP) = (L2*Izz - N1*Ixz) / (Ixx*Izz - Ixz*Ixz);
115     vPQRdot(eQ) = (vMoments(eM) - (Ixx-Izz)*vPQR(eP)*vPQR(eR) - Ixz*(vPQR(eP)*vPQR(eP) - vPQR(eR)*vPQR(eR)))/Iyy;
116     vPQRdot(eR) = (N1*Ixx + L2*Ixz) / (Ixx*Izz - Ixz*Ixz);
117
118     vPQR += dt*rate*(vlastPQRdot + vPQRdot)/2.0;
119     vAeroPQR = vPQR + Atmosphere->GetTurbPQR();
120
121     State->IntegrateQuat(vPQR, rate);
122     State->CalcMatrices();
123     vEuler = State->CalcEuler();
124
125     cTht = cos(vEuler(eTht));   sTht = sin(vEuler(eTht));
126     cPhi = cos(vEuler(ePhi));   sPhi = sin(vEuler(ePhi));
127     cPsi = cos(vEuler(ePsi));   sPsi = sin(vEuler(ePsi));
128
129     vEulerRates(eTht) = vPQR(2)*cPhi - vPQR(3)*sPhi;
130     if (cTht != 0.0) {
131       tTheta = sTht/cTht;       // what's cheaper: / or tan() ?
132       vEulerRates(ePhi) = vPQR(1) + (vPQR(2)*sPhi + vPQR(3)*cPhi)*tTheta;
133       vEulerRates(ePsi) = (vPQR(2)*sPhi + vPQR(3)*cPhi)/cTht;
134     }
135
136     vlastPQRdot = vPQRdot;
137
138     if (debug_lvl > 1) Debug();
139
140     return false;
141   } else {
142     return true;
143   }
144 }
145
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147
148 void FGRotation::GetState(void)
149 {
150   dt = State->Getdt();
151   vMoments = Aircraft->GetMoments();
152
153   Ixx = MassBalance->GetIxx();
154   Iyy = MassBalance->GetIyy();
155   Izz = MassBalance->GetIzz();
156   Ixz = MassBalance->GetIxz();
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 void FGRotation::Debug(void)
162 {
163   if (debug_lvl & 16) { // Sanity check variables
164     if (fabs(vPQR(eP)) > 100)
165       cout << "FGRotation::P (Roll Rate) out of bounds: " << vPQR(eP) << endl;
166     if (fabs(vPQR(eQ)) > 100)
167       cout << "FGRotation::Q (Pitch Rate) out of bounds: " << vPQR(eQ) << endl;
168     if (fabs(vPQR(eR)) > 100)
169       cout << "FGRotation::R (Yaw Rate) out of bounds: " << vPQR(eR) << endl;
170   }
171 }
172