m-lundberg / simple-pid

A simple and easy to use PID controller in Python
MIT License
767 stars 209 forks source link

Is it possible to use variable inputs? #42

Closed jwagn96 closed 3 years ago

jwagn96 commented 3 years ago

Hello, i'm new to your PID-library. It works just fine with an constant input. Now I want to figure out how to make this controller work with variable inputs like ramps. Just restarting the PID in a timed loop won't work for the I-and D-values. Is there already a possibility to feed an array of setpoints (for example a temperature curve) to the controller? Thanks in advance jwagn

m-lundberg commented 3 years ago

I'm not sure I understand what you mean. Do you mean that you want to ramp up the setpoint while controlling? You can do that by simply setting pid.setpoint to the value you currently want the setpoint to be. You don't need to reset the whole PID to do it.

Something like this:

# Create the controller once
pid = PID(1, 0.1, 0.05, setpoint=1)

# Assume we have a system we want to control in controlled_system (a heater for example)
v = controlled_system.update(0)

while True:
    # For each value, give the current value to the PID and get a control variable
    control = pid(v)

    # Feed the PID output to the system and get its current value
    v = controlled_system.update(control)

    # Update the setpoint
    pid.setpoint = next_setpoint()

Or maybe I just completely misunderstood what you meant?

jwagn96 commented 3 years ago

that's exactly what I meant. Thank you for your quick reply!

m-lundberg commented 3 years ago

Happy to help!