etrobocon2019 feat.KatLab  ece30a9a007fff7d3ad48592c0d09a74643377bb
Pid.cpp
[詳解]
1 
6 #include "Pid.h"
7 
8 PidGain::PidGain(double Kp_, double Ki_, double Kd_) : Kp(Kp_), Ki(Ki_), Kd(Kd_) {}
9 
10 Pid::Pid(double target_, double Kp_, double Ki_, double Kd_)
11  : target(target_), gain(Kp_, Ki_, Kd_), integral(0.0f), preError(0.0f)
12 {
13 }
14 
15 void PidGain::setPidGain(double Kp_, double Ki_, double Kd_)
16 {
17  Kp = Kp_;
18  Ki = Ki_;
19  Kd = Kd_;
20 }
21 
28 double Pid::control(double value, double delta)
29 {
30  // 目標値と現在値との偏差を求める
31  double error = target - value;
32  // 偏差の積分処理
33  integral += error * delta;
34  // 偏差の微分処理
35  double diff = (error - preError) / delta;
36 
37  // 前回偏差の更新
38  preError = error;
39 
40  // P制御の計算(Pゲイン * 偏差)
41  double p = gain.Kp * error;
42  // I制御の計算(Iゲイン * 偏差の積分値)
43  double i = gain.Ki * integral;
44  // D制御の計算(Dゲイン * 偏差の微分値)
45  double d = gain.Kd * diff;
46 
47  return limit(p + i + d);
48 }
49 
58 const double Pid::setParameter(double target_, double Kp_, double Ki_, double Kd_)
59 {
60  target = target_;
61  setPidGain(Kp_, Ki_, Kd_);
62 
63  return target;
64 }
65 
73 const PidGain& Pid::setPidGain(double Kp_, double Ki_, double Kd_)
74 {
75  gain.setPidGain(Kp_, Ki_, Kd_);
76  return gain;
77 }
78 
84 double Pid::limit(double value)
85 {
86  if(value > 100.0) return 100.0;
87  if(value < -100.0) return -100.0;
88 
89  return value;
90 }
const double setParameter(double target_, double Kp_, double Ki_=0.0, double Kd_=0.0)
目標値とPIDゲインの設定をする関数
Definition: Pid.cpp:58
double Kp
Definition: Pid.h:11
double Kd
Definition: Pid.h:13
double Ki
Definition: Pid.h:12
void setPidGain(double Kp_, double Ki_, double Kd_)
Definition: Pid.cpp:15
double limit(double value)
Definition: Pid.cpp:84
PID制御クラス
PidGain(double Kp_, double Ki_, double Kd_)
Definition: Pid.cpp:8
double control(double value, double delta=0.004)
Definition: Pid.cpp:28
Pid(double target_, double Kp_, double Ki_=0.0, double Kd_=0.0)
Definition: Pid.cpp:10
const PidGain & setPidGain(double Kp_, double Ki_=0.0, double Kd_=0.0)
PIDゲインの設定をする関数
Definition: Pid.cpp:73
Definition: Pid.h:10