]> 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 221a8ce30af8972daca4bcaf1fd2fab867c9f7a2..f8417f8c57a22f6890c61ce26d7ad183cb1e0ff1 100644 (file)
@@ -54,7 +54,6 @@ FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
   fgic=ic;
   state=st;
   control=ctrl;
-  solver_eps=tolerance;
   max_iterations=10;
   control_value=0;
   its_to_stable_value=0;
@@ -63,6 +62,20 @@ FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
   state_convert=1.0;
   control_convert=1.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, State st,
     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, State st,
   case tRudder:
     control_min=-1;
     control_max=1;
-    state_convert=RADTODEG;
+    state_convert=radtodeg;
     solver_eps=tolerance/100;
     break;
   case tAltAGL:
@@ -103,60 +116,53 @@ FGTrimAxis::FGTrimAxis(FGFDMExec* fdex, FGInitialCondition* ic, State st,
     solver_eps=tolerance/100;
     break;
   case tTheta:
-    control_min=fdmex->GetRotation()->Gettht() - 5*DEGTORAD;
-    control_max=fdmex->GetRotation()->Gettht() + 5*DEGTORAD;
-    state_convert=RADTODEG;
+    control_min=fdmex->GetRotation()->Gettht() - 5*degtorad;
+    control_max=fdmex->GetRotation()->Gettht() + 5*degtorad;
+    state_convert=radtodeg;
     break;
   case tPhi:
-    control_min=fdmex->GetRotation()->Getphi() - 30*DEGTORAD;
-    control_max=fdmex->GetRotation()->Getphi() + 30*DEGTORAD;
-    state_convert=RADTODEG;
-    control_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;
+    control_min=fdmex->GetRotation()->Getpsi() - 30*degtorad;
+    control_max=fdmex->GetRotation()->Getpsi() + 30*degtorad;
+    state_convert=radtodeg;
     break;
   }
   
-  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;
-  }  
   
-  if (debug_lvl & 2) cout << "Instantiated: FGTrimAxis" << endl;
+  Debug(0);
 }
 
 /*****************************************************************************/
 
-FGTrimAxis::~FGTrimAxis()
+FGTrimAxis::~FGTrimAxis(void)
 {
-  if (debug_lvl & 2) cout << "Destroyed:    FGTrimAxis" << endl;
+  Debug(1);
 }
 
 /*****************************************************************************/
 
 void FGTrimAxis::getState(void) {
   switch(state) {
-  case tUdot: state_value=fdmex->GetTranslation()->GetUVWdot(1); break;
-  case tVdot: state_value=fdmex->GetTranslation()->GetUVWdot(2); break;
-  case tWdot: state_value=fdmex->GetTranslation()->GetUVWdot(3); break;
-  case tQdot: state_value=fdmex->GetRotation()->GetPQRdot(2);break;
-  case tPdot: state_value=fdmex->GetRotation()->GetPQRdot(1); break;
-  case tRdot: state_value=fdmex->GetRotation()->GetPQRdot(3); break;
-  case tHmgt: state_value=computeHmgt(); break;
+  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;
   }
 }
 
@@ -185,8 +191,8 @@ void FGTrimAxis::getControl(void) {
 
 /*****************************************************************************/
 
-float FGTrimAxis::computeHmgt(void) {
-  float diff;
+double FGTrimAxis::computeHmgt(void) {
+  double diff;
   
   diff   = fdmex->GetRotation()->Getpsi() - 
              fdmex->GetPosition()->GetGroundTrack();
@@ -235,7 +241,7 @@ 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
@@ -256,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->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
-    float ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
-    float lz=fdmex->GetGroundReactions()->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);
    
@@ -276,9 +282,9 @@ void FGTrimAxis::SetThetaOnGround(float ff) {
 
 bool FGTrimAxis::initTheta(void) {
   int i,N,iAft, iForward;
-  float zAft,zForward,zDiff,theta;
+  double zAft,zForward,zDiff,theta;
   bool level;  
-  float saveAlt;
+  double saveAlt;
   
   saveAlt=fgic->GetAltitudeAGLFtIC();
   fgic->SetAltitudeAGLFtIC(100);
@@ -324,9 +330,9 @@ bool FGTrimAxis::initTheta(void) {
        i++;   
   }                    
   //cout << i << endl;
-  cout << "    Initial Theta: " << fdmex->GetRotation()->Gettht()*RADTODEG << endl;
-  control_min=(theta+5)*DEGTORAD;
-  control_max=(theta-5)*DEGTORAD;
+  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;
@@ -336,7 +342,7 @@ bool FGTrimAxis::initTheta(void) {
 
 /*****************************************************************************/
 
-void FGTrimAxis::SetPhiOnGround(float ff) {
+void FGTrimAxis::SetPhiOnGround(double ff) {
   int i,ref;
 
   i=0; ref=-1;
@@ -348,12 +354,12 @@ void FGTrimAxis::SetPhiOnGround(float ff) {
     i++; 
   }
   if(ref >= 0) {
-    float st=fdmex->GetRotation()->GetSintht();
-    float ct=fdmex->GetRotation()->GetCostht();
-    float lx=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(1);
-    float ly=fdmex->GetGroundReactions()->GetGearUnit(ref)->GetBodyLocation(2);
-    float lz=fdmex->GetGroundReactions()->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;
    
@@ -367,7 +373,7 @@ void FGTrimAxis::SetPhiOnGround(float ff) {
 
 void FGTrimAxis::Run(void) {
 
-  float last_state_value;
+  double last_state_value;
   int i;
   setControl();
   //cout << "FGTrimAxis::Run: " << control_value << endl;
@@ -392,12 +398,15 @@ void FGTrimAxis::Run(void) {
 /*****************************************************************************/
 
 void FGTrimAxis::setThrottlesPct(void) {
-  float tMin,tMax;
+  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();
   }
 }
 
@@ -408,23 +417,64 @@ 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,
-           GetStateName().c_str(), GetState(), 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;
 }
 
 /*****************************************************************************/
-
-void FGTrimAxis::Debug(void)
+//    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;
+    }
+  }
 }
 
+