Skip to content

Commit

Permalink
added ic keyframe support in python
Browse files Browse the repository at this point in the history
  • Loading branch information
Zolkin1 committed Jul 26, 2024
1 parent 8522fbc commit cab6196
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions obelisk/python/obelisk_py/core/sim/mujoco.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, node_name: str = "obelisk_mujoco_robot") -> None:
"""Initialize the mujoco simulator."""
super().__init__(node_name)
self.declare_parameter("mujoco_setting", rclpy.Parameter.Type.STRING)
self.declare_parameter("ic_keyframe", "ic")

def _get_msg_type_from_string(self, msg_type_str: str) -> Type[ObeliskSensorMsg]:
"""Get the message type from a string.
Expand Down Expand Up @@ -421,6 +422,24 @@ def publish_true_sim_state(self) -> osm.TrueSimState:

def run_simulator(self) -> None:
"""Run the mujoco simulator."""
# Set the initial condition based on a keyframe
self.get_logger().info(f"Found {self.mj_model.nkey} Mujoco keyframes.")
for i in range(self.mj_model.nkey):
potential_keyframe = self.mj_model.key(i).name
if potential_keyframe == self.get_parameter("ic_keyframe").get_parameter_value().string_value:
self.get_logger().info(f"Setting initial condition to keyframe: {potential_keyframe}")
mujoco.mju_copy(
self.mj_data.qpos, self.mj_model.key_qpos[i * self.mj_model.nq : (i + 1) * self.mj_model.nq]
)
mujoco.mju_copy(
self.mj_data.qvel, self.mj_model.key_qvel[i * self.mj_model.nv : (i + 1) * self.mj_model.nv]
)
self.mj_data.time = self.mj_model.key_time[i]

mujoco.mju_copy(
self.mj_data.ctrl, self.mj_model.key_ctrl[i * self.mj_model.nu : (i + 1) * self.mj_model.nu]
)

with mujoco.viewer.launch_passive(self.mj_model, self.mj_data) as viewer:
while viewer.is_running() and self.is_sim_running.value:
# simulate at realtime rate
Expand Down
2 changes: 2 additions & 0 deletions obelisk_ws/src/obelisk_ros/config/dummy_py.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ onboard:
- is_simulated: True
pkg: obelisk_sim_py
executable: obelisk_mujoco_robot
params:
ic_keyframe: other
# callback_groups:
# publishers:
subscribers:
Expand Down

0 comments on commit cab6196

Please sign in to comment.