ETrobocon2017 - 片山研究所モデルベース開発推進事業部
example_SelfLocalization.cpp
[詳解]
1 
10 #include <iostream>
11 #include <random>
12 #include <unistd.h>
14 #include "LineTracerWalker.h"
15 #include "SelfLocalization.h"
16 
17 // グローバル変数
20 
21 // プロトタイプ宣言
22 void calcVirtualWheels();
23 
24 int main(){
26 
28  SelfLocalization sl(0, 0, true);
29  LineTracerWalker lineTracer;
30 
38  float target_x = 500.0;
39  float target_y = 500.0;
40  float start_x = 0.0;
41  float start_y = 0.0;
42  lineTracer.speedControl.setPid(2.0, 2.0, 0.024, 30.0);
43  lineTracer.turnControl.setPid(4.0, 0.0, 0.0, 0.0);
44  lineTracer.isLeftsideLine(true);
45  sl.update(0, 0);
46  target = sl.calculate_between_ev3_and_border(0.0, 0.0, target_x, target_y, 0.0, 0.0);
47  // 注)4msごとに実行されると仮定して2500回(10秒)回してます
48  std::cout << "スピード(100ms), forword値, Target値, turn値, 前進値" << std::endl;
49  while(sl.is_below_target_line_of_x(target_x) || sl.is_below_target_line_of_y(target_y)){
50  speed100ms = lineTracer.speedControl.getSpeed100ms();
56  target = sl.calculate_between_ev3_and_border(0.0, 0.0, target_x, target_y, sl.getPointX(), sl.getPointY());
57  // 結果を画面に出力
58  //std::cout << "スピード(100ms) " << speed100ms << ", forword値 " << forword << ",Target値" << target << ", turn値 " << turn << ", 前進値" << (right_motor + left_motor) / 2 << std::endl;
59  std::cout << speed100ms << "," << forword << "," << target << "," << turn << "," << (right_motor + left_motor) / 2 << std::endl;
60  }
61  target_x = 1000.0;
62  target_y = 750.0;
63  start_x = sl.getPointX();
64  start_y = sl.getPointY();
65  target = sl.calculate_between_ev3_and_border(start_x, start_y, target_x, target_y, sl.getPointX(), sl.getPointY());
66  while(sl.is_below_target_line_of_x(target_x) || sl.is_below_target_line_of_y(target_y)){
67  speed100ms = lineTracer.speedControl.getSpeed100ms();
73  target = sl.calculate_between_ev3_and_border(start_x, start_y, target_x, target_y, sl.getPointX(), sl.getPointY());
74  // 結果を画面に出力
75  //std::cout << "スピード(100ms) " << speed100ms << ", forword値 " << forword << ",Target値" << target << ", turn値 " << turn << ", 前進値" << (right_motor + left_motor) / 2 << std::endl;
76  std::cout << speed100ms << "," << forword << "," << target << "," << turn << "," << (right_motor + left_motor) / 2 << std::endl;
77  }
78  target_x = 0.0;
79  target_y = 0.0;
80  start_x = sl.getPointX();
81  start_y = sl.getPointY();
82  target = sl.calculate_between_ev3_and_border(start_x, start_y, target_x, target_y, sl.getPointX(), sl.getPointY());
83  while(sl.is_over_target_line_of_x(target_x) || sl.is_over_target_line_of_y(target_y)){
84  speed100ms = lineTracer.speedControl.getSpeed100ms();
90  target = sl.calculate_between_ev3_and_border(start_x, start_y, target_x, target_y, sl.getPointX(), sl.getPointY());
91  // 結果を画面に出力
92  //std::cout << "スピード(100ms) " << speed100ms << ", forword値 " << forword << ",Target値" << target << ", turn値 " << turn << ", 前進値" << (right_motor + left_motor) / 2 << std::endl;
93  std::cout << speed100ms << "," << forword << "," << target << "," << turn << "," << (right_motor + left_motor) / 2 << std::endl;
94  }
95  return 0;
96 }
97 
98 // 仮想的なモータエンコーダです。
100  // モータの前進にランダム性を持たせる
101  std::random_device rnd;
102  std::mt19937 mt(rnd());
103  std::uniform_int_distribution<int> rand50(0,50); // 1~10の一様乱数
104  double r_forword = rand50(mt) * 0.1;
105 
106  // 実際に進んだ距離とする
107  left_motor += (speed100ms/25 - turn + forword + r_forword) * 0.1;
108  right_motor += (speed100ms/25 + turn + forword + r_forword) * 0.1;
109 }
void isLeftsideLine(bool b)
void calcVirtualWheels()
std::int32_t calculateSpeedForPid(std::int32_t curAngleL, std::int32_t curAngleR)
std::int8_t calculateTurnForPid(std::int8_t forward, std::int8_t light_value)
Definition: TurnControl.cpp:14
void setPid(double _p_gain, double _i_gain, double _d_gain, double _target)
Definition: Pid.cpp:40
void update(std::int32_t left_motor_sl, std::int32_t right_motor_sl)
bool is_below_target_line_of_x(float target_x)
float calculate_between_ev3_and_border(float _start_x, float _start_y, float _goal_x, float _goal_y, float _current_x, float _current_y)
bool is_below_target_line_of_y(float target_y)
TurnControl turnControl
std::int16_t getSpeed100ms()
bool is_over_target_line_of_x(float target_x)
bool is_over_target_line_of_y(float target_y)
SpeedControl speedControl