discrete_pid

Crates.iodiscrete_pid
lib.rsdiscrete_pid
version0.1.0
created_at2025-05-13 12:15:01.292292+00
updated_at2025-05-13 12:15:01.292292+00
descriptionA PID controller for robotics and discrete control systems
homepage
repositoryhttps://github.com/Hs293Go/discrete_pid
max_upload_size
id1671816
size365,877
H S Helson Go (Hs293Go)

documentation

https://docs.rs/discrete_pid

README

Discrete PID Controller in Rust

Rust license CI codecov crates.io docs.rs

A PID controller for robotics and discrete control systems in rust.

Step Response Quadrotor Rate Control
Step Response Quadrotor Control

Why This PID?

  • Principled and Reliable

    • Inspired by Brett Beauregard's battle-tested and well documented1 PID for Arduino
    • Verified in an extensive test suite, including numerical verification against Simulink’s Discrete PID Controller --- this controller has the same behavior you remember from your first controls course
    • Supports functionally pure (stateless) PID computation, enabling perfectly reproducible control outputs and easy thread-safety
  • Real-World Ready

    • Explicitly designed for discrete-time control --- supports variable sample time and tunable LPF on derivative.
    • Supports fine-grained control over PID activity: online (de)activation, (re)initialization, and pausing/resuming integration
    • As an alternative to the functional PID controller, the stateful PID controller is competitive with the naive PID law in terms of speed
  • Lightweight and Dependency-Free

    • Usage in #![no_std] mode only requires core float traits from num_traits

Application Example

One of the most notable achievements of PID controllers in recent years has been the stabilization and control of quadrotors. We demonstrate just that using our PID controller in an example.

Quadrotor 3D trajectory

Run the example:

cargo run --example quadrotor_control --features simulation

Visualize the results:

python3 examples/plot_quadrotor_trajectory.py         # Generates static plot
python3 examples/plot_quadrotor_trajectory.py --animate show  # Live 3D animation
python3 examples/plot_quadrotor_trajectory.py --animate save  # Save to GIF

In this example, we used our PID controller to track quadrotor body rates computed by an upstream geometric tracking controller. The README for the examples contains more details about the cascaded controller and the quadrotor simulation.

Quick Start

use core::time::Duration;
use discrete_pid::{pid, time};

let loop_time = Duration::from_micros(125);
let cfg = pid::PidConfigBuilder::default()
    .kp(2.0)
    .ki(1.5)
    .sample_time(loop_time)
    .filter_tc(0.1)
    .build()
    .expect("Failed to build a PID configuration");

let controller = pid::FuncPidController::new(cfg);
let mut ctx = pid::PidContext::new_uninit();

let pos_meas = 0.5;
let pos_setpoint = 1.0;
let timestamp = time::Micros(125);
let vel_setpoint = 2.0;

let (output, new_ctx) =
    controller.compute(ctx, pos_meas, pos_setpoint, timestamp, vel_setpoint.into());

Shoutouts

  • pid-rs: well-known and effective; You may consider using their crate instead if you:

    1. Are working with simple, slow-acting processes that don't benefit from sample time handling and D-term filtering

    2. Need to use integer process values/setpoints

    3. Need to apply clamping on P, I, and D terms separately

[!WARNING]

We doubt the usefulness of per-term clamping since it breaks linear superposition of the terms, generating subtle nonlinearities and potentially disrupting tuning techniques

  • pidgeon: Strong support for synchronization and built-in visualization; You may consider using their crate instead if you:

    1. Need support for concurrent PID control and don't mind depending on std through std::sync::Mutex

Documentation and Links

📦 View on crates.io
📚 API Docs on docs.rs

License

This project is licensed under the MIT License © 2025 Hs293Go

Footnotes

  1. Beauregard, B. Improving the Beginner's PID. http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

Commit count: 31

cargo fmt