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