]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPosition.cpp
Updates from JSBSim, including new turbine engine model from David Culp
[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 <simgear/compiler.h>
58 #  ifdef SG_HAVE_STD_INCLUDES
59 #    include <cmath>
60 #    include <iomanip>
61 #  else
62 #    include <math.h>
63 #    include <iomanip.h>
64 #  endif
65 #else
66 #  if defined(sgi) && !defined(__GNUC__)
67 #    include <math.h>
68 #    include <iomanip.h>
69 #  else
70 #    include <cmath>
71 #    include <iomanip>
72 #  endif
73 #endif
74
75 #include "FGPosition.h"
76 #include "FGAtmosphere.h"
77 #include "FGState.h"
78 #include "FGFDMExec.h"
79 #include "FGFCS.h"
80 #include "FGAircraft.h"
81 #include "FGMassBalance.h"
82 #include "FGTranslation.h"
83 #include "FGRotation.h"
84 #include "FGAuxiliary.h"
85 #include "FGOutput.h"
86 #include "FGPropertyManager.h"
87
88
89 namespace JSBSim {
90
91 static const char *IdSrc = "$Id$";
92 static const char *IdHdr = ID_POSITION;
93
94 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 CLASS IMPLEMENTATION
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
97
98 extern double globalTriNormal[3];
99 extern double globalSceneryAltitude;
100 extern double globalSeaLevelRadius;
101
102 FGPosition::FGPosition(FGFDMExec* fdmex) : FGModel(fdmex)
103 {
104   Name = "FGPosition";
105   LongitudeDot = LatitudeDot = RadiusDot = 0.0;
106   
107   for (int i=0;i<3;i++) {
108     LatitudeDot_prev[i]  = 0.0;
109     LongitudeDot_prev[i] = 0.0;
110     RadiusDot_prev[i]    = 0.0;
111   }
112   
113   Longitude = Latitude = 0.0;
114   gamma = Vt = Vground = 0.0;
115   hoverbmac = hoverbcg = 0.0;
116   psigt = 0.0;
117   bind();
118   Debug(0);
119 }
120
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123 FGPosition::~FGPosition(void)
124 {
125   unbind();
126   Debug(1);
127 }
128
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 bool FGPosition::InitModel(void)
132 {
133   FGModel::InitModel();
134
135   h = 3.0;                                 // Est. height of aircraft cg off runway
136   SeaLevelRadius = Inertial->RefRadius();  // For initialization ONLY
137   Radius         = SeaLevelRadius + h;
138   RunwayRadius   = SeaLevelRadius;
139   DistanceAGL    = Radius - RunwayRadius;  // Geocentric
140   vRunwayNormal(3) = -1.0;                 // Initialized for standalone mode
141   b = 1;
142   return true;
143 }
144
145 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 /*
147 Purpose: Called on a schedule to perform Positioning algorithms
148 Notes:   [TP] Make sure that -Vt <= hdot <= Vt, which, of course, should always
149          be the case
150          [JB] Run in standalone mode, SeaLevelRadius will be reference radius.
151                In FGFS, SeaLevelRadius is stuffed from FGJSBSim in JSBSim.cxx each pass.
152 */
153
154 bool FGPosition::Run(void)
155 {
156   double cosLat;
157   double hdot_Vt;
158   FGColumnVector3 vMac;
159
160   if (!FGModel::Run()) {
161     GetState();
162
163     Vground = sqrt( vVel(eNorth)*vVel(eNorth) + vVel(eEast)*vVel(eEast) );
164     psigt =  atan2(vVel(eEast), vVel(eNorth));
165     if (psigt < 0.0)
166       psigt += 2*M_PI;
167
168     Radius    = h + SeaLevelRadius;
169
170     cosLat = cos(Latitude);
171     if (cosLat != 0) LongitudeDot = vVel(eEast) / (Radius * cosLat);
172     LatitudeDot = vVel(eNorth) / Radius;
173     RadiusDot   = -vVel(eDown);
174
175     Longitude += State->Integrate(FGState::TRAPZ, dt*rate, LongitudeDot, LongitudeDot_prev);
176     Latitude  += State->Integrate(FGState::TRAPZ, dt*rate, LatitudeDot, LatitudeDot_prev);
177     Radius    += State->Integrate(FGState::TRAPZ, dt*rate, RadiusDot, RadiusDot_prev);
178
179     h = Radius - SeaLevelRadius;           // Geocentric
180
181     DistanceAGL = Radius - RunwayRadius;   // Geocentric
182     
183     hoverbcg = DistanceAGL/b;
184     
185     vMac = State->GetTb2l()*Aircraft->GetXYZrp();
186     
187     vMac *= inchtoft;
188     hoverbmac = (DistanceAGL + vMac(3))/b;
189
190     if (Vt > 0) {
191       hdot_Vt = RadiusDot/Vt;
192       if (fabs(hdot_Vt) <= 1) gamma = asin(hdot_Vt);
193     } else {
194       gamma = 0.0;
195     }
196
197     return false;
198
199   } else {
200     return true;
201   }
202 }
203
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205
206 void FGPosition::GetState(void)
207 {
208   dt = State->Getdt();
209
210   Vt        = Translation->GetVt();
211   vVel      = State->GetTb2l() * Translation->GetUVW();
212   vVelDot   = State->GetTb2l() * Translation->GetUVWdot();
213   
214   b = Aircraft->GetWingSpan();
215 }
216
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
219 void FGPosition::Seth(double tt)
220 {
221  h = tt;
222  Radius    = h + SeaLevelRadius;
223  DistanceAGL = Radius - RunwayRadius;   // Geocentric
224  hoverbcg = DistanceAGL/b;
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 void FGPosition::SetDistanceAGL(double tt)
230 {
231   DistanceAGL=tt;
232   Radius = RunwayRadius + DistanceAGL;
233   h = Radius - SeaLevelRadius;
234   hoverbcg = DistanceAGL/b;
235 }
236
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238
239 void FGPosition::bind(void)
240 {
241   PropertyManager->Tie("velocities/v-north-fps", this,
242                        &FGPosition::GetVn);
243   PropertyManager->Tie("velocities/v-east-fps", this,
244                        &FGPosition::GetVe);
245   PropertyManager->Tie("velocities/v-down-fps", this,
246                        &FGPosition::GetVd);
247   PropertyManager->Tie("velocities/vg-fps", this,
248                        &FGPosition::GetVground);
249   PropertyManager->Tie("flight-path/psi-gt-rad", this,
250                        &FGPosition::GetGroundTrack);
251   PropertyManager->Tie("position/h-sl-ft", this,
252                        &FGPosition::Geth,
253                        &FGPosition::Seth,
254                        true);
255   PropertyManager->Tie("velocities/h-dot-fps", this,
256                        &FGPosition::Gethdot);
257   PropertyManager->Tie("position/lat-gc-rad", this,
258                        &FGPosition::GetLatitude,
259                        &FGPosition::SetLatitude);
260   PropertyManager->Tie("position/lat-dot-gc-rad", this,
261                        &FGPosition::GetLatitudeDot);
262   PropertyManager->Tie("position/long-gc-rad", this,
263                        &FGPosition::GetLongitude,
264                        &FGPosition::SetLongitude,
265                        true);
266   PropertyManager->Tie("position/long-dot-gc-rad", this,
267                        &FGPosition::GetLongitudeDot);
268   PropertyManager->Tie("metrics/runway-radius", this,
269                        &FGPosition::GetRunwayRadius,
270                        &FGPosition::SetRunwayRadius);
271   PropertyManager->Tie("position/h-agl-ft", this,
272                        &FGPosition::GetDistanceAGL,
273                        &FGPosition::SetDistanceAGL);
274   PropertyManager->Tie("position/radius-to-vehicle-ft", this,
275                        &FGPosition::GetRadius);
276   PropertyManager->Tie("flight-path/gamma-rad", this,
277                        &FGPosition::GetGamma,
278                        &FGPosition::SetGamma);
279   PropertyManager->Tie("aero/h_b-cg-ft", this,
280                        &FGPosition::GetHOverBCG);
281   PropertyManager->Tie("aero/h_b-mac-ft", this,
282                        &FGPosition::GetHOverBMAC);
283 }
284
285 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286
287 void FGPosition::unbind(void)
288 {
289   PropertyManager->Untie("velocities/v-north-fps");
290   PropertyManager->Untie("velocities/v-east-fps");
291   PropertyManager->Untie("velocities/v-down-fps");
292   PropertyManager->Untie("velocities/vg-fps");
293   PropertyManager->Untie("flight-path/psi-gt-rad");
294   PropertyManager->Untie("position/h-sl-ft");
295   PropertyManager->Untie("velocities/h-dot-fps");
296   PropertyManager->Untie("position/lat-gc-rad");
297   PropertyManager->Untie("position/lat-dot-gc-rad");
298   PropertyManager->Untie("position/long-gc-rad");
299   PropertyManager->Untie("position/long-dot-gc-rad");
300   PropertyManager->Untie("metrics/runway-radius");
301   PropertyManager->Untie("position/h-agl-ft");
302   PropertyManager->Untie("position/radius-to-vehicle-ft");
303   PropertyManager->Untie("flight-path/gamma-rad");
304   PropertyManager->Untie("aero/h_b-cg-ft");
305   PropertyManager->Untie("aero/h_b-mac-ft");
306 }
307
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 //    The bitmasked value choices are as follows:
310 //    unset: In this case (the default) JSBSim would only print
311 //       out the normally expected messages, essentially echoing
312 //       the config files as they are read. If the environment
313 //       variable is not set, debug_lvl is set to 1 internally
314 //    0: This requests JSBSim not to output any messages
315 //       whatsoever.
316 //    1: This value explicity requests the normal JSBSim
317 //       startup messages
318 //    2: This value asks for a message to be printed out when
319 //       a class is instantiated
320 //    4: When this value is set, a message is displayed when a
321 //       FGModel object executes its Run() method
322 //    8: When this value is set, various runtime state variables
323 //       are printed out periodically
324 //    16: When set various parameters are sanity checked and
325 //       a message is printed out when they go out of bounds
326
327 void FGPosition::Debug(int from)
328 {
329   if (debug_lvl <= 0) return;
330
331   if (debug_lvl & 1) { // Standard console startup message output
332     if (from == 0) { // Constructor
333
334     }
335   }
336   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
337     if (from == 0) cout << "Instantiated: FGPosition" << endl;
338     if (from == 1) cout << "Destroyed:    FGPosition" << endl;
339   }
340   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
341   }
342   if (debug_lvl & 8 ) { // Runtime state variables
343   }
344   if (debug_lvl & 16) { // Sanity checking
345   }
346   if (debug_lvl & 64) {
347     if (from == 0) { // Constructor
348       cout << IdSrc << endl;
349       cout << IdHdr << endl;
350     }
351   }
352 }
353 }