simple-pid


Namesimple-pid JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/m-lundberg/simple-pid
SummaryA simple, easy to use PID controller
upload_time2021-04-11 10:04:15
maintainer
docs_urlNone
authorMartin Lundberg
requires_python
licenseMIT
keywords pid controller control
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # simple-pid

[![Travis](https://travis-ci.com/m-lundberg/simple-pid.svg?branch=master)](https://travis-ci.com/m-lundberg/simple-pid)
[![PyPI](https://img.shields.io/pypi/v/simple-pid.svg)](https://pypi.org/project/simple-pid/)
[![Read the Docs](https://img.shields.io/readthedocs/simple-pid.svg)](https://simple-pid.readthedocs.io/)
[![License](https://img.shields.io/github/license/m-lundberg/simple-pid.svg)](https://github.com/m-lundberg/simple-pid/blob/master/LICENSE.md)
[![Downloads](https://pepy.tech/badge/simple-pid)](https://pepy.tech/project/simple-pid)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/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](http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/).

Usage is very simple:

```python
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](https://simple-pid.readthedocs.io/en/latest/simple_pid.html#module-simple_pid.PID).

## 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:
```python
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:
```python
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:
```python
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:
```python
pid.Ki = 1.0
pid.tunings = (1.0, 0.2, 0.4)
```

To use the PID in [reverse mode](http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-direction/), 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:

```python
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](https://en.wikipedia.org/wiki/Integral_windup) (since the integral term will never be allowed to grow outside of these limits), the output can be limited to a range:
```python
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:
```python
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:
```python
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:
```python
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](http://brettbeauregard.com/blog/2017/06/introducing-proportional-on-measurement/) instead of the error. This can be enabled like this:
```python
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):
```python
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](https://github.com/m-lundberg/simple-pid/blob/master/LICENSE.md).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/m-lundberg/simple-pid",
    "name": "simple-pid",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "pid controller control",
    "author": "Martin Lundberg",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/00/27/73b35b5e57c0dec32cf180724d6c7ac2075b655f96c79217632f9ba9c7e1/simple-pid-1.0.1.tar.gz",
    "platform": "",
    "description": "# simple-pid\n\n[![Travis](https://travis-ci.com/m-lundberg/simple-pid.svg?branch=master)](https://travis-ci.com/m-lundberg/simple-pid)\n[![PyPI](https://img.shields.io/pypi/v/simple-pid.svg)](https://pypi.org/project/simple-pid/)\n[![Read the Docs](https://img.shields.io/readthedocs/simple-pid.svg)](https://simple-pid.readthedocs.io/)\n[![License](https://img.shields.io/github/license/m-lundberg/simple-pid.svg)](https://github.com/m-lundberg/simple-pid/blob/master/LICENSE.md)\n[![Downloads](https://pepy.tech/badge/simple-pid)](https://pepy.tech/project/simple-pid)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nA 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](http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/).\n\nUsage is very simple:\n\n```python\nfrom simple_pid import PID\npid = PID(1, 0.1, 0.05, setpoint=1)\n\n# Assume we have a system we want to control in controlled_system\nv = controlled_system.update(0)\n\nwhile True:\n    # Compute new output from the PID according to the systems current value\n    control = pid(v)\n\n    # Feed the PID output to the system and get its current value\n    v = controlled_system.update(control)\n```\n\nComplete API documentation can be found [here](https://simple-pid.readthedocs.io/en/latest/simple_pid.html#module-simple_pid.PID).\n\n## Installation\nTo install, run:\n```\npip install simple-pid\n```\n\n## Usage\nThe `PID` class implements `__call__()`, which means that to compute a new output value, you simply call the object like this:\n```python\noutput = pid(current_value)\n```\n\n### The basics\nThe 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:\n```python\npid.sample_time = 0.01  # Update every 0.01 seconds\n\nwhile True:\n    output = pid(current_value)\n```\n\nTo set the setpoint, ie. the value that the PID is trying to achieve, simply set it like this:\n```python\npid.setpoint = 10\n```\n\nThe tunings can be changed any time when the PID is running. They can either be set individually or all at once:\n```python\npid.Ki = 1.0\npid.tunings = (1.0, 0.2, 0.4)\n```\n\nTo use the PID in [reverse mode](http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-direction/), 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:\n\n```python\npid.tunings = (-1.0, -0.1, 0)\n```\n\nNote that all the tunings should have the same sign.\n\nIn order to get output values in a certain range, and also to avoid [integral windup](https://en.wikipedia.org/wiki/Integral_windup) (since the integral term will never be allowed to grow outside of these limits), the output can be limited to a range:\n```python\npid.output_limits = (0, 10)    # Output value will be between 0 and 10\npid.output_limits = (0, None)  # Output will always be above 0, but with no upper bound\n```\n\n### Other features\n#### Auto mode\nTo disable the PID so that no new values are computed, set auto mode to False:\n```python\npid.auto_mode = False  # No new values will be computed when pid is called\npid.auto_mode = True   # pid is enabled again\n```\nWhen 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:\n```python\npid.set_auto_mode(True, last_output=8.0)\n```\nThis 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.\n\n#### Observing separate components\nWhen tuning the PID, it can be useful to see how each of the components contribute to the output. They can be seen like this:\n```python\np, i, d = pid.components  # The separate terms are now in p, i, d\n```\n\n#### Proportional on measurement\nTo eliminate overshoot in certain types of systems, you can calculate the [proportional term directly on the measurement](http://brettbeauregard.com/blog/2017/06/introducing-proportional-on-measurement/) instead of the error. This can be enabled like this:\n```python\npid.proportional_on_measurement = True\n```\n\n#### Error mapping\nTo 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):\n```python\nimport math\n\ndef pi_clip(angle):\n    if angle > 0:\n        if angle > math.pi:\n            return angle - 2*math.pi\n    else:\n        if angle < -math.pi:\n            return angle + 2*math.pi\n    return angle\n\npid.error_map = pi_clip\n```\n\n## Tests\nUse the following to run tests:\n```\ntox\n```\n\n## License\nLicensed under the [MIT License](https://github.com/m-lundberg/simple-pid/blob/master/LICENSE.md).\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple, easy to use PID controller",
    "version": "1.0.1",
    "split_keywords": [
        "pid",
        "controller",
        "control"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "3c3109ede6c05308dc568b36b0b34fd0",
                "sha256": "99254d8f9a630062264ce4842ef1e90443558f336697268d16b9de52d3baec0b"
            },
            "downloads": -1,
            "filename": "simple_pid-1.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3c3109ede6c05308dc568b36b0b34fd0",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 8053,
            "upload_time": "2021-04-11T10:04:07",
            "upload_time_iso_8601": "2021-04-11T10:04:07.092501Z",
            "url": "https://files.pythonhosted.org/packages/39/72/489743f43655a6f168bd9ac1e24a2daf9c44998f19d7bbd8e7f070b3f451/simple_pid-1.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "60c580a6be3894cd4108d99b23179d47",
                "sha256": "60d6b3a7c3f19734d33bbc5938293c4924d7cfea74176def03306057b3f99524"
            },
            "downloads": -1,
            "filename": "simple-pid-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "60c580a6be3894cd4108d99b23179d47",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16795,
            "upload_time": "2021-04-11T10:04:15",
            "upload_time_iso_8601": "2021-04-11T10:04:15.574531Z",
            "url": "https://files.pythonhosted.org/packages/00/27/73b35b5e57c0dec32cf180724d6c7ac2075b655f96c79217632f9ba9c7e1/simple-pid-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-04-11 10:04:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "m-lundberg",
    "github_project": "simple-pid",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "simple-pid"
}
        
Elapsed time: 0.01637s