sum_error in pid controller [closed]

Hi all,

I'm trying to implement a PID controller by myself and I've a question about the sum_error in I control. Here is a short code based on the PID theory.

void pid()
{
error = target - current;

pTerm = Kp * error;

sum_error = sum_error + error * deltaT ;
iTerm = Ki * sum_error;

dTerm = Kd * (error - last_error) / deltaT;
last_error = error;

Term = K*(pTerm + iTerm + dTerm);
}


Now, I start my commands:

1. Phase 1, If at t=0, I set target=1.0, and the controller begins to drive motor to go to the target=1.0,
2. Phase 2, and then, at t=N, I set target=2.0, and the controller begins to drive motor to go to the target=2.0

My question is, in the beginning of phase 1, the error=1.0, the sum_error=0, and after the phase 1, the sum_error is not zero anymore, it's positive. And in the beginning of phase 2, the error=1.0 (it is also the same with above), but the sum_error is positive. So, the iTerm at t=N is much greater than iTerm at t=0.

It means, the curves between phase 2 and phase 1 are different!!!

But to end-user, the command 1, and the command 2 is almost the same, and it should drive the same effort.

Should I set the sum_error to zero or bound it? Can anyone tell me how to handle the sum_error in typical?

Any comment will be much appreciated!!

Kevin Kuei

edit retag reopen merge delete