Skip to content

Commit 3a22750

Browse files
authored
Merge pull request #110 from erikfrey/position_control
Add support for actuator gain/bias used in position control.
2 parents 922c245 + 64b7d59 commit 3a22750

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

mujoco_warp/_src/forward.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,29 +510,35 @@ def fwd_actuation(m: Model, d: Data):
510510

511511
# TODO support stateful actuators
512512

513-
disable_clampctrl = m.opt.disableflags & DisableBit.CLAMPCTRL
514-
515513
@kernel
516514
def _force(
517515
m: Model,
518-
ctrl: array2df,
519-
disable_clampctrl: bool,
516+
d: Data,
520517
# outputs
521518
force: array2df,
522519
):
523-
worldid, dofid = wp.tid()
524-
gain = m.actuator_gainprm[dofid, 0]
525-
bias = m.actuator_biasprm[dofid, 0]
526-
# TODO support gain types other than FIXED
527-
c = ctrl[worldid, dofid]
528-
if m.actuator_ctrllimited[dofid] and not disable_clampctrl:
529-
r = m.actuator_ctrlrange[dofid]
530-
c = wp.clamp(c, r[0], r[1])
531-
f = gain * c + bias
532-
if m.actuator_forcelimited[dofid]:
533-
r = m.actuator_forcerange[dofid]
534-
f = wp.clamp(f, r[0], r[1])
535-
force[worldid, dofid] = f
520+
worldid, uid = wp.tid()
521+
522+
actuator_length = d.actuator_length[worldid, uid]
523+
actuator_velocity = d.actuator_velocity[worldid, uid]
524+
525+
gain = m.actuator_gainprm[uid, 0]
526+
gain += m.actuator_gainprm[uid, 1] * actuator_length
527+
gain += m.actuator_gainprm[uid, 2] * actuator_velocity
528+
529+
bias = m.actuator_biasprm[uid, 0]
530+
bias += m.actuator_biasprm[uid, 1] * actuator_length
531+
bias += m.actuator_biasprm[uid, 2] * actuator_velocity
532+
533+
ctrl = d.ctrl[worldid, uid]
534+
disable_clampctrl = m.opt.disableflags & wp.static(DisableBit.CLAMPCTRL.value)
535+
if m.actuator_ctrllimited[uid] and not disable_clampctrl:
536+
r = m.actuator_ctrlrange[uid]
537+
ctrl = wp.clamp(ctrl, r[0], r[1])
538+
f = gain * ctrl + bias
539+
if m.actuator_forcelimited[uid]:
540+
r = m.actuator_forcerange[uid]
541+
force[worldid, uid] = f
536542

537543
@kernel
538544
def _qfrc_limited(m: Model, d: Data):
@@ -561,12 +567,7 @@ def _qfrc(m: Model, moment: array3df, force: array2df, qfrc: array2df):
561567
s = wp.clamp(s, r[0], r[1])
562568
qfrc[worldid, vid] = s
563569

564-
wp.launch(
565-
_force,
566-
dim=[d.nworld, m.nu],
567-
inputs=[m, d.ctrl, disable_clampctrl],
568-
outputs=[d.actuator_force],
569-
)
570+
wp.launch(_force, dim=[d.nworld, m.nu], inputs=[m, d], outputs=[d.actuator_force])
570571

571572
if m.opt.is_sparse:
572573
# TODO(team): sparse version

mujoco_warp/_src/forward_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def test_fwd_actuation(self):
7878

7979
# TODO(team): test DisableBit.CLAMPCTRL
8080
# TODO(team): test DisableBit.ACTUATION
81+
# TODO(team): test actuator gain/bias (e.g. position control)
8182

8283
def test_fwd_acceleration(self):
8384
_, mjd, m, d = self._load("humanoid/humanoid.xml", is_sparse=False)

0 commit comments

Comments
 (0)