]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/FGTrimAxis.cpp
Latest JSBSim changes, including some gear tweaking from Jon and some
[flightgear.git] / src / FDM / JSBSim / FGTrimAxis.cpp
index a4df730636bc08df3251adfeb10607a25a78d9e1..f8417f8c57a22f6890c61ce26d7ad183cb1e0ff1 100644 (file)
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  Header:       FGTrimAxis.cpp
  Author:       Tony Peden
@@ -28,9 +28,9 @@
 --------------------------------------------------------------------------------
 7/3/00   TP   Created
  
-********************************************************************************
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 INCLUDES
-*******************************************************************************/
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
 #include <string>
 #include <stdlib.h>
@@ -40,29 +40,42 @@ INCLUDES
 #include "FGInitialCondition.h"
 #include "FGTrimAxis.h"
 #include "FGAircraft.h"
+#include "FGPropulsion.h"
 
-static const char *IdSrc = "$Header$";
+static const char *IdSrc = "$Id$";
 static const char *IdHdr = ID_TRIMAXIS;
 
 /*****************************************************************************/
 
-FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, Accel acc,
-                       Control ctrl, float ff) {
+FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
+                       Control ctrl) {
 
   fdmex=fdex;
   fgic=ic;
-  accel=acc;
+  state=st;
   control=ctrl;
-  tolerance=ff;
-  solver_eps=tolerance;
   max_iterations=10;
   control_value=0;
   its_to_stable_value=0;
   total_iterations=0;
   total_stability_iterations=0;
-  accel_convert=1.0;
+  state_convert=1.0;
   control_convert=1.0;
-  accel_value=0;
+  state_value=0;
+  state_target=0;
+  switch(state) {
+    case tUdot: tolerance = DEFAULT_TOLERANCE; break;
+    case tVdot: tolerance = DEFAULT_TOLERANCE; break;
+    case tWdot: tolerance = DEFAULT_TOLERANCE; break;
+    case tQdot: tolerance = DEFAULT_TOLERANCE / 10; break;
+    case tPdot: tolerance = DEFAULT_TOLERANCE / 10; break;
+    case tRdot: tolerance = DEFAULT_TOLERANCE / 10; break;
+    case tHmgt: tolerance = 0.01; break;
+    case  tNlf: state_target=1.0; tolerance = 1E-5; break;
+    case tAll: break;
+  }  
+  
+  solver_eps=tolerance;
   switch(control) {
   case tThrottle:
     control_min=0;
@@ -70,19 +83,19 @@ FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, Accel acc,
     control_value=0.5;
     break;
   case tBeta:
-    control_min=-30*DEGTORAD;
-    control_max=30*DEGTORAD;
-    control_convert=RADTODEG;
+    control_min=-30*degtorad;
+    control_max=30*degtorad;
+    control_convert=radtodeg;
     break;
   case tAlpha:
     control_min=fdmex->GetAircraft()->GetAlphaCLMin();
     control_max=fdmex->GetAircraft()->GetAlphaCLMax();
     if(control_max <= control_min) {
-      control_max=20*DEGTORAD;
-      control_min=-5*DEGTORAD;
+      control_max=20*degtorad;
+      control_min=-5*degtorad;
     }
     control_value= (control_min+control_max)/2;
-    control_convert=RADTODEG;
+    control_convert=radtodeg;
     solver_eps=tolerance/100;
     break;
   case tPitchTrim:
@@ -93,7 +106,7 @@ FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, Accel acc,
   case tRudder:
     control_min=-1;
     control_max=1;
-    accel_convert=RADTODEG;
+    state_convert=radtodeg;
     solver_eps=tolerance/100;
     break;
   case tAltAGL:
@@ -103,45 +116,59 @@ FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, Accel acc,
     solver_eps=tolerance/100;
     break;
   case tTheta:
-    control_min=-10*DEGTORAD;
-    control_max=10*DEGTORAD;
-    accel_convert=RADTODEG;
+    control_min=fdmex->GetRotation()->Gettht() - 5*degtorad;
+    control_max=fdmex->GetRotation()->Gettht() + 5*degtorad;
+    state_convert=radtodeg;
     break;
   case tPhi:
-    control_min=-30*DEGTORAD;
-    control_max=30*DEGTORAD;
-    accel_convert=RADTODEG;
+    control_min=fdmex->GetRotation()->Getphi() - 30*degtorad;
+    control_max=fdmex->GetRotation()->Getphi() + 30*degtorad;
+    state_convert=radtodeg;
+    control_convert=radtodeg;
     break;
   case tGamma:
     solver_eps=tolerance/100;
-    control_min=-80*DEGTORAD;
-    control_max=80*DEGTORAD;
-    control_convert=RADTODEG;
+    control_min=-80*degtorad;
+    control_max=80*degtorad;
+    control_convert=radtodeg;
+    break;
+  case tHeading:
+    control_min=fdmex->GetRotation()->Getpsi() - 30*degtorad;
+    control_max=fdmex->GetRotation()->Getpsi() + 30*degtorad;
+    state_convert=radtodeg;
     break;
   }
   
+  
+  Debug(0);
 }
 
 /*****************************************************************************/
 
