A simple PID controller in Python
Project description
PID Controller: How to use
Step 1: Importing
from simplepidcontroller import PID
You have now imported the PID Controller
Step 2: Make a system
We will use a first order process system to simulate a real life application
class Process:
def __init__(self, k=1, tau=1):
self.k = k
self.tau = tau
self.y = 0
def update(self, u, dt):
self.y += (self.k * (u - self.y) / self.tau) * dt
return self.y
process = Process()
It is not the most complicated but it will work
Step 3: Defining constants
Start by making the pid controller. First, define the set point and plug it in to a new PID object
set_point = 100
pid = PID(set_point)
This package supports two types of the PID controller equation.
The parallel form,
$$u(t) = K_p e(t) + K_i \int e(t) dt + K_d \frac{de}{dt}$$
where...
$K_p$, $K_i$, and $K_d$ are all constants,
and $K_pe(t)$ is the proportional action,
$K_i \int e(t) dt$ is the integral action,
and $K_d \frac{de}{dt}$ is the derivative action
and the ideal form,
$$u(t) = K_p \bigg(e(t) + \frac{1}{\tau_i} \int e(t) dt + \tau_d \frac{de(t)}{dt}\bigg)$$
where...
$K_p$ is the proportional gain,
$\tau_i$ and $\tau_d$ are integral and derivative time constants respectively
$K_pe(t)$ is the proportional action,
$K_i \int e(t) dt$ is the integral action,
and $K_d \frac{de}{dt}$ is the derivative action
$t$ is the time and $e(t)$ and $u(t)$ are the error and output at time $t$ respectively in both
You can input the parallel constants with
pid.parallel_constants = kp, ki, kd
and the ideal constants with
pid.ideal_constants = kp, ti, td
Step 4: Using the Controller
You can use a simple for loop to use the controller
import time
INTERVAL = 0.1
y = process.update(0, INTERVAL)
for _ in range(150): # Or the amount of times you want to use the controller
time.sleep(INTERVAL)
u = pid.compute(y, INTERVAL) # Compute the value
process.update(u, INTERVAL) # Use the value to update system
This will be sufficient to make sure the value reaches the set point
You can also turn on derivative filtering with the filter time constant and filter flag parameters
pid = PID(set_point, tf=filter_time_constant)
...
pid.compute(y, INTERVAL, filter_derivative=True)
or anti-windup with the minimum and maximum bounds for the output and the anti-windup flag parameters
pid = PID(set_point, u_min=minimum, u_max=maximum)
...
pid.compute(y, INTERVAL, anti_windup=True)
um thats it hope u liked
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simplepidcontroller-1.0.0.tar.gz.
File metadata
- Download URL: simplepidcontroller-1.0.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbfbc07ca50267ff865409a28227f894d204038cf7d6aceee67a818292799514
|
|
| MD5 |
31dd5d6482329cad8056ceeab6189ebf
|
|
| BLAKE2b-256 |
4908d42bbd571561cc08a6ff5d9ae56d4bc8a75cdf2e93bc583e37bd42b4b721
|
File details
Details for the file simplepidcontroller-1.0.0-py2.py3-none-any.whl.
File metadata
- Download URL: simplepidcontroller-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13d7c35dee4c688598be5c3a279484f00fa7cc0846fd5b56427837acbf5c801c
|
|
| MD5 |
b4abb02139bf19e19832726af4113716
|
|
| BLAKE2b-256 |
35504d1041352dd131c5b739d81170aae49470982a5014f6d0645f5257b464b4
|