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
115 lines (100 loc) · 3.54 KB
/
Copy pathController.py
File metadata and controls
115 lines (100 loc) · 3.54 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
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
from transforms3d.euler import euler2mat
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
# Set default for foot locations and joint angles
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,
)
# Apply the desired body rotation
# TODO: See https://github.com/Nate711/PupperPythonSim/issues/2
rotated_foot_locations = (
euler2mat(
controller.movement_reference.roll, controller.movement_reference.pitch, 0.0
)
@ controller.foot_locations
)
controller.joint_angles = four_legs_inverse_kinematics(
rotated_foot_locations, controller.robot_config
)
controller.ticks += 1