Skip to main content

A simple, easy to use PID controller

Project description

simple-pid

Travis PyPI Read the Docs License Downloads Code style: black

A simple and easy to use PID controller in Python. If you want a PID controller without external dependencies that just works, this is for you! The PID was designed to be robust with help from Brett Beauregards guide.

Usage is very simple:

from simple_pid import PID
pid = PID(1, 0.1, 0.05, setpoint=1)

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

while True:
    # Compute new output from the PID according to the systems current value
    control = pid(v)

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

Complete API documentation can be found here.

Installation

To install, run:

pip install simple-pid

Usage

The PID class implements __call__(), which means that to compute a new output value, you simply call the object like this:

output = pid(current_value)

The basics

The PID works best when it is updated at regular intervals. To achieve this, set sample_time to the amount of time there should be between each update and then call the PID every time in the program loop. A new output will only be calculated when sample_time seconds has passed:

pid.sample_time = 0.01  # Update every 0.01 seconds

while True:
    output = pid(current_value)

To set the setpoint, ie. the value that the PID is trying to achieve, simply set it like this:

pid.setpoint = 10

The tunings can be changed any time when the PID is running. They can either be set individually or all at once:

pid.Ki = 1.0
pid.tunings = (1.0, 0.2, 0.4)

To use the PID in reverse mode, meaning that an increase in the input leads to a decrease in the output (like when cooling for example), you can set the tunings to negative values:

pid.tunings = (-1.0, -0.1, 0)

Note that all the tunings should have the same sign.

In order to get output values in a certain range, and also to avoid integral windup (since the integral term will never be allowed to grow outside of these limits), the output can be limited to a range:

pid.output_limits = (0, 10)    # Output value will be between 0 and 10
pid.output_limits = (0, None)  # Output will always be above 0, but with no upper bound

Other features

Auto mode

To disable the PID so that no new values are computed, set auto mode to False:

pid.auto_mode = False  # No new values will be computed when pid is called
pid.auto_mode = True   # pid is enabled again

When disabling the PID and controlling a system manually, it might be useful to tell the PID controller where to start from when giving back control to it. This can be done by enabling auto mode like this:

pid.set_auto_mode(True, last_output=8.0)

This will set the I-term to the value given to last_output, meaning that if the system that is being controlled was stable at that output value the PID will keep the system stable if started from that point, without any big bumps in the output when turning the PID back on.

Observing separate components

When tuning the PID, it can be useful to see how each of the components contribute to the output. They can be seen like this:

p, i, d = pid.components  # The separate terms are now in p, i, d

Proportional on measurement

To eliminate overshoot in certain types of systems, you can calculate the proportional term directly on the measurement instead of the error. This can be enabled like this:

pid.proportional_on_measurement = True

Error mapping

To transform the error value to another domain before doing any computations on it, you can supply an error_map callback function to the PID. The callback function should take one argument which is the error from the setpoint. This can be used e.g. to get a degree value error in a yaw angle control with values between [-pi, pi):

import math

def pi_clip(angle):
    if angle > 0:
        if angle > math.pi:
            return angle - 2*math.pi
    else:
        if angle < -math.pi:
            return angle + 2*math.pi
    return angle

pid.error_map = pi_clip

Tests

Use the following to run tests:

tox

License

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple-pid-1.0.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_pid-1.0.0-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file simple-pid-1.0.0.tar.gz.

File metadata

  • Download URL: simple-pid-1.0.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for simple-pid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f101661e3ae6596342f2fe6d8e56a0ec96525fae335f91c7b59cf1660f97a7ef
MD5 a1e0737c4447fb1be36b04f9520fb7fa
BLAKE2b-256 4dbb80bb78ebb1242a5569fd40f012c5629f04e656d2b830afd299797fa75a82

See more details on using hashes here.

File details

Details for the file simple_pid-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: simple_pid-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for simple_pid-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ef9f1dd7357128f6cf870498fecdec9d9c9b3e420d78d406239adf516e348f59
MD5 4a08cb05a119bdb793b95f94233ad3cb
BLAKE2b-256 f023fa4e51987a031580f3af54a75eae60aacce593e23669075ae65664511244

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page