]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGState.cpp
Latest JSBSim changes, including some gear tweaking from Jon and some
[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 _MSC_VER
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 #define RegisterVariable(ID,DEF) coeffdef[#ID] = ID; paramdef[ID] = DEF
64
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 CLASS IMPLEMENTATION
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 //
71 // For every term registered here there must be a corresponding handler in
72 // GetParameter() below that retrieves that parameter. Also, there must be an
73 // entry in the enum eParam definition in FGJSBBase.h. The ID is what must be used
74 // in any config file entry which references that item.
75
76 FGState::FGState(FGFDMExec* fdex)
77 {
78   FDMExec = fdex;
79
80   a = 1000.0;
81   sim_time = 0.0;
82   dt = 1.0/120.0;
83   ActiveEngine = -1;
84
85   Aircraft     = FDMExec->GetAircraft();
86   Translation  = FDMExec->GetTranslation();
87   Rotation     = FDMExec->GetRotation();
88   Position     = FDMExec->GetPosition();
89   FCS          = FDMExec->GetFCS();
90   Output       = FDMExec->GetOutput();
91   Atmosphere   = FDMExec->GetAtmosphere();
92   Aerodynamics = FDMExec->GetAerodynamics();
93   GroundReactions = FDMExec->GetGroundReactions();
94   Propulsion      = FDMExec->GetPropulsion();
95
96   RegisterVariable(FG_TIME,           " time "           );
97   RegisterVariable(FG_QBAR,           " qbar "           );
98   RegisterVariable(FG_WINGAREA,       " wing_area "      );
99   RegisterVariable(FG_WINGSPAN,       " wingspan "       );
100   RegisterVariable(FG_CBAR,           " cbar "           );
101   RegisterVariable(FG_ALPHA,          " alpha "          );
102   RegisterVariable(FG_ALPHADOT,       " alphadot "       );
103   RegisterVariable(FG_BETA,           " beta "           );
104   RegisterVariable(FG_ABETA,          " |beta| "         );
105   RegisterVariable(FG_BETADOT,        " betadot "        );
106   RegisterVariable(FG_PHI,            " roll_angle "     );
107   RegisterVariable(FG_THT,            " pitch_angle "    );
108   RegisterVariable(FG_PSI,            " heading_angle "  );
109   RegisterVariable(FG_PITCHRATE,      " pitch_rate "     );
110   RegisterVariable(FG_ROLLRATE,       " roll_rate "      );
111   RegisterVariable(FG_YAWRATE,        " yaw_rate "       );
112   RegisterVariable(FG_AEROQ,          " aero_pitch_rate ");
113   RegisterVariable(FG_AEROP,          " aero_roll_rate " );
114   RegisterVariable(FG_AEROR,          " aero_yaw_rate "  );
115   RegisterVariable(FG_CL_SQRD,        " Clift_sqrd "     );
116   RegisterVariable(FG_MACH,           " mach "           );
117   RegisterVariable(FG_ALTITUDE,       " altitude "       );
118   RegisterVariable(FG_BI2VEL,         " BI2Vel "         );
119   RegisterVariable(FG_CI2VEL,         " CI2Vel "         );
120   RegisterVariable(FG_ELEVATOR_POS,   " elevator_pos "   );
121   RegisterVariable(FG_AILERON_POS,    " aileron_pos "    );
122   RegisterVariable(FG_RUDDER_POS,     " rudder_pos "     );
123   RegisterVariable(FG_SPDBRAKE_POS,   " speedbrake_pos " );
124   RegisterVariable(FG_SPOILERS_POS,   " spoiler_pos "    );
125   RegisterVariable(FG_FLAPS_POS,      " flaps_pos "      );
126   RegisterVariable(FG_GEAR_POS,       " gear_pos "       );
127   RegisterVariable(FG_ELEVATOR_CMD,   " elevator_cmd "   );
128   RegisterVariable(FG_AILERON_CMD,    " aileron_cmd "    );
129   RegisterVariable(FG_RUDDER_CMD,     " rudder_cmd "     );
130   RegisterVariable(FG_SPDBRAKE_CMD,   " speedbrake_cmd " );
131   RegisterVariable(FG_SPOILERS_CMD,   " spoiler_cmd "    );
132   RegisterVariable(FG_FLAPS_CMD,      " flaps_cmd "      );
133   RegisterVariable(FG_THROTTLE_CMD,   " throttle_cmd "   );
134   RegisterVariable(FG_GEAR_CMD,       " gear_cmd "       );
135   RegisterVariable(FG_THROTTLE_POS,   " throttle_pos "   );
136   RegisterVariable(FG_MIXTURE_CMD,    " mixture_cmd "    );
137   RegisterVariable(FG_MIXTURE_POS,    " mixture_pos "    );
138   RegisterVariable(FG_MAGNETO_CMD,    " magneto_cmd "    );
139   RegisterVariable(FG_STARTER_CMD,    " starter_cmd "    );
140   RegisterVariable(FG_ACTIVE_ENGINE,  " active_engine "  );
141   RegisterVariable(FG_HOVERB,         " height/span "    );
142   RegisterVariable(FG_PITCH_TRIM_CMD, " pitch_trim_cmd " );
143   RegisterVariable(FG_YAW_TRIM_CMD,   " yaw_trim_cmd " );
144   RegisterVariable(FG_ROLL_TRIM_CMD,  " roll_trim_cmd " );
145   RegisterVariable(FG_LEFT_BRAKE_CMD, " left_brake_cmd " );
146   RegisterVariable(FG_RIGHT_BRAKE_CMD," right_brake_cmd ");
147   RegisterVariable(FG_CENTER_BRAKE_CMD," center_brake_cmd ");
148   RegisterVariable(FG_ALPHAH,          " h-tail alpha " );
149   RegisterVariable(FG_ALPHAW,          " wing alpha " );
150   RegisterVariable(FG_LBARH,           " h-tail arm " );
151   RegisterVariable(FG_LBARV,           " v-tail arm " );
152   RegisterVariable(FG_HTAILAREA,       " h-tail area " );
153   RegisterVariable(FG_VTAILAREA,       " v-tail area " );
154   RegisterVariable(FG_VBARH,           " h-tail volume " );
155   RegisterVariable(FG_VBARV,           " v-tail volume " );
156   RegisterVariable(FG_SET_LOGGING,    " data_logging "   );
157
158   Debug(0);
159 }
160
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162
163 FGState::~FGState()
164 {
165   Debug(1);
166 }
167
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169
170 double FGState::GetParameter(eParam val_idx) {
171   double scratch;
172   
173   switch(val_idx) {
174   case FG_TIME:
175     return sim_time;
176   case FG_QBAR:
177     return Translation->Getqbar();
178   case FG_WINGAREA:
179     return Aircraft->GetWingArea();
180   case FG_WINGSPAN:
181     return Aircraft->GetWingSpan();
182   case FG_CBAR:
183     return Aircraft->Getcbar();
184   case FG_LBARH:
185     return Aircraft->Getlbarh();
186   case FG_LBARV:
187     return Aircraft->Getvbarh();
188   case FG_HTAILAREA:
189     return Aircraft->GetHTailArea();
190   case FG_VTAILAREA:
191     return Aircraft->GetVTailArea();
192   case FG_VBARH:
193     return Aircraft->Getvbarh();
194   case FG_VBARV:
195     return Aircraft->Getvbarv();
196   case FG_ALPHA:
197     return Translation->Getalpha();
198   case FG_ALPHAW:
199     return  Translation->Getalpha() + Aircraft->GetWingIncidence();
200   case FG_ALPHADOT:
201     return Translation->Getadot();
202   case FG_BETA:
203     return Translation->Getbeta();
204   case FG_ABETA:
205     return fabs(Translation->Getbeta());   
206   case FG_BETADOT:
207     return Translation->Getbdot();
208   case FG_PHI:
209     return Rotation->Getphi();
210   case FG_THT:
211     return Rotation->Gettht();
212   case FG_PSI:
213     return Rotation->Getpsi();
214   case FG_PITCHRATE:
215     return Rotation->GetPQR(eQ);
216   case FG_ROLLRATE:
217     return Rotation->GetPQR(eP);
218   case FG_YAWRATE:
219     return Rotation->GetPQR(eR);
220   case FG_AEROP:
221     return Rotation->GetAeroPQR(eP);
222   case FG_AEROQ:
223     return Rotation->GetAeroPQR(eQ);
224   case FG_AEROR:
225     return Rotation->GetAeroPQR(eR);
226   case FG_CL_SQRD:
227     if (Translation->Getqbar() > 0.00)
228       scratch = Aerodynamics->GetvLastFs(eLift)/(Aircraft->GetWingArea()*Translation->Getqbar());
229     else
230       scratch = 0.0;
231     return scratch*scratch;                                        
232   case FG_ELEVATOR_POS:
233     return FCS->GetDePos();
234   case FG_AILERON_POS:
235     return FCS->GetDaPos();
236   case FG_RUDDER_POS:
237     return FCS->GetDrPos();
238   case FG_SPDBRAKE_POS:
239     return FCS->GetDsbPos();
240   case FG_SPOILERS_POS:
241     return FCS->GetDspPos();
242   case FG_FLAPS_POS:
243     return FCS->GetDfPos();
244   case FG_ELEVATOR_CMD:
245     return FCS->GetDeCmd();
246   case FG_AILERON_CMD:
247     return FCS->GetDaCmd();
248   case FG_RUDDER_CMD:
249     return FCS->GetDrCmd();
250   case FG_SPDBRAKE_CMD:
251     return FCS->GetDsbCmd();
252   case FG_SPOILERS_CMD:
253     return FCS->GetDspCmd();
254   case FG_FLAPS_CMD:
255     return FCS->GetDfCmd();
256   case FG_MACH:
257     return Translation->GetMach();
258   case FG_ALTITUDE:
259     return Position->Geth();
260   case FG_BI2VEL:
261     if(Translation->GetVt() > 0)
262         return Aircraft->GetWingSpan()/(2.0 * Translation->GetVt());
263     else
264         return 0;
265   case FG_CI2VEL:
266     if(Translation->GetVt() > 0)
267         return Aircraft->Getcbar()/(2.0 * Translation->GetVt());
268     else
269         return 0;
270   case FG_THROTTLE_CMD:
271     if (ActiveEngine < 0) return FCS->GetThrottleCmd(0);
272     else return FCS->GetThrottleCmd(ActiveEngine);
273   case FG_THROTTLE_POS:
274     if (ActiveEngine < 0) return FCS->GetThrottlePos(0);
275     else return FCS->GetThrottlePos(ActiveEngine);
276   case FG_MAGNETO_CMD:
277     if (ActiveEngine < 0) return Propulsion->GetEngine(0)->GetMagnetos();
278     else return Propulsion->GetEngine(ActiveEngine)->GetMagnetos();
279   case FG_STARTER_CMD:
280     if (ActiveEngine < 0) {
281       if (Propulsion->GetEngine(0)->GetStarter()) return 1.0;
282       else return 0.0;
283     } else {
284       if (Propulsion->GetEngine(ActiveEngine)->GetStarter()) return 1.0;
285       else return 0.0;
286     }
287   case FG_MIXTURE_CMD:
288     if (ActiveEngine < 0) return FCS->GetMixtureCmd(0);
289     else return FCS->GetMixtureCmd(ActiveEngine);
290   case FG_MIXTURE_POS:
291     if (ActiveEngine < 0) return FCS->GetMixturePos(0);
292     else return FCS->GetMixturePos(ActiveEngine);
293   case FG_HOVERB:
294     return Position->GetHOverBMAC();
295   case FG_PITCH_TRIM_CMD:
296     return FCS->GetPitchTrimCmd();
297   case FG_YAW_TRIM_CMD:
298     return FCS->GetYawTrimCmd();
299   case FG_ROLL_TRIM_CMD:
300     return FCS->GetRollTrimCmd();
301   case FG_GEAR_CMD:
302     return FCS->GetGearCmd();
303   case FG_GEAR_POS:
304     return FCS->GetGearPos();    
305   default:
306     cerr << "FGState::GetParameter() - No handler for parameter " << paramdef[val_idx] << endl;
307     return 0.0;
308   }
309   return 0;
310 }
311
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313
314 double FGState::GetParameter(string val_string) {
315   return GetParameter(coeffdef[val_string]);
316 }
317
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319
320 eParam FGState::GetParameterIndex(string val_string)
321 {
322   return coeffdef[val_string];
323 }
324
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326
327 void FGState::SetParameter(eParam val_idx, double val)
328 {
329   unsigned i;
330
331   switch(val_idx) {
332   case FG_ELEVATOR_POS:
333     FCS->SetDePos(val);
334     break;
335   case FG_AILERON_POS:
336     FCS->SetDaPos(val);
337     break;
338   case FG_RUDDER_POS:
339     FCS->SetDrPos(val);
340     break;
341   case FG_SPDBRAKE_POS:
342     FCS->SetDsbPos(val);
343     break;
344   case FG_SPOILERS_POS:
345     FCS->SetDspPos(val);
346     break;
347   case FG_FLAPS_POS:
348     FCS->SetDfPos(val);
349     break;
350   case FG_THROTTLE_POS:
351     if (ActiveEngine == -1) {
352       for (i=0; i<Propulsion->GetNumEngines(); i++) {
353         FCS->SetThrottlePos(i,val);
354             }
355           } else {
356       FCS->SetThrottlePos(ActiveEngine,val);
357           }
358     break;
359   case FG_MIXTURE_POS:
360     if (ActiveEngine == -1) {
361       for (i=0; i<Propulsion->GetNumEngines(); i++) {
362         FCS->SetMixturePos(i,val);
363             }
364           } else {
365       FCS->SetMixturePos(ActiveEngine,val);
366           }
367     break;
368
369   case FG_ELEVATOR_CMD:
370     FCS->SetDeCmd(val);
371     break;
372   case FG_AILERON_CMD:
373     FCS->SetDaCmd(val);
374     break;
375   case FG_RUDDER_CMD:
376     FCS->SetDrCmd(val);
377     break;
378   case FG_SPDBRAKE_CMD:
379     FCS->SetDsbCmd(val);
380     break;
381   case FG_SPOILERS_CMD:
382     FCS->SetDspCmd(val);
383     break;
384   case FG_FLAPS_CMD:
385     FCS->SetDfCmd(val);
386     break;
387   case FG_THROTTLE_CMD:
388     if (ActiveEngine == -1) {
389       for (i=0; i<Propulsion->GetNumEngines(); i++) {
390         FCS->SetThrottleCmd(i,val);
391             }
392           } else {
393       FCS->SetThrottleCmd(ActiveEngine,val);
394           }
395     break;
396   case FG_MIXTURE_CMD:
397     if (ActiveEngine == -1) {
398       for (i=0; i<Propulsion->GetNumEngines(); i++) {
399         FCS->SetMixtureCmd(i,val);
400             }
401           } else {
402       FCS->SetMixtureCmd(ActiveEngine,val);
403           }
404     break;
405   case FG_MAGNETO_CMD:
406     if (ActiveEngine == -1) {
407       for (i=0; i<Propulsion->GetNumEngines(); i++) {
408         Propulsion->GetEngine(i)->SetMagnetos((int)val);
409       }
410     } else {
411       Propulsion->GetEngine(ActiveEngine)->SetMagnetos((int)val);
412     }
413     break;
414   case FG_STARTER_CMD:
415     if (ActiveEngine == -1) {
416       for (i=0; i<Propulsion->GetNumEngines(); i++) {
417         if (val < 0.001) 
418           Propulsion->GetEngine(i)->SetStarter(false);
419         else if (val >=  0.001)
420           Propulsion->GetEngine(i)->SetStarter(true);
421       }
422     } else {
423       Propulsion->GetEngine(ActiveEngine)->SetStarter(true);
424     }
425     break;
426   case FG_ACTIVE_ENGINE:
427     ActiveEngine = (int)val;
428     break;
429
430   case FG_LEFT_BRAKE_CMD:
431     FCS->SetLBrake(val);
432     break;
433   case FG_CENTER_BRAKE_CMD:
434     FCS->SetCBrake(val);
435     break;
436   case FG_RIGHT_BRAKE_CMD:
437     FCS->SetRBrake(val);
438     break;
439   case FG_GEAR_CMD:
440     FCS->SetGearCmd(val);
441     break;
442   case  FG_GEAR_POS:
443     FCS->SetGearPos(val);
444     break; 
445   case FG_SET_LOGGING:
446     if      (val < -0.01) Output->Disable();
447     else if (val >  0.01) Output->Enable();
448     else                  Output->Toggle();
449     break;
450
451   default:
452     cerr << "Parameter '" << val_idx << "' (" << paramdef[val_idx] << ") not handled" << endl;
453   }
454 }
455
456 //***************************************************************************
457 //
458 // Reset: Assume all angles READ FROM FILE IN DEGREES !!
459 //
460
461 bool FGState::Reset(string path, string acname, string fname)
462 {
463   string resetDef;
464   string token="";
465
466   double U, V, W;
467   double phi, tht, psi;
468   double latitude, longitude, h;
469   double wdir, wmag, wnorth, weast;
470
471 # ifndef macintosh
472   resetDef = path + "/" + acname + "/" + fname + ".xml";
473 # else
474   resetDef = path + ";" + acname + ";" + fname + ".xml";
475 # endif
476
477   FGConfigFile resetfile(resetDef);
478   if (!resetfile.IsOpen()) return false;
479
480   resetfile.GetNextConfigLine();
481   token = resetfile.GetValue();
482   if (token != string("initialize")) {
483     cerr << "The reset file " << resetDef
484          << " does not appear to be a reset file" << endl;
485     return false;
486   } else {
487     resetfile.GetNextConfigLine();
488     resetfile >> token;
489     cout << "Resetting using: " << token << endl << endl;
490   }
491   
492   while (token != string("/initialize") && token != string("EOF")) {
493     if (token == "UBODY") resetfile >> U;
494     if (token == "VBODY") resetfile >> V;
495     if (token == "WBODY") resetfile >> W;
496     if (token == "LATITUDE") resetfile >> latitude;
497     if (token == "LONGITUDE") resetfile >> longitude;
498     if (token == "PHI") resetfile >> phi;
499     if (token == "THETA") resetfile >> tht;
500     if (token == "PSI") resetfile >> psi;
501     if (token == "ALTITUDE") resetfile >> h;
502     if (token == "WINDDIR") resetfile >> wdir;
503     if (token == "VWIND") resetfile >> wmag;
504
505     resetfile >> token;
506   }
507   
508   
509   Position->SetLatitude(latitude*degtorad);
510   Position->SetLongitude(longitude*degtorad);
511   Position->Seth(h);
512
513   wnorth = wmag*ktstofps*cos(wdir*degtorad);
514   weast = wmag*ktstofps*sin(wdir*degtorad);
515   
516   Initialize(U, V, W, phi*degtorad, tht*degtorad, psi*degtorad,
517                latitude*degtorad, longitude*degtorad, h, wnorth, weast, 0.0);
518
519   return true;
520 }
521
522 //***************************************************************************
523 //
524 // Initialize: Assume all angles GIVEN IN RADIANS !!
525 //
526
527 void FGState::Initialize(double U, double V, double W,
528                          double phi, double tht, double psi,
529                          double Latitude, double Longitude, double H,
530                          double wnorth, double weast, double wdown)
531 {
532   double alpha, beta;
533   double qbar, Vt;
534   FGColumnVector3 vAeroUVW;
535
536   Position->SetLatitude(Latitude);
537   Position->SetLongitude(Longitude);
538   Position->Seth(H);
539
540   Atmosphere->Run();
541   
542   vLocalEuler << phi << tht << psi;
543   Rotation->SetEuler(vLocalEuler);
544
545   InitMatrices(phi, tht, psi);
546   
547   vUVW << U << V << W;
548   Translation->SetUVW(vUVW);
549   
550   Atmosphere->SetWindNED(wnorth, weast, wdown);
551   
552   vAeroUVW = vUVW + mTl2b*Atmosphere->GetWindNED();
553   
554   if (vAeroUVW(eW) != 0.0)
555     alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
556   else
557     alpha = 0.0;
558   if (vAeroUVW(eV) != 0.0)
559     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;
560   else
561     beta = 0.0;
562
563   Translation->SetAB(alpha, beta);
564
565   Vt = sqrt(U*U + V*V + W*W);
566   Translation->SetVt(Vt);
567
568   Translation->SetMach(Vt/Atmosphere->GetSoundSpeed());
569
570   qbar = 0.5*(U*U + V*V + W*W)*Atmosphere->GetDensity();
571   Translation->Setqbar(qbar);
572
573   vLocalVelNED = mTb2l*vUVW;
574   Position->SetvVel(vLocalVelNED);
575 }
576
577 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578
579 void FGState::Initialize(FGInitialCondition *FGIC) {
580
581   double tht,psi,phi;
582   double U, V, W, h;
583   double latitude, longitude;
584   double wnorth,weast, wdown;
585   
586   latitude = FGIC->GetLatitudeRadIC();
587   longitude = FGIC->GetLongitudeRadIC();
588   h = FGIC->GetAltitudeFtIC();
589   U = FGIC->GetUBodyFpsIC();
590   V = FGIC->GetVBodyFpsIC();
591   W = FGIC->GetWBodyFpsIC();
592   tht = FGIC->GetThetaRadIC();
593   phi = FGIC->GetPhiRadIC();
594   psi = FGIC->GetPsiRadIC();
595   wnorth = FGIC->GetWindNFpsIC();
596   weast = FGIC->GetWindEFpsIC();
597   wdown = FGIC->GetWindDFpsIC();
598   
599   Position->SetSeaLevelRadius( FGIC->GetSeaLevelRadiusFtIC() );
600   Position->SetRunwayRadius( FGIC->GetSeaLevelRadiusFtIC() + 
601                                              FGIC->GetTerrainAltitudeFtIC() );
602
603   // need to fix the wind speed args, here.  
604   Initialize(U, V, W, phi, tht, psi, latitude, longitude, h, wnorth, weast, wdown);
605 }
606
607 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
608
609 bool FGState::StoreData(string fname) {
610   ofstream datafile(fname.c_str());
611
612   if (datafile) {
613     datafile << Translation->GetUVW(eU);
614     datafile << Translation->GetUVW(eV);
615     datafile << Translation->GetUVW(eW);
616     datafile << Position->GetLatitude();
617     datafile << Position->GetLongitude();
618     datafile << Rotation->GetEuler(ePhi);
619     datafile << Rotation->GetEuler(eTht);
620     datafile << Rotation->GetEuler(ePsi);
621     datafile << Position->Geth();
622     datafile.close();
623     return true;
624   } else {
625     cerr << "Could not open dump file " << fname << endl;
626     return false;
627   }
628 }
629
630 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631
632 void FGState::InitMatrices(double phi, double tht, double psi) {
633   double thtd2, psid2, phid2;
634   double Sthtd2, Spsid2, Sphid2;
635   double Cthtd2, Cpsid2, Cphid2;
636   double Cphid2Cthtd2;
637   double Cphid2Sthtd2;
638   double Sphid2Sthtd2;
639   double Sphid2Cthtd2;
640
641   thtd2 = tht/2.0;
642   psid2 = psi/2.0;
643   phid2 = phi/2.0;
644
645   Sthtd2 = sin(thtd2);
646   Spsid2 = sin(psid2);
647   Sphid2 = sin(phid2);
648
649   Cthtd2 = cos(thtd2);
650   Cpsid2 = cos(psid2);
651   Cphid2 = cos(phid2);
652
653   Cphid2Cthtd2 = Cphid2*Cthtd2;
654   Cphid2Sthtd2 = Cphid2*Sthtd2;
655   Sphid2Sthtd2 = Sphid2*Sthtd2;
656   Sphid2Cthtd2 = Sphid2*Cthtd2;
657
658   vQtrn(1) = Cphid2Cthtd2*Cpsid2 + Sphid2Sthtd2*Spsid2;
659   vQtrn(2) = Sphid2Cthtd2*Cpsid2 - Cphid2Sthtd2*Spsid2;
660   vQtrn(3) = Cphid2Sthtd2*Cpsid2 + Sphid2Cthtd2*Spsid2;
661   vQtrn(4) = Cphid2Cthtd2*Spsid2 - Sphid2Sthtd2*Cpsid2;
662
663   CalcMatrices();
664 }
665
666 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667
668 void FGState::CalcMatrices(void) {
669   double Q0Q0, Q1Q1, Q2Q2, Q3Q3;
670   double Q0Q1, Q0Q2, Q0Q3, Q1Q2;
671   double Q1Q3, Q2Q3;
672
673   Q0Q0 = vQtrn(1)*vQtrn(1);
674   Q1Q1 = vQtrn(2)*vQtrn(2);
675   Q2Q2 = vQtrn(3)*vQtrn(3);
676   Q3Q3 = vQtrn(4)*vQtrn(4);
677   Q0Q1 = vQtrn(1)*vQtrn(2);
678   Q0Q2 = vQtrn(1)*vQtrn(3);
679   Q0Q3 = vQtrn(1)*vQtrn(4);
680   Q1Q2 = vQtrn(2)*vQtrn(3);
681   Q1Q3 = vQtrn(2)*vQtrn(4);
682   Q2Q3 = vQtrn(3)*vQtrn(4);
683
684   mTl2b(1,1) = Q0Q0 + Q1Q1 - Q2Q2 - Q3Q3;
685   mTl2b(1,2) = 2*(Q1Q2 + Q0Q3);
686   mTl2b(1,3) = 2*(Q1Q3 - Q0Q2);
687   mTl2b(2,1) = 2*(Q1Q2 - Q0Q3);
688   mTl2b(2,2) = Q0Q0 - Q1Q1 + Q2Q2 - Q3Q3;
689   mTl2b(2,3) = 2*(Q2Q3 + Q0Q1);
690   mTl2b(3,1) = 2*(Q1Q3 + Q0Q2);
691   mTl2b(3,2) = 2*(Q2Q3 - Q0Q1);
692   mTl2b(3,3) = Q0Q0 - Q1Q1 - Q2Q2 + Q3Q3;
693
694   mTb2l = mTl2b;
695   mTb2l.T();
696 }
697
698 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699
700 void FGState::IntegrateQuat(FGColumnVector3 vPQR, int rate) {
701   vQdot(1) = -0.5*(vQtrn(2)*vPQR(eP) + vQtrn(3)*vPQR(eQ) + vQtrn(4)*vPQR(eR));
702   vQdot(2) =  0.5*(vQtrn(1)*vPQR(eP) + vQtrn(3)*vPQR(eR) - vQtrn(4)*vPQR(eQ));
703   vQdot(3) =  0.5*(vQtrn(1)*vPQR(eQ) + vQtrn(4)*vPQR(eP) - vQtrn(2)*vPQR(eR));
704   vQdot(4) =  0.5*(vQtrn(1)*vPQR(eR) + vQtrn(2)*vPQR(eQ) - vQtrn(3)*vPQR(eP));
705   vQtrn += 0.5*dt*rate*(vlastQdot + vQdot);
706
707   vQtrn.Normalize();
708
709   vlastQdot = vQdot;
710 }
711
712 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713
714 FGColumnVector3& FGState::CalcEuler(void) {
715   if (mTl2b(3,3) == 0.0) mTl2b(3,3) = 0.0000001;
716   if (mTl2b(1,1) == 0.0) mTl2b(1,1) = 0.0000001;
717
718   vEuler(ePhi) = atan2(mTl2b(2,3), mTl2b(3,3));
719   vEuler(eTht) = asin(-mTl2b(1,3));
720   vEuler(ePsi) = atan2(mTl2b(1,2), mTl2b(1,1));
721
722   if (vEuler(ePsi) < 0.0) vEuler(ePsi) += 2*M_PI;
723
724   return vEuler;
725 }
726
727 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728
729 FGMatrix33& FGState::GetTs2b(void)
730 {
731   double ca, cb, sa, sb;
732
733   double alpha = Translation->Getalpha();
734   double beta  = Translation->Getbeta();
735
736   ca = cos(alpha);
737   sa = sin(alpha);
738   cb = cos(beta);
739   sb = sin(beta);
740
741   mTs2b(1,1) = ca*cb;
742   mTs2b(1,2) = -ca*sb;
743   mTs2b(1,3) = -sa;
744   mTs2b(2,1) = sb;
745   mTs2b(2,2) = cb;
746   mTs2b(2,3) = 0.0;
747   mTs2b(3,1) = sa*cb;
748   mTs2b(3,2) = -sa*sb;
749   mTs2b(3,3) = ca;
750
751   return mTs2b;
752 }
753
754 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755
756 FGMatrix33& FGState::GetTb2s(void)
757 {
758   float alpha,beta;
759   float ca, cb, sa, sb;
760   
761   alpha = Translation->Getalpha();
762   beta  = Translation->Getbeta();
763   
764   ca = cos(alpha);
765   sa = sin(alpha);
766   cb = cos(beta);
767   sb = sin(beta);
768
769   mTb2s(1,1) = ca*cb;
770   mTb2s(1,2) = sb;
771   mTb2s(1,3) = sa*cb;
772   mTb2s(2,1) = -ca*sb;
773   mTb2s(2,2) = cb;
774   mTb2s(2,3) = -sa*sb;
775   mTb2s(3,1) = -sa;
776   mTb2s(3,2) = 0.0;
777   mTb2s(3,3) = ca;
778
779   return mTb2s;
780 }
781
782 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
783
784 void FGState::ReportState(void) {
785   char out[80], flap[10], gear[12];
786   
787   cout << endl << "  JSBSim State" << endl;
788   snprintf(out,80,"    Weight: %7.0f lbs.  CG: %5.1f, %5.1f, %5.1f inches\n",
789                    FDMExec->GetMassBalance()->GetWeight(),
790                    FDMExec->GetMassBalance()->GetXYZcg(1),
791                    FDMExec->GetMassBalance()->GetXYZcg(2),
792                    FDMExec->GetMassBalance()->GetXYZcg(3));
793   cout << out;             
794   if( FCS->GetDfPos() <= 0.01)
795     snprintf(flap,10,"Up");
796   else
797     snprintf(flap,10,"%2.0f",FCS->GetDfPos());
798   if(FCS->GetGearPos() < 0.01)
799     snprintf(gear,12,"Up");
800   else if(FCS->GetGearPos() > 0.99)
801     snprintf(gear,12,"Down");
802   else
803     snprintf(gear,12,"In Transit");   
804   snprintf(out,80, "    Flaps: %3s  Gear: %12s\n",flap,gear);
805   cout << out;
806   snprintf(out,80, "    Speed: %4.0f KCAS  Mach: %5.2f\n",
807                     FDMExec->GetAuxiliary()->GetVcalibratedKTS(),
808                     GetParameter(FG_MACH) );
809   cout << out;
810   snprintf(out,80, "    Altitude: %7.0f ft.  AGL Altitude: %7.0f ft.\n",
811                     Position->Geth(),
812                     Position->GetDistanceAGL() );
813   cout << out;
814   snprintf(out,80, "    Angle of Attack: %6.2f deg  Pitch Angle: %6.2f deg\n",
815                     GetParameter(FG_ALPHA)*radtodeg,
816                     Rotation->Gettht()*radtodeg );
817   cout << out;
818   snprintf(out,80, "    Flight Path Angle: %6.2f deg  Climb Rate: %5.0f ft/min\n",
819                     Position->GetGamma()*radtodeg,
820                     Position->Gethdot()*60 );
821   cout << out;                  
822   snprintf(out,80, "    Normal Load Factor: %4.2f g's  Pitch Rate: %5.2f deg/s\n",
823                     Aircraft->GetNlf(),
824                     GetParameter(FG_PITCHRATE)*radtodeg );
825   cout << out;
826   snprintf(out,80, "    Heading: %3.0f deg true  Sideslip: %5.2f deg\n",
827                     Rotation->Getpsi()*radtodeg,
828                     GetParameter(FG_BETA)*radtodeg );                  
829   cout << out;
830   snprintf(out,80, "    Bank Angle: %5.2f deg\n",
831                     Rotation->Getphi()*radtodeg );
832   cout << out;
833   snprintf(out,80, "    Elevator: %5.2f deg  Left Aileron: %5.2f deg  Rudder: %5.2f deg\n",
834                     GetParameter(FG_ELEVATOR_POS)*radtodeg,
835                     GetParameter(FG_AILERON_POS)*radtodeg,
836                     GetParameter(FG_RUDDER_POS)*radtodeg );
837   cout << out;                  
838   snprintf(out,80, "    Throttle: %5.2f%c\n",
839                     FCS->GetThrottlePos(0)*100,'%' );
840   cout << out;
841   
842   snprintf(out,80, "    Wind Components: %5.2f kts head wind, %5.2f kts cross wind\n",
843                     FDMExec->GetAuxiliary()->GetHeadWind()*fpstokts,
844                     FDMExec->GetAuxiliary()->GetCrossWind()*fpstokts );
845   cout << out; 
846   
847   snprintf(out,80, "    Ground Speed: %4.0f knots , Ground Track: %3.0f deg true\n",
848                     Position->GetVground()*fpstokts,
849                     Position->GetGroundTrack()*radtodeg );
850   cout << out;                                   
851
852
853 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 //    The bitmasked value choices are as follows:
855 //    unset: In this case (the default) JSBSim would only print
856 //       out the normally expected messages, essentially echoing
857 //       the config files as they are read. If the environment
858 //       variable is not set, debug_lvl is set to 1 internally
859 //    0: This requests JSBSim not to output any messages
860 //       whatsoever.
861 //    1: This value explicity requests the normal JSBSim
862 //       startup messages
863 //    2: This value asks for a message to be printed out when
864 //       a class is instantiated
865 //    4: When this value is set, a message is displayed when a
866 //       FGModel object executes its Run() method
867 //    8: When this value is set, various runtime state variables
868 //       are printed out periodically
869 //    16: When set various parameters are sanity checked and
870 //       a message is printed out when they go out of bounds
871
872 void FGState::Debug(int from)
873 {
874   if (debug_lvl <= 0) return;
875
876   if (debug_lvl & 1) { // Standard console startup message output
877     if (from == 0) { // Constructor
878
879     }
880   }
881   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
882     if (from == 0) cout << "Instantiated: FGState" << endl;
883     if (from == 1) cout << "Destroyed:    FGState" << endl;
884   }
885   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
886   }
887   if (debug_lvl & 8 ) { // Runtime state variables
888   }
889   if (debug_lvl & 16) { // Sanity checking
890   }
891   if (debug_lvl & 64) {
892     if (from == 0) { // Constructor
893       cout << IdSrc << endl;
894       cout << IdHdr << endl;
895     }
896   }
897 }
898
899