GKD.RoboCtrl
载入中...
搜索中...
未找到
pid.h
浏览该文件的文档.
1
6
#pragma once
7
#include <cmath>
8
#include <algorithm>
9
10
#include "
device/motor/base.hpp
"
11
#include "
controller.hpp
"
12
#include "
utils/controller.hpp
"
13
14
namespace
roboctrl::utils
{
20
template
<
typename
T, auto error_measurer>
21
struct
pid_base
{
22
T kp = 0;
23
T ki = 0;
24
T kd = 0;
25
26
T max_out{};
27
T max_iout{};
28
29
using
input_type = T;
30
using
state_type = T;
31
35
struct
params_type
{
36
T kp = 0;
37
T ki = 0;
38
T kd = 0;
39
40
T max_out{};
41
T max_iout{};
42
};
43
44
pid_base
() =
default
;
45
46
pid_base
(
const
params_type
&
params
)
47
: kp(
params
.kp), ki(
params
.ki), kd(
params
.kd),
48
max_out(
params
.max_out), max_iout(
params
.max_iout) {}
49
53
void
set_target
(T target) { target_ = target; }
54
55
inline
T target()
const
{
return
target_; }
56
66
void
update
(T current) {
67
T
error_prev
= last_error_;
68
T
error_curr
=
error_measurer
(current, target_);
69
last_error_ =
error_curr
;
70
71
T
pout
= kp *
error_curr
;
72
73
integral_ += ki *
error_curr
;
74
integral_ = std::clamp(integral_, -max_iout, max_iout);
75
76
T
derivative
= kd * (
error_curr
-
error_prev
);
77
78
T
out
=
pout
+ integral_ +
derivative
;
79
out
= std::clamp(
out
, -max_out, max_out);
80
81
output_ =
out
;
82
}
83
87
void
clean
() {
88
integral_ = 0;
89
last_error_ = 0;
90
output_ = 0;
91
target_ = 0;
92
}
93
97
T
state
()
const
{
return
output_; }
98
99
private
:
100
T target_ = 0;
101
T integral_ = 0;
102
T last_error_ = 0;
103
T output_ = 0;
104
};
105
106
namespace
details{
107
109
constexpr
auto
linear_error
= [](
fp32
cur
,
fp32
target) {
110
return
target -
cur
;
111
};
112
114
constexpr
auto
rad_error
= [](
fp32
cur
,
fp32
target) {
115
fp32
diff
= target -
cur
;
116
diff
=
fmod
(
diff
+
M_PI
, 2*
M_PI
);
117
if
(
diff
< 0)
diff
+= 2*
M_PI
;
118
diff
-=
M_PI
;
119
return
diff
;
120
};
121
122
}
123
125
using
linear_pid
=
pid_base<fp32, details::linear_error>
;
127
using
rad_pid
=
pid_base<fp32, details::rad_error>
;
128
129
static_assert
(
utils::controller<linear_pid>
);
130
static_assert
(
utils::controller<rad_pid>
);
131
133
template
<device::motor motor_type>
134
using
linear_pid_motor
=
device::controlled_motor<motor_type, linear_pid>
;
135
137
template
<device::motor motor_type>
138
using
rad_pid_motor
=
device::controlled_motor<motor_type, rad_pid>
;
139
140
}
roboctrl::utils::controller
控制器概念,约束 update/state/构造能力。
Definition
controller.hpp:18
controller.hpp
控制器概念与串联控制链。
base.hpp
电机基础组件。
roboctrl::async::awaitable
asio::awaitable< T > awaitable
协程任务类型。
Definition
async.hpp:42
roboctrl::utils
用于存放工具函数的命名空间。
Definition
concepts.hpp:13
roboctrl::utils::rad_pid
pid_base< fp32, details::rad_error > rad_pid
角度 PID 控制器。
Definition
pid.h:127
roboctrl::utils::details::rad_error
constexpr auto rad_error
角度误差计算方式,自动处理 2π 周期。
Definition
pid.h:114
roboctrl::utils::details::linear_error
constexpr auto linear_error
线性误差计算方式。
Definition
pid.h:109
roboctrl::device::controlled_motor
被控制算法(例如PID)控制的电机
Definition
base.hpp:142
roboctrl::utils::pid_base::params_type
统一的参数封装,方便序列化。
Definition
pid.h:35
roboctrl::utils::pid_base
PID 控制器基础模板。
Definition
pid.h:21
roboctrl::utils::pid_base::clean
void clean()
清空积分项、误差缓存与输出。
Definition
pid.h:87
roboctrl::utils::pid_base::state
T state() const
获取最新的控制输出。
Definition
pid.h:97
roboctrl::utils::pid_base::set_target
void set_target(T target)
设置期望目标。
Definition
pid.h:53
roboctrl::utils::pid_base::update
void update(T current)
根据当前值更新 PID 输出。
Definition
pid.h:66
roboctrl::fp32
float fp32
单精度浮点别名。
Definition
utils.hpp:22
include
utils
pid.h
制作者
1.9.8