|
1 | | -import numpy |
| 1 | +import numpy as np |
| 2 | +import math |
| 3 | +from matplotlib import pyplot as plt |
2 | 4 |
|
3 | | -class FootstepPlanner(object): |
4 | | - def __init__(self): |
5 | | - pass |
6 | | - |
7 | | - def plan(self, goal): |
8 | | - pass |
9 | | - |
10 | 5 | class BipedalPlanner(object): |
11 | 6 | def __init__(self): |
12 | | - self.footstep_planner = FootstepPlanner() |
13 | | - |
14 | | - ''' |
15 | | - self.footstep = [[0.0, 0.2], |
16 | | - [0.3, 0.2], |
17 | | - [0.3, 0.2], |
18 | | - [0.3, 0.2], |
19 | | - [0.3, 0.2], |
20 | | - [0.0, 0.2]] |
21 | | - ''' |
| 7 | + self.footstep = None |
| 8 | + self.g = 9.8 |
22 | 9 |
|
23 | 10 | def set_footstep(self, footstep): |
24 | 11 | self.footstep = footstep |
25 | 12 |
|
26 | | - def send_goal(self, goal): |
27 | | - footstep = self.footstep_planner.plan(goal) |
28 | | - if footstep != None: |
29 | | - self.footstep = footstep |
| 13 | + def inverted_pendulum(self, x, x_dot, px_star, y, y_dot, py_star, z_c, time_width): |
| 14 | + time_split = 100 |
| 15 | + |
| 16 | + for i in range(time_split): |
| 17 | + delta_time = time_width / time_split |
| 18 | + |
| 19 | + x_dot2 = self.g / z_c * (x - px_star) |
| 20 | + x += x_dot * delta_time |
| 21 | + x_dot += x_dot2 * delta_time |
| 22 | + |
| 23 | + y_dot2 = self.g / z_c * (y - py_star) |
| 24 | + y += y_dot * delta_time |
| 25 | + y_dot += y_dot2 * delta_time |
| 26 | + |
| 27 | + self.com_trajectory.append([x, y]) |
| 28 | + |
| 29 | + return x, x_dot, y, y_dot |
| 30 | + |
| 31 | + 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") |
| 34 | + return |
| 35 | + |
| 36 | + self.com_trajectory = [] |
| 37 | + self.footsteps = [] |
| 38 | + |
| 39 | + px, py = 0., 0. |
| 40 | + px_star, py_star = px, py |
| 41 | + xi, xi_dot, yi, yi_dot = 0., 0., 0., 0. |
| 42 | + time = 0. |
| 43 | + n = 0 |
| 44 | + for i in range(len(self.footstep)): |
| 45 | + # simulate x, y o finverted pendulum |
| 46 | + xi, xi_dot, yi, yi_dot = self.inverted_pendulum(xi, xi_dot, px_star, yi, yi_dot, py_star, z_c, T_sup) |
| 47 | + |
| 48 | + # update time |
| 49 | + time += T_sup |
| 50 | + n += 1 |
| 51 | + |
| 52 | + |
| 53 | + # calculate px, py, x_, y_, vx_, vy_ |
| 54 | + f_x, f_y, f_theta = self.footstep[n - 1] |
| 55 | + rotate_mat = np.array([[math.cos(f_theta), -math.sin(f_theta)], |
| 56 | + [math.sin(f_theta), math.cos(f_theta)]]) |
| 57 | + |
| 58 | + if n == len(self.footstep): |
| 59 | + f_x_next, f_y_next, f_theta_next = 0., 0., 0. |
| 60 | + else: |
| 61 | + f_x_next, f_y_next, f_theta_next = self.footstep[n] |
| 62 | + rotate_mat_next = np.array([[math.cos(f_theta_next), -math.sin(f_theta_next)], |
| 63 | + [math.sin(f_theta_next), math.cos(f_theta_next)]]) |
| 64 | + |
| 65 | + T_c = math.sqrt(z_c / self.g) |
| 66 | + C = math.cosh(T_sup / T_c) |
| 67 | + S = math.sinh(T_sup / T_c) |
| 68 | + |
| 69 | + px, py = list(np.array([px, py]) + np.dot(rotate_mat, np.array([f_x, -1 * math.pow(-1, n) * f_y]))) |
| 70 | + x_, y_ = list(np.dot(rotate_mat_next, np.array([f_x_next / 2., math.pow(-1, n) * f_y_next / 2.]))) |
| 71 | + vx_, vy_ = list(np.dot(rotate_mat_next, np.array([(1 + C) / (T_c * S) * x_, (C - 1) / (T_c * S) * y_]))) |
| 72 | + |
| 73 | + |
| 74 | + # calculate reference COM |
| 75 | + xd, xd_dot = px + x_, vx_ |
| 76 | + yd, yd_dot = py + y_, vy_ |
| 77 | + |
| 78 | + # calculate modified footstep |
| 79 | + D = a * math.pow(C - 1, 2) + b * math.pow(S / T_c, 2) |
| 80 | + 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) |
| 81 | + 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) |
| 82 | + |
| 83 | + self.footsteps.append([px_star, py_star]) |
| 84 | + |
| 85 | + 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]) |
| 88 | + plt.show() |
| 89 | + |
| 90 | + |
30 | 91 |
|
31 | 92 | if __name__ == "__main__": |
32 | 93 | bipedal_planner = BipedalPlanner() |
33 | | - goal = None |
34 | | - bipedal_planner.send_goal(goal) |
| 94 | + |
| 95 | + footstep = [[0.0, 0.2, 0.0], |
| 96 | + [0.3, 0.2, 0.0], |
| 97 | + [0.3, 0.2, 0.0], |
| 98 | + [0.3, 0.2, 0.0], |
| 99 | + [0.0, 0.2, 0.0]] |
| 100 | + bipedal_planner.set_footstep(footstep) |
| 101 | + bipedal_planner.walk(plot=True) |
0 commit comments