Skip to content

Commit

Permalink
added ability to start from mujoco key frames
Browse files Browse the repository at this point in the history
  • Loading branch information
Zolkin1 committed Jul 26, 2024
1 parent 9d4ed48 commit 8522fbc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions obelisk/cpp/obelisk_cpp/include/obelisk_mujoco_sim_robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace obelisk {
public:
explicit ObeliskMujocoRobot(const std::string& name) : ObeliskSimRobot<ControlMessageT>(name) {
this->template declare_parameter<std::string>("mujoco_setting", "");
this->template declare_parameter<std::string>(
"ic_keyframe",
"ic"); // Parameter is the name of a mujoco key frame used as the initial condition. Defualts to "ic".

mujoco_sim_instance_ = this;
activation_complete_ = false;
Expand Down Expand Up @@ -199,6 +202,21 @@ namespace obelisk {
}
RCLCPP_INFO_STREAM(this->get_logger(), "Starting Mujoco simulation loop.");

// Set the initial condition based on a keyframe
RCLCPP_INFO_STREAM(this->get_logger(), "Found " << this->model_->nkey << " Mujoco keyframes.");
for (int i = 0; i < this->model_->nkey; i++) {
std::string potential_keyframe(this->model_->names + this->model_->name_keyadr[i]);
if (potential_keyframe == this->get_parameter("ic_keyframe").as_string()) {
RCLCPP_INFO_STREAM(this->get_logger(),
"Setting initial condition to keyframe: " << potential_keyframe);
mju_copy(this->data_->qpos, &this->model_->key_qpos[i * this->model_->nq], this->model_->nq);
mju_copy(this->data_->qvel, &this->model_->key_qvel[i * this->model_->nv], this->model_->nv);
this->data_->time = this->model_->key_time[i];

mju_copy(this->data_->ctrl, &this->model_->key_ctrl[i * this->model_->nu], this->model_->nu);
}
}

while (!this->stop_thread_) {
if (!glfwWindowShouldClose(window_)) {
auto start_time = std::chrono::steady_clock::now();
Expand Down
2 changes: 2 additions & 0 deletions obelisk_ws/src/obelisk_ros/config/dummy_cpp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ onboard:
- is_simulated: True
pkg: obelisk_sim_cpp
executable: obelisk_mujoco_robot
params:
ic_keyframe: ic
# callback_groups:
# publishers:
subscribers:
Expand Down
5 changes: 5 additions & 0 deletions obelisk_ws/src/robots/dummy_bot/mujoco/dummy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@
<framepos name="tip_pos_sensor" objtype="site" objname="tip"/>
<framequat name="tip_orientation_sensor" objtype="site" objname="tip"/>
</sensor>

<keyframe>
<key name="other" qpos="-1" ctrl="-1" time="1"/>
<key name="ic" qpos="1" ctrl="1" time="3" />
</keyframe>
</mujoco>

0 comments on commit 8522fbc

Please sign in to comment.