Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

populate imu covariances when converting #375

Merged
merged 8 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion ros_gz_bridge/src/convert/sensor_msgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#include "convert/utils.hpp"
#include "ros_gz_bridge/convert/sensor_msgs.hpp"

#include "gz/msgs/config.hh"

#if GZ_MSGS_MAJOR_VERSION >= 10
#define GZ_MSGS_IMU_HAS_COVARIANCE
#endif

namespace ros_gz_bridge
{

Expand Down Expand Up @@ -274,6 +280,18 @@ convert_ros_to_gz(
convert_ros_to_gz(ros_msg.orientation, (*gz_msg.mutable_orientation()));
convert_ros_to_gz(ros_msg.angular_velocity, (*gz_msg.mutable_angular_velocity()));
convert_ros_to_gz(ros_msg.linear_acceleration, (*gz_msg.mutable_linear_acceleration()));

#ifdef GZ_MSGS_IMU_HAS_COVARIANCE
for (const auto & elem : ros_msg.linear_acceleration_covariance) {
gz_msg.mutable_linear_acceleration_covariance()->add_data(elem);
}
for (const auto & elem : ros_msg.orientation_covariance) {
gz_msg.mutable_orientation_covariance()->add_data(elem);
}
for (const auto & elem : ros_msg.angular_velocity_covariance) {
gz_msg.mutable_angular_velocity_covariance()->add_data(elem);
}
#endif
}

template<>
Expand All @@ -287,7 +305,29 @@ convert_gz_to_ros(
convert_gz_to_ros(gz_msg.angular_velocity(), ros_msg.angular_velocity);
convert_gz_to_ros(gz_msg.linear_acceleration(), ros_msg.linear_acceleration);

// Covariances not supported in gz::msgs::IMU
#ifdef GZ_MSGS_IMU_HAS_COVARIANCE
int data_size = gz_msg.linear_acceleration_covariance().data_size();
if (data_size == 9) {
for (int i = 0; i < data_size; ++i) {
auto data = gz_msg.linear_acceleration_covariance().data()[i];
ros_msg.linear_acceleration_covariance[i] = data;
}
}
data_size = gz_msg.angular_velocity_covariance().data_size();
if (data_size == 9) {
for (int i = 0; i < data_size; ++i) {
auto data = gz_msg.angular_velocity_covariance().data()[i];
ros_msg.angular_velocity_covariance[i] = data;
}
}
data_size = gz_msg.orientation_covariance().data_size();
if (data_size == 9) {
for (int i = 0; i < data_size; ++i) {
auto data = gz_msg.orientation_covariance().data()[i];
ros_msg.orientation_covariance[i] = data;
}
}
#endif
}

template<>
Expand Down
18 changes: 18 additions & 0 deletions ros_gz_bridge/test/utils/gz_test_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <memory>
#include <string>

#if GZ_MSGS_MAJOR_VERSION >= 10
#define GZ_MSGS_IMU_HAS_COVARIANCE
#endif

namespace ros_gz_bridge
{
namespace testing
Expand Down Expand Up @@ -807,6 +811,13 @@ void createTestMsg(gz::msgs::IMU & _msg)
_msg.mutable_orientation()->CopyFrom(quaternion_msg);
_msg.mutable_angular_velocity()->CopyFrom(vector3_msg);
_msg.mutable_linear_acceleration()->CopyFrom(vector3_msg);
#ifdef GZ_MSGS_IMU_HAS_COVARIANCE
for (int i = 0; i < 9; i++) {
_msg.mutable_orientation_covariance()->add_data(i + 1);
_msg.mutable_angular_velocity_covariance()->add_data(i + 1);
_msg.mutable_linear_acceleration_covariance()->add_data(i + 1);
}
#endif
}

void compareTestMsg(const std::shared_ptr<gz::msgs::IMU> & _msg)
Expand All @@ -815,6 +826,13 @@ void compareTestMsg(const std::shared_ptr<gz::msgs::IMU> & _msg)
compareTestMsg(std::make_shared<gz::msgs::Quaternion>(_msg->orientation()));
compareTestMsg(std::make_shared<gz::msgs::Vector3d>(_msg->angular_velocity()));
compareTestMsg(std::make_shared<gz::msgs::Vector3d>(_msg->linear_acceleration()));
#ifdef GZ_MSGS_IMU_HAS_COVARIANCE
for (int i = 0; i < 9; i++) {
EXPECT_EQ(_msg->orientation_covariance().data(i), i + 1);
EXPECT_EQ(_msg->angular_velocity_covariance().data(i), i + 1);
EXPECT_EQ(_msg->linear_acceleration_covariance().data(i), i + 1);
}
#endif
}

void createTestMsg(gz::msgs::Axis & _msg)
Expand Down
19 changes: 19 additions & 0 deletions ros_gz_bridge/test/utils/ros_test_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include <memory>
#include <string>

#include "gz/msgs/config.hh"

#if GZ_MSGS_MAJOR_VERSION >= 10
#define GZ_MSGS_IMU_HAS_COVARIANCE
#endif

namespace ros_gz_bridge
{
namespace testing
Expand Down Expand Up @@ -1064,6 +1070,11 @@ void createTestMsg(sensor_msgs::msg::Imu & _msg)
_msg.orientation = quaternion_msg;
_msg.angular_velocity = vector3_msg;
_msg.linear_acceleration = vector3_msg;
#ifdef GZ_MSGS_IMU_HAS_COVARIANCE
_msg.orientation_covariance = {1, 2, 3, 4, 5, 6, 7, 8, 9};
_msg.angular_velocity_covariance = {1, 2, 3, 4, 5, 6, 7, 8, 9};
_msg.linear_acceleration_covariance = {1, 2, 3, 4, 5, 6, 7, 8, 9};
#endif
}

void compareTestMsg(const std::shared_ptr<sensor_msgs::msg::Imu> & _msg)
Expand All @@ -1072,6 +1083,14 @@ void compareTestMsg(const std::shared_ptr<sensor_msgs::msg::Imu> & _msg)
compareTestMsg(_msg->orientation);
compareTestMsg(std::make_shared<geometry_msgs::msg::Vector3>(_msg->angular_velocity));
compareTestMsg(std::make_shared<geometry_msgs::msg::Vector3>(_msg->linear_acceleration));

#ifdef GZ_MSGS_IMU_HAS_COVARIANCE
for (int i = 0; i < 9; ++i) {
EXPECT_EQ(_msg->orientation_covariance[i], i + 1);
EXPECT_EQ(_msg->angular_velocity_covariance[i], i + 1);
EXPECT_EQ(_msg->linear_acceleration_covariance[i], i + 1);
}
#endif
}

void createTestMsg(sensor_msgs::msg::JointState & _msg)
Expand Down
Loading