Skip to content

Commit 7ea3424

Browse files
committed
Maintain the robot at fixed pose using inverse dynamics
1 parent 1d0bd84 commit 7ea3424

1 file changed

Lines changed: 13 additions & 56 deletions

File tree

examples/example_5_tsid_com_shift.py

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
2-
TSID torque-control example: fixed-feet COM transfer in PyBullet.
2+
TSID torque-control example: fixed-feet COM tracking in PyBullet.
33
44
This is the first inverse-dynamics replacement for the IK loop in
55
example_4_physics_simulation.py. Both feet stay rigidly constrained on the
6-
ground, and the COM reference moves laterally so the load shifts from one foot
7-
to the other. No foot swing or contact switching is performed yet.
6+
ground, and the COM reference is fixed for easier torque-control debugging.
7+
No foot swing, COM transfer, or contact switching is performed yet.
88
"""
99

1010
import argparse
@@ -30,13 +30,10 @@
3030
class GeneralParams:
3131
dt: float = 1.0 / 500.0
3232
duration: float = 8.0
33-
period: float = 4.0
34-
settle_duration: float = 1.0
35-
com_shift_ratio: float = 0.75
3633
n_solver_iter: int = 1000
3734

3835

39-
class TSIDComShiftController:
36+
class TSIDFixedComController:
4037
def __init__(
4138
self,
4239
urdf_path: Path,
@@ -56,7 +53,7 @@ def __init__(
5653
self.left_foot_id = self._get_frame_id(left_foot_frame)
5754
self.right_foot_id = self._get_frame_id(right_foot_frame)
5855

59-
self.invdyn = tsid.InverseDynamicsFormulationAccForce("tsid-com-shift", self.robot, False)
56+
self.invdyn = tsid.InverseDynamicsFormulationAccForce("tsid-fixed-com", self.robot, False)
6057
self.invdyn.computeProblemData(0.0, q0, self.v0)
6158

6259
self._add_foot_contacts()
@@ -156,13 +153,11 @@ def compute(
156153
q: np.ndarray,
157154
v: np.ndarray,
158155
com_ref: np.ndarray,
159-
com_vel_ref: np.ndarray,
160-
com_acc_ref: np.ndarray,
161156
) -> np.ndarray:
162157
com_sample = tsid.TrajectorySample(3)
163158
com_sample.pos(com_ref)
164-
com_sample.vel(com_vel_ref)
165-
com_sample.acc(com_acc_ref)
159+
com_sample.vel(np.zeros(3))
160+
com_sample.acc(np.zeros(3))
166161

167162
self.com_task.setReference(com_sample)
168163
self.posture_task.setReference(self.posture_sample)
@@ -173,6 +168,7 @@ def compute(
173168
raise RuntimeError(f"TSID QP solver failed at t={t:.3f} with status {sol.status}")
174169

175170
return self.invdyn.getActuatorForces(sol)
171+
Memmo 2020 summer school
176172

177173

178174
def parse_args():
@@ -187,44 +183,12 @@ def parse_args():
187183
parser.add_argument("--launch-gui", action="store_true")
188184
parser.add_argument("--record-video", action="store_true")
189185
parser.add_argument("--duration", type=float, default=GeneralParams.duration)
190-
parser.add_argument("--period", type=float, default=GeneralParams.period)
191-
parser.add_argument("--com-shift-ratio", type=float, default=GeneralParams.com_shift_ratio)
192186
return parser.parse_args()
193187

194188

195-
def make_com_reference(
196-
t: float,
197-
params: GeneralParams,
198-
com_center: np.ndarray,
199-
left_foot_pos: np.ndarray,
200-
right_foot_pos: np.ndarray,
201-
):
202-
com_ref = com_center.copy()
203-
com_vel_ref = np.zeros(3)
204-
com_acc_ref = np.zeros(3)
205-
206-
if t < params.settle_duration:
207-
return com_ref, com_vel_ref, com_acc_ref
208-
209-
half_step = 0.5 * abs(left_foot_pos[1] - right_foot_pos[1])
210-
amplitude = params.com_shift_ratio * half_step
211-
omega = 2.0 * math.pi / params.period
212-
phase = omega * (t - params.settle_duration)
213-
214-
com_ref[1] = com_center[1] + amplitude * math.sin(phase)
215-
com_vel_ref[1] = amplitude * omega * math.cos(phase)
216-
com_acc_ref[1] = -amplitude * omega * omega * math.sin(phase)
217-
218-
return com_ref, com_vel_ref, com_acc_ref
219-
220-
221189
def main():
222190
args = parse_args()
223-
params = GeneralParams(
224-
duration=args.duration,
225-
period=args.period,
226-
com_shift_ratio=args.com_shift_ratio,
227-
)
191+
params = GeneralParams(duration=args.duration)
228192

229193
np.set_printoptions(suppress=True, precision=3)
230194

@@ -260,7 +224,7 @@ def main():
260224
simulator.reset_robot_configuration(q_init)
261225
simulator.disable_joint_motors()
262226

263-
controller = TSIDComShiftController(
227+
controller = TSIDFixedComController(
264228
urdf_path=urdf_path,
265229
package_root=package_root,
266230
q0=q_init,
@@ -270,7 +234,8 @@ def main():
270234

271235
com0 = pin.centerOfMass(talos.model, talos.data, q_init)
272236
feet_mid = 0.5 * (oMf_lf_tgt.translation + oMf_rf_tgt.translation)
273-
com_center = np.array([feet_mid[0], feet_mid[1], com0[2]])
237+
com_ref = np.array([feet_mid[0], feet_mid[1], com0[2]])
238+
print(f"Fixed COM reference: [{com_ref[0]:.3f}, {com_ref[1]:.3f}, {com_ref[2]:.3f}]")
274239

275240
pb.setGravity(0, 0, 0)
276241
for _ in range(10):
@@ -294,16 +259,8 @@ def main():
294259
q = simulator.get_q(talos.model.nq)
295260
v = simulator.get_v(talos.model.nv)
296261

297-
com_ref, com_vel_ref, com_acc_ref = make_com_reference(
298-
t,
299-
params,
300-
com_center,
301-
oMf_lf_tgt.translation,
302-
oMf_rf_tgt.translation,
303-
)
304-
305262
try:
306-
tau = controller.compute(t, q, v, com_ref, com_vel_ref, com_acc_ref)
263+
tau = controller.compute(t, q, v, com_ref)
307264
except RuntimeError as exc:
308265
print(exc)
309266
break

0 commit comments

Comments
 (0)