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