etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
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,
20  bool save)
21  : left(left_motor_sl), right(right_motor_sl)
22 {
23  //メンバ変数の初期化 基本的に0
24  between_wheels = 12.7;
25  moving_distance_mean = 0;
26  turning_angle = 0;
27  current_x = current_y = current_angle = 0;
28  errno = 0;
29  isSave = save;
30 }
31 
32 void SelfLocalization::update(std::int32_t left_motor_sl, std::int32_t right_motor_sl)
33 {
34  //左車輪の回転角
35  left.update(left_motor_sl);
36 
37  //右車輪の回転角
38  right.update(right_motor_sl);
39 
40  //移動距離と、車体の回転角
41  moving_distance_mean = (right.moving_distance + left.moving_distance) / 2.0;
42  turning_angle = (right.moving_distance - left.moving_distance) / between_wheels;
43 
44  //座標
45  current_x += (moving_distance_mean * std::cos(current_angle + (turning_angle / 2.0)));
46  current_y += (moving_distance_mean * std::sin(current_angle + (turning_angle / 2.0)));
47  current_angle += turning_angle;
48 
49  //保存
50  if(isSave == true) {
52  }
53 }
54 
56 {
57  return current_x;
58 }
59 
61 {
62  return current_y;
63 }
64 
66 {
67  fprintf(fp, "%f %f\n", current_x, current_y);
68 
69  return;
70 }
71 
72 bool SelfLocalization::approached_target_coordinates(float target_x, float target_y,
73  float target_radius)
74 {
75  float distance = std::sqrt((target_x - current_x) * (target_x - current_x)
76  + (target_y - current_y) * (target_y - current_y));
77  if(distance < target_radius) return true;
78  return false;
79 }
80 
81 //高校の時の数学で出た点と直線の距離の公式
82 //点(x0, y0)と直線ax + by + c = 0の距離dは、d = |a*x0 + by0 + c| / (a^2 + b^2)^(1/2)
83 //式の整理は自分で計算したやつ
84 float SelfLocalization::calculate_between_ev3_and_border(float _start_x, float _start_y,
85  float _goal_x, float _goal_y,
86  float _current_x, float _current_y)
87 {
88  float a, b, c;
89 
90  a = _goal_y - _start_y;
91  b = -(_goal_x - _start_x);
92  c = -b * _start_y - a * _start_x;
93 
94  return (a * _current_x + b * _current_y + c) / std::sqrt(a * a + b * b);
95 }
96 
98 {
99  current_angle_degree = int(current_angle * 180 / 3.14);
100 }
101 
103 {
104  return target_x < current_x;
105 }
107 {
108  return target_y < current_y;
109 }
110 
112 {
113  return target_x > current_x;
114 }
116 {
117  return target_y > current_y;
118 }
119 
120 //スタートとゴールの登録、現在の機体位置のyがゴールの法線の下か否かの登録
121 void SelfLocalization::init_normal_vector(float _start_x, float _start_y, float _goal_x,
122  float _goal_y, float _current_x, float _current_y)
123 {
124  start_x = _start_x;
125  start_y = _start_y;
126  goal_x = _goal_x;
127  goal_y = _goal_y;
128 
129  is_below_normal_vector = (start_y < goal_y);
130 }
131 
132 //指定した二点 (start, goal)を結んだ直線の点goalにおける法線 (normal vector)
133 bool SelfLocalization::is_over_normal_vector(float _current_x, float _current_y)
134 {
135  float k = (start_x - goal_x) / (goal_y - start_y);
136  float border_y = k * _current_x + goal_y - k * goal_x;
137 
138  if(is_below_normal_vector) { // init時に機体が法線より下の場合
139  return _current_y >= border_y;
140  } else { // init時に機体が法線以上にある場合
141  return _current_y < border_y;
142  }
143 }
144 
146 {
147  fclose(fp);
148 }
void init_normal_vector(float _start_x, float _start_y, float _goal_x, float _goal_y, float _current_x, float _current_y)
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)
void update(std::int32_t left_motor, std::int32_t right_motor)
左右モータ角度の過去値を現在値へ更新する
Definition: MotorAngle.cpp:14
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)