etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
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),
12  forward(0),
13  preAngleL(0),
14  preAngleR(0)
15 {
16  for(int i = 0; i < 25; i++) speed_value[i] = 0;
17  speedCount = 0;
18  speed_value_all = 0;
19  pid_value_old = 0.0;
20  distance4ms = 0.0;
21  // 150のときいい感じ pid(0.8, 1.2, 0.0, 30.0), forward(30){
22 }
23 
24 std::int32_t SpeedControl::calculateSpeedForPid(std::int32_t curAngleL, std::int32_t curAngleR)
25 {
26  int8_t speed_value_thistime = calcDistance4ms(curAngleL, curAngleR);
27  speed_value_all += (speed_value_thistime - speed_value[speedCount]);
28  speed_value[speedCount] = speed_value_thistime;
29  calculate((double)speed_value_all);
30  double pid_value = -get_output();
31  speedCount++;
32  if(speedCount >= 25) speedCount = 0;
33  forward += (pid_value - pid_value_old) / 10;
34  pid_value_old = pid_value;
35  return (int)limitOutput(forward);
36 }
37 
38 /* 距離更新(4ms間の移動距離を毎回加算している) */
39 std::int8_t SpeedControl::calcDistance4ms(std::int32_t curAngleL, std::int32_t curAngleR)
40 {
41  float distance4msFloat = 0.0; // 4msの距離
42 
43  // 4ms間の走行距離 = ((円周率 * タイヤの直径) / 360) * (モータ角度過去値 - モータ角度現在値)
44  float distance4msL
45  = ((2 * 3.14 * 81) / 360.0) * (float)(curAngleL - preAngleL); // 4ms間の左モータ距離
46  float distance4msR
47  = ((2 * 3.14 * 81) / 360.0) * (float)(curAngleR - preAngleR); // 4ms間の右モータ距離
48  distance4msFloat = (distance4msL + distance4msR) / 2.0; //左右タイヤの走行距離を足して割る
49 
50  //モータの回転角度の過去値を更新
51  preAngleL = curAngleL;
52  preAngleR = curAngleR;
53 
54  distance4ms = (int)distance4msFloat;
55  // 単位はmm
56  return distance4ms;
57 }
58 
60 {
61  return speed_value_all;
62 }
double get_output()
Definition: Pid.cpp:32
void calculate(double light_value)
Definition: Pid.cpp:19
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:46
PID制御の計算を行うクラス
Definition: Pid.h:38
std::int16_t getSpeed100ms()
PID制御による速度制御クラス