11import numpy as np
22from mujoco import MjData , MjModel
33from mujoco_rust_server .booster_types import LowCommand
4+ from numpy .typing import NDArray
45
56from mujoco_simulator .joint_actuator_info import JointActuatorInfo
67
78
89class RobotPositionControl :
10+ model : MjModel
11+ qpos_indices : NDArray
12+ qvel_indices : NDArray
13+ actuator_indices : NDArray
14+
915 def __init__ (
1016 self , model : MjModel , actuator_info : list [JointActuatorInfo ]
1117 ) -> None :
1218 self .model = model
13- self .qpos_indices = np .array ([info .qpos_addr for info in actuator_info ])
14- self .qvel_indices = np .array ([info .qvel_addr for info in actuator_info ])
19+ self .qpos_indices = np .array (
20+ [info .qpos_addr for info in actuator_info ], dtype = np .uint32
21+ )
22+ self .qvel_indices = np .array (
23+ [info .qvel_addr for info in actuator_info ], dtype = np .uint32
24+ )
1525 self .actuator_indices = np .array (
16- [info .qfrc_actuator_addr for info in actuator_info ]
26+ [info .qfrc_actuator_addr for info in actuator_info ], dtype = np . uint32
1727 )
1828
1929 def apply_control (
@@ -33,13 +43,17 @@ def apply_control(
3343 current_velocity = data .qvel [self .qvel_indices ]
3444
3545 # TODO(oleflb): booster supposedly clips position to joint limits first
36- control_torque = np .clip (
37- kp * (q - current_position ) + kd * (dq - current_velocity ) + tau ,
38- self .model .actuator_ctrlrange [:, 0 ],
39- self .model .actuator_ctrlrange [:, 1 ],
46+ desired = (
47+ kp * (q - current_position ) + kd * (dq - current_velocity ) + tau
4048 )
49+
50+ ctrl_min = self .model .actuator_ctrlrange [self .actuator_indices , 0 ]
51+ ctrl_max = self .model .actuator_ctrlrange [self .actuator_indices , 1 ]
52+ control_torque = np .clip (desired , ctrl_min , ctrl_max )
53+
54+ # Ensure existing control values are used as floats when smoothing
55+ current_ctrl = data .ctrl [self .actuator_indices ]
4156 smoothed_control_torque = (
42- weight * control_torque
43- + (1 - weight ) * data .ctrl [self .actuator_indices ]
57+ weight * control_torque + (1 - weight ) * current_ctrl
4458 )
4559 data .ctrl [self .actuator_indices ] = smoothed_control_torque
0 commit comments