-FGTrimAxis::~FGTrimAxis() {}
+FGTrimAxis::~FGTrimAxis(void)
+{
+  Debug(1);
+}
 
 /*****************************************************************************/
 
-void FGTrimAxis::getAccel(void) {
-  switch(accel) {
-  case tUdot: accel_value=fdmex -> GetTranslation()->GetUVWdot()(1); break;
-  case tVdot: accel_value=fdmex -> GetTranslation()->GetUVWdot()(2); break;
-  case tWdot: accel_value=fdmex -> GetTranslation()->GetUVWdot()(3); break;
-  case tQdot: accel_value=fdmex -> GetRotation()->GetPQRdot()(2);break;
-  case tPdot: accel_value=fdmex -> GetRotation()->GetPQRdot()(1); break;
-  case tRdot: accel_value=fdmex -> GetRotation()->GetPQRdot()(3); break;
+void FGTrimAxis::getState(void) {
+  switch(state) {
+  case tUdot: state_value=fdmex->GetTranslation()->GetUVWdot(1)-state_target; break;
+  case tVdot: state_value=fdmex->GetTranslation()->GetUVWdot(2)-state_target; break;
+  case tWdot: state_value=fdmex->GetTranslation()->GetUVWdot(3)-state_target; break;
+  case tQdot: state_value=fdmex->GetRotation()->GetPQRdot(2)-state_target;break;
+  case tPdot: state_value=fdmex->GetRotation()->GetPQRdot(1)-state_target; break;
+  case tRdot: state_value=fdmex->GetRotation()->GetPQRdot(3)-state_target; break;
+  case tHmgt: state_value=computeHmgt()-state_target; break;
+  case tNlf:  state_value=fdmex->GetAircraft()->GetNlf()-state_target; break;
+  case tAll: break;
   }
 }
 
 /*****************************************************************************/
 
-//Accels are not settable
+//States are not settable
 
 void FGTrimAxis::getControl(void) {
   switch(control) {
@@ -158,30 +185,54 @@ void FGTrimAxis::getControl(void) {
   case tTheta:     control_value=fdmex->GetRotation()->Gettht(); break;
   case tPhi:       control_value=fdmex->GetRotation()->Getphi(); break;
   case tGamma:     control_value=fdmex->GetPosition()->GetGamma();break;
+  case tHeading:   control_value=fdmex->GetRotation()->Getpsi(); break;
   }
 }
 
 /*****************************************************************************/
 
+double FGTrimAxis::computeHmgt(void) {
+  double diff;
+  
+  diff   = fdmex->GetRotation()->Getpsi() - 
+             fdmex->GetPosition()->GetGroundTrack();
+  
+  if( diff < -M_PI ) {
+     return (diff + 2*M_PI);
+  } else if( diff > M_PI ) {
+     return (diff - 2*M_PI);
+  } else {
+     return diff;
+  }
+
+}
+       
+/*****************************************************************************/
+
 
 void FGTrimAxis::setControl(void) {
   switch(control) {
   case tThrottle:  setThrottlesPct(); break;
   case tBeta:      fgic->SetBetaRadIC(control_value); break;
   case tAlpha:     fgic->SetAlphaRadIC(control_value);  break;
-  case tPitchTrim: fdmex->GetFCS() -> SetPitchTrimCmd(control_value); break;
-  case tElevator:  fdmex-> GetFCS() -> SetDeCmd(control_value); break;
+  case tPitchTrim: fdmex->GetFCS()->SetPitchTrimCmd(control_value); break;
+  case tElevator:  fdmex->GetFCS()->SetDeCmd(control_value); break;
   case tRollTrim:
-  case tAileron:   fdmex-> GetFCS() -> SetDaCmd(control_value); break;
+  case tAileron:   fdmex->GetFCS()->SetDaCmd(control_value); break;
   case tYawTrim:
-  case tRudder:    fdmex-> GetFCS() -> SetDrCmd(control_value); break;
+  case tRudder:    fdmex->GetFCS()->SetDrCmd(control_value); break;
   case tAltAGL:    fgic->SetAltitudeAGLFtIC(control_value); break;
   case tTheta:     fgic->SetPitchAngleRadIC(control_value); break;
   case tPhi:       fgic->SetRollAngleRadIC(control_value); break;
   case tGamma:     fgic->SetFlightPathAngleRadIC(control_value); break;
+  case tHeading:   fgic->SetTrueHeadingRadIC(control_value); break;
   }
 }
 
+
+  
+
+
 /*****************************************************************************/
 
 // the aircraft center of rotation is no longer the cg once the gear
@@ -190,16 +241,16 @@ void FGTrimAxis::setControl(void) {
 // new center of rotation, pick a gear unit as a reference and use its
 // location vector to calculate the new height change. i.e. new altitude =
 // earth z component of that vector (which is in body axes )  
-void FGTrimAxis::SetThetaOnGround(float ff) {
+void FGTrimAxis::SetThetaOnGround(double ff) {
   int center,i,ref;
 
   // favor an off-center unit so that the same one can be used for both
   // pitch and roll.  An on-center unit is used (for pitch)if that's all 
   // that's in contact with the ground.
   i=0; ref=-1; center=-1;
-  while( (ref < 0) && (i < fdmex->GetAircraft()->GetNumGearUnits()) ) {
-    if(fdmex->GetAircraft()->GetGearUnit(i)->GetWOW()) {
-      if(fabs(fdmex->GetAircraft()->GetGearUnit(i)->GetBodyLocation()(2)) > 0.01)
+  while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
+    if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) {
+      if(fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01)
         ref=i;
       else
         center=i;
@@ -211,12 +262,12 @@ void FGTrimAxis::SetThetaOnGround(float ff) {
   }
   cout << "SetThetaOnGround ref gear: " << ref << endl;
   if(ref >= 0) {
-    float sp=fdmex->GetRotation()->GetSinphi();
-    float cp=fdmex->GetRotation()->GetCosphi();
-    float lx=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation()(1);
-    float ly=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation()(2);
-    float lz=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation()(3);
-    float hagl = -1*lx*sin(ff) +
+    double sp=fdmex->GetRotation()->GetSinphi();
+    double cp=fdmex->GetRotation()->GetCosphi();
+    double lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
+    double ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
+    double lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
+    double hagl = -1*lx*sin(ff) +
                     ly*sp*cos(ff) +
                     lz*cp*cos(ff);
    
@@ -229,24 +280,86 @@ void FGTrimAxis::SetThetaOnGround(float ff) {
 
 /*****************************************************************************/
 
-void FGTrimAxis::SetPhiOnGround(float ff) {
+bool FGTrimAxis::initTheta(void) {
+  int i,N,iAft, iForward;
+  double zAft,zForward,zDiff,theta;
+  bool level;  
+  double saveAlt;
+  
+  saveAlt=fgic->GetAltitudeAGLFtIC();
+  fgic->SetAltitudeAGLFtIC(100);
+  
+  
+  N=fdmex->GetGroundReactions()->GetNumGearUnits();
+  
+  //find the first wheel unit forward of the cg
+  //the list is short so a simple linear search is fine
+  for( i=0; i<N; i++ ) {
+    if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) > 0 ) {
+        iForward=i;
+        break;
+    }
+  }
+  //now find the first wheel unit aft of the cg
+  for( i=0; i<N; i++ ) {
+    if(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(1) < 0 ) {
+        iAft=i;
+        break;
+    }
+  }
+         
+  // now adjust theta till the wheels are the same distance from the ground
+  zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
+  zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
+  zDiff = zForward - zAft;
+  level=false;
+  theta=fgic->GetPitchAngleDegIC(); 
+  while(!level && (i < 100)) {
+       theta+=2.0*zDiff;
+       fgic->SetPitchAngleDegIC(theta);   
+       fdmex->RunIC(fgic);
+       zAft=fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear(3);
+        zForward=fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear(3);
+        zDiff = zForward - zAft;
+       //cout << endl << theta << "  " << zDiff << endl;
+       //cout << "0: " << fdmex->GetGroundReactions()->GetGearUnit(0)->GetLocalGear() << endl;
+       //cout << "1: " << fdmex->GetGroundReactions()->GetGearUnit(1)->GetLocalGear() << endl;
+
+       if(fabs(zDiff ) < 0.1) 
+           level=true;
+       i++;   
+  }                    
+  //cout << i << endl;
+  cout << "    Initial Theta: " << fdmex->GetRotation()->Gettht()*radtodeg << endl;
+  control_min=(theta+5)*degtorad;
+  control_max=(theta-5)*degtorad;
+  fgic->SetAltitudeAGLFtIC(saveAlt);
+  if(i < 100) 
+    return true;
+  else
+    return false;  
+} 
+
+/*****************************************************************************/
+
+void FGTrimAxis::SetPhiOnGround(double ff) {
   int i,ref;
 
   i=0; ref=-1;
   //must have an off-center unit here 
-  while( (ref < 0) && (i < fdmex->GetAircraft()->GetNumGearUnits()) ) {
-    if( (fdmex->GetAircraft()->GetGearUnit(i)->GetWOW()) && 
-      (fabs(fdmex->GetAircraft()->GetGearUnit(i)->GetBodyLocation()(2)) > 0.01))
+  while( (ref < 0) && (i < fdmex->GetGroundReactions()->GetNumGearUnits()) ) {
+    if( (fdmex->GetGroundReactions()->GetGearUnit(i)->GetWOW()) && 
+      (fabs(fdmex->GetGroundReactions()->GetGearUnit(i)->GetBodyLocation(2)) > 0.01))
         ref=i;
     i++; 
   }
   if(ref >= 0) {
-    float st=fdmex->GetRotation()->GetSintht();
-    float ct=fdmex->GetRotation()->GetCostht();
-    float lx=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation()(1);
-    float ly=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation()(2);
-    float lz=fdmex->GetAircraft()->GetGearUnit(ref)->GetBodyLocation()(3);
-    float hagl = -1*lx*st +
+    double st=fdmex->GetRotation()->GetSintht();
+    double ct=fdmex->GetRotation()->GetCostht();
+    double lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
+    double ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
+    double lz=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(3);
+    double hagl = -1*lx*st +
                     ly*sin(ff)*ct +
                     lz*cos(ff)*ct;
    
@@ -260,7 +373,7 @@ void FGTrimAxis::SetPhiOnGround(float ff) {
 
 void FGTrimAxis::Run(void) {
 
-  float last_accel_value;
+  double last_state_value;
   int i;
   setControl();
   //cout << "FGTrimAxis::Run: " << control_value << endl;
@@ -268,11 +381,11 @@ void FGTrimAxis::Run(void) {
   bool stable=false;
   while(!stable) {
     i++;
-    last_accel_value=accel_value;
+    last_state_value=state_value;
     fdmex->RunIC(fgic);
-    getAccel();
+    getState();
     if(i > 1) {
-      if((fabs(last_accel_value - accel_value) < tolerance) || (i >= 100) )
+      if((fabs(last_state_value - state_value) < tolerance) || (i >= 100) )
         stable=true;
     }
   }
@@ -285,36 +398,83 @@ void FGTrimAxis::Run(void) {
 /*****************************************************************************/
 
 void FGTrimAxis::setThrottlesPct(void) {
-  float tMin,tMax;
-  for(unsigned i=0;i<fdmex->GetAircraft()->GetNumEngines();i++) {
-      tMin=fdmex->GetAircraft()->GetEngine(i)->GetThrottleMin();
-      tMax=fdmex->GetAircraft()->GetEngine(i)->GetThrottleMax();
+  double tMin,tMax;
+  for(unsigned i=0;i<fdmex->GetPropulsion()->GetNumEngines();i++) {
+      tMin=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMin();
+      tMax=fdmex->GetPropulsion()->GetEngine(i)->GetThrottleMax();
       //cout << "setThrottlespct: " << i << ", " << control_min << ", " << control_max << ", " << control_value;
-      fdmex -> GetFCS() -> SetThrottleCmd(i,tMin+control_value*(tMax-tMin));
+      fdmex->GetFCS()->SetThrottleCmd(i,tMin+control_value*(tMax-tMin));
+      //cout << "setThrottlespct: " << fdmex->GetFCS()->GetThrottleCmd(i) << endl;
+      fdmex->RunIC(fgic); //apply throttle change
+      fdmex->GetPropulsion()->GetSteadyState();
   }
 }
 
-
 /*****************************************************************************/
 
-
 void FGTrimAxis::AxisReport(void) {
   
   char out[80];
   sprintf(out,"  %20s: %6.2f %5s: %9.2e Tolerance: %3.0e\n",
            GetControlName().c_str(), GetControl()*control_convert,
-           GetAccelName().c_str(), GetAccel(), GetTolerance()); 
+           GetStateName().c_str(), GetState()+state_target, GetTolerance()); 
   cout << out;
 
 }
 
-
 /*****************************************************************************/
 
-float FGTrimAxis::GetAvgStability( void ) {
+double FGTrimAxis::GetAvgStability( void ) {
   if(total_iterations > 0) {
-    return float(total_stability_iterations)/float(total_iterations);
+    return double(total_stability_iterations)/double(total_iterations);
   }
   return 0;
 }
 
+/*****************************************************************************/
+//    The bitmasked value choices are as follows:
+//    unset: In this case (the default) JSBSim would only print
+//       out the normally expected messages, essentially echoing
+//       the config files as they are read. If the environment
+//       variable is not set, debug_lvl is set to 1 internally
+//    0: This requests JSBSim not to output any messages
+//       whatsoever.
+//    1: This value explicity requests the normal JSBSim
+//       startup messages
+//    2: This value asks for a message to be printed out when
+//       a class is instantiated
+//    4: When this value is set, a message is displayed when a
+//       FGModel object executes its Run() method
+//    8: When this value is set, various runtime state variables
+//       are printed out periodically
+//    16: When set various parameters are sanity checked and
+//       a message is printed out when they go out of bounds
+
+void FGTrimAxis::Debug(int from)
+{
+
+  if (debug_lvl <= 0) return;
+  if (debug_lvl & 1 ) { // Standard console startup message output
+    if (from == 0) { // Constructor
+
+    }
+  }
+  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
+    if (from == 0) cout << "Instantiated: FGTrimAxis" << endl;
+    if (from == 1) cout << "Destroyed:    FGTrimAxis" << endl;
+  }
+  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
+  }
+  if (debug_lvl & 8 ) { // Runtime state variables
+  }
+  if (debug_lvl & 16) { // Sanity checking
+  }
+  if (debug_lvl & 64) {
+    if (from == 0) { // Constructor
+      cout << IdSrc << endl;
+      cout << IdHdr << endl;
+    }
+  }
+}
+
+