Skip to content
Snippets Groups Projects
Commit c6a2afe4 authored by Mirko Wächter's avatar Mirko Wächter
Browse files

refactoring in PIDController

parent 368186eb
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
using namespace armarx; using namespace armarx;
PIDController::PIDController(float Kp, float Ki, float Kd, double maxControlValue, double maxAcceleration) : PIDController::PIDController(float Kp, float Ki, float Kd, double maxControlValue, double maxDerivation) :
Kp(Kp), Kp(Kp),
Ki(Ki), Ki(Ki),
Kd(Kd), Kd(Kd),
...@@ -40,7 +40,8 @@ PIDController::PIDController(float Kp, float Ki, float Kd, double maxControlValu ...@@ -40,7 +40,8 @@ PIDController::PIDController(float Kp, float Ki, float Kd, double maxControlValu
target(0), target(0),
controlValue(0), controlValue(0),
maxControlValue(maxControlValue), maxControlValue(maxControlValue),
maxAcceleration(maxAcceleration) maxDerivation(maxDerivation),
controlValueDerivation(0)
{ {
reset(); reset();
} }
...@@ -113,10 +114,11 @@ void PIDController::update(double deltaSec, double measuredValue, double targetV ...@@ -113,10 +114,11 @@ void PIDController::update(double deltaSec, double measuredValue, double targetV
controlValue = Kp * error + Ki * integral + Kd * derivative; controlValue = Kp * error + Ki * integral + Kd * derivative;
if (deltaSec > 0.0) if (deltaSec > 0.0)
{ {
double acc = (controlValue - oldControlValue) / deltaSec; double deriv = (controlValue - oldControlValue) / deltaSec;
if (fabs(acc) > maxAcceleration) if (fabs(deriv) > maxDerivation)
{ {
controlValue = oldControlValue + maxAcceleration * deltaSec * math::MathUtils::Sign(acc); controlValueDerivation = maxDerivation * deltaSec * math::MathUtils::Sign(deriv);
controlValue = oldControlValue + controlValueDerivation;
} }
} }
controlValue = std::min(fabs(controlValue), maxControlValue) * math::MathUtils::Sign(controlValue); controlValue = std::min(fabs(controlValue), maxControlValue) * math::MathUtils::Sign(controlValue);
...@@ -134,7 +136,7 @@ double PIDController::getControlValue() const ...@@ -134,7 +136,7 @@ double PIDController::getControlValue() const
} }
MultiDimPIDController::MultiDimPIDController(float Kp, float Ki, float Kd, double maxControlValue, double maxAcceleration) : MultiDimPIDController::MultiDimPIDController(float Kp, float Ki, float Kd, double maxControlValue, double maxDerivation) :
Kp(Kp), Kp(Kp),
Ki(Ki), Ki(Ki),
Kd(Kd), Kd(Kd),
...@@ -142,7 +144,7 @@ MultiDimPIDController::MultiDimPIDController(float Kp, float Ki, float Kd, doubl ...@@ -142,7 +144,7 @@ MultiDimPIDController::MultiDimPIDController(float Kp, float Ki, float Kd, doubl
derivative(0), derivative(0),
previousError(0), previousError(0),
maxControlValue(maxControlValue), maxControlValue(maxControlValue),
maxAcceleration(maxAcceleration) maxDerivation(maxDerivation)
{ {
reset(); reset();
} }
...@@ -195,10 +197,10 @@ void MultiDimPIDController::update(const double deltaSec, const Eigen::VectorXf& ...@@ -195,10 +197,10 @@ void MultiDimPIDController::update(const double deltaSec, const Eigen::VectorXf&
float maxNewJointAcc = accVec.maxCoeff(); float maxNewJointAcc = accVec.maxCoeff();
float minNewJointAcc = accVec.minCoeff(); float minNewJointAcc = accVec.minCoeff();
maxNewJointAcc = std::max<float>(fabs(minNewJointAcc), fabs(maxNewJointAcc)); maxNewJointAcc = std::max<float>(fabs(minNewJointAcc), fabs(maxNewJointAcc));
if (maxNewJointAcc > maxAcceleration) if (maxNewJointAcc > maxDerivation)
{ {
auto newValue = oldControlValue + accVec * maxAcceleration / maxNewJointAcc * deltaSec; auto newValue = oldControlValue + accVec * maxDerivation / maxNewJointAcc * deltaSec;
ARMARX_DEBUG << deactivateSpam(0.5) << VAROUT(maxAcceleration) << VAROUT(maxNewJointAcc) << VAROUT(controlValue) << VAROUT(oldControlValue) << VAROUT(newValue); ARMARX_DEBUG << deactivateSpam(0.5) << VAROUT(maxDerivation) << VAROUT(maxNewJointAcc) << VAROUT(controlValue) << VAROUT(oldControlValue) << VAROUT(newValue);
controlValue = newValue; controlValue = newValue;
} }
} }
......
...@@ -35,7 +35,7 @@ namespace armarx ...@@ -35,7 +35,7 @@ namespace armarx
public Logging public Logging
{ {
public: public:
PIDController(float Kp, float Ki, float Kd, double maxControlValue = std::numeric_limits<double>::max(), double maxAcceleration = std::numeric_limits<double>::max()); PIDController(float Kp, float Ki, float Kd, double maxControlValue = std::numeric_limits<double>::max(), double maxDerivation = std::numeric_limits<double>::max());
void update(double deltaSec, double measuredValue, double targetValue); void update(double deltaSec, double measuredValue, double targetValue);
void update(double measuredValue, double targetValue); void update(double measuredValue, double targetValue);
void update(double measuredValue); void update(double measuredValue);
...@@ -53,8 +53,9 @@ namespace armarx ...@@ -53,8 +53,9 @@ namespace armarx
double target; double target;
IceUtil::Time lastUpdateTime; IceUtil::Time lastUpdateTime;
double controlValue; double controlValue;
double controlValueDerivation;
double maxControlValue; double maxControlValue;
double maxAcceleration; double maxDerivation;
bool firstRun; bool firstRun;
mutable RecursiveMutex mutex; mutable RecursiveMutex mutex;
}; };
...@@ -64,7 +65,7 @@ namespace armarx ...@@ -64,7 +65,7 @@ namespace armarx
public Logging public Logging
{ {
public: public:
MultiDimPIDController(float Kp, float Ki, float Kd, double maxControlValue = std::numeric_limits<double>::max(), double maxAcceleration = std::numeric_limits<double>::max()); MultiDimPIDController(float Kp, float Ki, float Kd, double maxControlValue = std::numeric_limits<double>::max(), double maxDerivation = std::numeric_limits<double>::max());
void update(const double deltaSec, const Eigen::VectorXf& measuredValue, const Eigen::VectorXf& targetValue); void update(const double deltaSec, const Eigen::VectorXf& measuredValue, const Eigen::VectorXf& targetValue);
void update(const Eigen::VectorXf& measuredValue, const Eigen::VectorXf& targetValue); void update(const Eigen::VectorXf& measuredValue, const Eigen::VectorXf& targetValue);
const Eigen::VectorXf& getControlValue() const; const Eigen::VectorXf& getControlValue() const;
...@@ -81,7 +82,7 @@ namespace armarx ...@@ -81,7 +82,7 @@ namespace armarx
IceUtil::Time lastUpdateTime; IceUtil::Time lastUpdateTime;
Eigen::VectorXf controlValue; Eigen::VectorXf controlValue;
double maxControlValue; double maxControlValue;
double maxAcceleration; double maxDerivation;
bool firstRun; bool firstRun;
mutable RecursiveMutex mutex; mutable RecursiveMutex mutex;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment