libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::PID< Scalar > Class Template Reference

通用 PID 控制器类。 Generic PID controller. More...

#include <pid.hpp>

Collaboration diagram for LibXR::PID< Scalar >:
[legend]

Data Structures

struct  Param
 PID 参数结构体。 Structure holding PID parameters. More...
 

Public Member Functions

template<typename P >
 PID (P &&p)
 构造 PID 控制器。 Construct a PID controller.
 
Scalar Calculate (Scalar sp, Scalar fb, Scalar dt)
 使用反馈值计算 PID 输出。 Compute output from feedback only.
 
Scalar Calculate (Scalar sp, Scalar fb, Scalar fb_dot, Scalar dt)
 使用外部导数计算 PID 输出。 Compute output using external feedback derivative.
 
void SetK (Scalar k)
 设置全局比例系数 Set global proportional gain
 
void SetP (Scalar p)
 设置 P 项系数 Set proportional gain
 
void SetI (Scalar i)
 设置 I 项系数 Set integral gain
 
void SetD (Scalar d)
 设置 D 项系数 Set derivative gain
 
void SetILimit (Scalar limit)
 设置积分限幅 Set integral limit
 
void SetOutLimit (Scalar limit)
 设置输出限幅 Set output limit
 
Scalar K () const
 获取全局比例系数 Get global proportional gain
 
Scalar P () const
 获取 P 项系数 Get proportional gain
 
Scalar I () const
 获取 I 项系数 Get integral gain
 
Scalar D () const
 获取 D 项系数 Get derivative gain
 
Scalar ILimit () const
 获取积分限幅 Get integral limit
 
Scalar OutLimit () const
 获取输出限幅 Get output limit
 
Scalar LastError () const
 获取上一次误差 Get last error
 
Scalar LastFeedback () const
 获取上一次反馈值(未缩放)Get last feedback (raw)
 
Scalar LastOutput () const
 获取上一次输出 Get last output
 
Scalar LastDerivative () const
 获取上一次导数(k * d(fb)/dt 或 k * fb_dot)Get last derivative (scaled by k)
 
void Reset ()
 重置控制器状态。 Reset all internal states.
 
void SetIntegralError (Scalar err)
 设置累计误差 Set integral error
 
Scalar GetIntegralError () const
 获取累计误差 Get integral error
 
void SetFeedForward (Scalar feed_forward)
 设置前馈项 Set feedforward
 
Scalar GetFeedForward () const
 获取前馈项 Get feedforward
 

Private Attributes

Param param_
 PID 参数 PID parameter set.
 
Scalar i_ = 0
 积分状态 Integral state
 
Scalar last_err_ = 0
 上次误差 Last error
 
Scalar last_fb_ = 0
 上次反馈(未缩放)Last feedback (raw)
 
Scalar last_der_ = 0
 上次导数(k 缩放)Last derivative (scaled by k)
 
Scalar last_out_ = 0
 上次输出 Last output
 
Scalar feed_forward_ = 0
 前馈项 Feedforward term
 

Detailed Description

template<typename Scalar = DefaultScalar>
class LibXR::PID< Scalar >

通用 PID 控制器类。 Generic PID controller.

支持周期角度处理、积分限幅与输出限幅,适用于基础控制应用。 Supports cyclic inputs, integral/output saturation. No derivative filtering.

Template Parameters
Scalar控制器标量类型,默认为 DefaultScalar。 Scalar type for internal calculations.

Definition at line 26 of file pid.hpp.

Constructor & Destructor Documentation

◆ PID()

template<typename Scalar = DefaultScalar>
template<typename P >
LibXR::PID< Scalar >::PID ( P && p)
inline

构造 PID 控制器。 Construct a PID controller.

Template Parameters
ParamPID 参数结构体。 PID parameter struct.
Parameters
PARAM

Definition at line 53 of file pid.hpp.

53 : param_(std::forward<P>(p))
54 {
55 Reset();
56 }
void Reset()
重置控制器状态。 Reset all internal states.
Definition pid.hpp:314
Param param_
PID 参数 PID parameter set.
Definition pid.hpp:352

Member Function Documentation

◆ Calculate() [1/2]

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::Calculate ( Scalar sp,
Scalar fb,
Scalar dt )
inline

使用反馈值计算 PID 输出。 Compute output from feedback only.

Define: err = (cycle ? CycleValue(sp) - fb : sp - fb) e_k = k * err fb_d_k = k * (fb - last_fb) / dt

