-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.py
239 lines (195 loc) · 8.44 KB
/
move.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import math
from datetime import datetime
from vision import Vision
MAX_VX = 2000
MAX_AX = 3000
MAX_VW = 5
MAX_AW = 5
ACC = 0 #¼ÓËÙ
DEC = 1 #¼õËÙ
FOWWARD = 0
BACKWORD = 1
class Move:
def __init__(self, vision: Vision, waypoint: list, vx, vw) -> None:
self.direction = FOWWARD
self.vision = vision
self.my_robot = self.vision.my_robot
self.cur_x = self.my_robot.x
self.cur_y = self.my_robot.y
self.orientation = self.my_robot.orientation
self.cur_vx = vx
self.cur_vw = vw
self.waypoint = waypoint
self.num_waypoint = len(waypoint)
self.cur_waypoint = 0
self.cur_point_angle = self._turn_angle(self.cur_waypoint)
self.next_point_angle = self._turn_angle(self.cur_waypoint+1)
self.cur_point_speed = self._turn_speed(self.cur_point_angle)
self.next_point_speed = self._turn_speed(self.next_point_angle)
self.is_angle_update = False
self.k1 = 2
self.k21 = 3
self.k22 = 0.5
self.cur_time = datetime.now()
self.cur_status = ACC
def move_plan(self) -> tuple:
flag = 0
self.cur_x = self.my_robot.x
self.cur_y = self.my_robot.y
next_distance = self._point_distance(self.cur_x, self.cur_y,
self.waypoint[self.cur_waypoint + 1][0],
self.waypoint[self.cur_waypoint + 1][1])
pre_distance = self._point_distance(self.cur_x, self.cur_y,
self.waypoint[self.cur_waypoint][0],
self.waypoint[self.cur_waypoint][1])
if next_distance < 20:
self.cur_waypoint += 1
if self.cur_waypoint == self.num_waypoint - 1:
vx = vw = 0
flag = 1
return vx, vw, flag
else:
self.cur_point_angle = self._turn_angle(self.cur_waypoint)
self.next_point_angle = self._turn_angle(self.cur_waypoint+1)
self.cur_point_speed = self._turn_speed(self.cur_point_angle)
self.next_point_speed = self._turn_speed(self.next_point_angle)
self.cur_status = ACC
next_distance = self._point_distance(self.cur_x, self.cur_y,
self.waypoint[self.cur_waypoint + 1][0],
self.waypoint[self.cur_waypoint + 1][1])
pre_distance = self._point_distance(self.cur_x, self.cur_y,
self.waypoint[self.cur_waypoint][0],
self.waypoint[self.cur_waypoint][1])
# way_distance = self._point_distance(self.waypoint[self.cur_waypoint][0],
# self.waypoint[self.cur_waypoint][1],
# self.waypoint[self.cur_waypoint + 1][0],
# self.waypoint[self.cur_waypoint + 1][1])
self.orientation = self.my_robot.orientation
if self.direction == BACKWORD:
self.orientation = self.orientation + math.pi
if self.orientation > math.pi:
self.orientation = self.orientation -2 * math.pi
theta = self.orientation
beta = self._point_arccos(self.cur_x, self.cur_y,
self.waypoint[self.cur_waypoint+1][0],
self.waypoint[self.cur_waypoint+1][1])
if self.orientation < 0:
theta = 2 * math.pi + self.orientation
alpha = beta - theta
if alpha < - math.pi:
alpha = 2 * math.pi + alpha
elif alpha > math.pi:
alpha = alpha - 2 * math.pi
if alpha > math.pi / 2:
alpha = alpha - math.pi
self.direction = 1 - self.direction
elif alpha < - math.pi / 2:
alpha = alpha + math.pi
self.direction = 1 - self.direction
delta_t = (datetime.now() - self.cur_time).microseconds * 0.000001
self.cur_time = datetime.now()
# print(self.cur_vx)
# print(pre_distance,next_distance)
# print(self.cur_point_angle, self.next_point_angle)
# vx¹æ»®
if self.cur_status == ACC:
if abs(self.cur_vx) - self.next_point_speed >= self.k1 * next_distance:
self.cur_status = DEC
vx = self.next_point_speed + self.k1 * next_distance
else:
vx = self.cur_point_speed + self.k1 * pre_distance
elif self.cur_status == DEC:
vx = self.next_point_speed + self.k1 * next_distance
# print(self.cur_status)
# print('------------------------')
# ÏÞÖÆvx·¶Î§
vx = self.limit_vx_for_alpha(vx, alpha)
if self.direction == BACKWORD:
vx = -1 * vx
vx_min,vx_max = self._vx_space(delta_t)
if vx < vx_min:
vx = vx_min
elif vx > vx_max:
vx = vx_max
self.cur_vx = vx
# vw¹æ»®
if alpha < math.pi/2 and alpha > -math.pi/2:
vw = self.k21 * alpha
elif alpha > math.pi/2:
vw = self.k21 * math.pi/2 + self.k22 * (alpha - math.pi/2)
elif alpha < -math.pi/2:
vw = -self.k21 * math.pi/2 + self.k22 * (-alpha + math.pi/2)
vw_min,vw_max = self._vw_space(delta_t)
if vw < vw_min:
vw = vw_min
elif vw > vw_max:
vw = vw_max
self.cur_vw = vw
# print(self.cur_waypoint,self.next_point_angle,self.next_point_speed)
return vx, vw, flag
def _point_distance(self, x1: float, y1: float, x2: float, y2: float) -> float:
return pow(pow(x2 - x1, 2) + pow(y2 - y1, 2), 0.5)
def _point_arccos(self, x1: float, y1: float, x2: float, y2: float) -> float:
if y2 - y1 >= 0:
beta = math.acos((x2-x1) / self._point_distance(x1, y1, x2, y2))
else:
beta = 2 * math.pi - \
math.acos((x2-x1) / self._point_distance(x1, y1, x2, y2))
return beta
def _turn_speed(self, alpha: float) -> float:
degree = abs(alpha / (2 * math.pi) * 360)
# if degree < 30:
# turn_speed = 14/9 * pow((degree - 30), 2) + 600
# elif degree < 85:
# turn_speed = -10 * degree + 900
if degree < 70:
turn_speed = -10 * degree + 750
else:
turn_speed = 50
return turn_speed
def _vx_space(self, delta_t: float) -> tuple:
vx_max = min(MAX_VX, self.cur_vx+delta_t*MAX_AX)
vx_min = max(-MAX_VX, self.cur_vx-delta_t*MAX_AX)
return vx_min, vx_max
def _vw_space(self, delta_t: float) -> tuple:
vw_max = min(MAX_VW, self.cur_vw+delta_t*MAX_AW)
vw_min = max(-MAX_VW, self.cur_vw-delta_t*MAX_AW)
return vw_min, vw_max
def get_speed(self) -> tuple:
return self.cur_vx, self.cur_vw
def get_location(self) -> int:
return self.cur_waypoint
def _turn_angle(self, seq: int) -> float:
if seq == self.num_waypoint -1:
angle = 180
return angle
if seq == 0:
pre_angle = self.orientation
if pre_angle < 0:
pre_angle = 2 * math.pi + pre_angle
else:
pre_angle = self._point_arccos(self.waypoint[seq-1][0],
self.waypoint[seq-1][1],
self.waypoint[seq][0],
self.waypoint[seq][1])
next_angle = self._point_arccos(self.waypoint[seq][0],
self.waypoint[seq][1],
self.waypoint[seq+1][0],
self.waypoint[seq+1][1])
angle = next_angle-pre_angle
if angle < - math.pi:
alpha = 2 * math.pi + angle
elif angle > math.pi:
alpha = angle - 2 * math.pi
return angle
def limit_vx_for_alpha(self, vx0:float, alpha: float) -> float:
degree = abs(alpha / (2 * math.pi) * 360)
if degree >= 70:
vx = 0
elif degree >= 30:
vx = vx0 / 3
elif degree >= 10:
vx = vx0 / 2
else:
vx = vx0
return vx