]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPosition.cpp
Curt Olson:
[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 "FGAtmosphere.h"
81 #include "FGState.h"
82 #include "FGFDMExec.h"
83 #include "FGFCS.h"
84 #include "FGAircraft.h"
85 #include "FGMassBalance.h"
86 #include "FGTranslation.h"
87 #include "FGRotation.h"
88 #include "FGAuxiliary.h"
89 #include "FGOutput.h"
90 #include "FGPropertyManager.h"
91
92
93 namespace JSBSim {
94
95 static const char *IdSrc = "$Id$";
96 static const char *IdHdr = ID_POSITION;
97
98 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 CLASS IMPLEMENTATION
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
101
102 extern double globalTriNormal[3];
103 extern double globalSceneryAltitude;
104 extern double globalSeaLevelRadius;
105
106 FGPosition::FGPosition(FGFDMExec* fdmex) : FGModel(fdmex)
107 {
108   Name = "FGPosition";
109   LongitudeDot = LatitudeDot = RadiusDot = 0.0;
110   
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;
115   }
116
117   vVRPoffset.InitMatrix();
118
119   Longitude = Latitude = 0.0;
120   LongitudeVRP = LatitudeVRP = 0.0;
121   gamma = Vt = Vground = 0.0;
122   hoverbmac = hoverbcg = 0.0;
123   psigt = 0.0;
124   bind();
125   Debug(0);
126 }
127
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129
130 FGPosition::~FGPosition(void)
131 {
132   unbind();
133   Debug(1);
134 }
135
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138 bool FGPosition::InitModel(void)
139 {
140   FGModel::InitModel();
141
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
148   b = 1;
149   return true;
150 }
151
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 /*
154 Purpose: Called on a schedule to perform Positioning algorithms
155 Notes:   [TP] Make sure that -Vt <= hdot <= Vt, which, of course, should always
156          be the case
157          [JB] Run in standalone mode, SeaLevelRadius will be reference radius.
158                In FGFS, SeaLevelRadius is stuffed from FGJSBSim in JSBSim.cxx each pass.
159 */
160
161 bool FGPosition::Run(void)
162 {
163   double cosLat;
164   double hdot_Vt;
165
166   if (!FGModel::Run()) {
167     GetState();
168
169     Vground = sqrt( vVel(eNorth)*vVel(eNorth) + vVel(eEast)*vVel(eEast) );
170     psigt =  atan2(vVel(eEast), vVel(eNorth));
171     if (psigt < 0.0)
172       psigt += 2*M_PI;
173
174     Radius    = h + SeaLevelRadius;
175
176     cosLat = cos(Latitude);
177     if (cosLat != 0) LongitudeDot = vVel(eEast) / (Radius * cosLat);
178     LatitudeDot = vVel(eNorth) / Radius;
179     RadiusDot   = -vVel(eDown);
180
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);
184
185     h = Radius - SeaLevelRadius;           // Geocentric
186
187     vVRPoffset = State->GetTb2l() * (vVRP - MassBalance->GetXYZcg());
188     vVRPoffset /= 12.0; // converted to feet
189
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.
194
195     if (cosLat != 0)
196       LongitudeVRP = vVRPoffset(eEast) / (Radius * cosLat) + Longitude;
197
198     LatitudeVRP = vVRPoffset(eNorth) / Radius + Latitude;
199     hVRP = vVRPoffset(eDown) + h;
200 /*
201 cout << "Lat/Lon/Alt : " << Latitude << " / " << Longitude << " / " << h << endl;
202 cout << "Lat/Lon/Alt VRP: " << LatitudeVRP << " / " << LongitudeVRP << " / " << hVRP << endl << endl;
203 */
204     DistanceAGL = Radius - RunwayRadius;   // Geocentric
205     
206     hoverbcg = DistanceAGL/b;
207     
208     vMac = State->GetTb2l()*Aircraft->GetXYZrp();
209     
210     vMac *= inchtoft;
211     hoverbmac = (DistanceAGL + vMac(3))/b;
212
213     if (Vt > 0) {
214       hdot_Vt = RadiusDot/Vt;
215       if (fabs(hdot_Vt) <= 1) gamma = asin(hdot_Vt);
216     } else {
217       gamma = 0.0;
218     }
219
220     return false;
221
222   } else {
223     return true;
224   }
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 void FGPosition::GetState(void)
230 {
231   dt = State->Getdt();
232
233   Vt        = Translation->GetVt();
234   vVel      = State->GetTb2l() * Translation->GetUVW();
235   vVelDot   = State->GetTb2l() * Translation->GetUVWdot();
236   
237   b = Aircraft->GetWingSpan();
238 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
242 void FGPosition::Seth(double tt)
243 {
244  h = tt;
245  Radius    = h + SeaLevelRadius;
246  DistanceAGL = Radius - RunwayRadius;   // Geocentric
247  hoverbcg = DistanceAGL/b;
248 }
249
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251
252 void FGPosition::SetDistanceAGL(double tt)
253 {
254   DistanceAGL=tt;
255   Radius = RunwayRadius + DistanceAGL;
256   h = Radius - SeaLevelRadius;
257   hoverbcg = DistanceAGL/b;
258 }
259
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261
262 void FGPosition::bind(void)
263 {
264   PropertyManager->Tie("velocities/v-north-fps", this,
265                        &FGPosition::GetVn);
266   PropertyManager->Tie("velocities/v-east-fps", this,
267                        &FGPosition::GetVe);
268   PropertyManager->Tie("velocities/v-down-fps", this,
269                        &FGPosition::GetVd);
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,
275                        &FGPosition::Geth,
276                        &FGPosition::Seth,
277                        true);
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,
288                        true);
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);
306 }
307
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309
310 void FGPosition::unbind(void)
311 {
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");
329 }
330
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
338 //       whatsoever.
339 //    1: This value explicity requests the normal JSBSim
340 //       startup messages
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
349
350 void FGPosition::Debug(int from)
351 {
352   if (debug_lvl <= 0) return;
353
354   if (debug_lvl & 1) { // Standard console startup message output
355     if (from == 0) { // Constructor
356
357     }
358   }
359   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
360     if (from == 0) cout << "Instantiated: FGPosition" << endl;
361     if (from == 1) cout << "Destroyed:    FGPosition" << endl;
362   }
363   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
364   }
365   if (debug_lvl & 8 ) { // Runtime state variables
366   }
367   if (debug_lvl & 16) { // Sanity checking
368   }
369   if (debug_lvl & 64) {
370     if (from == 0) { // Constructor
371       cout << IdSrc << endl;
372       cout << IdHdr << endl;
373     }
374   }
375 }
376 }