out = p * e_k + i * ∫(e_k)dt - d * fb_d_k + feed_forward

Parameters
sp期望值 Setpoint
fb反馈值 Feedback
dt控制周期(秒) Time step in seconds
Returns
控制器输出 Controller output

Definition at line 74 of file pid.hpp.

75 {
76 if (!std::isfinite(sp) || !std::isfinite(fb) || !std::isfinite(dt) || dt <= Scalar(0))
77 {
78 return last_out_;
79 }
80
81 // Compute error
82 const Scalar ERR = param_.cycle ? (CycleValue<Scalar>(sp) - fb) : (sp - fb);
83 const Scalar E_K = ERR * param_.k;
84
85 // Derivative from feedback change (scaled by k)
86 Scalar fb_dot = (fb - last_fb_) / dt;
87 if (!std::isfinite(fb_dot))
88 {
89 fb_dot = Scalar(0);
90 }
91
92 Scalar fb_d_k = fb_dot * param_.k;
93 if (!std::isfinite(fb_d_k))
94 {
95 fb_d_k = Scalar(0);
96 }
97
98 // Compute PD
99 const Scalar OUTPUT_PD = (E_K * param_.p) - (fb_d_k * param_.d);
100
101 // -------------------------------
102 // Integrator update: anti-windup + allow unwind
103 // Rule: i_limit == 0 disables I (force i_ = 0)
104 // -------------------------------
105 if (param_.i > PID_SIGMA && param_.i_limit > PID_SIGMA)
106 {
107 Scalar i_candidate = i_ + E_K * dt;
108
109 if (std::isfinite(i_candidate))
110 {
111 // Clamp integrator state
112 i_candidate = std::clamp(i_candidate, -param_.i_limit, param_.i_limit);
113
114 bool accept = true;
115
116 // Output-limit-aware gating (windup prevention + unwind)
117 if (param_.out_limit > PID_SIGMA)
118 {
119 const Scalar OUT_BEFORE = OUTPUT_PD + (i_ * param_.i) + feed_forward_;
120 const Scalar OUT_AFTER = OUTPUT_PD + (i_candidate * param_.i) + feed_forward_;
121
122 if (std::isfinite(OUT_BEFORE) && std::isfinite(OUT_AFTER))
123 {
124 const bool BEFORE_SAT = (std::abs(OUT_BEFORE) > param_.out_limit);
125 const bool AFTER_SAT = (std::abs(OUT_AFTER) > param_.out_limit);
126
127 if (AFTER_SAT)
128 {
129 // If saturated (or would be), only allow integral update if it reduces
130 // saturation magnitude
131 // - If not saturated before but would saturate after: reject (prevent
132 // windup)
133 // - If already saturated: allow only if |out_after| < |out_before| (unwind)
134 accept = BEFORE_SAT && (std::abs(OUT_AFTER) < std::abs(OUT_BEFORE));
135 }
136 }
137 else
138 {
139 accept = false;
140 }
141 }
142
143 if (accept)
144 {
145 i_ = i_candidate;
146 }
147 }
148 }
149 else
150 {
151 // Disable I
152 i_ = Scalar(0);
153 }
154
155 const Scalar I_OUT = i_ * param_.i;
156
157 // Apply output limits
158 Scalar output = OUTPUT_PD + I_OUT + feed_forward_;
159 if (std::isfinite(output) && param_.out_limit > PID_SIGMA)
160 {
161 output = std::clamp(output, -param_.out_limit, param_.out_limit);
162 }
163
164 // Store states
165 last_err_ = ERR;
166 last_fb_ = fb; // store raw feedback
167 last_out_ = output;
168 last_der_ = fb_d_k; // store scaled derivative: k * d(fb)/dt
169 return output;
170 }
Scalar last_err_
上次误差 Last error
Definition pid.hpp:354
Scalar last_fb_
上次反馈(未缩放)Last feedback (raw)
Definition pid.hpp:355
Scalar last_der_
上次导数(k 缩放)Last derivative (scaled by k)
Definition pid.hpp:356
Scalar last_out_
上次输出 Last output
Definition pid.hpp:357
Scalar i_
积分状态 Integral state
Definition pid.hpp:353
Scalar feed_forward_
前馈项 Feedforward term
Definition pid.hpp:358
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

◆ Calculate() [2/2]

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::Calculate ( Scalar sp,
Scalar fb,
Scalar fb_dot,
Scalar dt )
inline

