br3ttb / Arduino-PID-Library

1.91k stars 1.1k forks source link

Proportional on measurement in PID::Compute seems to modify integral error #139

Open yzhang2907 opened 10 months ago

yzhang2907 commented 10 months ago

I was reading the source code in PID_v1.cpp in conjunction with associated blog posts on Brett's site, and I spotted something interesting.

On line 71-72 of PID_v1.cpp, we have the following:

/*Add Proportional on Measurement, if P_ON_M is specified*/
if(!pOnE) outputSum-= kp * dInput;

To my understanding, kp * dInput is the P term when P_ON_M is specified.

However, if we had P_ON_E specified instead, the P term is added to the output instead of being accumulated into outputSum (lines 77-83):

/*Add Proportional on Error, if P_ON_E is specified*/
double output;
if(pOnE) output = kp * error;
else output = 0;

/*Compute Rest of PID Output*/
output += outputSum - kd * dInput;

In this case, I think kp * error is the P term when we have P_on_E specified.

Why is the P term treated differently when it is proportional on measurement? Why is it being accumulated into outputSum, which seems to be relevant only to the I term?

I am not sure if this is correct and something eclipsed my understanding, or if this is a mistake that should be fixed (if so, I can drop a pull request to fix it). If someone could enlighten me on this question, it would be much appreciated. Thanks!