forked from Nate711/PupperPythonSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.py
More file actions
129 lines (109 loc) · 3.88 KB
/
Copy pathController.py
File metadata and controls
129 lines (109 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from src.PupperConfig import SwingParams, StanceParams, GaitParams, MovementReference
from src.PupperConfig import PupperConfig
from src.Gaits import contacts, subphase_time
from src.Kinematics import four_legs_inverse_kinematics
from src.StanceController import stance_foot_location
from src.SwingLegController import swing_foot_location
import numpy as np
class Controller:
"""Controller and planner object
"""
def __init__(self):
self.swing_params = SwingParams()
self.stance_params = StanceParams()
self.gait_params = GaitParams()
self.movement_reference = MovementReference()
self.robot_config = PupperConfig()
self.ticks = 0
self.foot_locations = (
self.stance_params.default_stance
+ np.array([0, 0, self.movement_reference.z_ref])[:, np.newaxis]
)
self.joint_angles = four_legs_inverse_kinematics(
self.foot_locations, self.robot_config
)
def step(
ticks, foot_locations, swing_params, stance_params, gait_params, movement_reference
):
"""Calculate the desired foot locations for the next timestep
Parameters
----------
ticks : int
Number of clock ticks since the start. Time between ticks is given my the gait params dt variable.
foot_locations : Numpy array (3, 4)
Locations of all four feet.
swing_params : SwingParams
Swing parameters object.
stance_params : StanceParams
Stance parameters object.
gait_params : GaitParams
Gait parameters object.
movement_reference : MovementReference
Movement reference object.
Returns
-------
Numpy array (3, 4)
Matrix of new foot locations.
"""
contact_modes = contacts(ticks, gait_params)
new_foot_locations = np.zeros((3, 4))
for leg_index in range(4):
contact_mode = contact_modes[leg_index]
foot_location = foot_locations[:, leg_index]
if contact_mode == 1:
new_location = stance_foot_location(
foot_location, stance_params, gait_params, movement_reference
)
else:
swing_proportion = (
subphase_time(ticks, gait_params) / gait_params.swing_ticks
)
new_location = swing_foot_location(
swing_proportion,
foot_location,
leg_index,
swing_params,
stance_params,
gait_params,
movement_reference,
)
new_foot_locations[:, leg_index] = new_location
return new_foot_locations
def step_controller(controller):
"""Steps the controller forward one timestep
Parameters
----------
controller : Controller
Robot controller object.
"""
controller.foot_locations = step(
controller.ticks,
controller.foot_locations,
controller.swing_params,
controller.stance_params,
controller.gait_params,
controller.movement_reference,
)
controller.joint_angles = four_legs_inverse_kinematics(
controller.foot_locations, controller.robot_config
)
controller.ticks += 1
def run():
"""Testing function that runs the robot for one second.
Returns
-------
(Numpy array (3, 4, timesteps), Numpy array (3, 4, timesteps))
(history of foot locations, history of joint angles)
"""
c = Controller()
c.movement_reference.v_xy_ref = np.array([0.2, 0.0])
c.movement_reference.wz_ref = 0.5
tf = 1.0
time_steps = int(tf / c.gait_params.dt)
foot_loc_history = np.zeros((3, 4, time_steps))
joint_angle_history = np.zeros((3, 4, time_steps))
for i in range(time_steps):
step_controller(c)
foot_loc_history[:, :, i] = c.foot_locations
joint_angle_history[:, :, i] = c.joint_angles
return foot_loc_history, joint_angle_history