Skip to main content

An advanced PID controller

Project description

moat-lib-pid

An advanced PID controller in Python. The derivative term can also be used in practice thanks to built-in first-order filter. Detailed information can be found here.

Usage is very simple:

from moat.lib.pid import PID

# Create PID controller 
pid = PID(Kp=2.0, Ki=0.1, Kd=1.0, Tf=0.05)

# Control loop
while True:
    # Get current measurement from system
    timestamp, measurement = system.get_measurement()
    
    # Calculate control signal by using PID controller
    reference = 1.0
    control = pid(timestamp, reference - measurement)
    
    # Feed control signal to system
    system.set_input(control)

This module was vendorized from the advanced-pid module by Erkan Adali erkanadali91@gmail.com.

Complete API documentation can be found here.

Usage

Biggest advantage of advanced-pid, the derivative term has a built-in first-order filter.
advanced-pid package includes a toy mass-spring-damper system model for testing:

from moat.lib.pid import PID
from examples.mass_spring_damper import MassSpringDamper
from matplotlib import pyplot as plt
from numpy import diff

# Create a mass-spring-damper system model
system = MassSpringDamper(mass=1.0, spring_const=1.0, damping_const=0.2)
system.set_initial_value(initial_position=1.0, initial_velocity=0.0)

# Create PID controller 
pid = PID(Kp=1.0, Ki=0.0, Kd=2.0, Tf=0.5)

# Control loop
time, meas, cont = [], [], []
for i in range(800):
    # Get current measurement from system
    timestamp, measurement = system.get_measurement()
    
    # Calculate control signal by using PID controller
    control = pid(timestamp, -measurement)
    
    # Feed control signal to system
    system.set_input(control)
    
    # Record for plotting
    time.append(timestamp)
    meas.append(measurement)
    cont.append(control)

# Plot result
fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
fig.suptitle('Mass-Spring-Damper system')
ax1.set_ylabel('Measured Position [m]')
ax1.plot(time, meas, 'b')
ax1.grid()
ax2.set_ylabel('Force [N]')
ax2.plot(time, cont, 'g')
ax2.grid()
ax3.set_xlabel('Time [s]')
ax3.set_ylabel('Derivative Term')
ax3.plot(time[1:], diff(meas)/diff(time), 'r')
ax3.grid()
plt.show()

As It can be seen in the figure, derivative term cannot be use without a filter:
alt text

Installation

To install, run:

pip3 install moat-lib-pid

Tests

To run tests, run:

pytest tests

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

moat_lib_pid-0.6.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

moat_lib_pid-0.6.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file moat_lib_pid-0.6.2.tar.gz.

File metadata

  • Download URL: moat_lib_pid-0.6.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for moat_lib_pid-0.6.2.tar.gz
Algorithm Hash digest
SHA256 afd4119cfbae15d865aefdbbd3857135f1f45a41ed20605a3b314b29d5203f34
MD5 d0decfc821295dcc56289a335ecdc3f5
BLAKE2b-256 8e602607d571a2531a48862b795e7e0e81b03c07e786eb0a8625975f77acac88

See more details on using hashes here.

File details

Details for the file moat_lib_pid-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: moat_lib_pid-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for moat_lib_pid-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0c6ee771a1e889e830b468ac9315b1d8dd28f2b9b1993e7ca56ccc3a7d0e5b7e
MD5 b24eba6b0941772f86b3676a5ceaa496
BLAKE2b-256 5af2b90f2710af038700403dffd9ed18d142897105fd5cf04269a5a7fa49b3c9

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