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