]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPosition.cpp
Add the latest version of JSBSim including support for a Visual Reference Point
[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() * MassBalance->StructuralToBody(Aircraft->GetXYZvrp());
188
189     // vVRP  - the vector to the Visual Reference Point - now contains the 
190     // offset from the CG to the VRP, in units of feet, in the Local coordinate
191     // frame, where X points north, Y points East, and Z points down. This needs
192     // to be converted to Lat/Lon/Alt, now.
193
194     if (cosLat != 0)
195       LongitudeVRP = vVRPoffset(eEast) / (Radius * cosLat) + Longitude;
196
197     LatitudeVRP = vVRPoffset(eNorth) / Radius + Latitude;
198     hVRP = h - vVRPoffset(eDown);
199 /*
200 cout << "Lat/Lon/Alt : " << Latitude << " / " << Longitude << " / " << h << endl;
201 cout << "Lat/Lon/Alt VRP: " << LatitudeVRP << " / " << LongitudeVRP << " / " << hVRP << endl << endl;
202 */
203     DistanceAGL = Radius - RunwayRadius;   // Geocentric
204
205     hoverbcg = DistanceAGL/b;
206
207     vMac = State->GetTb2l()*MassBalance->StructuralToBody(Aircraft->GetXYZrp());
208     hoverbmac = (DistanceAGL + vMac(3)) / b;
209
210     if (Vt > 0) {
211       hdot_Vt = RadiusDot/Vt;
212       if (fabs(hdot_Vt) <= 1) gamma = asin(hdot_Vt);
213     } else {
214       gamma = 0.0;
215     }
216
217     return false;
218
219   } else {
220     return true;
221   }
222 }
223
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225
226 void FGPosition::GetState(void)
227 {
228   dt = State->Getdt();
229
230   Vt        = Translation->GetVt();
231   vVel      = State->GetTb2l() * Translation->GetUVW();
232   vVelDot   = State->GetTb2l() * Translation->GetUVWdot();
233   
234   b = Aircraft->GetWingSpan();
235 }
236
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238
239 void FGPosition::Seth(double tt)
240 {
241  h = tt;
242  Radius    = h + SeaLevelRadius;
243  DistanceAGL = Radius - RunwayRadius;   // Geocentric
244  hoverbcg = DistanceAGL/b;
245 }
246
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248
249 void FGPosition::SetDistanceAGL(double tt)
250 {
251   DistanceAGL=tt;
252   Radius = RunwayRadius + DistanceAGL;
253   h = Radius - SeaLevelRadius;
254   hoverbcg = DistanceAGL/b;
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 void FGPosition::bind(void)
260 {
261   PropertyManager->Tie("velocities/v-north-fps", this,
262                        &FGPosition::GetVn);
263   PropertyManager->Tie("velocities/v-east-fps", this,
264                        &FGPosition::GetVe);
265   PropertyManager->Tie("velocities/v-down-fps", this,
266                        &FGPosition::GetVd);
267   PropertyManager->Tie("velocities/vg-fps", this,
268                        &FGPosition::GetVground);
269   PropertyManager->Tie("flight-path/psi-gt-rad", this,
270                        &FGPosition::GetGroundTrack);
271   PropertyManager->Tie("position/h-sl-ft", this,
272                        &FGPosition::Geth,
273                        &FGPosition::Seth,
274                        true);
275   PropertyManager->Tie("velocities/h-dot-fps", this,
276                        &FGPosition::Gethdot);
277   PropertyManager->Tie("position/lat-gc-rad", this,
278                        &FGPosition::GetLatitude,
279                        &FGPosition::SetLatitude);
280   PropertyManager->Tie("position/lat-dot-gc-rad", this,
281                        &FGPosition::GetLatitudeDot);
282   PropertyManager->Tie("position/long-gc-rad", this,
283                        &FGPosition::GetLongitude,
284                        &FGPosition::SetLongitude,
285                        true);
286   PropertyManager->Tie("position/long-dot-gc-rad", this,
287                        &FGPosition::GetLongitudeDot);
288   PropertyManager->Tie("metrics/runway-radius", this,
289                        &FGPosition::GetRunwayRadius,
290                        &FGPosition::SetRunwayRadius);
291   PropertyManager->Tie("position/h-agl-ft", this,
292                        &FGPosition::GetDistanceAGL,
293                        &FGPosition::SetDistanceAGL);
294   PropertyManager->Tie("position/radius-to-vehicle-ft", this,
295                        &FGPosition::GetRadius);
296   PropertyManager->Tie("flight-path/gamma-rad", this,
297                        &FGPosition::GetGamma,
298                        &FGPosition::SetGamma);
299   PropertyManager->Tie("aero/h_b-cg-ft", this,
300                        &FGPosition::GetHOverBCG);
301   PropertyManager->Tie("aero/h_b-mac-ft", this,
302                        &FGPosition::GetHOverBMAC);
303 }
304
305 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306
307 void FGPosition::unbind(void)
308 {
309   PropertyManager->Untie("velocities/v-north-fps");
310   PropertyManager->Untie("velocities/v-east-fps");
311   PropertyManager->Untie("velocities/v-down-fps");
312   PropertyManager->Untie("velocities/vg-fps");
313   PropertyManager->Untie("flight-path/psi-gt-rad");
314   PropertyManager->Untie("position/h-sl-ft");
315   PropertyManager->Untie("velocities/h-dot-fps");
316   PropertyManager->Untie("position/lat-gc-rad");
317   PropertyManager->Untie("position/lat-dot-gc-rad");
318   PropertyManager->Untie("position/long-gc-rad");
319   PropertyManager->Untie("position/long-dot-gc-rad");
320   PropertyManager->Untie("metrics/runway-radius");
321   PropertyManager->Untie("position/h-agl-ft");
322   PropertyManager->Untie("position/radius-to-vehicle-ft");
323   PropertyManager->Untie("flight-path/gamma-rad");
324   PropertyManager->Untie("aero/h_b-cg-ft");
325   PropertyManager->Untie("aero/h_b-mac-ft");
326 }
327
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 //    The bitmasked value choices are as follows:
330 //    unset: In this case (the default) JSBSim would only print
331 //       out the normally expected messages, essentially echoing
332 //       the config files as they are read. If the environment
333 //       variable is not set, debug_lvl is set to 1 internally
334 //    0: This requests JSBSim not to output any messages
335 //       whatsoever.
336 //    1: This value explicity requests the normal JSBSim
337 //       startup messages
338 //    2: This value asks for a message to be printed out when
339 //       a class is instantiated
340 //    4: When this value is set, a message is displayed when a
341 //       FGModel object executes its Run() method
342 //    8: When this value is set, various runtime state variables
343 //       are printed out periodically
344 //    16: When set various parameters are sanity checked and
345 //       a message is printed out when they go out of bounds
346
347 void FGPosition::Debug(int from)
348 {
349   if (debug_lvl <= 0) return;
350
351   if (debug_lvl & 1) { // Standard console startup message output
352     if (from == 0) { // Constructor
353
354     }
355   }
356   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
357     if (from == 0) cout << "Instantiated: FGPosition" << endl;
358     if (from == 1) cout << "Destroyed:    FGPosition" << endl;
359   }
360   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
361   }
362   if (debug_lvl & 8 ) { // Runtime state variables
363   }
364   if (debug_lvl & 16) { // Sanity checking
365   }
366   if (debug_lvl & 64) {
367     if (from == 0) { // Constructor
368       cout << IdSrc << endl;
369       cout << IdHdr << endl;
370     }
371   }
372 }
373 }