使用外部导数计算 PID 输出。 Compute output using external feedback derivative.

Define: err = (cycle ? CycleValue(sp) - fb : sp - fb) e_k = k * err fb_d_k = k * fb_dot

out = p * e_k + i * ∫(e_k)dt - d * fb_d_k + feed_forward

Parameters
sp期望值 Setpoint
fb反馈值 Feedback
fb_dot反馈导数 Feedback rate (d(fb)/dt)
dt控制周期 Delta time
Returns
控制器输出 Controller output

Definition at line 189 of file pid.hpp.

190 {
191 if (!std::isfinite(sp) || !std::isfinite(fb) || !std::isfinite(fb_dot) ||
192 !std::isfinite(dt) || dt <= Scalar(0))
193 {
194 return last_out_;
195 }
196
197 // Compute error
198 const Scalar ERR = param_.cycle ? (CycleValue<Scalar>(sp) - fb) : (sp - fb);
199 const Scalar E_K = ERR * param_.k;
200
201 // Use externally provided derivative (scaled by k)
202 Scalar fb_d_k = fb_dot * param_.k;
203 if (!std::isfinite(fb_d_k))
204 {
205 fb_d_k = Scalar(0);
206 }
207
208 // Compute PD
209 const Scalar OUTPUT_PD = (E_K * param_.p) - (fb_d_k * param_.d);
210
211 // -------------------------------
212 // Integrator update: anti-windup + allow unwind
213 // Rule: i_limit == 0 disables I (force i_ = 0)
214 // -------------------------------
215 if (param_.i > PID_SIGMA && param_.i_limit > PID_SIGMA)
216 {
217 Scalar i_candidate = i_ + E_K * dt;
218
219 if (std::isfinite(i_candidate))
220 {
221 // Clamp integrator state
222 i_candidate = std::clamp(i_candidate, -param_.i_limit, param_.i_limit);
223
224 bool accept = true;
225
226 if (param_.out_limit > PID_SIGMA)
227 {
228 const Scalar OUT_BEFORE = OUTPUT_PD + (i_ * param_.i) + feed_forward_;
229 const Scalar OUT_AFTER = OUTPUT_PD + (i_candidate * param_.i) + feed_forward_;
230
231 if (std::isfinite(OUT_BEFORE) && std::isfinite(OUT_AFTER))
232 {
233 const bool BEFORE_SAT = (std::abs(OUT_BEFORE) > param_.out_limit);
234 const bool AFTER_SAT = (std::abs(OUT_AFTER) > param_.out_limit);
235
236 if (AFTER_SAT)
237 {
238 accept = BEFORE_SAT && (std::abs(OUT_AFTER) < std::abs(OUT_BEFORE));
239 }
240 }
241 else
242 {
243 accept = false;
244 }
245 }
246
247 if (accept)
248 {
249 i_ = i_candidate;
250 }
251 }
252 }
253 else
254 {
255 // Disable I
256 i_ = Scalar(0);
257 }
258
259 const Scalar I_OUT = i_ * param_.i;
260
261 // Apply output limits
262 Scalar output = OUTPUT_PD + I_OUT + feed_forward_;
263 if (std::isfinite(output) && param_.out_limit > PID_SIGMA)
264 {
265 output = std::clamp(output, -param_.out_limit, param_.out_limit);
266 }
267
268 // Store states
269 last_err_ = ERR;
270 last_fb_ = fb; // store raw feedback
271 last_out_ = output;
272 last_der_ = fb_d_k; // store scaled derivative: k * d(fb)/dt
273 return output;
274 }

◆ D()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::D ( ) const
inline

获取 D 项系数 Get derivative gain

Definition at line 296 of file pid.hpp.

296{ return param_.d; }

◆ GetFeedForward()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::GetFeedForward ( ) const
inline

获取前馈项 Get feedforward

Returns
前馈项 Feedforward

Definition at line 349 of file pid.hpp.

349{ return feed_forward_; }

◆ GetIntegralError()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::GetIntegralError ( ) const
inline

获取累计误差 Get integral error

Returns
累计误差 Integral error

Definition at line 335 of file pid.hpp.

335{ return i_; }

◆ I()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::I ( ) const
inline

获取 I 项系数 Get integral gain

Definition at line 294 of file pid.hpp.

294{ return param_.i; }

◆ ILimit()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::ILimit ( ) const
inline

获取积分限幅 Get integral limit

Definition at line 298 of file pid.hpp.

