UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
SpeedPid.hpp
[詳解]
1//
2// 速度型PIDコントローラクラス
3//
4
5#ifndef DEF_SpeedPidController_H
6#define DEF_SpeedPidController_H
7
8#include "Math.hpp"
10#include <Udon/Types/Range.hpp>
11
12namespace Udon
13{
16 {
17 public:
18 struct Parameter
19 {
20 double p;
21 double i;
22 double d;
23 };
24
25 private:
26 Parameter constant;
27
28 Udon::Optional<Parameter> requestConstant;
29
30 Parameter power;
31
32 double lastPowerPro;
33 double lastError;
34 double rowPassDif;
35 double output;
36 const double MAX_POWER;
37 const double INTERVAL_S;
38
39 public:
45 SpeedPidController(double kPro, double kInt, double kDif,
46 unsigned long callInterval_us,
47 double maxPower = 10000) noexcept
48 : constant{ kPro, kInt, kDif }
49 , requestConstant{}
50 , power{}
51 , lastPowerPro(0.0)
52 , lastError()
53 , rowPassDif()
54 , output()
55 , MAX_POWER(maxPower)
56 , INTERVAL_S(callInterval_us / 1000000.0)
57 {
58 }
59
64 void update(double controlValue, double targetValue) noexcept
65 {
66 // 係数の適用
67 const auto coefficient = requestConstant ? *requestConstant : constant;
68 requestConstant.reset();
69
70 // 偏差の計算
71 const double error = targetValue - controlValue;
72
73 // 比例量の計算
74 power.p = (error - lastError) / INTERVAL_S * coefficient.p;
75
76 // 積分量の計算
77 power.i = error * coefficient.i;
78
79 // 微分量の計算
80 power.d = (power.p - lastPowerPro) / INTERVAL_S;
81
82 // ローパスフィルタの計算
83 rowPassDif += (power.d - rowPassDif) / 8 * coefficient.d;
84
85 const double deltaPower = power.p + power.i + rowPassDif;
86
87 output = Udon::Constrain(output + deltaPower, -MAX_POWER, MAX_POWER);
88
89 // 偏差の保存
90 lastPowerPro = power.p;
91 lastError = error;
92 }
93
96 double getPower() const noexcept
97 {
98 return output;
99 }
100
105 double getPower(double min, double max) const noexcept
106 {
107 return Udon::Constrain(getPower(), min, max);
108 }
109
114 double getPower(const Udon::Range<double>& range) const noexcept
115 {
116 return Udon::Constrain(getPower(), range.min, range.max);
117 }
118
123 double operator()(double controlValue, double targetValue) noexcept
124 {
125 update(controlValue, targetValue);
126 return getPower();
127 }
128
135 double operator()(double controlValue, double targetValue, double min,
136 double max) noexcept
137 {
138 update(controlValue, targetValue);
139 return getPower(min, max);
140 }
141
147 double operator()(double controlValue, double targetValue, const Udon::Range<double>& range) noexcept
148 {
149 return operator()(controlValue, targetValue, range.min, range.max);
150 }
151
154 void clearPower() noexcept
155 {
156 power = {};
157 output = 0.0;
158 lastPowerPro = 0.0;
159 rowPassDif = 0.0;
160 lastError = 0.0;
161 }
162
164 void requestParamPro(double value) noexcept
165 {
166 if (requestConstant)
167 {
168 requestConstant->p = value;
169 }
170 else
171 {
172 requestConstant = Parameter{ value, constant.i, constant.d };
173 }
174 }
175
178 void requestParamInt(double value) noexcept
179 {
180 if (requestConstant)
181 {
182 requestConstant->i = value;
183 }
184 else
185 {
186 requestConstant = Parameter{ constant.p, value, constant.d };
187 }
188 }
189
192 void requestParamDif(double value) noexcept
193 {
194 if (requestConstant)
195 {
196 requestConstant->d = value;
197 }
198 else
199 {
200 requestConstant = Parameter{ constant.p, constant.i, value };
201 }
202 }
203
206 void requestParam(const Parameter& value) noexcept
207 {
208 requestConstant = value;
209 }
210
213 void setParamPro(double value) noexcept { constant.p = value; }
214
217 void setParamInt(double value) noexcept { constant.i = value; }
218
221 void setParamDif(double value) noexcept { constant.d = value; }
222
225 void setParam(const Parameter& value) noexcept { constant = value; }
226
229 double getParamPro() const noexcept { return constant.p; }
230
233 double getParamInt() const noexcept { return constant.i; }
234
237 double getParamDif() const noexcept { return constant.d; }
238
241 const Parameter& getParam() const noexcept { return constant; }
242
245 double getPowerPro() const noexcept { return power.p; }
246
249 double getPowerInt() const noexcept { return power.i; }
250
253 double getPowerDif() const noexcept { return power.d; }
254 };
255} // namespace Udon
256
257#endif
オプショナル型
Definition Optional.hpp:62
double getPowerInt() const noexcept
積分量の取得
Definition SpeedPid.hpp:249
void requestParamDif(double value) noexcept
一周期のみ適用する微分係数の設定
Definition SpeedPid.hpp:192
double getPowerDif() const noexcept
微分量の取得
Definition SpeedPid.hpp:253
void requestParam(const Parameter &value) noexcept
一周期のみ適用する係数の設定
Definition SpeedPid.hpp:206
void clearPower() noexcept
操作量のクリア
Definition SpeedPid.hpp:154
SpeedPidController(double kPro, double kInt, double kDif, unsigned long callInterval_us, double maxPower=10000) noexcept
コンストラクタ
Definition SpeedPid.hpp:45
const Parameter & getParam() const noexcept
係数の取得
Definition SpeedPid.hpp:241
double getParamPro() const noexcept
比例係数の取得
Definition SpeedPid.hpp:229
double operator()(double controlValue, double targetValue) noexcept
更新、操作量の取得
Definition SpeedPid.hpp:123
double getPower(const Udon::Range< double > &range) const noexcept
操作量の取得
Definition SpeedPid.hpp:114
void setParamPro(double value) noexcept
比例係数の設定
Definition SpeedPid.hpp:213
void requestParamInt(double value) noexcept
一周期のみ適用する積分係数の設定
Definition SpeedPid.hpp:178
double getPowerPro() const noexcept
比例量の取得
Definition SpeedPid.hpp:245
double getParamInt() const noexcept
積分係数の取得
Definition SpeedPid.hpp:233
double operator()(double controlValue, double targetValue, double min, double max) noexcept
更新、操作量の取得
Definition SpeedPid.hpp:135
double getPower() const noexcept
操作量の取得
Definition SpeedPid.hpp:96
double operator()(double controlValue, double targetValue, const Udon::Range< double > &range) noexcept
更新、操作量の取得
Definition SpeedPid.hpp:147
void update(double controlValue, double targetValue) noexcept
データ更新
Definition SpeedPid.hpp:64
void setParam(const Parameter &value) noexcept
係数の設定
Definition SpeedPid.hpp:225
void requestParamPro(double value) noexcept
一周期のみ適用する比例係数の設定
Definition SpeedPid.hpp:164
double getPower(double min, double max) const noexcept
操作量の取得
Definition SpeedPid.hpp:105
void setParamInt(double value) noexcept
積分係数の設定
Definition SpeedPid.hpp:217
void setParamDif(double value) noexcept
微分係数の設定
Definition SpeedPid.hpp:221
double getParamDif() const noexcept
微分係数の取得
Definition SpeedPid.hpp:237
Definition Bit.hpp:12
constexpr A Constrain(const A &amt, const B &low, const C &high)
値を指定された範囲内に収める (std::clamp)
Definition Math.hpp:69
範囲を表す型
Definition Range.hpp:8
Definition SpeedPid.hpp:19
double d
Definition SpeedPid.hpp:22
double p
Definition SpeedPid.hpp:20
double i
Definition SpeedPid.hpp:21