Skip to main content

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.

Python Dependencies License Size


โœจ 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.


Built with โค๏ธ by ByIbos โ€” extracted from the Auto-ReID Drone Tracker project.

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

servopilot-1.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

servopilot-1.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file servopilot-1.1.0.tar.gz.

File metadata

  • Download URL: servopilot-1.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for servopilot-1.1.0.tar.gz
Algorithm Hash digest
SHA256 768b6ac2ec6e8fc91a542c800e25cf32b6a9e6e5c4bce5deb2c0af101be95577
MD5 18433ac5406112b82ab0cd991c6dc53c
BLAKE2b-256 f544625150b37c1801b1f976bceb961053a8769deb59350a2de897271cdd0b03

See more details on using hashes here.

File details

Details for the file servopilot-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: servopilot-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for servopilot-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 746c06f241611d7bf1fe921e506ce6b33edb220ef4dc1ad54c56228d61eb7c77
MD5 c0be7c5c8e6456accffbd92fac3583c4
BLAKE2b-256 447848de65c1c49704b4f980ae1d8aa1b565ae03a224c06d5c341343bbf5f642

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