@@ -63,73 +63,6 @@ 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-
13366 // 操作量 = P制御 + I制御 + D制御
13467 return (p + i + d);
13568}
0 commit comments