1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Integrate the EOM to determine instantaneous position
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class encapsulates the integration of rates and accelerations to get the
31 current position of the aircraft.
34 --------------------------------------------------------------------------------
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
43 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
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
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 # include <simgear/compiler.h>
58 # ifdef SG_HAVE_STD_INCLUDES
66 # if defined(sgi) && !defined(__GNUC__)
68 # if (_COMPILER_VERSION < 740)
79 #include "FGPosition.h"
80 #include "FGAtmosphere.h"
82 #include "FGFDMExec.h"
84 #include "FGAircraft.h"
85 #include "FGMassBalance.h"
86 #include "FGTranslation.h"
87 #include "FGRotation.h"
88 #include "FGAuxiliary.h"
90 #include "FGPropertyManager.h"
95 static const char *IdSrc = "$Id$";
96 static const char *IdHdr = ID_POSITION;
98 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
102 extern double globalTriNormal[3];
103 extern double globalSceneryAltitude;
104 extern double globalSeaLevelRadius;
106 FGPosition::FGPosition(FGFDMExec* fdmex) : FGModel(fdmex)
109 LongitudeDot = LatitudeDot = RadiusDot = 0.0;
111 for (int i=0;i<4;i++) {
112 LatitudeDot_prev[i] = 0.0;
113 LongitudeDot_prev[i] = 0.0;
114 RadiusDot_prev[i] = 0.0;
117 vVRPoffset.InitMatrix();
119 Longitude = Latitude = 0.0;
120 LongitudeVRP = LatitudeVRP = 0.0;
121 gamma = Vt = Vground = 0.0;
122 hoverbmac = hoverbcg = 0.0;
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 FGPosition::~FGPosition(void)
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 bool FGPosition::InitModel(void)
140 FGModel::InitModel();
142 h = 3.0; // Est. height of aircraft cg off runway
143 SeaLevelRadius = Inertial->RefRadius(); // For initialization ONLY
144 Radius = SeaLevelRadius + h;
145 RunwayRadius = SeaLevelRadius;
146 DistanceAGL = Radius - RunwayRadius; // Geocentric
147 vRunwayNormal(3) = -1.0; // Initialized for standalone mode
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 Purpose: Called on a schedule to perform Positioning algorithms
155 Notes: [TP] Make sure that -Vt <= hdot <= Vt, which, of course, should always
157 [JB] Run in standalone mode, SeaLevelRadius will be reference radius.
158 In FGFS, SeaLevelRadius is stuffed from FGJSBSim in JSBSim.cxx each pass.
161 bool FGPosition::Run(void)
166 if (!FGModel::Run()) {
169 Vground = sqrt( vVel(eNorth)*vVel(eNorth) + vVel(eEast)*vVel(eEast) );
170 psigt = atan2(vVel(eEast), vVel(eNorth));
174 Radius = h + SeaLevelRadius;
176 cosLat = cos(Latitude);
177 if (cosLat != 0) LongitudeDot = vVel(eEast) / (Radius * cosLat);
178 LatitudeDot = vVel(eNorth) / Radius;
179 RadiusDot = -vVel(eDown);
181 Longitude += State->Integrate(FGState::TRAPZ, dt*rate, LongitudeDot, LongitudeDot_prev);
182 Latitude += State->Integrate(FGState::TRAPZ, dt*rate, LatitudeDot, LatitudeDot_prev);
183 Radius += State->Integrate(FGState::TRAPZ, dt*rate, RadiusDot, RadiusDot_prev);
185 h = Radius - SeaLevelRadius; // Geocentric
187 vVRPoffset = State->GetTb2l() * (vVRP - MassBalance->GetXYZcg());
188 vVRPoffset /= 12.0; // converted to feet
190 // vVRP - the vector to the Visual Reference Point - now contains the
191 // offset from the CG to the VRP, in units of feet, in the Local coordinate
192 // frame, where X points north, Y points East, and Z points down. This needs
193 // to be converted to Lat/Lon/Alt, now.
196 LongitudeVRP = vVRPoffset(eEast) / (Radius * cosLat) + Longitude;
198 LatitudeVRP = vVRPoffset(eNorth) / Radius + Latitude;
199 hVRP = vVRPoffset(eDown) + h;
201 cout << "Lat/Lon/Alt : " << Latitude << " / " << Longitude << " / " << h << endl;
202 cout << "Lat/Lon/Alt VRP: " << LatitudeVRP << " / " << LongitudeVRP << " / " << hVRP << endl << endl;
204 DistanceAGL = Radius - RunwayRadius; // Geocentric
206 hoverbcg = DistanceAGL/b;
208 vMac = State->GetTb2l()*Aircraft->GetXYZrp();
211 hoverbmac = (DistanceAGL + vMac(3))/b;
214 hdot_Vt = RadiusDot/Vt;
215 if (fabs(hdot_Vt) <= 1) gamma = asin(hdot_Vt);
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 void FGPosition::GetState(void)
233 Vt = Translation->GetVt();
234 vVel = State->GetTb2l() * Translation->GetUVW();
235 vVelDot = State->GetTb2l() * Translation->GetUVWdot();
237 b = Aircraft->GetWingSpan();
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 void FGPosition::Seth(double tt)
245 Radius = h + SeaLevelRadius;
246 DistanceAGL = Radius - RunwayRadius; // Geocentric
247 hoverbcg = DistanceAGL/b;
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 void FGPosition::SetDistanceAGL(double tt)
255 Radius = RunwayRadius + DistanceAGL;
256 h = Radius - SeaLevelRadius;
257 hoverbcg = DistanceAGL/b;
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 void FGPosition::bind(void)
264 PropertyManager->Tie("velocities/v-north-fps", this,
266 PropertyManager->Tie("velocities/v-east-fps", this,
268 PropertyManager->Tie("velocities/v-down-fps", this,
270 PropertyManager->Tie("velocities/vg-fps", this,
271 &FGPosition::GetVground);
272 PropertyManager->Tie("flight-path/psi-gt-rad", this,
273 &FGPosition::GetGroundTrack);
274 PropertyManager->Tie("position/h-sl-ft", this,
278 PropertyManager->Tie("velocities/h-dot-fps", this,
279 &FGPosition::Gethdot);
280 PropertyManager->Tie("position/lat-gc-rad", this,
281 &FGPosition::GetLatitude,
282 &FGPosition::SetLatitude);
283 PropertyManager->Tie("position/lat-dot-gc-rad", this,
284 &FGPosition::GetLatitudeDot);
285 PropertyManager->Tie("position/long-gc-rad", this,
286 &FGPosition::GetLongitude,
287 &FGPosition::SetLongitude,
289 PropertyManager->Tie("position/long-dot-gc-rad", this,
290 &FGPosition::GetLongitudeDot);
291 PropertyManager->Tie("metrics/runway-radius", this,
292 &FGPosition::GetRunwayRadius,
293 &FGPosition::SetRunwayRadius);
294 PropertyManager->Tie("position/h-agl-ft", this,
295 &FGPosition::GetDistanceAGL,
296 &FGPosition::SetDistanceAGL);
297 PropertyManager->Tie("position/radius-to-vehicle-ft", this,
298 &FGPosition::GetRadius);
299 PropertyManager->Tie("flight-path/gamma-rad", this,
300 &FGPosition::GetGamma,
301 &FGPosition::SetGamma);
302 PropertyManager->Tie("aero/h_b-cg-ft", this,
303 &FGPosition::GetHOverBCG);
304 PropertyManager->Tie("aero/h_b-mac-ft", this,
305 &FGPosition::GetHOverBMAC);
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 void FGPosition::unbind(void)
312 PropertyManager->Untie("velocities/v-north-fps");
313 PropertyManager->Untie("velocities/v-east-fps");
314 PropertyManager->Untie("velocities/v-down-fps");
315 PropertyManager->Untie("velocities/vg-fps");
316 PropertyManager->Untie("flight-path/psi-gt-rad");
317 PropertyManager->Untie("position/h-sl-ft");
318 PropertyManager->Untie("velocities/h-dot-fps");
319 PropertyManager->Untie("position/lat-gc-rad");
320 PropertyManager->Untie("position/lat-dot-gc-rad");
321 PropertyManager->Untie("position/long-gc-rad");
322 PropertyManager->Untie("position/long-dot-gc-rad");
323 PropertyManager->Untie("metrics/runway-radius");
324 PropertyManager->Untie("position/h-agl-ft");
325 PropertyManager->Untie("position/radius-to-vehicle-ft");
326 PropertyManager->Untie("flight-path/gamma-rad");
327 PropertyManager->Untie("aero/h_b-cg-ft");
328 PropertyManager->Untie("aero/h_b-mac-ft");
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 // The bitmasked value choices are as follows:
333 // unset: In this case (the default) JSBSim would only print
334 // out the normally expected messages, essentially echoing
335 // the config files as they are read. If the environment
336 // variable is not set, debug_lvl is set to 1 internally
337 // 0: This requests JSBSim not to output any messages
339 // 1: This value explicity requests the normal JSBSim
341 // 2: This value asks for a message to be printed out when
342 // a class is instantiated
343 // 4: When this value is set, a message is displayed when a
344 // FGModel object executes its Run() method
345 // 8: When this value is set, various runtime state variables
346 // are printed out periodically
347 // 16: When set various parameters are sanity checked and
348 // a message is printed out when they go out of bounds
350 void FGPosition::Debug(int from)
352 if (debug_lvl <= 0) return;
354 if (debug_lvl & 1) { // Standard console startup message output
355 if (from == 0) { // Constructor
359 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
360 if (from == 0) cout << "Instantiated: FGPosition" << endl;
361 if (from == 1) cout << "Destroyed: FGPosition" << endl;
363 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
365 if (debug_lvl & 8 ) { // Runtime state variables
367 if (debug_lvl & 16) { // Sanity checking
369 if (debug_lvl & 64) {
370 if (from == 0) { // Constructor
371 cout << IdSrc << endl;
372 cout << IdHdr << endl;