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.0.0.tar.gz (6.0 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.0.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

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

Hashes for servopilot-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f00be06958268bc5dac912ff02548e06cd4d5ac5c845014e737a2e4708a0aed3
MD5 42b2596380792e89cd77436c314002a7
BLAKE2b-256 0c4a2b78f28c225e0f242067247db3e5e3245b62962a01899b51ebcfd9086a75

See more details on using hashes here.

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

Hashes for servopilot-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a39489ce0e66f52167f29850bc8bec04070e06dba97fe94d2a981f5004a2236a
MD5 f5c0e383a7b69fdcfb0feac3cb7d3349
BLAKE2b-256 14a4b4a88080c574d8fb3772bd9c54296a3a79923b6f10a30832d5dc71ddbae9

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