simple-pid

2.0.1 · active · verified Sat Apr 11

simple-pid is a Python library that provides a simple and easy-to-use PID controller. It is designed to be robust and operate without external dependencies, making it suitable for various control system applications. The library is actively maintained, with the current version being 2.0.1, and new major versions released approximately every 2-3 years, supplemented by minor and patch releases as needed.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of `simple-pid`. It initializes a `PID` controller with proportional, integral, and derivative gains, and a target setpoint. The controller is then used within a loop to regulate a simulated system, updating its control output periodically based on the system's current value. Key parameters like `sample_time` and `output_limits` are configured for robust operation.

import time
from simple_pid import PID

# A dummy controlled system for demonstration
class ControlledSystem:
    def __init__(self, initial_value=0.0):
        self.value = initial_value
        self.time_step = 0.1

    def update(self, control_input):
        # Simulate system response, e.g., a simple first-order system
        self.value += (control_input - self.value) * (self.time_step * 0.5)
        return self.value

# Initialize PID controller: Kp, Ki, Kd, setpoint
# Here, a setpoint of 10 is desired.
pid = PID(2.0, 0.5, 0.2, setpoint=10.0)

# Configure PID parameters
pid.sample_time = 0.1  # Update every 0.1 seconds
pid.output_limits = (-10, 10) # Limit output to avoid integral windup

# Initialize the controlled system
system = ControlledSystem(initial_value=0.0)

print(f"Initial system value: {system.value:.2f}")

# Run the control loop for a few iterations
for i in range(100):
    # Compute new output from the PID according to the system's current value
    control_output = pid(system.value)

    # Feed the PID output to the system and get its current value
    system_value = system.update(control_output)

    # Optional: Print current state
    if i % 10 == 0:
        print(f"Iteration {i}: Setpoint={pid.setpoint:.2f}, System Value={system_value:.2f}, Control Output={control_output:.2f}")

    time.sleep(pid.sample_time)

print(f"Final system value: {system.value:.2f}")
# Expected output will show the system value approaching 10.0

view raw JSON →