Skip to content

Commit eb0aa8a

Browse files
committed
modify for good plot
1 parent dc184b2 commit eb0aa8a

1 file changed

Lines changed: 62 additions & 34 deletions

File tree

‎Bipedal/bipedal_planner/bipedal_planner.py‎

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import math
33
from matplotlib import pyplot as plt
44
import matplotlib.patches as pat
5+
from mpl_toolkits.mplot3d import Axes3D
6+
import mpl_toolkits.mplot3d.art3d as art3d
57

68
class BipedalPlanner(object):
79
def __init__(self):
@@ -25,7 +27,8 @@ def inverted_pendulum(self, x, x_dot, px_star, y, y_dot, py_star, z_c, time_widt
2527
y += y_dot * delta_time
2628
y_dot += y_dot2 * delta_time
2729

28-
self.com_trajectory.append([x, y])
30+
if i % 10 == 0:
31+
self.com_trajectory.append([x, y])
2932

3033
return x, x_dot, y, y_dot
3134

@@ -34,31 +37,36 @@ def walk(self, T_sup=0.8, z_c=0.8, a=10, b=1, plot=False):
3437
print("No footsteps")
3538
return
3639

40+
# set up plotter
41+
if plot:
42+
fig = plt.figure()
43+
ax = Axes3D(fig)
44+
com_trajectory_for_plot = []
45+
3746
self.com_trajectory = []
38-
self.ref_p = []
39-
self.act_p = []
47+
self.ref_p = [] # reference footstep positions
48+
self.act_p = [] # actual footstep positions
4049

41-
px, py = 0., 0.
42-
px_star, py_star = px, py
50+
px, py = 0., 0. # reference footstep position
51+
px_star, py_star = px, py # modified footstep position
4352
xi, xi_dot, yi, yi_dot = 0., 0., 0.01, 0. # TODO yi should be set as +epsilon, set xi, yi as COM
4453
time = 0.
4554
n = 0
4655
self.ref_p.append([px, py, 0])
4756
self.act_p.append([px, py, 0])
4857
for i in range(len(self.ref_footsteps)):
49-
# simulate x, y o finverted pendulum
58+
# simulate x, y and those of dot of inverted pendulum
5059
xi, xi_dot, yi, yi_dot = self.inverted_pendulum(xi, xi_dot, px_star, yi, yi_dot, py_star, z_c, T_sup)
5160

5261
# update time
5362
time += T_sup
5463
n += 1
55-
5664

5765
# calculate px, py, x_, y_, vx_, vy_
5866
f_x, f_y, f_theta = self.ref_footsteps[n - 1]
5967
rotate_mat = np.array([[math.cos(f_theta), -math.sin(f_theta)],
6068
[math.sin(f_theta), math.cos(f_theta)]])
61-
69+
6270
if n == len(self.ref_footsteps):
6371
f_x_next, f_y_next, f_theta_next = 0., 0., 0.
6472
else:
@@ -75,7 +83,6 @@ def walk(self, T_sup=0.8, z_c=0.8, a=10, b=1, plot=False):
7583
vx_, vy_ = list(np.dot(rotate_mat_next, np.array([(1 + C) / (T_c * S) * x_, (C - 1) / (T_c * S) * y_])))
7684
self.ref_p.append([px, py, f_theta])
7785

78-
7986
# calculate reference COM
8087
xd, xd_dot = px + x_, vx_
8188
yd, yd_dot = py + y_, vy_
@@ -86,34 +93,55 @@ def walk(self, T_sup=0.8, z_c=0.8, a=10, b=1, plot=False):
8693
py_star = -a * (C - 1) / D * (yd - C * yi - T_c * S * yi_dot) - b * S / (T_c * D) * (yd_dot - S / T_c * yi - C * yi_dot)
8794
self.act_p.append([px_star, py_star, f_theta])
8895

96+
# plot
97+
if plot:
98+
# for plot trajectory, plot in for loop
99+
for c in range(len(self.com_trajectory)):
100+
if c > len(com_trajectory_for_plot):
101+
# set up plotter
102+
plt.cla()
103+
ax.set_zlim(0, z_c * 2)
104+
ax.set_aspect('equal', 'datalim')
105+
106+
# update com_trajectory_for_plot
107+
com_trajectory_for_plot.append(self.com_trajectory[c])
108+
109+
# plot com
110+
ax.plot([p[0] for p in com_trajectory_for_plot], [p[1] for p in com_trajectory_for_plot], [0 for p in com_trajectory_for_plot], color="red")
111+
112+
# plot inverted pendulum
113+
ax.plot([px_star, com_trajectory_for_plot[-1][0]],
114+
[py_star , com_trajectory_for_plot[-1][1]],
115+
[0, z_c], color="green", linewidth=3)
116+
ax.scatter([com_trajectory_for_plot[-1][0]],
117+
[com_trajectory_for_plot[-1][1]],
118+
[z_c], color="green", s=300)
119+
120+
# foot rectangle for self.ref_p
121+
foot_width = 0.06
122+
foot_height = 0.04
123+
for i in range(len(self.ref_p)):
124+
angle = self.ref_p[i][2] + math.atan2(foot_height, foot_width) - math.pi
125+
r = math.sqrt(math.pow(foot_width / 3., 2) + math.pow(foot_height / 2., 2))
126+
rec = pat.Rectangle(xy = (self.ref_p[i][0] + r * math.cos(angle), self.ref_p[i][1] + r * math.sin(angle)),
127+
width=foot_width, height=foot_height, angle=self.ref_p[i][2] * 180 / math.pi, color="blue", fill=False, ls=":")
128+
ax.add_patch(rec)
129+
art3d.pathpatch_2d_to_3d(rec, z=0, zdir="z")
130+
131+
# foot rectangle for self.act_p
132+
for i in range(len(self.act_p)):
133+
angle = self.act_p[i][2] + math.atan2(foot_height, foot_width) - math.pi
134+
r = math.sqrt(math.pow(foot_width / 3., 2) + math.pow(foot_height / 2., 2))
135+
rec = pat.Rectangle(xy = (self.act_p[i][0] + r * math.cos(angle), self.act_p[i][1] + r * math.sin(angle)),
136+
width=foot_width, height=foot_height, angle=self.act_p[i][2] * 180 / math.pi, color="blue", fill=False)
137+
ax.add_patch(rec)
138+
art3d.pathpatch_2d_to_3d(rec, z=0, zdir="z")
139+
140+
plt.draw()
141+
plt.pause(0.001)
89142
if plot:
90-
fig = plt.figure()
91-
ax = fig.subplots()
92-
ax.set_xlim(0, 1)
93-
ax.set_ylim(-0.1, 0.2 + 0.1)
94-
ax.set_aspect('equal', 'datalim')
95-
96-
ax.plot([i[0] for i in self.com_trajectory], [i[1] for i in self.com_trajectory])
97-
98-
foot_width = 0.06
99-
foot_height = 0.04
100-
for i in range(len(self.ref_p)):
101-
angle = self.ref_p[i][2] + math.atan2(foot_height, foot_width) - math.pi
102-
r = math.sqrt(math.pow(foot_width / 3., 2) + math.pow(foot_height / 2., 2))
103-
rec = pat.Rectangle(xy = (self.ref_p[i][0] + r * math.cos(angle), self.ref_p[i][1] + r * math.sin(angle)),
104-
width=foot_width, height=foot_height, angle=self.ref_p[i][2] * 180 / math.pi, color="green", fill=False, ls=":")
105-
ax.add_patch(rec)
106-
for i in range(len(self.act_p)):
107-
angle = self.act_p[i][2] + math.atan2(foot_height, foot_width) - math.pi
108-
r = math.sqrt(math.pow(foot_width / 3., 2) + math.pow(foot_height / 2., 2))
109-
rec = pat.Rectangle(xy = (self.act_p[i][0] + r * math.cos(angle), self.act_p[i][1] + r * math.sin(angle)),
110-
width=foot_width, height=foot_height, angle=self.act_p[i][2] * 180 / math.pi, color="blue", fill=False)
111-
ax.add_patch(rec)
112-
113143
plt.show()
114144

115-
116-
117145
if __name__ == "__main__":
118146
bipedal_planner = BipedalPlanner()
119147

0 commit comments

Comments
 (0)