298{ return param_.i_limit; }

◆ K()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::K ( ) const
inline

获取全局比例系数 Get global proportional gain

Definition at line 290 of file pid.hpp.

290{ return param_.k; }

◆ LastDerivative()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::LastDerivative ( ) const
inline

获取上一次导数(k * d(fb)/dt 或 k * fb_dot)Get last derivative (scaled by k)

Definition at line 308 of file pid.hpp.

308{ return last_der_; }

◆ LastError()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::LastError ( ) const
inline

获取上一次误差 Get last error

Definition at line 302 of file pid.hpp.

302{ return last_err_; }

◆ LastFeedback()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::LastFeedback ( ) const
inline

获取上一次反馈值(未缩放)Get last feedback (raw)

Definition at line 304 of file pid.hpp.

304{ return last_fb_; }

◆ LastOutput()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::LastOutput ( ) const
inline

获取上一次输出 Get last output

Definition at line 306 of file pid.hpp.

306{ return last_out_; }

◆ OutLimit()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::OutLimit ( ) const
inline

获取输出限幅 Get output limit

Definition at line 300 of file pid.hpp.

300{ return param_.out_limit; }

◆ P()

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::P ( ) const
inline

获取 P 项系数 Get proportional gain

Definition at line 292 of file pid.hpp.

292{ return param_.p; }

◆ Reset()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::Reset ( )
inline

重置控制器状态。 Reset all internal states.

Definition at line 314 of file pid.hpp.

315 {
316 i_ = 0;
317 last_err_ = 0;
318 last_fb_ = 0;
319 last_out_ = 0;
320 last_der_ = 0;
321 }

◆ SetD()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetD ( Scalar d)
inline

设置 D 项系数 Set derivative gain

Definition at line 283 of file pid.hpp.

283{ param_.d = d; }

◆ SetFeedForward()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetFeedForward ( Scalar feed_forward)
inline

设置前馈项 Set feedforward

Parameters
feed_forward前馈项 Feedforward

Definition at line 342 of file pid.hpp.

342{ feed_forward_ = feed_forward; }

◆ SetI()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetI ( Scalar i)
inline

设置 I 项系数 Set integral gain

Definition at line 281 of file pid.hpp.

281{ param_.i = i; }

◆ SetILimit()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetILimit ( Scalar limit)
inline

设置积分限幅 Set integral limit

Definition at line 285 of file pid.hpp.

285{ param_.i_limit = limit; }

◆ SetIntegralError()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetIntegralError ( Scalar err)
inline

设置累计误差 Set integral error

Parameters
err累计误差 Integral error

Definition at line 328 of file pid.hpp.

328{ i_ = err; }

◆ SetK()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetK ( Scalar k)
inline

设置全局比例系数 Set global proportional gain

Definition at line 277 of file pid.hpp.

277{ param_.k = k; }

◆ SetOutLimit()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetOutLimit ( Scalar limit)
inline

设置输出限幅 Set output limit

Definition at line 287 of file pid.hpp.

287{ param_.out_limit = limit; }

◆ SetP()

template<typename Scalar = DefaultScalar>
void LibXR::PID< Scalar >::SetP ( Scalar p)
inline

设置 P 项系数 Set proportional gain

Definition at line 279 of file pid.hpp.

279{ param_.p = p; }

Field Documentation

◆ feed_forward_

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::feed_forward_ = 0
private

前馈项 Feedforward term

Definition at line 358 of file pid.hpp.

◆ i_

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::i_ = 0
private

积分状态 Integral state

Definition at line 353 of file pid.hpp.

◆ last_der_

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::last_der_ = 0
private

上次导数(k 缩放)Last derivative (scaled by k)

Definition at line 356 of file pid.hpp.

◆ last_err_

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::last_err_ = 0
private

上次误差 Last error

Definition at line 354 of file pid.hpp.

◆ last_fb_

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::last_fb_ = 0
private

上次反馈(未缩放)Last feedback (raw)

Definition at line 355 of file pid.hpp.

◆ last_out_

template<typename Scalar = DefaultScalar>
Scalar LibXR::PID< Scalar >::last_out_ = 0
private

上次输出 Last output

Definition at line 357 of file pid.hpp.

◆ param_

template<typename Scalar = DefaultScalar>
Param LibXR::PID< Scalar >::param_
private

PID 参数 PID parameter set.

Definition at line 352 of file pid.hpp.


The documentation for this class was generated from the following file: