forked from micromouseonline/pyTurnProfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.py
256 lines (225 loc) · 9.33 KB
/
path.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import copy
import math
from enum import Enum
from PyQt5.QtCore import QRectF
from PyQt5.QtGui import QPen, QBrush
from PyQt5.QtCore import (Qt)
from PyQt5.QtWidgets import QGraphicsItem
from parameters import TurnParameters
from robot_state import RobotState
class ProfileType(Enum):
TRAPEZOID = 1
SINUSOID = 2
QUADRATIC = 3
CUBIC = 4
class Path(QGraphicsItem):
def __init__(self, parent=None):
super().__init__(parent)
self.path_points = []
self.turnRadius = 0.0
self.turnSpeed = 0.0
self.turnAngle = 0.0
self.start_angle = 0.0
self.turn_end = 0
self.min_x = 0
self.min_y = 0
self.max_x = 0
self.max_y = 0
def boundingRect(self):
return QRectF(-100.0, -100.0, 600.0, 600.0)
def path_count(self):
return len(self.path_points)
def get_state_at(self, index):
if index >= self.path_count():
index = self.path_count() - 1
if index < 0:
index = 0
return self.path_points[index]
def get_pose_at(self, index):
state = self.get_state_at(index)
return state.get_pose()
def get_max_acceleration(self):
max_acc = 0
for state in self.path_points[:self.turn_end]:
if state.acceleration > max_acc:
max_acc = state.acceleration
return max_acc
def get_max_alpha(self):
max_alpha = 0
for state in self.path_points[:self.turn_end]:
if state.alpha > max_alpha:
max_alpha = state.alpha
return max_alpha
def get_max_omega(self):
max_omega = 0
for state in self.path_points[:self.turn_end]:
if state.omega > max_omega:
max_omega = state.omega
return max_omega
def paint(self, painter, option, widget):
if len(self.path_points) == 0:
return
colors = [Qt.magenta, Qt.green, Qt.red, Qt.yellow, Qt.cyan, Qt.magenta]
pen = QPen(Qt.white)
pen.setWidthF(2.0)
painter.setPen(pen)
for i in range(0, len(self.path_points), 5):
state = self.path_points[i]
pen.setColor(colors[state.phase])
painter.setPen(pen)
rect = QRectF(state.x, state.y, 1.0, 1.0)
painter.drawEllipse(rect)
def calculate_trapezoid(self, params: TurnParameters, startx, starty, loop_interval):
self.path_points.clear()
state = RobotState()
state.set_interval(loop_interval)
state.x = startx
state.y = starty
state.speed = params.speed
state.theta = params.startAngle
self.path_points.append(copy.copy(state))
end_angle = params.startAngle + params.angle
arc_omega = math.degrees(params.speed / params.radius)
transition_angle = params.delta * arc_omega / (2.0 * params.speed)
arc_angle = params.angle - 2 * transition_angle
arc_length = params.speed * arc_angle / arc_omega
turn_distance = 2 * params.delta + arc_length
while state.theta <= end_angle - 0.01:
if state.theta < params.startAngle + transition_angle:
state.omega = arc_omega * state.distance / params.delta
state.phase = 1
elif state.theta <= (end_angle - transition_angle):
state.omega = arc_omega
state.phase = 2
else:
state.omega = arc_omega * (turn_distance - state.distance) / params.delta
state.phase = 3
state.update()
self.path_points.append(copy.copy(state))
self.turn_end = len(self.path_points)-1
self.calculate_leadout(state)
self.update()
def calculate_sinusoid(self, params: TurnParameters, startx, starty, loop_interval):
self.path_points.clear()
state = RobotState()
state.set_interval(loop_interval)
state.x = startx
state.y = starty
state.speed = params.speed
state.theta = params.startAngle
self.path_points.append(copy.copy(state))
end_angle = params.startAngle + params.angle
arc_omega = math.degrees(params.speed / params.radius)
transition_angle = params.delta * 2 * arc_omega / (math.pi * params.speed)
arc_angle = params.angle - 2 * transition_angle
arc_length = params.speed * arc_angle / arc_omega
turn_distance = 2 * params.delta + arc_length
while state.theta <= end_angle - 0.01:
if state.theta < params.startAngle + transition_angle:
t = state.distance / params.delta
state.omega = arc_omega * math.sin(math.pi / 2 * t)
state.phase = 1
elif state.theta <= (end_angle - transition_angle):
state.omega = arc_omega
state.phase = 2
else:
t = (turn_distance - state.distance) / params.delta
state.omega = arc_omega * math.sin(math.pi / 2 * t)
state.phase = 3
state.update()
self.path_points.append(copy.copy(state))
self.turn_end = len(self.path_points)-1
self.calculate_leadout(state)
self.update()
def calculate_quadratic(self, params: TurnParameters, startx, starty, loop_interval):
self.path_points.clear()
state = RobotState()
state.set_interval(loop_interval)
state.x = startx
state.y = starty
state.speed = params.speed
state.theta = params.startAngle
self.path_points.append(copy.copy(state))
end_angle = params.startAngle + params.angle
arc_omega = math.degrees(params.speed / params.radius)
transition_angle = params.delta * 2 * arc_omega / (3.0 * params.speed)
arc_angle = params.angle - 2 * transition_angle
arc_length = params.speed * arc_angle / arc_omega
turn_distance = 2 * params.delta + arc_length
while state.theta <= end_angle - 0.01:
if state.theta < params.startAngle + transition_angle:
t = state.distance / params.delta
state.omega = arc_omega * t * (2.0 - t)
state.phase = 1
elif state.theta <= (end_angle - transition_angle):
state.omega = arc_omega
state.phase = 2
else:
t = (turn_distance - state.distance) / params.delta
state.omega = arc_omega * t * (2.0 - t)
state.phase = 3
state.update()
self.path_points.append(copy.copy(state))
self.turn_end = len(self.path_points)-1
self.calculate_leadout(state)
self.update()
def calculate_cubic(self, params: TurnParameters, startx, starty, loop_interval):
self.path_points.clear()
state = RobotState()
state.set_interval(loop_interval)
state.x = startx
state.y = starty
state.speed = params.speed
state.theta = params.startAngle
state.phase = 0
self.path_points.append(copy.copy(state))
turn_distance = params.length
turn_gamma = params.gamma
turn_speed = params.speed
turn_angle = params.angle
k = 6.0 * turn_angle / (turn_distance * turn_distance * turn_distance)
while state.distance < turn_distance:
t = state.distance / turn_distance
# calculate the speed change if used
q = 4.0 * turn_gamma * (t - 1) * t + turn_gamma + 1
state.speed = turn_speed * q
# now the angular velocity
omega = state.speed * k * state.distance * (turn_distance - state.distance)
state.omega = omega
if t < 0.5:
state.phase = 1
else:
state.phase = 3
state.update()
self.path_points.append(copy.copy(state))
self.turn_end = len(self.path_points)-1
self.calculate_leadout(state)
self.update()
def calculate_leadout(self, state : RobotState):
state.phase = 4
state.omega = 0
target_distance = state.distance + 100.0
while state.distance < target_distance:
state.update()
self.path_points.append(copy.copy(state))
def calculate(self, profile_type: ProfileType, params: TurnParameters, startx, starty, loop_interval):
if profile_type == ProfileType.QUADRATIC:
self.calculate_quadratic(params, startx, starty, loop_interval)
elif profile_type == ProfileType.SINUSOID:
self.calculate_sinusoid(params, startx, starty, loop_interval)
elif profile_type == ProfileType.CUBIC:
self.calculate_cubic(params, startx, starty, loop_interval)
else:
self.calculate_trapezoid(params, startx, starty, loop_interval)
return
def get_turn_acceleration(self, profile_type: ProfileType, params: TurnParameters):
acc = 0
if profile_type == ProfileType.TRAPEZOID:
acc = params.speed * params.speed / params.radius
elif profile_type == ProfileType.QUADRATIC:
acc = params.speed * params.speed / params.radius
elif profile_type == ProfileType.SINUSOID:
acc = params.speed * params.speed / params.radius
elif profile_type == ProfileType.CUBIC:
acc = 6 * params.speed * params.speed * math.radians(params.angle) / 4 / params.length
return acc