advanced_pid/lib.rs
1//! An advanced PID control library implemented in Rust.
2//!
3//! This library provides an interface for PID controllers and several implementations.
4//! It supports various types of PID controls and allows for customization of PID gains and limits.
5//! It is designed to be `no_std` compatible, but can also be used with `std`.
6//!
7//! ## Features
8//! - Standard (Positional) PID Control ([`pid`] module)
9//! - Velocity form PID Control ([`vel_pid`] module)
10//! - PI-D Control where the Derivative action is based on the Process Variable (PV) ([`pi_d`] module)
11//! - I-PD Control where both Proportional and Derivative actions are based on the Process Variable (PV) ([`i_pd`] module)
12//! - Customizable PID gains and limits ([`config`] module)
13//!
14//! ## Usage
15//! To use, implement the [`PidController`] trait for your controller.
16//! The trait provides a `new` method for creating a new controller, an `update` method for updating the controller,
17//! and a `reset_config` method for resetting the controller's configuration.
18//!
19//! ## Installation
20//! To install, run the following Cargo command in your project directory:
21//! ```bash
22//! cargo add advanced-pid
23//! ```
24//!
25//! Or add the following to your Cargo.toml:
26//! ```toml
27//! [dependencies]
28//! advanced-pid = "0.2.3"
29//! ```
30//!
31//! ## No-std support
32//! This library is designed to be `no_std` compatible.
33//! To use this library in a `no_std` environment, disable the default features in your `Cargo.toml`:
34//! ```toml
35//! [dependencies]
36//! advanced-pid = { version = "0.2.3", default-features = false }
37//! ```
38//!
39//! ## Floating point precision
40//! This library allows switching between `f32` and `f64` floating point types through feature flags.
41//! By default, `f32` precision is used.
42//! To use `f64` precision, enable the `f64` feature in your `Cargo.toml`:
43//! ```toml
44//! [dependencies]
45//! advanced-pid = { version = "0.2.3", features = ["f64"] }
46//! ```
47//!
48//! ## Examples
49//! ```
50//! use advanced_pid::{prelude::*, Pid, PidGain};
51//!
52//! let target = 1.0;
53//! let actual = 0.0;
54//! let dt = 1.0;
55//!
56//! let gain = PidGain {
57//! kp: 1.0,
58//! ki: 0.3,
59//! kd: 0.1,
60//! };
61//! let mut pid = Pid::new(gain.into());
62//! println!("{}", pid.update(target, actual, dt));
63//! ```
64//!
65//! For more examples, see the [examples](https://github.com/teruyamato0731/advanced-pid-rs/tree/main/examples).
66
67#![no_std]
68#[cfg(feature = "std")]
69#[allow(unused_imports)]
70#[macro_use]
71extern crate std;
72
73#[cfg(not(feature = "f64"))]
74type FloatType = f32;
75#[cfg(feature = "f64")]
76type FloatType = f64;
77
78pub mod prelude;
79
80pub mod config;
81pub mod i_pd;
82pub mod pi_d;
83pub mod pid;
84pub mod vel_pid;
85
86/// Type alias for [PID gains](config::Gain).
87pub type PidGain = config::Gain;
88/// Type alias for [PID configuration](config::Config).
89pub type PidConfig = config::Config;
90
91pub use crate::i_pd::Ipd;
92pub use crate::pi_d::PiD;
93pub use crate::pid::Pid;
94pub use crate::vel_pid::VelPid;
95
96/// `PidController` is a trait that provides a standard interface for PID controllers.
97///
98/// It provides methods for creating a new controller [`Self::new()`], updating the controller [`Self::update()`], and resetting the controller's configuration [`Self::reset_config()`].
99pub trait PidController {
100 /// Creates a new controller with the specified configuration.
101 /// ```
102 /// use advanced_pid::{prelude::*, Pid, PidConfig};
103 ///
104 /// let config = PidConfig::new(1.0, 0.3, 0.1);
105 /// let controller = Pid::new(config);
106 /// ```
107 fn new(config: PidConfig) -> Self;
108
109 /// Updates the controller with the specified set point, actual value, and time delta.
110 /// Returns the controller output.
111 /// ```
112 /// use advanced_pid::{prelude::*, Pid};
113 ///
114 /// let mut controller = Pid::default();
115 /// let output = controller.update(1.0, 0.0, 0.1);
116 /// ```
117 fn update(&mut self, set_point: FloatType, actual: FloatType, dt: FloatType) -> FloatType;
118
119 /// Resets the controller's configuration to the specified configuration.
120 /// ```
121 /// use advanced_pid::{prelude::*, Pid, PidConfig};
122 ///
123 /// let mut controller = Pid::default();
124 /// let config = PidConfig::new(1.0, 0.3, 0.1);
125 /// controller.reset_config(config);
126 /// ```
127 fn reset_config(&mut self, config: PidConfig)
128 where
129 Self: core::marker::Sized,
130 {
131 *self = Self::new(config);
132 }
133}