]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
JSBSim updates. This update changes the file format, so an update of the base
[flightgear.git] / src / FDM / JSBSim / FGState.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2                                                                        
3  Module:       FGState.cpp
4  Author:       Jon Berndt
5  Date started: 11/17/98
6  Called by:    FGFDMExec and accessed by all models.
7  
8  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
9  
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14  
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19  
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23  
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26  
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 See header file.
30  
31 HISTORY
32 --------------------------------------------------------------------------------
33 11/17/98   JSB   Created
34  
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #ifdef FGFS
40 #  include <simgear/compiler.h>
41 #  include <math.h>
42 #else
43 #  if defined(sgi) && !defined(__GNUC__)
44 #    include <math.h>
45 #  else
46 #    include <cmath>
47 #  endif
48 #endif
49
50 #ifdef _WIN32
51 #define snprintf _snprintf
52 #endif
53
54 #include "FGState.h"
55
56 static const char *IdSrc = "$Id$";
57 static const char *IdHdr = ID_STATE;
58
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 MACROS
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 CLASS IMPLEMENTATION
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66
67 FGState::FGState(FGFDMExec* fdex)
68 {
69   FDMExec = fdex;
70
71   a = 1000.0;
72   sim_time = 0.0;
73   dt = 1.0/120.0;
74
75   Aircraft     = FDMExec->GetAircraft();
76   Translation  = FDMExec->GetTranslation();
77   Rotation     = FDMExec->GetRotation();
78   Position     = FDMExec->GetPosition();
79   FCS          = FDMExec->GetFCS();
80   Output       = FDMExec->GetOutput();
81   Atmosphere   = FDMExec->GetAtmosphere();
82   Aerodynamics = FDMExec->GetAerodynamics();
83   GroundReactions = FDMExec->GetGroundReactions();
84   Propulsion      = FDMExec->GetPropulsion();
85   PropertyManager = FDMExec->GetPropertyManager();
86
87   for(int i=0;i<3;i++) vQdot_prev[i].InitMatrix();
88
89   bind();
90   
91   Debug(0);
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 FGState::~FGState()
97 {
98   unbind();
99   Debug(1);
100 }
101
102 //***************************************************************************
103 //
104 // Initialize: Assume all angles GIVEN IN RADIANS !!
105 //
106
107 void FGState::Initialize(double U, double V, double W,
108                          double phi, double tht, double psi,
109                          double Latitude, double Longitude, double H,
110                          double wnorth, double weast, double wdown)
111 {
112   double alpha, beta;
113   double qbar, Vt;
114   FGColumnVector3 vAeroUVW;
115
116   Position->SetLatitude(Latitude);
117   Position->SetLongitude(Longitude);
118   Position->Seth(H);
119
120   Atmosphere->Run();
121   
122   vLocalEuler << phi << tht << psi;
123   Rotation->SetEuler(vLocalEuler);
124
125   InitMatrices(phi, tht, psi);
126   
127   vUVW << U << V << W;
128   Translation->SetUVW(vUVW);
129   
130   Atmosphere->SetWindNED(wnorth, weast, wdown);
131   
132   vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
133   
134   if (vAeroUVW(eW) != 0.0)
135     alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
136   else
137     alpha = 0.0;
138   if (vAeroUVW(eV) != 0.0)
139     beta = vAeroUVW(eU)*vAeroUVW(eU)+vAeroUVW(eW)*vAeroUVW(eW) > 0.0 ? atan2(vAeroUVW(eV), (fabs(vAeroUVW(eU))/vAeroUVW(eU))*sqrt(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW))) : 0.0;
140   else
141     beta = 0.0;
142
143   Translation->SetAB(alpha, beta);
144
145   Vt = sqrt(U*U + V*V + W*W);
146   Translation->SetVt(Vt);
147
148   Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
149
150   qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
151   Translation->Setqbar(qbar);
152
153   vLocalVelNED = mTb2l*vUVW;
154   Position->SetvVel(vLocalVelNED);
155 }
156
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158
159 void FGState::Initialize(FGInitialCondition *FGIC)
160 {
161   double tht,psi,phi;
162   double U, V, W, h;
163   double latitude, longitude;
164   double wnorth,weast, wdown;
165   
166   latitude = FGIC->GetLatitudeRadIC();
167   longitude = FGIC->GetLongitudeRadIC();
168   h = FGIC->GetAltitudeFtIC();
169   U = FGIC->GetUBodyFpsIC();
170   V = FGIC->GetVBodyFpsIC();
171   W = FGIC->GetWBodyFpsIC();
172   tht = FGIC->GetThetaRadIC();
173   phi = FGIC->GetPhiRadIC();
174   psi = FGIC->GetPsiRadIC();
175   wnorth = FGIC->GetWindNFpsIC();
176   weast = FGIC->GetWindEFpsIC();
177   wdown = FGIC->GetWindDFpsIC();
178   
179   Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
180   Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() + 
181                                              FGIC->GetTerrainAltitudeFtIC() );
182
183   // need to fix the wind speed args, here.  
184   Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
185 }
186
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189 void FGState::InitMatrices(double phi, double tht, double psi)
190 {
191   double thtd2, psid2, phid2;
192   double Sthtd2, Spsid2, Sphid2;
193   double Cthtd2, Cpsid2, Cphid2;
194   double Cphid2Cthtd2;
195   double Cphid2Sthtd2;
196   double Sphid2Sthtd2;
197   double Sphid2Cthtd2;
198
199   thtd2 = tht/2.0;
200   psid2 = psi/2.0;
201   phid2 = phi/2.0;
202
203   Sthtd2 = sin(thtd2);
204   Spsid2 = sin(psid2);
205   Sphid2 = sin(phid2);
206
207   Cthtd2 = cos(thtd2);
208   Cpsid2 = cos(psid2);
209   Cphid2 = cos(phid2);
210
211   Cphid2Cthtd2 = Cphid2*Cthtd2;
212   Cphid2Sthtd2 = Cphid2*Sthtd2;
213   Sphid2Sthtd2 = Sphid2*Sthtd2;
214   Sphid2Cthtd2 = Sphid2*Cthtd2;
215
216   vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
217   vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
218   vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
219   vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
220
221   CalcMatrices();
222 }
223
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225
226 void FGState::CalcMatrices(void)
227 {
228   double Q0Q0, Q1Q1, Q2Q2, Q3Q3;
229   double Q0Q1, Q0Q2, Q0Q3, Q1Q2;
230   double Q1Q3, Q2Q3;
231
232   Q0Q0 = vQtrn(1)*vQtrn(1);
233   Q1Q1 = vQtrn(2)*vQtrn(2);
234   Q2Q2 = vQtrn(3)*vQtrn(3);
235   Q3Q3 = vQtrn(4)*vQtrn(4);
236   Q0Q1 = vQtrn(1)*vQtrn(2);
237   Q0Q2 = vQtrn(1)*vQtrn(3);
238   Q0Q3 = vQtrn(1)*vQtrn(4);
239   Q1Q2 = vQtrn(2)*vQtrn(3);
240   Q1Q3 = vQtrn(2)*vQtrn(4);
241   Q2Q3 = vQtrn(3)*vQtrn(4);
242
243   mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
244   mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
245   mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
246   mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
247   mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
248   mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
249   mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
250   mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
251   mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
252
253   mTb2l = mTl2b;
254   mTb2l.T();
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate)
260 {
261   vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
262   vQdot(2) =  0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
263   vQdot(3) =  0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
264   vQdot(4) =  0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
265
266   vQtrn += Integrate(TRAPZ, dt*rate, vQdot, vQdot_prev);
267
268   vQtrn.Normalize();
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 FGColumnVector3& FGState::CalcEuler(void)
274 {
275   if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
276   if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
277
278   vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
279   vEuler(eTht) = asin(-mTl2b(1,3));
280   vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
281
282   if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
283
284   return vEuler;
285 }
286
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288
289 FGMatrix33& FGState::GetTs2b(void)
290 {
291   double ca, cb, sa, sb;
292
293   double alpha = Translation->Getalpha();
294   double beta  = Translation->Getbeta();
295
296   ca = cos(alpha);
297   sa = sin(alpha);
298   cb = cos(beta);
299   sb = sin(beta);
300
301   mTs2b(1,1) = ca*cb;
302   mTs2b(1,2) = -ca*sb;
303   mTs2b(1,3) = -sa;
304   mTs2b(2,1) = sb;
305   mTs2b(2,2) = cb;
306   mTs2b(2,3) = 0.0;
307   mTs2b(3,1) = sa*cb;
308   mTs2b(3,2) = -sa*sb;
309   mTs2b(3,3) = ca;
310
311   return mTs2b;
312 }
313
314 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315
316 FGMatrix33& FGState::GetTb2s(void)
317 {
318   float alpha,beta;
319   float ca, cb, sa, sb;
320   
321   alpha = Translation->Getalpha();
322   beta  = Translation->Getbeta();
323   
324   ca = cos(alpha);
325   sa = sin(alpha);
326   cb = cos(beta);
327   sb = sin(beta);
328
329   mTb2s(1,1) = ca*cb;
330   mTb2s(1,2) = sb;
331   mTb2s(1,3) = sa*cb;
332   mTb2s(2,1) = -ca*sb;
333   mTb2s(2,2) = cb;
334   mTb2s(2,3) = -sa*sb;
335   mTb2s(3,1) = -sa;
336   mTb2s(3,2) = 0.0;
337   mTb2s(3,3) = ca;
338
339   return mTb2s;
340 }
341
342 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343
344 void FGState::ReportState(void)
345 {
346 #if !defined(__BORLANDCPP__)
347   char out[80], flap[10], gear[12];
348   
349   cout << endl << "  JSBSim State" << endl;
350   snprintf(out,80,"    Weight: %7.0f lbs.  CG: %5.1f, %5.1f, %5.1f inches\n",
351                    FDMExec->GetMassBalance()->GetWeight(),
352                    FDMExec->GetMassBalance()->GetXYZcg(1),
353                    FDMExec->GetMassBalance()->GetXYZcg(2),
354                    FDMExec->GetMassBalance()->GetXYZcg(3));
355   cout << out;             
356   if ( FCS->GetDfPos() <= 0.01)
357     snprintf(flap,10,"Up");
358   else
359     snprintf(flap,10,"%2.0f",FCS->GetDfPos());
360
361   if (FCS->GetGearPos() < 0.01)
362     snprintf(gear,12,"Up");
363   else if (FCS->GetGearPos() > 0.99)
364     snprintf(gear,12,"Down");
365   else
366     snprintf(gear,12,"In Transit");
367
368   snprintf(out,80, "    Flaps: %3s  Gear: %12s\n",flap,gear);
369   cout << out;
370   snprintf(out,80, "    Speed: %4.0f KCAS  Mach: %5.2f\n",
371                     FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
372                     Translation->GetMach() );
373   cout << out;
374   snprintf(out,80, "    Altitude: %7.0f ft.  AGL Altitude: %7.0f ft.\n",
375                     Position->Geth(),
376                     Position->GetDistanceAGL() );
377   cout << out;
378   snprintf(out,80, "    Angle of Attack: %6.2f deg  Pitch Angle: %6.2f deg\n",
379                     Translation->Getalpha()*radtodeg,
380                     Rotation->Gettht()*radtodeg );
381   cout << out;
382   snprintf(out,80, "    Flight Path Angle: %6.2f deg  Climb Rate: %5.0f ft/min\n",
383                     Position->GetGamma()*radtodeg,
384                     Position->Gethdot()*60 );
385   cout << out;                  
386   snprintf(out,80, "    Normal Load Factor: %4.2f g's  Pitch Rate: %5.2f deg/s\n",
387                     Aircraft->GetNlf(),
388                     Rotation->GetPQR(2)*radtodeg );
389   cout << out;
390   snprintf(out,80, "    Heading: %3.0f deg true  Sideslip: %5.2f deg  Yaw Rate: %5.2f deg/s\n",
391                     Rotation->Getpsi()*radtodeg,
392                     Translation->Getbeta()*radtodeg,
393                     Rotation->GetPQR(3)*radtodeg  );                  
394   cout << out;
395   snprintf(out,80, "    Bank Angle: %5.2f deg  Roll Rate: %5.2f deg/s\n",
396                     Rotation->Getphi()*radtodeg, 
397                     Rotation->GetPQR(1)*radtodeg );
398   cout << out;
399   snprintf(out,80, "    Elevator: %5.2f deg  Left Aileron: %5.2f deg  Rudder: %5.2f deg\n",
400                     FCS->GetDePos(ofRad)*radtodeg,
401                     FCS->GetDaLPos(ofRad)*radtodeg,
402                     FCS->GetDrPos(ofRad)*radtodeg );
403   cout << out;                  
404   snprintf(out,80, "    Throttle: %5.2f%c\n",
405                     FCS->GetThrottlePos(0)*100,'%' );
406   cout << out;
407   
408   snprintf(out,80, "    Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
409                     FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
410                     FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
411   cout << out; 
412   
413   snprintf(out,80, "    Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
414                     Position->GetVground()*fpstokts,
415                     Position->GetGroundTrack()*radtodeg );
416   cout << out;                                   
417 #endif
418
419
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421
422 void FGState::bind(void)
423 {
424   PropertyManager->Tie("sim-time-sec",this,
425                         &FGState::Getsim_time);
426 }
427
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429                         
430 void FGState::unbind(void)
431 {
432   PropertyManager->Untie("sim-time-sec");
433 }
434
435 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 //    The bitmasked value choices are as follows:
437 //    unset: In this case (the default) JSBSim would only print
438 //       out the normally expected messages, essentially echoing
439 //       the config files as they are read. If the environment
440 //       variable is not set, debug_lvl is set to 1 internally
441 //    0: This requests JSBSim not to output any messages
442 //       whatsoever.
443 //    1: This value explicity requests the normal JSBSim
444 //       startup messages
445 //    2: This value asks for a message to be printed out when
446 //       a class is instantiated
447 //    4: When this value is set, a message is displayed when a
448 //       FGModel object executes its Run() method
449 //    8: When this value is set, various runtime state variables
450 //       are printed out periodically
451 //    16: When set various parameters are sanity checked and
452 //       a message is printed out when they go out of bounds
453
454 void FGState::Debug(int from)
455 {
456   if (debug_lvl <= 0) return;
457
458   if (debug_lvl & 1) { // Standard console startup message output
459     if (from == 0) { // Constructor
460
461     }
462   }
463   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
464     if (from == 0) cout << "Instantiated: FGState" << endl;
465     if (from == 1) cout << "Destroyed:    FGState" << endl;
466   }
467   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
468   }
469   if (debug_lvl & 8 ) { // Runtime state variables
470   }
471   if (debug_lvl & 16) { // Sanity checking
472   }
473   if (debug_lvl & 64) {
474     if (from == 0) { // Constructor
475       cout << IdSrc << endl;
476       cout << IdHdr << endl;
477     }
478   }
479 }
480