Files
UniversalTrain-ai/rl/mjrl/controllers/bindings.py
T
chenlin c32df70326
Checks / Layout, translation and Colab checks (push) Has been cancelled
Checks / Ruff (push) Has been cancelled
Checks / Framework tests (native backend, CPU) (push) Has been cancelled
Checks / Portable Rust controller (push) Has been cancelled
release: v0.1 generic robot training and application platform
2026-11-05 11:05:01 +08:00

67 lines
2.1 KiB
Python

"""Generate operator documents for named native command axes."""
from dataclasses import dataclass
@dataclass(frozen=True)
class AxisBinding:
source: str
sign: int
plus: str
minus: str
unit: str
positive: str
_VELOCITY = {
"lin_vel_x": AxisBinding("Ly", -1, "key_w", "key_s", "m/s", "forward"),
"lin_vel_y": AxisBinding("Lx", -1, "key_a", "key_d", "m/s", "left"),
"ang_vel_z": AxisBinding("Rx", -1, "key_j", "key_l", "rad/s", "turn left"),
}
def velocity_controls(axes, *, differential: bool = False) -> dict:
bindings = {axis: _VELOCITY[axis] for axis in axes}
if differential and "ang_vel_z" in bindings:
bindings["ang_vel_z"] = AxisBinding("Rx", -1, "key_a", "key_d", "rad/s", "turn left")
return controls_document(bindings, term="twist", feeds="velocity_commands", frame="body")
def controls_document(bindings: dict[str, AxisBinding], *, term: str, feeds: str, frame: str):
"""Build a document for validation through controller.controls.load_controls."""
return {
"schema_version": 2,
"command": [
{
"term": term,
"feeds": feeds,
"frame": frame,
"axes": [
{"name": axis, "unit": spec.unit, "positive": spec.positive}
for axis, spec in bindings.items()
],
"scaling": {"rule": "full_deflection_is_range_edge"},
}
],
"devices": {
"gamepad": {
"scheme": "absolute",
"layout": "xbox",
"deadzone": "device_reported_rescaled",
"release_button": "B",
"axes": {
axis: {"source": spec.source, "sign": spec.sign}
for axis, spec in bindings.items()
},
},
"keyboard": {
"scheme": "keys",
"full_after_s": 0.5,
"release": ["key_escape"],
"axes": {
axis: {"+": [spec.plus], "-": [spec.minus]} for axis, spec in bindings.items()
},
},
},
}