]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGRotation.cpp
JSBSim updates. This update changes the file format, so an update of the base
[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 #include "FGPropertyManager.h"
70
71
72 static const char *IdSrc = "$Id$";
73 static const char *IdHdr = ID_ROTATION;
74
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 CLASS IMPLEMENTATION
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
78
79
80 FGRotation::FGRotation(FGFDMExec* fdmex) : FGModel(fdmex)
81 {
82   Name = "FGRotation";
83   cTht = cPhi = cPsi = 1.0;
84   sTht = sPhi = sPsi = 0.0;
85
86   vPQRdot.InitMatrix();
87   vPQRdot_prev[0].InitMatrix();
88   vPQRdot_prev[1].InitMatrix();
89   vPQRdot_prev[2].InitMatrix();
90
91   bind();
92   
93   Debug(0);
94 }
95
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98 FGRotation::~FGRotation()
99 {
100   unbind();
101   Debug(1);
102 }
103
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
106 bool FGRotation::Run(void)
107 {
108   double L2, N1;
109   double tTheta;
110
111   if (!FGModel::Run()) {
112     GetState();
113
114     L2 = vMoments(eL) + Ixz*vPQR(eP)*vPQR(eQ) - (Izz-Iyy)*vPQR(eR)*vPQR(eQ);
115     N1 = vMoments(eN) - (Iyy-Ixx)*vPQR(eP)*vPQR(eQ) - Ixz*vPQR(eR)*vPQR(eQ);
116
117     vPQRdot(eP) = (L2*Izz - N1*Ixz) / (Ixx*Izz - Ixz*Ixz);
118     vPQRdot(eQ) = (vMoments(eM) - (Ixx-Izz)*vPQR(eP)*vPQR(eR)
119                           - Ixz*(vPQR(eP)*vPQR(eP) - vPQR(eR)*vPQR(eR)))/Iyy;
120     vPQRdot(eR) = (N1*Ixx + L2*Ixz) / (Ixx*Izz - Ixz*Ixz);
121
122     vPQR += State->Integrate(FGState::TRAPZ, dt*rate, vPQRdot, vPQRdot_prev);
123     
124     vAeroPQR = vPQR + Atmosphere->GetTurbPQR();
125
126     State->IntegrateQuat(vPQR, rate);
127     State->CalcMatrices();
128     vEuler = State->CalcEuler();
129
130     cTht = cos(vEuler(eTht));   sTht = sin(vEuler(eTht));
131     cPhi = cos(vEuler(ePhi));   sPhi = sin(vEuler(ePhi));
132     cPsi = cos(vEuler(ePsi));   sPsi = sin(vEuler(ePsi));
133
134     vEulerRates(eTht) = vPQR(2)*cPhi - vPQR(3)*sPhi;
135     if (cTht != 0.0) {
136       tTheta = sTht/cTht;       // what's cheaper: / or tan() ?
137       vEulerRates(ePhi) = vPQR(1) + (vPQR(2)*sPhi + vPQR(3)*cPhi)*tTheta;
138       vEulerRates(ePsi) = (vPQR(2)*sPhi + vPQR(3)*cPhi)/cTht;
139     }
140
141     if (debug_lvl > 1) Debug(2);
142
143     return false;
144   } else {
145     return true;
146   }
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 void FGRotation::GetState(void)
152 {
153   dt = State->Getdt();
154   vMoments = Aircraft->GetMoments();
155
156   Ixx = MassBalance->GetIxx();
157   Iyy = MassBalance->GetIyy();
158   Izz = MassBalance->GetIzz();
159   Ixz = MassBalance->GetIxz();
160 }
161
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164 void FGRotation::bind(void)
165 {
166   typedef double (FGRotation::*PMF)(int) const;
167   PropertyManager->Tie("velocities/p-rad_sec", this,1,
168                        (PMF)&FGRotation::GetPQR);
169   PropertyManager->Tie("velocities/q-rad_sec", this,2,
170                        (PMF)&FGRotation::GetPQR);
171   PropertyManager->Tie("velocities/r-rad_sec", this,3,
172                        (PMF)&FGRotation::GetPQR);
173   PropertyManager->Tie("velocities/p-aero-rad_sec", this,1,
174                        (PMF)&FGRotation::GetAeroPQR);
175   PropertyManager->Tie("velocities/q-aero-rad_sec", this,2,
176                        (PMF)&FGRotation::GetAeroPQR);
177   PropertyManager->Tie("velocities/r-aero-rad_sec", this,3,
178                        (PMF)&FGRotation::GetAeroPQR);
179   PropertyManager->Tie("accelerations/pdot-rad_sec", this,1,
180                        (PMF)&FGRotation::GetPQRdot);
181   PropertyManager->Tie("accelerations/qdot-rad_sec", this,2,
182                        (PMF)&FGRotation::GetPQRdot);
183   PropertyManager->Tie("accelerations/rdot-rad_sec", this,3,
184                        (PMF)&FGRotation::GetPQRdot);
185   PropertyManager->Tie("attitude/roll-rad", this,1,
186                        (PMF)&FGRotation::GetEuler);
187   PropertyManager->Tie("attitude/pitch-rad", this,2,
188                        (PMF)&FGRotation::GetEuler);
189   PropertyManager->Tie("attitude/heading-true-rad", this,3,
190                        (PMF)&FGRotation::GetEuler);
191   PropertyManager->Tie("velocities/phidot-rad_sec", this,1,
192                        (PMF)&FGRotation::GetEulerRates);
193   PropertyManager->Tie("velocities/thetadot-rad_sec", this,2,
194                        (PMF)&FGRotation::GetEulerRates);
195   PropertyManager->Tie("velocities/psidot-rad_sec", this,3,
196                        (PMF)&FGRotation::GetEulerRates);
197   PropertyManager->Tie("attitude/phi-rad", this,
198                        &FGRotation::Getphi);
199   PropertyManager->Tie("attitude/theta-rad", this,
200                        &FGRotation::Gettht);
201   PropertyManager->Tie("attitude/psi-true-rad", this,
202                        &FGRotation::Getpsi);
203 }
204
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206
207 void FGRotation::unbind(void)
208 {
209   PropertyManager->Untie("velocities/p-rad_sec");
210   PropertyManager->Untie("velocities/q-rad_sec");
211   PropertyManager->Untie("velocities/r-rad_sec");
212   PropertyManager->Untie("velocities/p-aero-rad_sec");
213   PropertyManager->Untie("velocities/q-aero-rad_sec");
214   PropertyManager->Untie("velocities/r-aero-rad_sec");
215   PropertyManager->Untie("accelerations/pdot-rad_sec");
216   PropertyManager->Untie("accelerations/qdot-rad_sec");
217   PropertyManager->Untie("accelerations/rdot-rad_sec");
218   PropertyManager->Untie("attitude/roll-rad");
219   PropertyManager->Untie("attitude/pitch-rad");
220   PropertyManager->Untie("attitude/heading-true-rad");
221   PropertyManager->Untie("velocities/phidot-rad_sec");
222   PropertyManager->Untie("velocities/thetadot-rad_sec");
223   PropertyManager->Untie("velocities/psidot-rad_sec");
224   PropertyManager->Untie("attitude/phi-rad");
225   PropertyManager->Untie("attitude/theta-rad");
226   PropertyManager->Untie("attitude/psi-true-rad");
227 }
228
229 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 //    The bitmasked value choices are as follows:
231 //    unset: In this case (the default) JSBSim would only print
232 //       out the normally expected messages, essentially echoing
233 //       the config files as they are read. If the environment
234 //       variable is not set, debug_lvl is set to 1 internally
235 //    0: This requests JSBSim not to output any messages
236 //       whatsoever.
237 //    1: This value explicity requests the normal JSBSim
238 //       startup messages
239 //    2: This value asks for a message to be printed out when
240 //       a class is instantiated
241 //    4: When this value is set, a message is displayed when a
242 //       FGModel object executes its Run() method
243 //    8: When this value is set, various runtime state variables
244 //       are printed out periodically
245 //    16: When set various parameters are sanity checked and
246 //       a message is printed out when they go out of bounds
247
248 void FGRotation::Debug(int from)
249 {
250   if (debug_lvl <= 0) return;
251
252   if (debug_lvl & 1) { // Standard console startup message output
253     if (from == 0) { // Constructor
254
255     }
256   }
257   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
258     if (from == 0) cout << "Instantiated: FGRotation" << endl;
259     if (from == 1) cout << "Destroyed:    FGRotation" << endl;
260   }
261   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
262   }
263   if (debug_lvl & 8 ) { // Runtime state variables
264   }
265   if (debug_lvl & 16) { // Sanity check variables
266     if (from == 2) {
267       if (fabs(vPQR(eP)) > 100)
268         cout << "FGRotation::P (Roll Rate) out of bounds: " << vPQR(eP) << endl;
269       if (fabs(vPQR(eQ)) > 100)
270         cout << "FGRotation::Q (Pitch Rate) out of bounds: " << vPQR(eQ) << endl;
271       if (fabs(vPQR(eR)) > 100)
272         cout << "FGRotation::R (Yaw Rate) out of bounds: " << vPQR(eR) << endl;
273     }
274   }
275   if (debug_lvl & 64) {
276     if (from == 0) { // Constructor
277       cout << IdSrc << endl;
278       cout << IdHdr << endl;
279     }
280   }
281 }
282