diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 63bac63c..e769230c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -51,7 +51,7 @@ repos: # Python hooks - repo: https://github.com/asottile/pyupgrade - rev: v3.17.0 + rev: v3.22.0 hooks: - id: pyupgrade args: [--py36-plus] diff --git a/mujoco_ros2_control/src/mujoco_system_interface.cpp b/mujoco_ros2_control/src/mujoco_system_interface.cpp index 559157b7..d8059092 100644 --- a/mujoco_ros2_control/src/mujoco_system_interface.cpp +++ b/mujoco_ros2_control/src/mujoco_system_interface.cpp @@ -2462,8 +2462,10 @@ void MujocoSystemInterface::reset_simulation_state(bool /*fill_initial_state*/) // Reset command interfaces to initial position commands for (auto& actuator : mujoco_actuator_data_) { - restore_authored_position_gains(simulation_.get(), &actuator); - actuator.is_impedance_control_enabled = false; + // Impedance mode follows the claimed kp/kd interfaces, not the world state: + // keep it so the active controller still drives the joint after a reset. + // Force write() to reprogram native-position gains on its next cycle. + actuator.last_requested_parameters.reset(); actuator.position_interface.state_ = simulation_->data()->qpos[actuator.mj_pos_adr]; actuator.velocity_interface.state_ = simulation_->data()->qvel[actuator.mj_vel_adr]; actuator.effort_interface.state_ = 0.0; @@ -2507,7 +2509,6 @@ void MujocoSystemInterface::reset_simulation_state(bool /*fill_initial_state*/) // Set joint commands to current state (maintaining position control) for (auto& joint : urdf_joint_data_) { - joint.is_impedance_control_enabled = false; joint.position_interface.command_ = joint.position_interface.state_; joint.velocity_interface.command_ = 0.0; joint.effort_interface.command_ = 0.0; diff --git a/mujoco_ros2_control/tests/test_headless_init.cpp b/mujoco_ros2_control/tests/test_headless_init.cpp index 37980e55..a7a24a70 100644 --- a/mujoco_ros2_control/tests/test_headless_init.cpp +++ b/mujoco_ros2_control/tests/test_headless_init.cpp @@ -493,7 +493,7 @@ TEST_F(HeadlessInitTest, PositionImpedanceHonorsAuthoredRangesAndRestoresAuthore EXPECT_DOUBLE_EQ(model->actuator_biasprm[actuator_id * mjNBIAS + 2], -0.7); mj_deleteModel(model); - // Re-enter impedance mode so reset exercises restoration independently. + // Re-enter impedance mode before the world reset. ASSERT_EQ(interface_->perform_command_mode_switch( { "hinge/position", "hinge/velocity", "hinge/effort", "hinge/kp", "hinge/kd" }, {}), hardware_interface::return_type::OK); @@ -512,27 +512,23 @@ TEST_F(HeadlessInitTest, PositionImpedanceHonorsAuthoredRangesAndRestoresAuthore rclcpp::FutureReturnCode::SUCCESS); ASSERT_TRUE(reset_future.get()->success); + // The controller still claims kp/kd, so the reset keeps impedance mode and + // the next write programs the commanded gains without a new mode switch. + ASSERT_EQ(interface_->write(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)), + hardware_interface::return_type::OK); model = nullptr; interface_->get_model(model); ASSERT_NE(model, nullptr); - EXPECT_DOUBLE_EQ(model->actuator_gainprm[actuator_id * mjNGAIN], 3.0); - EXPECT_DOUBLE_EQ(model->actuator_biasprm[actuator_id * mjNBIAS + 1], -3.0); - EXPECT_DOUBLE_EQ(model->actuator_biasprm[actuator_id * mjNBIAS + 2], -0.7); + EXPECT_DOUBLE_EQ(model->actuator_gainprm[actuator_id * mjNGAIN], 20.0); + EXPECT_DOUBLE_EQ(model->actuator_biasprm[actuator_id * mjNBIAS + 2], -4.0); mj_deleteModel(model); data = nullptr; interface_->get_data(data); ASSERT_NE(data, nullptr); EXPECT_DOUBLE_EQ(data->qpos[0], 0.5); - EXPECT_DOUBLE_EQ(data->qfrc_actuator[0], -1.5); mj_deleteData(data); - ASSERT_EQ(interface_->perform_command_mode_switch( - { "hinge/position", "hinge/velocity", "hinge/effort", "hinge/kp", "hinge/kd" }, {}), - hardware_interface::return_type::OK); - ASSERT_EQ(interface_->write(rclcpp::Time(0), rclcpp::Duration::from_seconds(0.01)), - hardware_interface::return_type::OK); - rclcpp_lifecycle::State inactive_state(0, "inactive"); ASSERT_EQ(interface_->on_deactivate(inactive_state), hardware_interface::CallbackReturn::SUCCESS);