Skip to content

Commit 46a13fa

Browse files
committed
Squashed commit of the following:
commit 51df4ac Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Tue May 20 12:02:15 2025 +0900 refactor:alphaを定数定義化、可読性の向上 commit 57eace8 Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Mon May 19 12:28:07 2025 +0900 refactor:Pid.cpp,Pid.h,PidTest.cppのコメントの改善、可読性の向上 commit 404752c Merge: 2f42676 219f638 Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Mon May 19 11:59:03 2025 +0900 Merge branch 'ticket-KL25-20' into work-KL25-20 commit 2f42676 Author: miyahara046 <taka.rocket.nba.1104@gmail.com> Date: Sun May 18 01:48:13 2025 +0900 update:ゲインが0の時のテストの追加、誤字の修正 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 84f4ecd commit 46a13fa

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

modules/calculators/Pid.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "Pid.h"
8+
89
PidGain::PidGain(double _kp, double _ki, double _kd)
910
// pidゲインが負の値にならないようにする
1011
: kp(_kp < 0 ? 0 : _kp), ki(_ki < 0 ? 0 : _ki), kd(_kd < 0 ? 0 : _kd)
@@ -38,7 +39,7 @@ double Pid::calculatePid(double currentValue, double delta)
3839
// 現在の目標値との偏差を求める
3940
double currentDeviation = targetValue - currentValue;
4041
// 積分の処理を行う
41-
integral += (currentDeviation + prevDeviation) * delta / 2;
42+
integral += (currentDeviation + prevDeviation) * delta / 2.0;
4243
// 累積する積分値の大きさ制限
4344
if(integral > maxIntegral) {
4445
integral = maxIntegral;
@@ -52,7 +53,7 @@ double Pid::calculatePid(double currentValue, double delta)
5253
* 偏差が大きい際に過大な変化量を一気に与えず
5354
* 滑らかな変化にし、機体の暴走を防ぐため
5455
*/
55-
filteredDerivative = alpha * currentDerivative + (1 - alpha) * filteredDerivative;
56+
filteredDerivative = alpha * currentDerivative + (1.0 - alpha) * filteredDerivative;
5657

5758
// 前回の偏差を更新する
5859
prevDeviation = currentDeviation;

modules/calculators/Pid.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class Pid {
5353

5454
private:
5555
PidGain pidGain;
56-
double prevDeviation; // 前回の偏差
57-
double integral; // 偏差の累積
58-
double filteredDerivative = 0.0; // フィルタされた微分項を保持する変数
59-
double targetValue; // 目標値
60-
double maxIntegral = 100.0; // 累積積分値の最大値
61-
double minIntegral = -100.0; // 累積積分値の最小値
62-
double alpha = 0.8; // ローパスフィルタの係数
56+
double prevDeviation; // 前回の偏差
57+
double integral; // 偏差の累積
58+
double filteredDerivative = 0.0; // フィルタされた微分項を保持する変数
59+
double targetValue; // 目標値
60+
double maxIntegral = 100.0; // 累積積分値の最大値
61+
double minIntegral = -100.0; // 累積積分値の最小値
62+
static constexpr double alpha = 0.8; // ローパスフィルタの係数
6363
};
6464

6565
#endif // PID_H

tests/PidTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Pid.h"
88
#include "gtest/gtest.h"
9+
910
namespace etrobocon2025_test {
1011

1112
// PidGainの正のゲインがそのまま格納されるかをテスト

0 commit comments

Comments
 (0)