1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-05 21:52:10 +01:00

Tweak the integral calculation so it is scaled in ms internally. This avoids a

loss of precision on the accumulation.
This commit is contained in:
James Cotton 2011-06-22 18:51:25 -05:00
parent 373689207a
commit a09642675c

View File

@ -296,15 +296,15 @@ float ApplyPid(pid_type * pid, const float err)
{
float diff = (err - pid->lastErr);
pid->lastErr = err;
pid->iAccumulator += err * pid->i * dT;
if(fabs(pid->iAccumulator) > pid->iLim) {
if(pid->iAccumulator >0) {
pid->iAccumulator = pid->iLim;
} else {
pid->iAccumulator = -pid->iLim;
}
// Scale up accumulator by 1000 while computing to avoid losing precision
pid->iAccumulator += err * (pid->i * dT * 1000);
if(pid->iAccumulator > (pid->iLim * 1000)) {
pid->iAccumulator = pid->iLim * 1000;
} else if (pid->iAccumulator < -(pid->iLim * 1000)) {
pid->iAccumulator = -pid->iLim * 1000;
}
return ((err * pid->p) + pid->iAccumulator + (diff * pid->d / dT));
return ((err * pid->p) + pid->iAccumulator / 1000 + (diff * pid->d / dT));
}