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