libstp
Robotics library for Botball
Loading...
Searching...
No Matches
pid.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <limits>
4
6
7namespace libstp::foundation
8{
35 struct PidConfig
36 {
37 double kp{1.0};
38 double ki{0.0};
39 double kd{0.0};
40 double integral_max{10.0};
45 double integral_min{std::numeric_limits<double>::quiet_NaN()};
46 double integral_deadband{0.01};
48 double output_min{-10.0};
49 double output_max{10.0};
50
51 PidConfig() = default;
52
53 PidConfig(double kp, double ki, double kd,
54 double integral_max = 10.0, double integral_deadband = 0.01,
55 double derivative_lpf_alpha = 0.1,
56 double output_min = -10.0, double output_max = 10.0)
57 : kp(kp), ki(ki), kd(kd)
61 {}
62
63 // Convenience: construct from simple PidGains (advanced params get defaults).
64 PidConfig(const PidGains& g) : kp(g.kp), ki(g.ki), kd(g.kd) {}
65 };
66
69 {
70 public:
71 explicit PidController(PidConfig config = {});
72
74 double update(double error, double dt);
75
80 double update(double error, double dt, double deriv_signal);
81
83 void reset();
84
86 void setGains(double kp, double ki, double kd);
87
88 [[nodiscard]] double getIntegral() const { return integral_; }
89 [[nodiscard]] double getDerivative() const { return filtered_derivative_; }
90
91 private:
92 PidConfig cfg_;
93 double integral_{0.0};
94 double prev_error_{0.0};
95 double filtered_derivative_{0.0};
96 bool first_update_{true};
97 };
98}
double getDerivative() const
Definition pid.hpp:89
PidController(PidConfig config={})
double update(double error, double dt, double deriv_signal)
void reset()
Clear accumulated integral and derivative state.
double update(double error, double dt)
Update the controller using error for both the proportional and derivative signals.
void setGains(double kp, double ki, double kd)
Replace only the proportional, integral, and derivative gains.
PidConfig(double kp, double ki, double kd, double integral_max=10.0, double integral_deadband=0.01, double derivative_lpf_alpha=0.1, double output_min=-10.0, double output_max=10.0)
Definition pid.hpp:53
PidConfig(const PidGains &g)
Definition pid.hpp:64
double integral_deadband
Error magnitude below which the integrator ignores the sample.
Definition pid.hpp:46
double derivative_lpf_alpha
IIR weight on new derivative samples; clamped to [0, 1] at runtime.
Definition pid.hpp:47
Basic PID gains without integrator and output shaping parameters.
Definition motor.hpp:18