Skip to content

Commit 7d65fb2

Browse files
committed
simulates piwars blind maze
1 parent e8ffc01 commit 7d65fb2

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

PathTracking/pure_pursuit/pure_pursuit.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,44 @@
1111
import matplotlib.pyplot as plt
1212

1313

14-
k = 0.1 # look forward gain
15-
Lfc = 2.0 # look-ahead distance
16-
Kp = 1.0 # speed proportional gain
17-
dt = 0.1 # [s]
18-
L = 2.9 # [m] wheel base of vehicle
14+
k = 0 # look forward gain
15+
Lfc = 0.3 # look-ahead distance
16+
Kp = 7 # speed proportional gain
17+
dt = 0.05 # [s]
18+
L = 0.1 # [m] wheel base of vehicle
1919

2020

2121
show_animation = True
2222

2323

24+
blindMaze = [[0.0, 0.0],
25+
[0.0, 0.3],
26+
[0.0, 0.6],
27+
[0.0, 0.9],
28+
[0.0, 1.1],
29+
[0.3, 1.1],
30+
[0.5, 1.1],
31+
[0.7, 1.1],
32+
[1.0, 1.1],
33+
[1.1, 1.0],
34+
[1, 0.8],
35+
[1, 0.5],
36+
[1, 0.2],
37+
[1.1, 0.1],
38+
[1.3, 0.2],
39+
[1.6, 0.2],
40+
[2, 0.2],
41+
[2.1, 0.3],
42+
[2, 0.5],
43+
[2, 0.8],
44+
[2, 1.1],
45+
[2.1, 1.2],
46+
[2.3, 1.1],
47+
[2.6, 1.1],
48+
[2.9, 1.1],
49+
[3.2, 1.1],
50+
[3.6, 1.1]]
51+
2452
class State:
2553

2654
def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
@@ -91,6 +119,7 @@ def search_target_index(self, state):
91119
ind = ind + 1 if (ind + 1) < len(self.cx) else ind
92120
distance_next_index = state.calc_distance(self.cx[ind], self.cy[ind])
93121
if distance_this_index < distance_next_index:
122+
ind = ind-1
94123
break
95124
distance_this_index = distance_next_index
96125
self.old_nearest_point_index = ind
@@ -103,7 +132,6 @@ def search_target_index(self, state):
103132
while Lf > L and (ind + 1) < len(self.cx):
104133
L = state.calc_distance(self.cx[ind], self.cy[ind])
105134
ind += 1
106-
107135
return ind
108136

109137

@@ -130,8 +158,18 @@ def pure_pursuit_control(state, trajectory, pind):
130158

131159
return delta, ind
132160

161+
def addArena():
162+
addRect(-0.25, -0.25, 3, 1.8)
163+
addRect(0.25, -0.25, 0.5, 1)
164+
addRect(1.25, 0.55, 0.5, 1)
165+
addRect(2.25, -0.25, 0.5, 1)
166+
167+
def addRect(x, y, w, h):
168+
p = plt.Rectangle((x,y),w,h,fill=False)
169+
ax = plt.gca()
170+
ax.add_patch(p)
133171

134-
def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
172+
def plot_arrow(x, y, yaw, length=0.1, width=0.05, fc="r", ec="k"):
135173
"""
136174
Plot arrow
137175
"""
@@ -147,15 +185,15 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"):
147185

148186
def main():
149187
# target course
150-
cx = np.arange(0, 50, 0.1)
151-
cy = [math.sin(ix / 5.0) * ix / 2.0 for ix in cx]
188+
cx = [row[0] for row in blindMaze]
189+
cy = [row[1] for row in blindMaze]
152190

153-
target_speed = 10.0 / 3.6 # [m/s]
191+
target_speed = 2 # [m/s]
154192

155193
T = 100.0 # max simulation time
156194

157195
# initial state
158-
state = State(x=-0.0, y=-3.0, yaw=0.0, v=0.0)
196+
state = State(x=-0.0, y=0, yaw=1.57, v=0.0)
159197

160198
lastIndex = len(cx) - 1
161199
time = 0.0
@@ -171,12 +209,12 @@ def main():
171209

172210
time += dt
173211
states.append(time, state)
174-
175212
if show_animation: # pragma: no cover
176213
plt.cla()
177214
# for stopping simulation with the esc key.
178215
plt.gcf().canvas.mpl_connect('key_release_event',
179216
lambda event: [exit(0) if event.key == 'escape' else None])
217+
addArena()
180218
plot_arrow(state.x, state.y, state.yaw)
181219
plt.plot(cx, cy, "-r", label="course")
182220
plt.plot(states.x, states.y, "-b", label="trajectory")
@@ -191,19 +229,20 @@ def main():
191229

192230
if show_animation: # pragma: no cover
193231
plt.cla()
232+
addArena()
194233
plt.plot(cx, cy, ".r", label="course")
195-
plt.plot(states.x, states.y, "-b", label="trajectory")
234+
plt.plot(states.x, states.y, "--b", label="trajectory")
196235
plt.legend()
197236
plt.xlabel("x[m]")
198237
plt.ylabel("y[m]")
199238
plt.axis("equal")
200239
plt.grid(True)
201240

202-
plt.subplots(1)
203-
plt.plot(states.t, [iv * 3.6 for iv in states.v], "-r")
204-
plt.xlabel("Time[s]")
205-
plt.ylabel("Speed[km/h]")
206-
plt.grid(True)
241+
#plt.subplots(1)
242+
#plt.plot(states.t, [iv * 3.6 for iv in states.v], "-r")
243+
#plt.xlabel("Time[s]")
244+
#plt.ylabel("Speed[km/h]")
245+
#plt.grid(True)
207246
plt.show()
208247

209248

0 commit comments

Comments
 (0)