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