libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
pid.hpp
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5
6#include "cycle_value.hpp"
7#include "libxr_def.hpp"
8
9namespace LibXR
10{
11using DefaultScalar = LIBXR_DEFAULT_SCALAR;
12
13constexpr DefaultScalar PID_SIGMA = 1e-6f;
14
25template <typename Scalar = DefaultScalar>
26class PID
27{
28 public:
33 struct Param
34 {
35 Scalar k = 1.0;
36 Scalar p = 0.0;
37 Scalar i = 0.0;
38 Scalar d = 0.0;
41 bool cycle = false;
42 };
43
51 PID(const Param PARAM) : param_(std::move(PARAM)) { Reset(); }
52
64 {
65 if (!std::isfinite(sp) || !std::isfinite(fb) || !std::isfinite(dt))
66 {
67 return last_out_;
68 }
69
70 // Compute error
73
74 // Derivative from feedback change
75 fb *= param_.k;
76 Scalar d = (fb - last_fb_) / dt;
77 if (!std::isfinite(d))
78 {
79 d = 0;
80 }
81
82 // Compute PD
83 Scalar output = (k_err * param_.p) - (d * param_.d);
84
85 // Integrate if within limits
86 Scalar i_term = i_ + k_err * dt;
88
89 if (param_.i > PID_SIGMA && std::isfinite(i_term))
90 {
91 if (std::abs(output + i_out) <= param_.out_limit &&
92 std::abs(i_term) <= param_.i_limit)
93 {
94 i_ = i_term;
95 }
96 }
97
98 // Apply output limits
99 output += i_out;
101 if (std::isfinite(output) && param_.out_limit > PID_SIGMA)
102 {
103 output = std::clamp(output, -param_.out_limit, param_.out_limit);
104 }
105
106 // Store states
107 last_err_ = err;
108 last_fb_ = fb;
110 last_der_ = d;
111 return output;
112 }
113
126 {
127 if (!std::isfinite(sp) || !std::isfinite(fb) || !std::isfinite(fb_dot) ||
128 !std::isfinite(dt))
129 {
130 return last_out_;
131 }
132
133 // Compute error
135 Scalar k_err = err * param_.k;
136
137 // Use externally provided derivative
138 Scalar d = fb_dot;
139 if (!std::isfinite(d))
140 {
141 d = 0;
142 }
143
144 // Compute PD
145 Scalar output = (k_err * param_.p) - (d * param_.d);
146
147 // Integrate if within limits
148 Scalar i_term = i_ + k_err * dt;
150
151 if (param_.i > PID_SIGMA && std::isfinite(i_term))
152 {
153 if (std::abs(output + i_out) <= param_.out_limit &&
154 std::abs(i_term) <= param_.i_limit)
155 {
156 i_ = i_term;
157 }
158 }
159
160 // Apply output limits
161 output += i_out;
163 if (std::isfinite(output) && param_.out_limit > PID_SIGMA)
164 {
165 output = std::clamp(output, -param_.out_limit, param_.out_limit);
166 }
167
168 // Store states
169 last_err_ = err;
170 last_fb_ = fb;
172 last_der_ = d;
173 return output;
174 }
175
177 void SetK(Scalar k) { param_.k = k; }
179 void SetP(Scalar p) { param_.p = p; }
181 void SetI(Scalar i) { param_.i = i; }
183 void SetD(Scalar d) { param_.d = d; }
188
190 Scalar K() const { return param_.k; }
192 Scalar P() const { return param_.p; }
194 Scalar I() const { return param_.i; }
196 Scalar D() const { return param_.d; }
198 Scalar ILimit() const { return param_.i_limit; }
200 Scalar OutLimit() const { return param_.out_limit; }
202 Scalar LastError() const { return last_err_; }
204 Scalar LastFeedback() const { return last_fb_; }
206 Scalar LastOutput() const { return last_out_; }
208 Scalar LastDerivative() const { return last_der_; }
209
214 void Reset()
215 {
216 i_ = 0;
217 last_err_ = 0;
218 last_fb_ = 0;
219 last_out_ = 0;
220 }
221
228
234 Scalar GetIntegralError() const { return i_; }
235
242
250
251 private:
253 Scalar i_ = 0;
259};
260
261} // namespace LibXR
通用 PID 控制器类。 Generic PID controller.
Definition pid.hpp:27
Scalar D() const
获取 D 项系数 Get derivative gain
Definition pid.hpp:196
void SetOutLimit(Scalar limit)
设置输出限幅 Set output limit
Definition pid.hpp:187
Scalar ILimit() const
获取积分限幅 Get integral limit
Definition pid.hpp:198
Scalar LastFeedback() const
获取上一次反馈值 Get last feedback
Definition pid.hpp:204
Scalar last_err_
上次误差 Last error
Definition pid.hpp:254
PID(const Param PARAM)
构造 PID 控制器。 Construct a PID controller.
Definition pid.hpp:51
Scalar GetFeedForward() const
获取前馈项 Get feedforward
Definition pid.hpp:249
Scalar P() const
获取 P 项系数 Get proportional gain
Definition pid.hpp:192
Scalar LastDerivative() const
获取上一次导数 Get last derivative
Definition pid.hpp:208
Scalar LastOutput() const
获取上一次输出 Get last output
Definition pid.hpp:206
void SetD(Scalar d)
设置 D 项系数 Set derivative gain
Definition pid.hpp:183
void Reset()
重置控制器状态。 Reset all internal states.
Definition pid.hpp:214
Scalar last_fb_
上次反馈 Last feedback
Definition pid.hpp:255
void SetP(Scalar p)
设置 P 项系数 Set proportional gain
Definition pid.hpp:179
Scalar last_der_
上次导数 Last derivative
Definition pid.hpp:256
Scalar Calculate(Scalar sp, Scalar fb, Scalar dt)
使用反馈值计算 PID 输出。 Compute output from feedback only. out = k(p*err + i*∫err*dt - d*(fb-last_fb)/dt)
Definition pid.hpp:63
void SetILimit(Scalar limit)
设置积分限幅 Set integral limit
Definition pid.hpp:185
Scalar I() const
获取 I 项系数 Get integral gain
Definition pid.hpp:194
Scalar OutLimit() const
获取输出限幅 Get output limit
Definition pid.hpp:200
Scalar Calculate(Scalar sp, Scalar fb, Scalar fb_dot, Scalar dt)
使用外部导数计算 PID 输出。 Compute output using external feedback derivative. out = k(p*err + i*∫err*dt - d*fb_...
Definition pid.hpp:125
Scalar last_out_
上次输出 Last output
Definition pid.hpp:257
void SetIntegralError(Scalar err)
设置累计误差 Set integral error
Definition pid.hpp:227
Scalar GetIntegralError() const
获取累计误差 Get integral error
Definition pid.hpp:234
void SetI(Scalar i)
设置 I 项系数 Set integral gain
Definition pid.hpp:181
void SetK(Scalar k)
设置全局比例系数 Set global proportional gain
Definition pid.hpp:177
Scalar i_
积分状态 Integral state
Definition pid.hpp:253
Param param_
PID 参数 PID parameter set.
Definition pid.hpp:252
Scalar feed_forward_
前馈项 Feedforward term
Definition pid.hpp:258
Scalar LastError() const
获取上一次误差 Get last error
Definition pid.hpp:202
Scalar K() const
获取全局比例系数 Get global proportional gain
Definition pid.hpp:190
void SetFeedForward(Scalar feed_forward)
设置前馈项 Set feedforward
Definition pid.hpp:241
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
PID 参数结构体。 Structure holding PID parameters.
Definition pid.hpp:34
Scalar i_limit
积分限幅 Integral limit
Definition pid.hpp:39
Scalar out_limit
输出限幅 Output limit
Definition pid.hpp:40
Scalar p
比例项 Proportional gain
Definition pid.hpp:36
bool cycle
是否处理周期误差 Whether input is cyclic
Definition pid.hpp:41
Scalar k
全局比例因子 Global gain
Definition pid.hpp:35
Scalar i
积分项 Integral gain
Definition pid.hpp:37
Scalar d
微分项 Derivative gain
Definition pid.hpp:38