-
Notifications
You must be signed in to change notification settings - Fork 1
/
spd_utils.py
167 lines (146 loc) · 4.15 KB
/
spd_utils.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import mujoco
import numpy as np
def computePD(
model,
data,
desiredPositions,
desiredVelocities,
kps,
kds,
maxForces,
timeStep,
):
q = data.qpos
qdot = data.qvel
q_des = np.array(desiredPositions)
qdot_des = np.array(desiredVelocities)
Kp = np.diagflat(kps)
Kd = np.diagflat(kds)
# Create empty mass matrix
MassMatrix = np.empty(
shape=(model.nv, model.nv),
dtype=np.float64,
)
# Creates in self._data.crb
mujoco.mj_crb(model, data)
mujoco.mj_fullM(
model,
MassMatrix,
data.qM,
)
Bias_Forces = data.qfrc_bias
# print(f"q: {q},np.shape(q):{np.shape(q)}")
# print(f"q_des: {q_des},np.shape(q_des):{np.shape(q_des)}")
# print(f"qdot: {qdot},np.shape(qdot):{np.shape(qdot)}")
# print(f"qdot_des: {qdot_des},np.shape(qdot_des):{np.shape(qdot_des)}")
# print(f"Kp: {Kp},np.shape(Kp):{np.shape(Kp)}")
# print(f"Kd: {Kd},np.shape(Kd):{np.shape(Kd)}")
# print(
# f"MassMatrix: {MassMatrix},np.shape(MassMatrix):{np.shape(MassMatrix)}"
# )
# print(
# f"Bias_Forces: {Bias_Forces},np.shape(Bias_Forces):{np.shape(Bias_Forces)}"
# )
qError = q_des - q
qdotError = qdot_des - qdot
# Compute -Kp(q + qdot - qdes)
# p_term = Kp.dot(qError - qdot * timeStep)
# Compute -Kd(qdot - qdotdes)
# d_term = Kd.dot(qdotError)
qddot = np.linalg.solve(
a=(MassMatrix + Kd * timeStep),
b=(
-Bias_Forces + Kp.dot(qError - qdot * timeStep) + Kd.dot(qdotError)
),
)
tau = (
Kp.dot(qError - qdot * timeStep)
+ Kd.dot(qdotError)
- (Kd.dot(qddot) * timeStep)
)
# Clip generalized forces to actuator limits
maxF = np.array(maxForces)
generalized_forces = np.clip(tau, -maxF, maxF)
return generalized_forces
def show_actuator_forces(
viewer,
data,
rendered_axes,
f_render,
f_list,
show_force_labels=False,
) -> None:
if show_force_labels is False:
label = ""
for i in rendered_axes:
viewer.add_marker(
pos=data.site(f_render[i][1]).xpos,
mat=data.site(f_render[i][1]).xmat,
size=[
0.02,
0.02,
(data.actuator_force[f_list[i]] / f_render[i][2]),
],
rgba=f_render[i][3],
type=mujoco.mjtGeom.mjGEOM_ARROW,
label=label,
)
else:
for i in rendered_axes:
viewer.add_marker(
pos=data.site(f_render[i][1]).xpos,
mat=data.site(f_render[i][1]).xmat,
size=[
0.02,
0.02,
(data.actuator_force[f_list[i]] / f_render[i][2]),
],
rgba=f_render[i][3],
type=mujoco.mjtGeom.mjGEOM_ARROW,
label=str(data.actuator_force[f_list[i]]),
)
def populate_show_actuator_forces(model, to_be_rendered_axes) -> None:
"""
format :
self._f_render = {
"axis_name": ["act_name","geom_for_force_render","scaling"]
}
self._f_list = {
"axis_name": ["actuator_index"], # internally generated
}
"""
rendered_axes = to_be_rendered_axes
f_render = {
"hinge_1": [
"pos_servo_1",
"site_1",
20.0,
[1, 0, 1, 0.2],
],
"hinge_2": [
"pos_servo_2",
"site_2",
20.0,
[1, 0, 1, 0.2],
],
"hinge_3": [
"pos_servo_3",
"site_3",
20.0,
[1, 0, 1, 0.2],
],
}
f_list_keys = []
f_list_values = []
for key in rendered_axes:
values = mujoco.mj_name2id(
model,
mujoco.mjtObj.mjOBJ_ACTUATOR,
f_render[key][0],
)
# print("values:", values)
f_list_keys.append(key)
f_list_values.append(values)
f_list = dict(zip(f_list_keys, f_list_values))
# print("self._f_list:", f_list)
return rendered_axes, f_render, f_list