ETrobocon2017 - 片山研究所モデルベース開発推進事業部
SpeedControl.cpp
[詳解]
1 
7 #include "SpeedControl.h"
8 
10  //pid(0.36, 1.2, 0.027, 30.0), colorSensor( PORT_3 ){
11  Pid(0.2, 0.5, 0.0, 30.0), forward(0), preAngleL(0), preAngleR(0){
12  for(int i=0; i<25; i++)speed_value[i] = 0;
13  speedCount = 0;
14  speed_value_all = 0;
15  pid_value_old = 0.0;
16  distance4ms = 0.0;
17  //150のときいい感じ pid(0.8, 1.2, 0.0, 30.0), forward(30){
18 }
19 
20 std::int32_t SpeedControl::calculateSpeedForPid(std::int32_t curAngleL, std::int32_t curAngleR) {
21  int8_t speed_value_thistime = calcDistance4ms(curAngleL, curAngleR);
22  speed_value_all += (speed_value_thistime - speed_value[ speedCount ]);
23  speed_value[ speedCount ] = speed_value_thistime;
24  calculate((double)speed_value_all);
25  double pid_value = - get_output();
26  speedCount++;
27  if( speedCount >= 25 ) speedCount = 0;
28  forward += (pid_value - pid_value_old) / 10;
29  pid_value_old = pid_value;
30  return (int)limitOutput(forward);
31 }
32 
33 /* 距離更新(4ms間の移動距離を毎回加算している) */
34 std::int8_t SpeedControl::calcDistance4ms(std::int32_t curAngleL, std::int32_t curAngleR){
35 
36  float distance4msFloat = 0.0; //4msの距離
37 
38  // 4ms間の走行距離 = ((円周率 * タイヤの直径) / 360) * (モータ角度過去値 - モータ角度現在値)
39  float distance4msL = ((2 * 3.14 * 81) / 360.0) * (float)(curAngleL - preAngleL); // 4ms間の左モータ距離
40  float distance4msR = ((2 * 3.14 * 81) / 360.0) * (float)(curAngleR - preAngleR); // 4ms間の右モータ距離
41  distance4msFloat = (distance4msL + distance4msR) / 2.0; //左右タイヤの走行距離を足して割る
42 
43  //モータの回転角度の過去値を更新
44  preAngleL = curAngleL;
45  preAngleR = curAngleR;
46 
47  distance4ms = (int)distance4msFloat;
48  // 単位はmm
49  return distance4ms;
50 }
51 
53  return speed_value_all;
54 }
double get_output()
Definition: Pid.cpp:33
void calculate(double light_value)
Definition: Pid.cpp:21
std::int8_t calcDistance4ms(std::int32_t curAngleL, std::int32_t curAngleR)
std::int32_t calculateSpeedForPid(std::int32_t curAngleL, std::int32_t curAngleR)
std::int16_t speed_value_all
Definition: SpeedControl.h:31
double limitOutput(double pid_value)
Definition: Pid.cpp:47
PID制御の計算を行うクラス
Definition: Pid.h:12
std::int16_t getSpeed100ms()
PID制御による速度制御クラス