]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPosition.cpp
Sync. with JSBSim CVS.
[flightgear.git] / src / FDM / JSBSim / FGPosition.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
2 \r
3  Module:       FGPosition.cpp\r
4  Author:       Jon S. Berndt\r
5  Date started: 01/05/99\r
6  Purpose:      Integrate the EOM to determine instantaneous position\r
7  Called by:    FGFDMExec\r
8 \r
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------\r
10 \r
11  This program is free software; you can redistribute it and/or modify it under\r
12  the terms of the GNU General Public License as published by the Free Software\r
13  Foundation; either version 2 of the License, or (at your option) any later\r
14  version.\r
15 \r
16  This program is distributed in the hope that it will be useful, but WITHOUT\r
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
19  details.\r
20 \r
21  You should have received a copy of the GNU General Public License along with\r
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple\r
23  Place - Suite 330, Boston, MA  02111-1307, USA.\r
24 \r
25  Further information about the GNU General Public License can also be found on\r
26  the world wide web at http://www.gnu.org.\r
27 \r
28 FUNCTIONAL DESCRIPTION\r
29 --------------------------------------------------------------------------------\r
30 This class encapsulates the integration of rates and accelerations to get the\r
31 current position of the aircraft.\r
32 \r
33 HISTORY\r
34 --------------------------------------------------------------------------------\r
35 01/05/99   JSB   Created\r
36 \r
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
38 COMMENTS, REFERENCES,  and NOTES\r
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
40 [1] Cooke, Zyda, Pratt, and McGhee, "NPSNET: Flight Simulation Dynamic Modeling\r
41     Using Quaternions", Presence, Vol. 1, No. 4, pp. 404-420  Naval Postgraduate\r
42     School, January 1994\r
43 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",\r
44     JSC 12960, July 1977\r
45 [3] Richard E. McFarland, "A Standard Kinematic Model for Flight Simulation at\r
46     NASA-Ames", NASA CR-2497, January 1975\r
47 [4] Barnes W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics",\r
48     Wiley & Sons, 1979 ISBN 0-471-03032-5\r
49 [5] Bernard Etkin, "Dynamics of Flight, Stability and Control", Wiley & Sons,\r
50     1982 ISBN 0-471-08936-2\r
51 \r
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
53 INCLUDES\r
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
55 \r
56 #ifdef FGFS\r
57 #  include <simgear/compiler.h>\r
58 #  ifdef SG_HAVE_STD_INCLUDES\r
59 #    include <cmath>\r
60 #    include <iomanip>\r
61 #  else\r
62 #    include <math.h>\r
63 #    include <iomanip.h>\r
64 #  endif\r
65 #else\r
66 #  if defined(sgi) && !defined(__GNUC__)\r
67 #    include <math.h>\r
68 #    if (_COMPILER_VERSION < 740)\r
69 #      include <iomanip.h>\r
70 #    else\r
71 #      include <iomanip>\r
72 #    endif\r
73 #  else\r
74 #    include <cmath>\r
75 #    include <iomanip>\r
76 #  endif\r
77 #endif\r
78 \r
79 #include "FGPosition.h"\r
80 #include "FGState.h"\r
81 #include "FGFDMExec.h"\r
82 #include "FGAircraft.h"\r
83 #include "FGMassBalance.h"\r
84 #include "FGTranslation.h"\r
85 #include "FGRotation.h"\r
86 #include "FGInertial.h"\r
87 #include "FGPropertyManager.h"\r
88 \r
89 namespace JSBSim {\r
90 \r
91 static const char *IdSrc = "$Id$";\r
92 static const char *IdHdr = ID_POSITION;\r
93 \r
94 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
95 CLASS IMPLEMENTATION\r
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
97 \r
98 FGPosition::FGPosition(FGFDMExec* fdmex) : FGModel(fdmex)\r
99 {\r
100   Name = "FGPosition";\r
101   LongitudeDot = LatitudeDot = RadiusDot = 0.0;\r
102 \r
103   for (int i=0;i<4;i++) {\r
104     LatitudeDot_prev[i]  = 0.0;\r
105     LongitudeDot_prev[i] = 0.0;\r
106     RadiusDot_prev[i]    = 0.0;\r
107   }\r
108 \r
109   vVRPoffset.InitMatrix();\r
110 \r
111   Longitude = Latitude = 0.0;\r
112   LongitudeVRP = LatitudeVRP = 0.0;\r
113   gamma = Vt = Vground = 0.0;\r
114   hoverbmac = hoverbcg = 0.0;\r
115   psigt = 0.0;\r
116   bind();\r
117   Debug(0);\r
118 }\r
119 \r
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
121 \r
122 FGPosition::~FGPosition(void)\r
123 {\r
124   unbind();\r
125   Debug(1);\r
126 }\r
127 \r
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
129 \r
130 bool FGPosition::InitModel(void)\r
131 {\r
132   FGModel::InitModel();\r
133 \r
134   h = 3.0;                                 // Est. height of aircraft cg off runway\r
135   SeaLevelRadius = Inertial->RefRadius();  // For initialization ONLY\r
136   Radius         = SeaLevelRadius + h;\r
137   RunwayRadius   = SeaLevelRadius;\r
138   DistanceAGL    = Radius - RunwayRadius;  // Geocentric\r
139   vRunwayNormal(3) = -1.0;                 // Initialized for standalone mode\r
140   b = 1;\r
141   return true;\r
142 }\r
143 \r
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
145 /*\r
146 Purpose: Called on a schedule to perform Positioning algorithms\r
147 Notes:   [TP] Make sure that -Vt <= hdot <= Vt, which, of course, should always\r
148          be the case\r
149          [JB] Run in standalone mode, SeaLevelRadius will be reference radius.\r
150                In FGFS, SeaLevelRadius is stuffed from FGJSBSim in JSBSim.cxx each pass.\r
151 */\r
152 \r
153 bool FGPosition::Run(void)\r
154 {\r
155   double cosLat;\r
156   double hdot_Vt;\r
157 \r
158   if (!FGModel::Run()) {\r
159     GetState();\r
160 \r
161     Vground = sqrt( vVel(eNorth)*vVel(eNorth) + vVel(eEast)*vVel(eEast) );\r
162 \r
163     if (vVel(eNorth) == 0) psigt = 0;\r
164     else psigt =  atan2(vVel(eEast), vVel(eNorth));\r
165 \r
166     if (psigt < 0.0) psigt += 2*M_PI;\r
167 \r
168     Radius    = h + SeaLevelRadius;\r
169 \r
170     cosLat = cos(Latitude);\r
171     if (cosLat != 0) LongitudeDot = vVel(eEast) / (Radius * cosLat);\r
172     LatitudeDot = vVel(eNorth) / Radius;\r
173     RadiusDot   = -vVel(eDown);\r
174 \r
175     Longitude += State->Integrate(FGState::TRAPZ, dt*rate, LongitudeDot, LongitudeDot_prev);\r
176     Latitude  += State->Integrate(FGState::TRAPZ, dt*rate, LatitudeDot, LatitudeDot_prev);\r
177     Radius    += State->Integrate(FGState::TRAPZ, dt*rate, RadiusDot, RadiusDot_prev);\r
178 \r
179     h = Radius - SeaLevelRadius;           // Geocentric\r
180 \r
181     vVRPoffset = State->GetTb2l() * MassBalance->StructuralToBody(Aircraft->GetXYZvrp());\r
182 \r
183     // vVRP  - the vector to the Visual Reference Point - now contains the\r
184     // offset from the CG to the VRP, in units of feet, in the Local coordinate\r
185     // frame, where X points north, Y points East, and Z points down. This needs\r
186     // to be converted to Lat/Lon/Alt, now.\r
187 \r
188     if (cosLat != 0)\r
189       LongitudeVRP = vVRPoffset(eEast) / (Radius * cosLat) + Longitude;\r
190 \r
191     LatitudeVRP = vVRPoffset(eNorth) / Radius + Latitude;\r
192     hVRP = h - vVRPoffset(eDown);\r
193 /*\r
194 cout << "Lat/Lon/Alt : " << Latitude << " / " << Longitude << " / " << h << endl;\r
195 cout << "Lat/Lon/Alt VRP: " << LatitudeVRP << " / " << LongitudeVRP << " / " << hVRP << endl << endl;\r
196 */\r
197     DistanceAGL = Radius - RunwayRadius;   // Geocentric\r
198 \r
199     hoverbcg = DistanceAGL/b;\r
200 \r
201     vMac = State->GetTb2l()*MassBalance->StructuralToBody(Aircraft->GetXYZrp());\r
202     hoverbmac = (DistanceAGL + vMac(3)) / b;\r
203 \r
204     if (Vt > 0) {\r
205       hdot_Vt = RadiusDot/Vt;\r
206       if (fabs(hdot_Vt) <= 1) gamma = asin(hdot_Vt);\r
207     } else {\r
208       gamma = 0.0;\r
209     }\r
210 \r
211     return false;\r
212 \r
213   } else {\r
214     return true;\r
215   }\r
216 }\r
217 \r
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
219 \r
220 void FGPosition::GetState(void)\r
221 {\r
222   dt = State->Getdt();\r
223 \r
224   Vt        = Translation->GetVt();\r
225   vVel      = State->GetTb2l() * Translation->GetUVW();\r
226   vVelDot   = State->GetTb2l() * Translation->GetUVWdot();\r
227 \r
228   b = Aircraft->GetWingSpan();\r
229 }\r
230 \r
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
232 \r
233 void FGPosition::Seth(double tt)\r
234 {\r
235  h = tt;\r
236  Radius    = h + SeaLevelRadius;\r
237  DistanceAGL = Radius - RunwayRadius;   // Geocentric\r
238  hoverbcg = DistanceAGL/b;\r
239 }\r
240 \r
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
242 \r
243 void FGPosition::SetDistanceAGL(double tt)\r
244 {\r
245   DistanceAGL=tt;\r
246   Radius = RunwayRadius + DistanceAGL;\r
247   h = Radius - SeaLevelRadius;\r
248   hoverbcg = DistanceAGL/b;\r
249 }\r
250 \r
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
252 \r
253 void FGPosition::bind(void)\r
254 {\r
255   PropertyManager->Tie("velocities/v-north-fps", this,\r
256                        &FGPosition::GetVn);\r
257   PropertyManager->Tie("velocities/v-east-fps", this,\r
258                        &FGPosition::GetVe);\r
259   PropertyManager->Tie("velocities/v-down-fps", this,\r
260                        &FGPosition::GetVd);\r
261   PropertyManager->Tie("velocities/vg-fps", this,\r
262                        &FGPosition::GetVground);\r
263   PropertyManager->Tie("flight-path/psi-gt-rad", this,\r
264                        &FGPosition::GetGroundTrack);\r
265   PropertyManager->Tie("position/h-sl-ft", this,\r
266                        &FGPosition::Geth,\r
267                        &FGPosition::Seth,\r
268                        true);\r
269   PropertyManager->Tie("velocities/h-dot-fps", this,\r
270                        &FGPosition::Gethdot);\r
271   PropertyManager->Tie("position/lat-gc-rad", this,\r
272                        &FGPosition::GetLatitude,\r
273                        &FGPosition::SetLatitude);\r
274   PropertyManager->Tie("position/lat-dot-gc-rad", this,\r
275                        &FGPosition::GetLatitudeDot);\r
276   PropertyManager->Tie("position/long-gc-rad", this,\r
277                        &FGPosition::GetLongitude,\r
278                        &FGPosition::SetLongitude,\r
279                        true);\r
280   PropertyManager->Tie("position/long-dot-gc-rad", this,\r
281                        &FGPosition::GetLongitudeDot);\r
282   PropertyManager->Tie("metrics/runway-radius", this,\r
283                        &FGPosition::GetRunwayRadius,\r
284                        &FGPosition::SetRunwayRadius);\r
285   PropertyManager->Tie("position/h-agl-ft", this,\r
286                        &FGPosition::GetDistanceAGL,\r
287                        &FGPosition::SetDistanceAGL);\r
288   PropertyManager->Tie("position/radius-to-vehicle-ft", this,\r
289                        &FGPosition::GetRadius);\r
290   PropertyManager->Tie("flight-path/gamma-rad", this,\r
291                        &FGPosition::GetGamma,\r
292                        &FGPosition::SetGamma);\r
293   PropertyManager->Tie("aero/h_b-cg-ft", this,\r
294                        &FGPosition::GetHOverBCG);\r
295   PropertyManager->Tie("aero/h_b-mac-ft", this,\r
296                        &FGPosition::GetHOverBMAC);\r
297 }\r
298 \r
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
300 \r
301 void FGPosition::unbind(void)\r
302 {\r
303   PropertyManager->Untie("velocities/v-north-fps");\r
304   PropertyManager->Untie("velocities/v-east-fps");\r
305   PropertyManager->Untie("velocities/v-down-fps");\r
306   PropertyManager->Untie("velocities/vg-fps");\r
307   PropertyManager->Untie("flight-path/psi-gt-rad");\r
308   PropertyManager->Untie("position/h-sl-ft");\r
309   PropertyManager->Untie("velocities/h-dot-fps");\r
310   PropertyManager->Untie("position/lat-gc-rad");\r
311   PropertyManager->Untie("position/lat-dot-gc-rad");\r
312   PropertyManager->Untie("position/long-gc-rad");\r
313   PropertyManager->Untie("position/long-dot-gc-rad");\r
314   PropertyManager->Untie("metrics/runway-radius");\r
315   PropertyManager->Untie("position/h-agl-ft");\r
316   PropertyManager->Untie("position/radius-to-vehicle-ft");\r
317   PropertyManager->Untie("flight-path/gamma-rad");\r
318   PropertyManager->Untie("aero/h_b-cg-ft");\r
319   PropertyManager->Untie("aero/h_b-mac-ft");\r
320 }\r
321 \r
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r
323 //    The bitmasked value choices are as follows:\r
324 //    unset: In this case (the default) JSBSim would only print\r
325 //       out the normally expected messages, essentially echoing\r
326 //       the config files as they are read. If the environment\r
327 //       variable is not set, debug_lvl is set to 1 internally\r
328 //    0: This requests JSBSim not to output any messages\r
329 //       whatsoever.\r
330 //    1: This value explicity requests the normal JSBSim\r
331 //       startup messages\r
332 //    2: This value asks for a message to be printed out when\r
333 //       a class is instantiated\r
334 //    4: When this value is set, a message is displayed when a\r
335 //       FGModel object executes its Run() method\r
336 //    8: When this value is set, various runtime state variables\r
337 //       are printed out periodically\r
338 //    16: When set various parameters are sanity checked and\r
339 //       a message is printed out when they go out of bounds\r
340 \r
341 void FGPosition::Debug(int from)\r
342 {\r
343   if (debug_lvl <= 0) return;\r
344 \r
345   if (debug_lvl & 1) { // Standard console startup message output\r
346     if (from == 0) { // Constructor\r
347 \r
348     }\r
349   }\r
350   if (debug_lvl & 2 ) { // Instantiation/Destruction notification\r
351     if (from == 0) cout << "Instantiated: FGPosition" << endl;\r
352     if (from == 1) cout << "Destroyed:    FGPosition" << endl;\r
353   }\r
354   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects\r
355   }\r
356   if (debug_lvl & 8 ) { // Runtime state variables\r
357   }\r
358   if (debug_lvl & 16) { // Sanity checking\r
359   }\r
360   if (debug_lvl & 64) {\r
361     if (from == 0) { // Constructor\r
362       cout << IdSrc << endl;\r
363       cout << IdHdr << endl;\r
364     }\r
365   }\r
366 }\r
367 }\r