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