Skip to main content

A simple, easy to use PID controller

Project description

simple-pid

Travis PyPI Read the Docs license

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 ouput 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 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 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

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

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

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-0.1.4.tar.gz (4.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-0.1.4-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: simple-pid-0.1.4.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for simple-pid-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9c7aeae16a1b883cd886377bc682f9ba67457a7bee98fbe8a95b56d72bf354f8
MD5 7f37cfe50caa9fe5f49be0c09b7df7dc
BLAKE2b-256 3c52523db0c9ad0db12d3fc38006af195729600d97b8feee98b2233acff52284

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_pid-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for simple_pid-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a3ad09260215025bf3e7e3eb41dd0a58cddfc589e4200f74e42af6f2ffa9b2e9
MD5 56f0096fef3f778986575a504373916e
BLAKE2b-256 c4d9bfe25f8afcb3131f96a907bd11a2a72757cd64968d72f578728752a8343b

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