UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
PidController.hpp
[詳解]
1//
2// PIDコントローラクラス
3//
4// Copyright (c) 2016-2023 谷川 豊章
5// Copyright (c) 2016-2023 udonrobo
6//
7
8#ifndef DEF_PidController_H
9#define DEF_PidController_H
10
11#include "Math.hpp"
13
14namespace Udon
15{
16
19 {
20 public:
21 struct Parameter
22 {
23 double p;
24 double i;
25 double d;
26 };
27
28 private:
29 Parameter constant;
30
31 Udon::Optional<Parameter> requestConstant;
32
33 Parameter power;
34
35 double lastError;
36
37 const double MAX_INT_POWER;
38 const double INTERVAL_S;
39
40 public:
47 PidController(double kPro, double kInt, double kDif, unsigned long callInterval_us, double maxIntPower = 1023.0) noexcept
48 : constant{ kPro, kInt, kDif }
49 , requestConstant{}
50 , power{}
51 , lastError()
52 , MAX_INT_POWER(maxIntPower)
53 , INTERVAL_S(callInterval_us / 1000000.0)
54 {
55 }
56
61 void update(double controlValue, double targetValue) noexcept
62 {
63 // 係数の適用
64 const auto coefficient = requestConstant ? *requestConstant : constant;
65 requestConstant.reset();
66
67 // 偏差の計算
68 const double error = targetValue - controlValue;
69
70 // 比例量の計算
71 power.p = coefficient.p * error;
72
73 // 積分量の計算
74 power.i += coefficient.i * error * INTERVAL_S;
75 power.i = Udon::Constrain(power.i, -MAX_INT_POWER, MAX_INT_POWER);
76
77 // 微分量の計算
78 power.d = coefficient.d * (error - lastError) / INTERVAL_S;
79
80 // 偏差の保存
81 lastError = error;
82 }
83
86 double getPower() const noexcept
87 {
88 return power.p + power.i + power.d;
89 }
90
95 double getPower(double min, double max) const noexcept
96 {
97 return Udon::Constrain(getPower(), min, max);
98 }
99
104 double operator()(double controlValue, double targetValue) noexcept
105 {
106 update(controlValue, targetValue);
107 return getPower();
108 }
109
116 double operator()(double controlValue, double targetValue, double min, double max) noexcept
117 {
118 update(controlValue, targetValue);
119 return getPower(min, max);
120 }
121
124 void clearPower() noexcept
125 {
126 power = {};
127 lastError = 0.0;
128 }
129 void clearIntegral() noexcept
130 {
131 power.i = 0.0;
132 }
135 void requestParamPro(double value) noexcept
136 {
137 if (requestConstant)
138 {
139 requestConstant->p = value;
140 }
141 else
142 {
143 requestConstant = Parameter{ value, constant.i, constant.d };
144 }
145 }
146
149 void requestParamInt(double value) noexcept
150 {
151 if (requestConstant)
152 {
153 requestConstant->i = value;
154 }
155 else
156 {
157 requestConstant = Parameter{ constant.p, value, constant.d };
158 }
159 }
160
163 void requestParamDif(double value) noexcept
164 {
165 if (requestConstant)
166 {
167 requestConstant->d = value;
168 }
169 else
170 {
171 requestConstant = Parameter{ constant.p, constant.i, value };
172 }
173 }
174
177 void requestParam(const Parameter& value) noexcept { requestConstant = value; }
178
181 void setParamPro(double value) noexcept { constant.p = value; }
182
185 void setParamInt(double value) noexcept { constant.i = value; }
186
189 void setParamDif(double value) noexcept { constant.d = value; }
190
193 void setParam(const Parameter& value) noexcept { constant = value; }
194
197 double getParamPro() const noexcept { return constant.p; }
198
201 double getParamInt() const noexcept { return constant.i; }
202
205 double getParamDif() const noexcept { return constant.d; }
206
209 const Parameter& getParam() const noexcept { return constant; }
210
213 double getPowerPro() const noexcept { return power.p; }
214
217 double getPowerInt() const noexcept { return power.i; }
218
221 double getPowerDif() const noexcept { return power.d; }
222 };
223
224} // namespace Udon
225
226#endif
オプショナル型
Definition Optional.hpp:62
void reset() noexcept(std::is_nothrow_destructible< ValueType >::value)
無効状態にする (トリビアルな型)
Definition Optional.hpp:457
PID制御器
Definition PidController.hpp:19
double operator()(double controlValue, double targetValue, double min, double max) noexcept
更新、操作量の取得
Definition PidController.hpp:116
void clearIntegral() noexcept
Definition PidController.hpp:129
double getPowerPro() const noexcept
比例量の取得
Definition PidController.hpp:213
double getPower() const noexcept
操作量の取得
Definition PidController.hpp:86
PidController(double kPro, double kInt, double kDif, unsigned long callInterval_us, double maxIntPower=1023.0) noexcept
コンストラクタ
Definition PidController.hpp:47
double getPowerDif() const noexcept
微分量の取得
Definition PidController.hpp:221
void setParamInt(double value) noexcept
積分係数の設定
Definition PidController.hpp:185
void setParamDif(double value) noexcept
微分係数の設定
Definition PidController.hpp:189
const Parameter & getParam() const noexcept
係数の取得
Definition PidController.hpp:209
void update(double controlValue, double targetValue) noexcept
データ更新
Definition PidController.hpp:61
void setParamPro(double value) noexcept
比例係数の設定
Definition PidController.hpp:181
double getParamDif() const noexcept
微分係数の取得
Definition PidController.hpp:205
double getPowerInt() const noexcept
積分量の取得
Definition PidController.hpp:217
void setParam(const Parameter &value) noexcept
係数の設定
Definition PidController.hpp:193
void requestParamPro(double value) noexcept
一周期のみ適用する比例係数の設定
Definition PidController.hpp:135
double operator()(double controlValue, double targetValue) noexcept
更新、操作量の取得
Definition PidController.hpp:104
void requestParamDif(double value) noexcept
一周期のみ適用する微分係数の設定
Definition PidController.hpp:163
double getPower(double min, double max) const noexcept
操作量の取得
Definition PidController.hpp:95
void clearPower() noexcept
操作量のクリア
Definition PidController.hpp:124
void requestParam(const Parameter &value) noexcept
一周期のみ適用する係数の設定
Definition PidController.hpp:177
void requestParamInt(double value) noexcept
一周期のみ適用する積分係数の設定
Definition PidController.hpp:149
double getParamPro() const noexcept
比例係数の取得
Definition PidController.hpp:197
double getParamInt() const noexcept
積分係数の取得
Definition PidController.hpp:201
Definition Bit.hpp:12
constexpr A Constrain(const A &amt, const B &low, const C &high)
値を指定された範囲内に収める (std::clamp)
Definition Math.hpp:68
Definition PidController.hpp:22
double d
Definition PidController.hpp:25
double i
Definition PidController.hpp:24
double p
Definition PidController.hpp:23