Skip to content

Commit

Permalink
Merge pull request #1337 from gelanchez/master
Browse files Browse the repository at this point in the history
PID avoid derivative kick
  • Loading branch information
knmcguire committed Jan 30, 2024
2 parents d1339b8 + d536c30 commit 64d5f21
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/utils/interface/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct
{
float desired; //< set point
float error; //< error
float prevError; //< previous error
float prevMeasured; //< previous measurement
float integ; //< integral
float deriv; //< derivative
float kp; //< proportional gain
Expand Down
118 changes: 57 additions & 61 deletions src/utils/src/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void pidInit(PidObject* pid, const float desired, const float kp,
bool enableDFilter)
{
pid->error = 0;
pid->prevError = 0;
pid->prevMeasured = 0;
pid->integ = 0;
pid->deriv = 0;
pid->desired = desired;
Expand All @@ -56,86 +56,82 @@ void pidInit(PidObject* pid, const float desired, const float kp,

float pidUpdate(PidObject* pid, const float measured, const bool updateError)
{
float output = 0.0f;
float output = 0.0f;

if (updateError)
{
pid->error = pid->desired - measured;
}

pid->outP = pid->kp * pid->error;
output += pid->outP;
if (updateError)
{
pid->error = pid->desired - measured;
}

float deriv = (pid->error - pid->prevError) / pid->dt;

#if CONFIG_CONTROLLER_PID_FILTER_ALL
pid->outP = pid->kp * pid->error;
output += pid->outP;

float deriv = -(measured - pid->prevMeasured) / pid->dt;

#if CONFIG_CONTROLLER_PID_FILTER_ALL
pid->deriv = deriv;
#else
if (pid->enableDFilter){
pid->deriv = lpf2pApply(&pid->dFilter, deriv);
} else {
pid->deriv = deriv;
#else
if (pid->enableDFilter){
pid->deriv = lpf2pApply(&pid->dFilter, deriv);
} else {
pid->deriv = deriv;
}
#endif
if (isnan(pid->deriv)) {
pid->deriv = 0;
}
pid->outD = pid->kd * pid->deriv;
output += pid->outD;
#endif
if (isnan(pid->deriv)) {
pid->deriv = 0;
}
pid->outD = pid->kd * pid->deriv;
output += pid->outD;

pid->integ += pid->error * pid->dt;
pid->integ += pid->error * pid->dt;

// Constrain the integral (unless the iLimit is zero)
if(pid->iLimit != 0)
{
pid->integ = constrain(pid->integ, -pid->iLimit, pid->iLimit);
}
// Constrain the integral (unless the iLimit is zero)
if(pid->iLimit != 0)
{
pid->integ = constrain(pid->integ, -pid->iLimit, pid->iLimit);
}

pid->outI = pid->ki * pid->integ;
output += pid->outI;

pid->outI = pid->ki * pid->integ;
output += pid->outI;

pid->outFF = pid->kff * pid->desired;
output += pid->outFF;

#if CONFIG_CONTROLLER_PID_FILTER_ALL
//filter complete output instead of only D component to compensate for increased noise from increased barometer influence
if (pid->enableDFilter)
{
output = lpf2pApply(&pid->dFilter, output);
}
else {
output = output;
}
if (isnan(output)) {
output = 0;
}
#endif



// Constrain the total PID output (unless the outputLimit is zero)
if(pid->outputLimit != 0)
pid->outFF = pid->kff * pid->desired;
output += pid->outFF;

#if CONFIG_CONTROLLER_PID_FILTER_ALL
//filter complete output instead of only D component to compensate for increased noise from increased barometer influence
if (pid->enableDFilter)
{
output = constrain(output, -pid->outputLimit, pid->outputLimit);
output = lpf2pApply(&pid->dFilter, output);
}
else {
output = output;
}
if (isnan(output)) {
output = 0;
}
#endif

// Constrain the total PID output (unless the outputLimit is zero)
if(pid->outputLimit != 0)
{
output = constrain(output, -pid->outputLimit, pid->outputLimit);
}

pid->prevError = pid->error;
pid->prevMeasured = measured;

return output;
return output;
}

void pidSetIntegralLimit(PidObject* pid, const float limit) {
pid->iLimit = limit;
}


void pidReset(PidObject* pid)
{
pid->error = 0;
pid->prevError = 0;
pid->integ = 0;
pid->deriv = 0;
pid->error = 0;
pid->prevMeasured = 0;
pid->integ = 0;
pid->deriv = 0;
}

void pidSetError(PidObject* pid, const float error)
Expand Down

0 comments on commit 64d5f21

Please sign in to comment.