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