Skip to content

Commit 40ca5ac

Browse files
committed
fix: body quaternion fetching in GetBodyStateCB
fixes #36
1 parent 602208d commit 40ca5ac

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Loading and reset times are reported in the server debug log. All plugin stats c
1414
* *mujoco_ros_control*: fixed sometimes using wrong joint id in default hardware interface (would only be correct, if the joints appear first and in the same order in the compiled MuJoCo model).
1515
* *mujoco_ros_sensors*: now skipping user sensors, as they should be handled in separate, dedicated plugins.
1616
* When loading takes more than 0.25 seconds the simulation is no longer automatically paused.
17+
* Fixed fetching of body quaternion in `get_body_state` service.
1718

1819
### Changed
1920
* Moved `mujoco_ros::Viewer::Clock` definition to `mujoco_ros::Clock` (into common_types.h).

mujoco_ros/src/callbacks.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ bool MujocoEnv::getBodyStateCB(mujoco_ros_msgs::GetBodyState::Request &req,
413413
resp.state.pose.pose.position.x = data_->xpos[body_id * 3];
414414
resp.state.pose.pose.position.y = data_->xpos[body_id * 3 + 1];
415415
resp.state.pose.pose.position.z = data_->xpos[body_id * 3 + 2];
416-
resp.state.pose.pose.orientation.w = data_->xquat[body_id * 3];
417-
resp.state.pose.pose.orientation.x = data_->xquat[body_id * 3 + 1];
418-
resp.state.pose.pose.orientation.y = data_->xquat[body_id * 3 + 2];
419-
resp.state.pose.pose.orientation.z = data_->xquat[body_id * 3 + 3];
416+
resp.state.pose.pose.orientation.w = data_->xquat[body_id * 4];
417+
resp.state.pose.pose.orientation.x = data_->xquat[body_id * 4 + 1];
418+
resp.state.pose.pose.orientation.y = data_->xquat[body_id * 4 + 2];
419+
resp.state.pose.pose.orientation.z = data_->xquat[body_id * 4 + 3];
420420

421421
resp.state.twist.header = std_msgs::Header();
422422
resp.state.twist.header.frame_id = "world";

mujoco_ros/test/ros_interface_test.cpp

+50-3
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,54 @@ TEST_F(PendulumEnvFixture, SetBodyStateCallback)
538538
compare_qvel(d, m->jnt_dofadr[id_free], "ball_freejoint", { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 });
539539
}
540540

541+
TEST_F(PendulumEnvFixture, GetBodyStateInvalidName)
542+
{
543+
EXPECT_FALSE(env_ptr->settings_.run) << "Simulation should be paused!";
544+
EXPECT_NEAR(env_ptr->getDataPtr()->time, 0, 1e-6) << "Simulation time should be 0.0!";
545+
EXPECT_TRUE(ros::service::exists(env_ptr->getHandleNamespace() + "/set_body_state", true))
546+
<< "Set body state service should be available!";
547+
EXPECT_TRUE(ros::service::exists(env_ptr->getHandleNamespace() + "/get_body_state", true))
548+
<< "Get body state service should be available!";
549+
550+
mujoco_ros_msgs::GetBodyState g_srv;
551+
// wrong body name
552+
g_srv.request.name = "unknown";
553+
EXPECT_TRUE(ros::service::call(env_ptr->getHandleNamespace() + "/get_body_state", g_srv))
554+
<< "get body state service call failed!";
555+
EXPECT_FALSE(g_srv.response.success);
556+
}
557+
558+
TEST_F(PendulumEnvFixture, GetBodyStateStaticBody)
559+
{
560+
EXPECT_FALSE(env_ptr->settings_.run) << "Simulation should be paused!";
561+
EXPECT_NEAR(env_ptr->getDataPtr()->time, 0, 1e-6) << "Simulation time should be 0.0!";
562+
EXPECT_TRUE(ros::service::exists(env_ptr->getHandleNamespace() + "/set_body_state", true))
563+
<< "Set body state service should be available!";
564+
EXPECT_TRUE(ros::service::exists(env_ptr->getHandleNamespace() + "/get_body_state", true))
565+
<< "Get body state service should be available!";
566+
567+
mujoco_ros_msgs::GetBodyState g_srv;
568+
g_srv.request.name = "immovable";
569+
EXPECT_TRUE(ros::service::call(env_ptr->getHandleNamespace() + "/get_body_state", g_srv))
570+
<< "get body state service call failed!";
571+
EXPECT_TRUE(g_srv.response.success);
572+
EXPECT_EQ(g_srv.response.state.name, "immovable");
573+
EXPECT_DOUBLE_EQ(g_srv.response.state.mass, 0.10239999741315842);
574+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.position.x, 0.56428);
575+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.position.y, 0.221972);
576+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.position.z, 0.6);
577+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.orientation.w, 1.0);
578+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.orientation.x, 0.0);
579+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.orientation.y, 0.0);
580+
EXPECT_DOUBLE_EQ(g_srv.response.state.pose.pose.orientation.z, 0.0);
581+
EXPECT_DOUBLE_EQ(g_srv.response.state.twist.twist.linear.x, 0.0);
582+
EXPECT_DOUBLE_EQ(g_srv.response.state.twist.twist.linear.y, 0.0);
583+
EXPECT_DOUBLE_EQ(g_srv.response.state.twist.twist.linear.z, 0.0);
584+
EXPECT_DOUBLE_EQ(g_srv.response.state.twist.twist.angular.x, 0.0);
585+
EXPECT_DOUBLE_EQ(g_srv.response.state.twist.twist.angular.y, 0.0);
586+
EXPECT_DOUBLE_EQ(g_srv.response.state.twist.twist.angular.z, 0.0);
587+
}
588+
541589
TEST_F(PendulumEnvFixture, GetBodyStateCallback)
542590
{
543591
EXPECT_FALSE(env_ptr->settings_.run) << "Simulation should be paused!";
@@ -572,8 +620,6 @@ TEST_F(PendulumEnvFixture, GetBodyStateCallback)
572620
EXPECT_TRUE(srv.response.success);
573621

574622
mujoco_ros_msgs::GetBodyState g_srv;
575-
// wrong body name
576-
g_srv.request.name = "unknown";
577623

578624
// correct request
579625
g_srv.request.name = "body_ball";
@@ -582,7 +628,8 @@ TEST_F(PendulumEnvFixture, GetBodyStateCallback)
582628
EXPECT_TRUE(g_srv.response.success);
583629
EXPECT_EQ(g_srv.response.state.mass, srv.request.state.mass);
584630
EXPECT_EQ(g_srv.response.state.name, srv.request.state.name);
585-
EXPECT_EQ(g_srv.response.state.pose.pose, srv.request.state.pose.pose);
631+
EXPECT_EQ(g_srv.response.state.pose.pose.position, srv.request.state.pose.pose.position);
632+
EXPECT_EQ(g_srv.response.state.pose.pose.orientation, srv.request.state.pose.pose.orientation);
586633
EXPECT_EQ(g_srv.response.state.twist.twist, srv.request.state.twist.twist);
587634

588635
// TODO(dleins): tests for bodies with a non-freejoint, no joint, and multiple joints (cannot set position but read!)

0 commit comments

Comments
 (0)