Skip to content

Commit 6bcfac6

Browse files
committed
Squashed commit of the following:
commit bf07fe8 Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Sat May 17 20:43:27 2025 +0900 update:gtestのアサーションをdauble型に変更 commit 43a7bf5 Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Sat May 17 20:40:43 2025 +0900 update:ローパスフィルタの係数をメンバ変数に変更 commit 7a3a536 Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Sat May 17 15:14:16 2025 +0900 add:Pid.cppのテストをするファイルの追加 commit 84558db Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Sat May 17 15:12:15 2025 +0900 add:Pidを計算するファイルの追加
1 parent 31e8d81 commit 6bcfac6

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

modules/Pid.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,73 @@ double Pid::calculatePid(double currentValue, double delta)
6363
// D制御の計算を行う
6464
double d = pidGain.kd * filteredDerivative;
6565

66+
// 操作量 = P制御 + I制御 + D制御
67+
return (p + i + d);
68+
} /**
69+
* @file Pid.cpp
70+
* @brief PIDを計算するクラス
71+
* @author miyahara046
72+
*/
73+
74+
#include "Pid.h"
75+
PidGain::PidGain(double _kp, double _ki, double _kd)
76+
// pidゲインが負の値にならないようにする
77+
: kp(_kp < 0 ? 0 : _kp), ki(_ki < 0 ? 0 : _ki), kd(_kd < 0 ? 0 : _kd)
78+
{
79+
}
80+
81+
Pid::Pid(double _kp, double _ki, double _kd, double _targetValue, double _maxIntegral,
82+
double _minIntegral)
83+
: pidGain(_kp, _ki, _kd),
84+
prevDeviation(0.0),
85+
integral(0.0),
86+
targetValue(_targetValue),
87+
maxIntegral(_maxIntegral),
88+
minIntegral(_minIntegral)
89+
{
90+
}
91+
92+
void Pid::setPidGain(double _kp, double _ki, double _kd)
93+
{
94+
// pidゲインが負の値にならないようにする
95+
pidGain.kp = _kp < 0 ? 0 : _kp;
96+
pidGain.ki = _ki < 0 ? 0 : _ki;
97+
pidGain.kd = _kd < 0 ? 0 : _kd;
98+
}
99+
100+
double Pid::calculatePid(double currentValue, double delta)
101+
{
102+
// 0除算を避けるために0の場合はデフォルト周期0.01とする
103+
if(delta == 0) delta = 0.01;
104+
105+
// 現在の目標値との偏差を求める
106+
double currentDeviation = targetValue - currentValue;
107+
// 積分の処理を行う
108+
integral += (currentDeviation + prevDeviation) * delta / 2;
109+
// 累積する積分値の大きさ制限
110+
if(integral > maxIntegral)
111+
integral = maxIntegral;
112+
else if(integral < minIntegral)
113+
integral = minIntegral;
114+
// 微分の処理を行う
115+
double currentDerivative = (currentDeviation - prevDeviation) / delta;
116+
/**
117+
* 微分項にローパスフィルタを適用
118+
* 偏差が大きい際に過大な変化量を一気に与えず
119+
* 滑らかな変化にし、機体の暴走を防ぐため
120+
*/
121+
filteredDerivative = alpha * currentDerivative + (1.0 - alpha) * filteredDerivative;
122+
123+
// 前回の偏差を更新する
124+
prevDeviation = currentDeviation;
125+
126+
// P制御の計算を行う
127+
double p = pidGain.kp * currentDeviation;
128+
// I制御の計算を行う
129+
double i = pidGain.ki * integral;
130+
// D制御の計算を行う
131+
double d = pidGain.kd * filteredDerivative;
132+
66133
// 操作量 = P制御 + I制御 + D制御
67134
return (p + i + d);
68135
}

modules/Pid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Pid {
5959
double targetValue; // 目標値
6060
double maxIntegral = 100; // 累積積分値の最大値
6161
double minIntegral = -100; // 累積積分値の最小値
62+
double alpha = 0.8; // ローパスフィルタの係数
6263
};
6364

6465
#endif

tests/PidTest.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace etrobocon2025_test {
166166
* prePID = 0 + 0 + 1.0 * 800 = 800
167167
*/
168168
double expected = 800.0;
169-
EXPECT_EQ(expected, pid.calculatePid(currentValu));
169+
EXPECT_DOUBLE_EQ(expected, pid.calculatePid(currentValu));
170170
}
171171

172172
// 同じ入力を連続して与えたときに微分出力の絶対値が減少することをテスト
@@ -215,8 +215,8 @@ namespace etrobocon2025_test {
215215
double expectedFirst = -240;
216216
double expectedSecond = -48;
217217

218-
EXPECT_EQ(derivationFirst, expectedFirst);
219-
EXPECT_EQ(derivationSecond, expectedSecond);
218+
EXPECT_DOUBLE_EQ(derivationFirst, expectedFirst);
219+
EXPECT_DOUBLE_EQ(derivationSecond, expectedSecond);
220220
}
221221

222222
// 偏差が0の状態が続くと出力も0を維持するかをテスト
@@ -225,7 +225,7 @@ namespace etrobocon2025_test {
225225
Pid pid(1.0, 1.0, 1.0, 50, 100.0, -100.0);
226226
double expected = 0.0;
227227
for(int i = 0; i < 5; ++i) {
228-
EXPECT_EQ(expected, pid.calculatePid(50));
228+
EXPECT_DOUBLE_EQ(expected, pid.calculatePid(50));
229229
}
230230
}
231231

@@ -257,8 +257,8 @@ namespace etrobocon2025_test {
257257
*/
258258
double expectedFirst = 0.5;
259259
double expectedSecond = 1.5;
260-
EXPECT_EQ(expectedFirst, first);
261-
EXPECT_EQ(expectedSecond, second);
260+
EXPECT_DOUBLE_EQ(expectedFirst, first);
261+
EXPECT_DOUBLE_EQ(expectedSecond, second);
262262
}
263263

264264
// 積分項がmaxIntegralで正しく制限されているかをテスト
@@ -270,7 +270,7 @@ namespace etrobocon2025_test {
270270
}
271271
double output = pid.calculatePid(0);
272272
double expected = 50.0;
273-
EXPECT_EQ(output, expected);
273+
EXPECT_DOUBLE_EQ(output, expected);
274274
}
275275

276276
// 積分項がminIntegralで正しく制限されているかをテスト
@@ -282,7 +282,7 @@ namespace etrobocon2025_test {
282282
}
283283
double output = pid.calculatePid(0);
284284
double expected = -50.0;
285-
EXPECT_EQ(output, expected);
285+
EXPECT_DOUBLE_EQ(output, expected);
286286
}
287287

288288
} // namespace etrobocon2025_test

0 commit comments

Comments
 (0)