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#include <Udon/Types/Range.hpp>
14
15namespace Udon
16{
17
20 {
21 public:
22 struct Parameter
23 {
24 double p;
25 double i;
26 double d;
27 };
28
29 private:
30 Parameter constant;
31
32 Udon::Optional<Parameter> requestConstant;
33
34 Parameter power;
35
36 double lastError;
37
38 const double MAX_INT_POWER;
39 const double INTERVAL_S;
40
41 public:
48 PidController(double kPro, double kInt, double kDif, unsigned long callInterval_us, double maxIntPower = 1023.0) noexcept
49 : constant{ kPro, kInt, kDif }
50 , requestConstant{}
51 , power{}
52 , lastError()
53 , MAX_INT_POWER(maxIntPower)
54 , INTERVAL_S(callInterval_us / 1000000.0)
55 {
56 }
57
62 void update(double controlValue, double targetValue) noexcept
63 {
64 // 係数の適用
65 const auto coefficient = requestConstant ? *requestConstant : constant;
66 requestConstant.reset();
67
68 // 偏差の計算
69 const double error = targetValue - controlValue;
70
71 // 比例量の計算
72 power.p = coefficient.p * error;
73
74 // 積分量の計算
75 power.i += coefficient.i * error * INTERVAL_S;
76 power.i = Udon::Constrain(power.i, -MAX_INT_POWER, MAX_INT_POWER);
77
78 // 微分量の計算
79 power.d = coefficient.d * (error - lastError) / INTERVAL_S;
80
81 // 偏差の保存
82 lastError = error;
83 }
84
87 double getPower() const noexcept
88 {
89 return power.p + power.i + power.d;
90 }
91
96 double getPower(double min, double max) const noexcept
97 {
98 return Udon::Constrain(getPower(), min, max);
99 }
100
104 double getPower(const Udon::Range<double>& range) const noexcept
105 {
106 return getPower(range.min, range.max);
107 }
108
113 double operator()(double controlValue, double targetValue) noexcept
114 {
115 update(controlValue, targetValue);
116 return getPower();
117 }
118
125 double operator()(double controlValue, double targetValue, double min, double max) noexcept
126 {
127 update(controlValue, targetValue);
128 return getPower(min, max);
129 }
130
136 double operator()(double controlValue, double targetValue, const Udon::Range<double>& range) noexcept
137 {
138 return operator()(controlValue, targetValue, range.min, range.max);
139 }
140
143 void clearPower() noexcept
144 {
145 power = {};
146 lastError = 0.0;
147 }
148 void clearIntegral() noexcept
149 {
150 power.i = 0.0;
151 }
152
154 void requestParamPro(double value) noexcept
155 {
156 if (requestConstant)
157 {
158 requestConstant->p = value;
159 }
160 else
161 {
162 requestConstant = Parameter{ value, constant.i, constant.d };
163 }
164 }
165
168 void requestParamInt(double value) noexcept
169 {
170 if (requestConstant)
171 {
172 requestConstant->i = value;
173 }
174 else
175 {
176 requestConstant = Parameter{ constant.p, value, constant.d };
177 }
178 }
179
182 void requestParamDif(double value) noexcept
183 {
184 if (requestConstant)
185 {
186 requestConstant->d = value;
187 }
188 else
189 {
190 requestConstant = Parameter{ constant.p, constant.i, value };
191 }
192 }
193
196 void requestParam(const Parameter& value) noexcept { requestConstant = value; }
197
200 void setParamPro(double value) noexcept { constant.p = value; }
201
204 void setParamInt(double value) noexcept { constant.i = value; }
205
208 void setParamDif(double value) noexcept { constant.d = value; }
209
212 void setParam(const Parameter& value) noexcept { constant = value; }
213
216 double getParamPro() const noexcept { return constant.p; }
217
220 double getParamInt() const noexcept { return constant.i; }
221
224 double getParamDif() const noexcept { return constant.d; }
225
228 const Parameter& getParam() const noexcept { return constant; }
229
232 double getPowerPro() const noexcept { return power.p; }
233
236 double getPowerInt() const noexcept { return power.i; }
237
240 double getPowerDif() const noexcept { return power.d; }
241 };
242
243} // namespace Udon
244
245#endif
オプショナル型
Definition Optional.hpp:62
double operator()(double controlValue, double targetValue, double min, double max) noexcept
更新、操作量の取得
Definition PidController.hpp:125
void clearIntegral() noexcept
Definition PidController.hpp:148
double getPowerPro() const noexcept
比例量の取得
Definition PidController.hpp:232
double getPower() const noexcept
操作量の取得 (操作量の範囲制限なし)
Definition PidController.hpp:87
PidController(double kPro, double kInt, double kDif, unsigned long callInterval_us, double maxIntPower=1023.0) noexcept
コンストラクタ
Definition PidController.hpp:48
double getPowerDif() const noexcept
微分量の取得
Definition PidController.hpp:240
void setParamInt(double value) noexcept
積分係数の設定
Definition PidController.hpp:204
void setParamDif(double value) noexcept
微分係数の設定
Definition PidController.hpp:208
double getPower(const Udon::Range< double > &range) const noexcept
操作量の取得
Definition PidController.hpp:104
const Parameter & getParam() const noexcept
係数の取得
Definition PidController.hpp:228
void update(double controlValue, double targetValue) noexcept
データ更新
Definition PidController.hpp:62
void setParamPro(double value) noexcept
比例係数の設定
Definition PidController.hpp:200
double getParamDif() const noexcept
微分係数の取得
Definition PidController.hpp:224
double getPowerInt() const noexcept
積分量の取得
Definition PidController.hpp:236
void setParam(const Parameter &value) noexcept
係数の設定
Definition PidController.hpp:212
void requestParamPro(double value) noexcept
一周期のみ適用する比例係数の設定
Definition PidController.hpp:154
double operator()(double controlValue, double targetValue) noexcept
更新、操作量の取得 (操作量の範囲制限なし)
Definition PidController.hpp:113
void requestParamDif(double value) noexcept
一周期のみ適用する微分係数の設定
Definition PidController.hpp:182
double operator()(double controlValue, double targetValue, const Udon::Range< double > &range) noexcept
更新、操作量の取得
Definition PidController.hpp:136
double getPower(double min, double max) const noexcept
操作量の取得
Definition PidController.hpp:96
void clearPower() noexcept
操作量のクリア
Definition PidController.hpp:143
void requestParam(const Parameter &value) noexcept
一周期のみ適用する係数の設定
Definition PidController.hpp:196
void requestParamInt(double value) noexcept
一周期のみ適用する積分係数の設定
Definition PidController.hpp:168
double getParamPro() const noexcept
比例係数の取得
Definition PidController.hpp:216
double getParamInt() const noexcept
積分係数の取得
Definition PidController.hpp:220
Definition Bit.hpp:12
constexpr A Constrain(const A &amt, const B &low, const C &high)
値を指定された範囲内に収める (std::clamp)
Definition Math.hpp:69
Definition PidController.hpp:23
double d
Definition PidController.hpp:26
double i
Definition PidController.hpp:25
double p
Definition PidController.hpp:24
範囲を表す型
Definition Range.hpp:8