]> git.mxchange.org Git - flightgear.git/blob - JSBsim/FGPosition.cpp
d7e4de682bfbaacff56b16c557c63a41730b75ce
[flightgear.git] / JSBsim / FGPosition.cpp
1 /*******************************************************************************
2
3  Module:       FGPosition.cpp
4  Author:       Jon S. Berndt
5  Date started: 01/05/99
6  Purpose:      Integrate the EOM to determine instantaneous position
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 encapsulates the integration of rates and accelerations to get the
31 current position of the aircraft.
32
33 ARGUMENTS
34 --------------------------------------------------------------------------------
35 None
36
37 HISTORY
38 --------------------------------------------------------------------------------
39 01/05/99   JSB   Created
40
41 ********************************************************************************
42 COMMENTS, REFERENCES,  and NOTES
43 ********************************************************************************
44 [1] Cooke, Zyda, Pratt, and McGhee, "NPSNET: Flight Simulation Dynamic Modeling
45     Using Quaternions", Presence, Vol. 1, No. 4, pp. 404-420  Naval Postgraduate
46     School, January 1994
47 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
48     JSC 12960, July 1977
49 [3] Richard E. McFarland, "A Standard Kinematic Model for Flight Simulation at
50     NASA-Ames", NASA CR-2497, January 1975
51 [4] Barnes W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics",
52     Wiley & Sons, 1979 ISBN 0-471-03032-5
53 [5] Bernard Etkin, "Dynamics of Flight, Stability and Control", Wiley & Sons,
54     1982 ISBN 0-471-08936-2
55
56 ********************************************************************************
57 INCLUDES
58 *******************************************************************************/
59
60 #include "FGPosition.h"
61 #include <math.h>
62
63 /*******************************************************************************
64 ************************************ CODE **************************************
65 *******************************************************************************/
66
67
68 FGPosition::FGPosition(void) : FGModel()
69 {
70   strcpy(Name, "FGPosition");
71   EarthRad = 20898908.00;       // feet
72   OmegaEarth = 7.2685E-3;       // rad/sec
73   AccelN = AccelE = AccelD = 0.0;
74   LongitudeDot = LatitudeDot = RadiusDot = 0.0;
75 }
76
77
78 FGPosition::~FGPosition(void)
79 {
80 }
81
82
83 bool FGPosition:: Run(void)
84 {
85   float tanLat, cosLat;
86
87   if (!FGModel::Run()) {
88     GetState();
89     T[1][1] = Q0*Q0 + Q1*Q1 - Q2*Q2 - Q3*Q3;                    // Page A-11
90     T[1][2] = 2*(Q1*Q2 + Q0*Q3);                                // From
91     T[1][3] = 2*(Q1*Q3 - Q0*Q2);                                // Reference [2]
92     T[2][1] = 2*(Q1*Q2 - Q0*Q3);
93     T[2][2] = Q0*Q0 - Q1*Q1 + Q2*Q2 - Q3*Q3;
94     T[2][3] = 2*(Q2*Q3 + Q0*Q1);
95     T[3][1] = 2*(Q1*Q3 + Q0*Q2);
96     T[3][2] = 2*(Q2*Q3 - Q0*Q1);
97     T[3][3] = Q0*Q0 - Q1*Q1 - Q2*Q2 + Q3*Q3;
98
99     Fn = T[1][1]*Fx + T[2][1]*Fy + T[3][1]*Fz;                  // Eqn. 3.5
100     Fe = T[1][2]*Fx + T[2][2]*Fy + T[3][2]*Fz;                  // From
101     Fd = T[1][3]*Fx + T[2][3]*Fy + T[3][3]*Fz;                  // Reference [3]
102
103     tanLat = tan(Latitude);                                     // I made this up
104     cosLat = cos(Latitude);
105
106     lastAccelN = AccelN;
107     lastAccelE = AccelE;
108     lastAccelD = AccelD;
109
110     Vn = T[1][1]*U + T[2][1]*V + T[3][1]*W;
111     Ve = T[1][2]*U + T[2][2]*V + T[3][2]*W;
112     Vd = T[1][3]*U + T[2][3]*V + T[3][3]*W;
113
114     AccelN = invMass * Fn + invRadius * (Vn*Vd - Ve*Ve*tanLat); // Eqn. 3.6
115     AccelE = invMass * Fe + invRadius * (Ve*Vd + Vn*Ve*tanLat); // From
116     AccelD = invMass * Fd - invRadius * (Vn*Vn + Ve*Ve);        // Reference [3]
117
118     Vn += 0.5*dt*(3.0*AccelN - lastAccelN);                     // Eqn. 3.7
119     Ve += 0.5*dt*(3.0*AccelE - lastAccelE);                     // From
120     Vd += 0.5*dt*(3.0*AccelD - lastAccelD);                     // Reference [3]
121
122     Vee = Ve - OmegaEarth * (Radius) * cosLat;                  // From Eq. 3.8
123                                                                 // Reference [3]
124     lastLatitudeDot = LatitudeDot;
125     lastLongitudeDot = LongitudeDot;
126     lastRadiusDot = RadiusDot;
127
128     if (cosLat != 0) LongitudeDot = Ve / (Radius * cosLat);
129     LatitudeDot = Vn * invRadius;
130     RadiusDot = -Vd;
131
132     Longitude += 0.5*dt*(LongitudeDot + lastLongitudeDot);
133     Latitude  += 0.5*dt*(LatitudeDot + lastLatitudeDot);
134     Radius    += 0.5*dt*(RadiusDot + lastRadiusDot);
135
136     PutState();
137     return false;
138   } else {
139     return true;
140   }
141 }
142
143
144 void FGPosition::GetState(void)
145 {
146   Q0 = State->GetQ0();
147   Q1 = State->GetQ1();
148   Q2 = State->GetQ2();
149   Q3 = State->GetQ3();
150
151   Fx = State->GetFx();
152   Fy = State->GetFy();
153   Fz = State->GetFz();
154
155   U = State->GetU();
156   V = State->GetV();
157   W = State->GetW();
158
159   Latitude = State->Getlatitude();
160   Longitude = State->Getlongitude();
161
162   invMass = 1.0 / State->Getm();
163   invRadius = 1.0 / (State->Geth() + EarthRad);
164   Radius = State->Geth() + EarthRad;
165   dt = State->Getdt();
166 }
167
168
169 void FGPosition::PutState(void)
170 {
171   for (int r=1;r<=3;r++)
172     for (int c=1;c<=3;c++)
173       State->SetT(r,c,T[r][c]);
174
175   State->Setlatitude(Latitude);
176   State->Setlongitude(Longitude);
177   State->Seth(Radius - EarthRad);
178
179   State->SetVn(Vn); // remove after testing
180   State->SetVe(Ve); // remove after testing
181   State->SetVd(Vd); // remove after testing
182 }
183