Skip to main content

A small deterministic PID controller library for Python.

Project description

pySimplePID

A small deterministic PID controller library for Python.

Usage

from pySimplePID import PID

pid = PID(kp=2.0, ki=0.1, target=22.0)

thermal_output = pid.update(current_value=20.5)
pid.set_target(24.0)

kp is required. ki and kd default to 0.0; any component with a gain of zero is disabled. Output is capped between -100 and 100 by default, where positive values heat and negative values cool.

Elapsed time is measured internally between calls to update(). Tests and simulations can inject a custom clock with time_function.

PID Output Limits for Heating and Cooling

The PID output sign is conventional:

positive output -> heat
negative output -> cool

Use output_min and output_max to match the actuators available in your system.

System type output_min output_max Meaning
Heating only 0.0 100.0 Output can only add heat
Cooling only -100.0 0.0 Output can only remove heat
Heating and cooling -100.0 100.0 Positive heats, negative cools

Heating Only

Use this when the system can add heat but cannot actively cool:

pid = PID(
    kp=2.0,
    ki=0.1,
    target=22.0,
    output_min=0.0,
    output_max=100.0,
)

Output range:

0% ... 100%

Cooling Only

Use this when the system can remove heat but cannot actively heat, such as a Peltier used only for cooling:

pid = PID(
    kp=2.0,
    ki=0.1,
    target=5.0,
    output_min=-100.0,
    output_max=0.0,
)

Output range:

-100% ... 0%

For a cooling actuator that expects a positive drive value, invert the sign:

cooling_drive = max(0.0, -pid.update(current_temperature))

Heating and Cooling

Use this when the system has both actuators, or one bidirectional actuator:

pid = PID(
    kp=2.0,
    ki=0.1,
    target=22.0,
    output_min=-100.0,
    output_max=100.0,
)

Output range:

-100% ... 100%

Then split the output by sign:

output = pid.update(current_temperature)
heating_drive = max(0.0, output)
cooling_drive = max(0.0, -output)

Temperature Simulation

The examples include a minimal first-order radiator model in examples/models.py. It is intentionally not part of the library API:

from models import TemperatureRadiator
from pySimplePID import PID

pid = PID(kp=30.0, ki=0.2, target=22.0)
radiator = TemperatureRadiator(
    initial_temperature=16.0,
    ambient_temperature=12.0,
    heat_capacity=120.0,
    heat_loss_coefficient=2.0,
)

thermal_output = pid.update(radiator.current_temperature)
temperature = radiator.step(thermal_output, dt=1.0)

The model uses:

temperature += (thermal_power - heat_loss_coefficient * (temperature - ambient))
               / heat_capacity
               * dt

Examples

PYTHONPATH=src python3 examples/basic_pid.py
PYTHONPATH=src python3 examples/temperature_radiator.py

The temperature example prints CSV-style data:

time,temperature,target,thermal_output

There is also an optional Qt example with live charts and editable PID parameters. Its PID interval control changes how often the controller is sampled while the radiator model continues to run on a fixed fast timestep. Heating and cooling power fields convert PID output percentage into watts: for example, 10 % output with 5 W heating power applies 0.5 W.

python3 -m pip install '.[gui]'
PYTHONPATH=src python3 examples/temperature_radiator_qt.py

The SiPM/Peltier Qt example uses a thin aluminium thermal mass, a small SiPM heat load, ambient exchange, and Peltier cooling:

PYTHONPATH=src python3 examples/sipm_qt.py

Tests

PYTHONPATH=src python3 -m unittest discover -s tests

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

pysimplepid-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

pysimplepid-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file pysimplepid-0.1.0.tar.gz.

File metadata

  • Download URL: pysimplepid-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for pysimplepid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a939de91c1b0ccede823357c5f66f0949f4a3941d82ecaa57a599845e8a9e42e
MD5 c8b2b28fa7a276987fb927e659512cef
BLAKE2b-256 670eda0b1b4b25745ff9976bc0ab313945e564d5f2182a49c513f0726aae6637

See more details on using hashes here.

File details

Details for the file pysimplepid-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pysimplepid-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for pysimplepid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25d1034c3f1488e82c8e6c1b0b148c056702bfb6e0a8030a69a7b967e83fc087
MD5 e97a50839d45acfb77594dff6c3975b3
BLAKE2b-256 7dbd2154b6f81015559e00e2852804aaddce6d01fcb6cd5380070e8b7941a595

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