Skip to content

Commit 96b3b85

Browse files
committed
code cleanup and add animation
1 parent 8a3e5a0 commit 96b3b85

File tree

5 files changed

+22
-26
lines changed

5 files changed

+22
-26
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "PathTracking/rear_wheel_feedback/pycubicspline"]
22
path = PathTracking/rear_wheel_feedback/pycubicspline
33
url = https://github.com/AtsushiSakai/pycubicspline.git
4+
[submodule "PathTracking/rear_wheel_feedback/matplotrecorder"]
5+
path = PathTracking/rear_wheel_feedback/matplotrecorder
6+
url = https://github.com/AtsushiSakai/matplotrecorder.git
2.98 MB
Loading
Submodule matplotrecorder added at aac964f

PathTracking/rear_wheel_feedback/rear_wheel_feedback.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
import matplotlib.pyplot as plt
1111
import unicycle_model
1212
from pycubicspline import pycubicspline
13+
from matplotrecorder import matplotrecorder
1314

1415
Kp = 1.0 # speed propotional gain
15-
animation = True
16-
# animation = False
16+
# steering control parameter
17+
KTH = 1.0
18+
KE = 0.5
19+
20+
21+
# animation = True
22+
animation = False
1723

1824

1925
def PIDControl(target, current):
@@ -33,9 +39,6 @@ def pi_2_pi(angle):
3339

3440

3541
def rear_wheel_feedback_control(state, cx, cy, cyaw, ck, preind):
36-
KTH = 1.0
37-
KE = 0.5
38-
3942
ind, e = calc_nearest_index(state, cx, cy, cyaw)
4043

4144
k = ck[ind]
@@ -44,13 +47,11 @@ def rear_wheel_feedback_control(state, cx, cy, cyaw, ck, preind):
4447

4548
omega = v * k * math.cos(th_e) / (1.0 - k * e) - \
4649
KTH * abs(v) * th_e - KE * v * math.sin(th_e) * e / th_e
47-
# pass
4850

4951
if th_e == 0.0 or omega == 0.0:
5052
return 0.0, ind
5153

5254
delta = math.atan2(unicycle_model.L * omega / v, 1.0)
53-
5455
# print(k, v, e, th_e, omega, delta)
5556

5657
return delta, ind
@@ -123,25 +124,23 @@ def closed_loop_prediction(cx, cy, cyaw, ck, speed_profile, goal):
123124
plt.plot(cx[target_ind], cy[target_ind], "xg", label="target")
124125
plt.axis("equal")
125126
plt.grid(True)
126-
plt.title("speed:" + str(round(state.v, 2)) +
127-
"tind:" + str(target_ind))
127+
plt.title("speed[km/h]:" + str(round(state.v * 3.6, 2)) +
128+
",target index:" + str(target_ind))
128129
plt.pause(0.0001)
130+
matplotrecorder.save_frame() # save each frame
129131

132+
plt.close()
130133
return t, x, y, yaw, v
131134

132135

133-
def set_stop_point(target_speed, cx, cy, cyaw):
136+
def calc_speed_profile(cx, cy, cyaw, target_speed):
137+
134138
speed_profile = [target_speed] * len(cx)
135139

136-
d = []
137140
direction = 1.0
138141

139142
# Set stop point
140143
for i in range(len(cx) - 1):
141-
dx = cx[i + 1] - cx[i]
142-
dy = cy[i + 1] - cy[i]
143-
td = math.sqrt(dx ** 2.0 + dy ** 2.0)
144-
d.append(td)
145144
dyaw = cyaw[i + 1] - cyaw[i]
146145
switch = math.pi / 4.0 <= dyaw < math.pi / 2.0
147146

@@ -158,15 +157,6 @@ def set_stop_point(target_speed, cx, cy, cyaw):
158157

159158
speed_profile[-1] = 0.0
160159

161-
d.append(d[-1])
162-
163-
return speed_profile, d
164-
165-
166-
def calc_speed_profile(cx, cy, cyaw, target_speed):
167-
168-
speed_profile, d = set_stop_point(target_speed, cx, cy, cyaw)
169-
170160
# flg, ax = plt.subplots(1)
171161
# plt.plot(speed_profile, "-r")
172162
# plt.show()
@@ -187,8 +177,10 @@ def main():
187177

188178
t, x, y, yaw, v = closed_loop_prediction(cx, cy, cyaw, ck, sp, goal)
189179

180+
if animation:
181+
matplotrecorder.save_movie("animation.gif", 0.1) # gif is ok.
182+
190183
flg, _ = plt.subplots(1)
191-
print(len(ax), len(ay))
192184
plt.plot(ax, ay, "xb", label="input")
193185
plt.plot(cx, cy, "-r", label="spline")
194186
plt.plot(x, y, "-g", label="tracking")

0 commit comments

Comments
 (0)