Skip to content

Commit 80f3908

Browse files
committed
Merge branch 'work-KL25-20' into ticket-KL25-20
2 parents 46a13fa + 2171a44 commit 80f3908

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

modules/calculators/Pid.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ PidGain::PidGain(double _kp, double _ki, double _kd)
1515
Pid::Pid(double _kp, double _ki, double _kd, double _targetValue, double _maxIntegral,
1616
double _minIntegral)
1717
: pidGain(_kp, _ki, _kd),
18-
prevDeviation(0.0),
19-
integral(0.0),
2018
targetValue(_targetValue),
2119
maxIntegral(_maxIntegral),
2220
minIntegral(_minIntegral)
2321
{
2422
}
2523

24+
Pid::Pid(double _kp, double _ki, double _kd, double _targetValue)
25+
: Pid(_kp, _ki, _kd, _targetValue, 100.0, -100.0)
26+
{
27+
}
28+
2629
void Pid::setPidGain(double _kp, double _ki, double _kd)
2730
{
2831
// pidゲインが負の値にならないようにする

modules/calculators/Pid.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ class Pid {
3535
Pid(double _kp, double _ki, double _kd, double _targetValue, double _maxIntegral,
3636
double _minIntegral);
3737

38+
/** 積分値制限を設定しない場合のコンストラクタ
39+
* @param _kp Pゲイン
40+
* @param _ki Iゲイン
41+
* @param _kd Dゲイン
42+
* @param _targetValue 目標値
43+
*/
44+
Pid(double _kp, double _ki, double _kd, double _targetValue);
45+
3846
/**
3947
* @brief PIDゲインを設定する
4048
* @param _kp Pゲイン
@@ -53,8 +61,8 @@ class Pid {
5361

5462
private:
5563
PidGain pidGain;
56-
double prevDeviation; // 前回の偏差
57-
double integral; // 偏差の累積
64+
double prevDeviation = 0.0; // 前回の偏差
65+
double integral = 0.0; // 偏差の累積
5866
double filteredDerivative = 0.0; // フィルタされた微分項を保持する変数
5967
double targetValue; // 目標値
6068
double maxIntegral = 100.0; // 累積積分値の最大値

tests/PidTest.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace etrobocon2025_test {
2323
EXPECT_DOUBLE_EQ(expected_ki, actualPidGain.ki);
2424
EXPECT_DOUBLE_EQ(expected_kd, actualPidGain.kd);
2525
}
26+
2627
// PidGainの0のゲインがそのまま格納されるかをテスト
2728
TEST(PidGainTest, PidGainZero)
2829
{
@@ -306,7 +307,7 @@ namespace etrobocon2025_test {
306307
// 積分項がmaxIntegralで正しく制限されているかをテスト
307308
TEST(PidTest, CalculatePidIntegralUpperBound)
308309
{
309-
Pid pid(0.0, 1.0, 0.0, 100, 50, -100.0); // maxIntegral = 50
310+
Pid pid(0.0, 1.0, 0.0, 100, 50, -100.0); // maxIntegral = 50.0
310311
double currentValue = 0.0;
311312
for(int i = 0; i < 500; ++i) { // 5秒間の累積
312313
pid.calculatePid(currentValue);
@@ -319,7 +320,7 @@ namespace etrobocon2025_test {
319320
// 積分項がminIntegralで正しく制限されているかをテスト
320321
TEST(PidTest, CalculatePidIntegralLowerBound)
321322
{
322-
Pid pid(0.0, 1.0, 0.0, -100, 100.0, -50.0); // minIntegral = -50
323+
Pid pid(0.0, 1.0, 0.0, -100, 100.0, -50.0); // minIntegral = -50.0
323324
double currentValue = 0.0;
324325
for(int i = 0; i < 500; ++i) { // 5秒間の累積
325326
pid.calculatePid(currentValue);
@@ -329,4 +330,30 @@ namespace etrobocon2025_test {
329330
EXPECT_DOUBLE_EQ(output, expected);
330331
}
331332

333+
// 積分項の制限をしない時積分項がmaxIntegral = 100.0で正しく制限されているかをテスト
334+
TEST(PidTest, CalculatePidNotSetIntegralUpperBound)
335+
{
336+
Pid pid(0.0, 1.0, 0.0, 100); // maxIntegral = 100.0
337+
double currentValue = 0.0;
338+
for(int i = 0; i < 500; ++i) { // 5秒間の累積
339+
pid.calculatePid(currentValue);
340+
}
341+
double output = pid.calculatePid(currentValue);
342+
double expected = 100.0;
343+
EXPECT_DOUBLE_EQ(output, expected);
344+
}
345+
346+
// 積分項の制限をしない時積分項がminIntegral = -100.0で正しく制限されているかをテスト
347+
TEST(PidTest, CalculatePidNotSetIntegralLowerBound)
348+
{
349+
Pid pid(0.0, 1.0, 0.0, -100); // minIntegral = -100.0
350+
double currentValue = 0.0;
351+
for(int i = 0; i < 500; ++i) { // 5秒間の累積
352+
pid.calculatePid(currentValue);
353+
}
354+
double output = pid.calculatePid(currentValue);
355+
double expected = -100.0;
356+
EXPECT_DOUBLE_EQ(output, expected);
357+
}
358+
332359
} // namespace etrobocon2025_test

0 commit comments

Comments
 (0)