1
\$\begingroup\$

I have come across a handful of examples of PID controllers where the process input is the accumulated PID output, i.e. the controller loop is u += pid(...) rather than u = pid(...) where u is the process input.

For the sake of example, say we are using PID to control the speed of a motor via PWM,

class PID:
    def __init__(self, kp, ki, kd):
        self.kp = kp
        self.ki = ki
        self.kd = kd

    ...

    def pid(self, set_point, process_variable):
        now = time()
        dt = now - self.last_time

        error = set_point - process_variable
        p = self.kp * error
        i = self.ki * error * dt + self.i_sum
        d = self.kd * (error - self.last_error) / dt
        output = p + i + d

        self.i_sum = i
        self.last_error = error
        self.last_time = now

        return output

My understanding of PID is that we should use the controller as

pid = PID(kp, ki, kd)
...
motor_pwm = pid.pid(target_speed, measured_speed)

But I often see it implemented as

pid = PID(kp, ki, kd)
...
motor_pwm += pid.pid(target_speed, measured_speed)

Now, for ki = kd = 0, the latter makes some intuitive sense to me; in fact, I think it actually gives you a kind of PI controller with ki = kp, and dt = 1 enforced. Once you introduce ki != 0 or kd != 0, however, I can't square this with any of the textbook explanations of PID I have read.

Is this 'accumulated' u += pid(...) controller simply an incorrect implementation of PID?

\$\endgroup\$
6
  • \$\begingroup\$ Can you please show the code or what variable u denotes. \$\endgroup\$
    – Long Pham
    Nov 18, 2018 at 12:41
  • \$\begingroup\$ A PI controller is a PID controller with Kd = 0. \$\endgroup\$
    – Long Pham
    Nov 18, 2018 at 12:42
  • \$\begingroup\$ @Long Pham u is the process input. The code examples I am referring to are linked in the question. \$\endgroup\$
    – Sam
    Nov 18, 2018 at 12:47
  • \$\begingroup\$ Remember, you are asking a bunch of random people on the Internet for help and it's your duty to present the question for them. \$\endgroup\$
    – Long Pham
    Nov 18, 2018 at 12:51
  • 1
    \$\begingroup\$ Fair. Question updated. \$\endgroup\$
    – Sam
    Nov 18, 2018 at 13:27

1 Answer 1

2
\$\begingroup\$

It is not necessarily an incorrect implementation, if the feedback signal that your PID loop is tracking and controlling is the first derivative of the control output you need to emit. The continual accumulation of the PID output is equivalent to integration.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Typical example: The output of the control loop is a voltage, which a motor turns into speed. Integrated speed is distance. \$\endgroup\$
    – Janka
    Nov 18, 2018 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.