Skip to main content

Simple (but complete) PID controller in Python

Project description

GitHub GitHub tag (latest by date) GitHub Release Date GitHub last commit GitHub issues Python version

PID_Py

PID_Py provide a PID controller wrote in Python. This PID controller is simple to use, but it's complete.

:bangbang: Non-responsability :bangbang:

I am not responsible for any material or personal damages in case of failure. Use at your own risk.

Installation

python3 -m pip install PID_Py

Usage

Minimum usage

from PID_Py.PID import PID

# Initialization
pid = PID(kp = 0.0, ki = 0.0, kd = 0.0)

...

# PID execution (call it as fast as you can)
command = pid(processValue = feedback, setpoint = targetValue)

In this usage the PID as no limitation, no history and the PID is in direct action (Error increasing -> Increase output).

Indirect action PID

If you have a system that required to decrease command to increase feedback, you can use indirectAction parameters.

from PID_Py.PID import PID

# Initialization
pid = PID(kp = 0.0, ki = 0.0, kd = 0.0, indirectAction = True)

...

# PID execution (call it as fast as you can)
command = pid(processValue = feedback, setpoint = targetValue)

Limiting output

If your command must be limit you can use outputLimits parameters.

from PID_Py.PID import PID

# Initialization
pid = PID(kp = 0.0, ki = 0.0, kd = 0.0, outputLimits = (0, 100))

...

# PID execution (call it as fast as you can)
command = pid(processValue = feedback, setpoint = targetValue)

By default the value is (None, None), which implies that there is no limits. You can activate just the maximum limit with (None, 100). The same for the minimum limit (-100, None).

Historian

If you want to historize PID values, you can configure the historian to record values.

from PID_Py.PID import PID
from PID_Py.PID import HistorianParameters

# Initialization
historianParameters = HistorianParamters.SETPOINT | HistorianParameters.PROCESS_VALUE
pid = PID(kp = 0.0, ki = 0.0, kd = 0.0, historianParameters = HistorianParameters)

...

# PID execution (call it as fast as you can)
command = pid(processValue = feedback, setpoint = targetValue)

...

# PID Historian
import matplotlib.pyplot as plt

plt.plot(pid.historian["TIME"], pid.historian["SETPOINT"], label="Setpoint")

plt.plot(pid.historian["TIME"], pid.historian["PROCESS_VALUE"], label="Process value")

plt.legend()
plt.show()

In the example above, the PID historian records setpoint, processValue and time. Time is the elapsed time from the start. After that a graphic is draw with matplotlib.

Historian parameters list

  • P : proportionnal part
  • I : integral part
  • D : derivative part
  • ERROR : PID error
  • SETPOINT : PID setpoint
  • PROCESS_VALUE : PID process value
  • OUTPUT : PID output

Integral limitation

The integral part of the PID can be limit to avoid overshoot of the output when the error is too high (When the setpoint variation is too high, or when the system have trouble to reach setpoint).

from PID_Py.PID import PID

# Initialization
pid = PID(kp = 0.0, ki = 0.0, kd = 0.0, integralLimit = 20.0)

...

# PID execution (call it as fast as you can)
command = pid(processValue = feedback, setpoint = targetValue)

In the example above, the integral part of the PID is clamped between -20 and 20.

Manual mode

The PID can be switch in manual mode, this allow to operate output directly through manualValue.

from PID_Py.PID import PID

# Initialization
pid = PID(kp = 0.0, ki = 0.0, kd = 0.0)

...

# Manual mode
pid.manualMode = True
pid.manualValue = 12.7

...

# PID execution (call it as fast as you can)
command = pid(processValue = feedback, setpoint = targetValue)

In the example above, command will be always equal to 12.7. The PID calculation is no longer executed. The proportionnal, integral and derivative parts still at the same value to avoid bump when switching back to automatic.

To avoid bump when switching in manual there is bumplessSwitching attribute. This attributes keep manualValue equal to output.

If you disable this function you will have bump when you switch in manual mode with manualValue different of output. If this case you can destabilise (:heavy_exclamation_mark:) your system. Be careful

Threaded PID

With the threaded PID you don't have to call pid(processValue, setpoint). It's call as fast as possible or with a constant cycle time. When you want to stop the PID use quit attribute to finish the current execution and exit.

from PID_Py.PID import ThreadedPID

# Initialization
pid = ThreadedPID(kp = 0.0, ki = 0.0, kd = 0.0, cycleTime = 0.01)
pid.start()

...

# PID inputs
pid.setpoint = targetValue
pid.processValue = feedback

# PID output
command = pid.output

...

# Stop PID
pid.quit = True
pid.join()

In the example above the threaded PID is created with 10ms (0.01s) of cyclic time. It means that the calculation is executed each 10ms.

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

PID_Py-0.2.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

PID_Py-0.2.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file PID_Py-0.2.1.tar.gz.

File metadata

  • Download URL: PID_Py-0.2.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for PID_Py-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c16c332818fa2222ee4357b4297a04376bf4359844c4e56d92ea8c4d10f519dd
MD5 75a17885eb9e2a638d79f0d0d7f6a9f7
BLAKE2b-256 85cc99b6be70005aed1b49dbc7e053f811d0c239c80bc3e1a82fa232f4ee112e

See more details on using hashes here.

File details

Details for the file PID_Py-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: PID_Py-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for PID_Py-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14011f7421ececf1b6b6fe0c5413fe6ec13448237e566f0d7b4d28920b9a85f0
MD5 8c50a12e1b42bbb44fe18fe9561114f0
BLAKE2b-256 b928f5a6f5389598b8dd19e95dbbba4cb32da6b1a1eacec83d31e7472708fbc4

See more details on using hashes here.

Supported by

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