Anti-windup PID controller with dual-axis gimbal/servo support and debug introspection.
Project description
๐ฎ servopilot
Anti-Windup PID Controller with Dual-Axis Support
Zero-dependency PID for gimbal control, servo motors, drones, and robotics.
โจ Why servopilot?
| Feature | servopilot | simple-pid | pid-py |
|---|---|---|---|
| Anti-windup (integral clamping) | โ | โ | โ |
| DualAxisPID (Yaw + Pitch) | โ | โ | โ |
| Debug introspection (P/I/D terms) | โ | โ | โ |
| Zero dependencies | โ | โ | โ |
| Configurable integral limit | โ | Partial | โ |
| Output clamping | โ | โ | โ |
servopilot was built for real-time drone gimbal tracking โ where you need two synchronized PID loops (yaw + pitch) running at 30+ FPS with zero overhead. No NumPy, no SciPy, no anything. Just pure Python math.
๐ฆ Installation
pip install servopilot
Or install from source:
git clone https://github.com/ByIbos/servopilot.git
cd servopilot
pip install -e .
๐ Quick Start
Single-Axis PID
from servopilot import PIDController
pid = PIDController(kp=0.5, ki=0.01, kd=0.3)
# In your control loop:
error = target_position - current_position
signal = pid.update(error, dt=1/30)
# Send `signal` to your motor/servo
motor.set_speed(signal)
Dual-Axis Gimbal Control
from servopilot import DualAxisPID
pid = DualAxisPID(kp=0.4, ki=0.008, kd=0.25)
# Every frame:
error_x = target_x - screen_center_x # Horizontal offset
error_y = target_y - screen_center_y # Vertical offset
yaw_signal, pitch_signal = pid.update(error_x, error_y, dt=1/30)
# Send to flight controller / gimbal
gimbal.set_yaw(yaw_signal) # [-1.0, +1.0]
gimbal.set_pitch(pitch_signal) # [-1.0, +1.0]
Debug Introspection
pid = PIDController(kp=0.5, ki=0.01, kd=0.3)
signal = pid.update(error=100, dt=0.033)
# Inspect each PID component:
print(f"P: {pid.p_term:.3f}") # Proportional contribution
print(f"I: {pid.i_term:.3f}") # Integral contribution
print(f"D: {pid.d_term:.3f}") # Derivative contribution
print(f"Output: {pid.output:.3f}")
# Perfect for real-time debug panels and telemetry
๐ง API Reference
PIDController(kp, ki, kd, output_limit, integral_limit)
| Parameter | Type | Default | Description |
|---|---|---|---|
kp |
float | 0.5 | Proportional gain |
ki |
float | 0.01 | Integral gain |
kd |
float | 0.3 | Derivative gain |
output_limit |
float | 1.0 | Max absolute output [-limit, +limit] |
integral_limit |
float | 100.0 | Anti-windup clamp for integral |
Methods
| Method | Returns | Description |
|---|---|---|
update(error, dt) |
float | Compute control signal |
reset() |
None | Zero all internal state |
Debug Properties
| Property | Description |
|---|---|
p_term |
Current proportional term value |
i_term |
Current integral term value |
d_term |
Current derivative term value |
output |
Last computed output |
integral |
Current integral accumulator |
DualAxisPID(kp, ki, kd)
| Method | Returns | Description |
|---|---|---|
update(error_x, error_y, dt) |
(float, float) | Control signals for both axes |
reset() |
None | Reset both axes |
pid_x |
PIDController | Access X-axis controller directly |
pid_y |
PIDController | Access Y-axis controller directly |
๐ How PID Works
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PID Controller โ
โ โ
Error โโโโโโโโโโโ โโโโ [Kp ร error] = P term โ
(target - actual) โ โ
โโโโ [Ki ร โซerrorยทdt] = I term โโโโ Output
โ (clamped ยฑ100) (anti-windup) โ (clamped ยฑ1.0)
โ โ
โโโโ [Kd ร d(error)/dt] = D term โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โข P = Reacts NOW (proportional to current error)
โข I = Remembers PAST (eliminates steady-state drift)
โข D = Predicts FUTURE (dampens oscillation)
๐ฏ Tuning Guide
| Symptom | Fix |
|---|---|
| Slow response | Increase kp |
| Overshooting / oscillating | Decrease kp, increase kd |
| Steady-state error (offset) | Increase ki |
| Integral windup (runaway) | Decrease integral_limit |
| Output too aggressive | Decrease output_limit |
Quick recipe for drone gimbals: Start with kp=0.4, ki=0.008, kd=0.25 and adjust from there.
๐ฏ Use Cases
- ๐ Drone Gimbal โ Keep camera centered on target
- ๐ค Line-Following Robot โ Steer based on sensor error
- ๐ญ Industrial Control โ Temperature, pressure, flow regulation
- ๐ฎ Game AI โ Smooth NPC aiming / steering
- ๐ญ Telescope Mount โ Star tracking with pan-tilt servos
๐ License
MIT License โ use it anywhere, commercially or personally.
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 servopilot-1.0.0.tar.gz.
File metadata
- Download URL: servopilot-1.0.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f00be06958268bc5dac912ff02548e06cd4d5ac5c845014e737a2e4708a0aed3
|
|
| MD5 |
42b2596380792e89cd77436c314002a7
|
|
| BLAKE2b-256 |
0c4a2b78f28c225e0f242067247db3e5e3245b62962a01899b51ebcfd9086a75
|
File details
Details for the file servopilot-1.0.0-py3-none-any.whl.
File metadata
- Download URL: servopilot-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a39489ce0e66f52167f29850bc8bec04070e06dba97fe94d2a981f5004a2236a
|
|
| MD5 |
f5c0e383a7b69fdcfb0feac3cb7d3349
|
|
| BLAKE2b-256 |
14a4b4a88080c574d8fb3772bd9c54296a3a79923b6f10a30832d5dc71ddbae9
|