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