ETrobocon2017 - 片山研究所モデルベース開発推進事業部
SelfLocalization.cpp
[詳解]
1 /*****************************
2  * self_localization.cpp
3  *
4  * Author Hiroki Tachiyama
5  * Katayama Laboratory
6  * Department of Computer Secience and Systems Engineering
7  * Faculty of Engineering, University of Miyazaki
8  *
9  * Created at: Mon Jul 10 3:34:25 JST 2017
10  *
11  * Coryrights (c) 2017 Katayama Laboratory
12  *
13  *****************************/
14 #include "SelfLocalization.h"
15 
16 FILE* SelfLocalization::fp = fopen("traveling_route.txt", "w");
17 bool SelfLocalization::isSave;
18 
19 SelfLocalization::SelfLocalization (std::int32_t left_motor_sl, std::int32_t right_motor_sl, bool save):
20  left(left_motor_sl), right(right_motor_sl){
21  //メンバ変数の初期化 基本的に0
22  between_wheels = 12.7;
23  moving_distance_mean = 0;
24  turning_angle = 0;
25  current_x = current_y = current_angle = 0;
26  errno = 0;
27  isSave = save;
28 }
29 
30 void SelfLocalization::update (std::int32_t left_motor_sl, std::int32_t right_motor_sl) {
31 
32  //左車輪の回転角
33  left.update(left_motor_sl);
34 
35  //右車輪の回転角
36  right.update(right_motor_sl);
37 
38  //移動距離と、車体の回転角
39  moving_distance_mean = (right.moving_distance + left.moving_distance) / 2.0;
40  turning_angle = (right.moving_distance - left.moving_distance) / between_wheels;
41 
42  //座標
43  current_x += (moving_distance_mean * std::cos(current_angle + (turning_angle/2.0)));
44  current_y += (moving_distance_mean * std::sin(current_angle + (turning_angle/2.0)));
45  current_angle += turning_angle;
46 
47  //保存
48  if(isSave == true){
50  }
51 }
52 
54  return current_x;
55 }
56 
58  return current_y;
59 }
60 
62 
63  fprintf(fp, "%f %f\n", current_x, current_y);
64 
65  return;
66 }
67 
68 bool SelfLocalization::approached_target_coordinates (float target_x, float target_y, float target_radius) {
69  float distance = std::sqrt(
70  (target_x - current_x) * (target_x - current_x) +
71  (target_y - current_y) * (target_y - current_y) );
72  if(distance < target_radius)
73  return true;
74  return false;
75 }
76 
77 //高校の時の数学で出た点と直線の距離の公式
78 //点(x0, y0)と直線ax + by + c = 0の距離dは、d = |a*x0 + by0 + c| / (a^2 + b^2)^(1/2)
79 //式の整理は自分で計算したやつ
81 (float _start_x, float _start_y, float _goal_x, float _goal_y, float _current_x, float _current_y) {
82  float a, b, c;
83 
84  a = _goal_y - _start_y;
85  b = -(_goal_x - _start_x);
86  c = -b * _start_y - a * _start_x;
87 
88  return (a*_current_x + b*_current_y + c) / std::sqrt(a*a + b*b);
89 }
90 
92  current_angle_degree = int(current_angle*180/3.14);
93 }
94 
96  return target_x < current_x;
97 }
99  return target_y < current_y;
100 }
101 
103  return target_x > current_x;
104 }
106  return target_y > current_y;
107 }
108 
109 
110 //スタートとゴールの登録、現在の機体位置のyがゴールの法線の下か否かの登録
112 (float _start_x, float _start_y, float _goal_x, float _goal_y, float _current_x, float _current_y) {
113  start_x = _start_x; start_y = _start_y;
114  goal_x = _goal_x; goal_y = _goal_y;
115 
116  is_below_normal_vector = (start_y < goal_y);
117 }
118 
119 //指定した二点 (start, goal)を結んだ直線の点goalにおける法線 (normal vector)
121 (float _current_x, float _current_y) {
122  float k = (start_x - goal_x) / (goal_y - start_y);
123  float border_y = k * _current_x + goal_y - k * goal_x;
124 
125  if (is_below_normal_vector) { //init時に機体が法線より下の場合
126  return _current_y >= border_y;
127  } else { //init時に機体が法線以上にある場合
128  return _current_y < border_y;
129  }
130 }
131 
133  fclose(fp);
134 }
void init_normal_vector(float _start_x, float _start_y, float _goal_x, float _goal_y, float _current_x, float _current_y)
void update(std::int32_t update_degree)
bool approached_target_coordinates(float target_x, float target_y, float target_radius)
void update(std::int32_t left_motor_sl, std::int32_t right_motor_sl)
bool is_below_target_line_of_x(float target_x)
void writing_current_coordinates()
float calculate_between_ev3_and_border(float _start_x, float _start_y, float _goal_x, float _goal_y, float _current_x, float _current_y)
float moving_distance
bool is_below_target_line_of_y(float target_y)
bool is_over_target_line_of_x(float target_x)
bool is_over_normal_vector(float _current_x, float _current_y)
bool is_over_target_line_of_y(float target_y)
SelfLocalization(std::int32_t left_motor_sl, std::int32_t right_motor_sl, bool save=true)