artf4165: D term of PID controller now uses change in input instead of change in error

This avoids large additions introduced by the D term when a step change occurs in the setpoint. Otherwise, the changes return the same values. Let error = setpoint - input and prevError = prevSetpoint - prevInput. If the D term is calculated via error - prevError, then:

error - prevError = (setpoint - input) - (prevSetpoint - prevInput)

If we ignore the setpoint changing, then we get:

error - prevError = (setpoint - input) - (setpoint - prevInput)
                  = prevInput - input

Change-Id: Ifa4af9b265e3c4bd263e8541355f2b80269693e9
This commit is contained in:
Tyler Veness
2015-07-14 17:01:42 -07:00
committed by Brad Miller (WPI)
parent 918cde0c0f
commit f0e3bb5164
4 changed files with 12 additions and 12 deletions

View File

@@ -33,7 +33,7 @@ public class PIDController implements PIDInterface, LiveWindowSendable, Controll
private boolean m_continuous = false; // do the endpoints wrap around? eg.
// Absolute encoder
private boolean m_enabled = false; // is the pid controller enabled
private double m_prevError = 0.0; // the prior sensor input (used to compute
private double m_prevInput = 0.0; // the prior sensor input (used to compute
// velocity)
private double m_totalError = 0.0; // the sum of the errors for use in the
// integral calc
@@ -270,8 +270,8 @@ public class PIDController implements PIDInterface, LiveWindowSendable, Controll
}
m_result =
m_P * m_error + m_I * m_totalError + m_D * (m_error - m_prevError) + m_setpoint * m_F;
m_prevError = m_error;
m_P * m_error + m_I * m_totalError + m_D * (m_prevInput - input) + m_setpoint * m_F;
m_prevInput = input;
if (m_result > m_maximumOutput) {
m_result = m_maximumOutput;
@@ -572,7 +572,7 @@ public class PIDController implements PIDInterface, LiveWindowSendable, Controll
*/
public synchronized void reset() {
disable();
m_prevError = 0;
m_prevInput = 0;
m_totalError = 0;
m_result = 0;
}