br3ttb / Arduino-PID-Library

1.93k stars 1.11k forks source link

Add deadband #126

Open mrx23dot opened 1 year ago

mrx23dot commented 1 year ago

Create a dual-sided deadband around the desired setpoint to prevent noisy feedback from producing control jitters This is disabled by setting deadband to zero, something like:

if (Math.Abs(error) <= DEADBAND)
{
  error = 0;
  if (target == 0)
    output = 0;
} 
else
  output = ((Kp * error) + (Ki * integral))/Ko;

could be inserted somewhere here

bool PID::Compute()
{
  const bool outofDeadband = (error > deadband) || (error < -deadband);

   if(!inAuto || !outDeadband) return false;
drf5n commented 1 year ago

Other solutions are that you could filter the noise out of your sensor, tune your parameters to be less sensitive to noise, or you could even apply this procedure in user space before your compute step:

if (Math.Abs(SetPoint - Input) <= DEADBAND){
   Input = Setpoint;
}
myPID.Compute();

... and then it wouldn't add the compute costs into the calculations for others.

I do think this filter would interfere with the ability of the integral term being able to adjust the process to within DEADBAND of the Setpoint.