11import numpy as np
22import math
33from matplotlib import pyplot as plt
4+ import matplotlib .patches as pat
45
56class BipedalPlanner (object ):
67 def __init__ (self ):
7- self .footstep = None
8+ self .footsteps = None
89 self .g = 9.8
910
10- def set_footstep (self , footstep ):
11- self .footstep = footstep
11+ def set_ref_footsteps (self , footsteps ):
12+ self .ref_footsteps = footsteps
1213
1314 def inverted_pendulum (self , x , x_dot , px_star , y , y_dot , py_star , z_c , time_width ):
1415 time_split = 100
@@ -29,19 +30,21 @@ def inverted_pendulum(self, x, x_dot, px_star, y, y_dot, py_star, z_c, time_widt
2930 return x , x_dot , y , y_dot
3031
3132 def walk (self , T_sup = 0.8 , z_c = 0.8 , a = 10 , b = 1 , plot = False ):
32- if self .footstep == None :
33- print ("No footstep " )
33+ if self .ref_footsteps == None :
34+ print ("No footsteps " )
3435 return
3536
3637 self .com_trajectory = []
3738 self .footsteps = []
39+ self .ref_p = []
3840
3941 px , py = 0. , 0.
4042 px_star , py_star = px , py
4143 xi , xi_dot , yi , yi_dot = 0. , 0. , 0. , 0.
4244 time = 0.
4345 n = 0
44- for i in range (len (self .footstep )):
46+ self .ref_p .append ([px , py , 0 ])
47+ for i in range (len (self .ref_footsteps )):
4548 # simulate x, y o finverted pendulum
4649 xi , xi_dot , yi , yi_dot = self .inverted_pendulum (xi , xi_dot , px_star , yi , yi_dot , py_star , z_c , T_sup )
4750
@@ -51,14 +54,14 @@ def walk(self, T_sup=0.8, z_c=0.8, a=10, b=1, plot=False):
5154
5255
5356 # calculate px, py, x_, y_, vx_, vy_
54- f_x , f_y , f_theta = self .footstep [n - 1 ]
57+ f_x , f_y , f_theta = self .ref_footsteps [n - 1 ]
5558 rotate_mat = np .array ([[math .cos (f_theta ), - math .sin (f_theta )],
5659 [math .sin (f_theta ), math .cos (f_theta )]])
5760
58- if n == len (self .footstep ):
61+ if n == len (self .ref_footsteps ):
5962 f_x_next , f_y_next , f_theta_next = 0. , 0. , 0.
6063 else :
61- f_x_next , f_y_next , f_theta_next = self .footstep [n ]
64+ f_x_next , f_y_next , f_theta_next = self .ref_footsteps [n ]
6265 rotate_mat_next = np .array ([[math .cos (f_theta_next ), - math .sin (f_theta_next )],
6366 [math .sin (f_theta_next ), math .cos (f_theta_next )]])
6467
@@ -69,33 +72,44 @@ def walk(self, T_sup=0.8, z_c=0.8, a=10, b=1, plot=False):
6972 px , py = list (np .array ([px , py ]) + np .dot (rotate_mat , np .array ([f_x , - 1 * math .pow (- 1 , n ) * f_y ])))
7073 x_ , y_ = list (np .dot (rotate_mat_next , np .array ([f_x_next / 2. , math .pow (- 1 , n ) * f_y_next / 2. ])))
7174 vx_ , vy_ = list (np .dot (rotate_mat_next , np .array ([(1 + C ) / (T_c * S ) * x_ , (C - 1 ) / (T_c * S ) * y_ ])))
75+ self .ref_p .append ([px , py , f_theta ])
7276
7377
7478 # calculate reference COM
7579 xd , xd_dot = px + x_ , vx_
7680 yd , yd_dot = py + y_ , vy_
7781
78- # calculate modified footstep
82+ # calculate modified footsteps
7983 D = a * math .pow (C - 1 , 2 ) + b * math .pow (S / T_c , 2 )
8084 px_star = - a * (C - 1 ) / D * (xd - C * xi - T_c * S * xi_dot ) - b * S / (T_c * D ) * (xd_dot - S / T_c * xi - C * xi_dot )
8185 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 )
8286
8387 self .footsteps .append ([px_star , py_star ])
8488
8589 if plot :
86- plt .plot ([i [0 ] for i in self .footsteps ], [i [1 ] for i in self .footsteps ])
87- plt .plot ([i [0 ] for i in self .com_trajectory ], [i [1 ] for i in self .com_trajectory ])
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+
95+ ax .plot ([i [0 ] for i in self .footsteps ], [i [1 ] for i in self .footsteps ])
96+ ax .plot ([i [0 ] for i in self .com_trajectory ], [i [1 ] for i in self .com_trajectory ])
97+
98+ for i in range (len (self .ref_p )):
99+ rec = pat .Rectangle (xy = (self .ref_p [i ][0 ], self .ref_p [i ][1 ]), width = 0.06 , height = 0.02 , angle = self .ref_p [i ][2 ] * 180 / math .pi , color = "blue" , alpha = 0.5 )
100+ ax .add_patch (rec )
101+
88102 plt .show ()
89103
90104
91105
92106if __name__ == "__main__" :
93107 bipedal_planner = BipedalPlanner ()
94108
95- footstep = [[0.0 , 0.2 , 0.0 ],
96- [0.3 , 0.2 , 0.0 ],
97- [0.3 , 0.2 , 0.0 ],
109+ footsteps = [[0.0 , 0.2 , 0.0 ],
98110 [0.3 , 0.2 , 0.0 ],
99- [0.0 , 0.2 , 0.0 ]]
100- bipedal_planner .set_footstep (footstep )
111+ [0.3 , 0.2 , 0.2 ],
112+ [0.3 , 0.2 , 0.2 ],
113+ [0.0 , 0.2 , 0.2 ]]
114+ bipedal_planner .set_ref_footsteps (footsteps )
101115 bipedal_planner .walk (plot = True )
0 commit comments