1#include "modulo_core/translators/message_readers.hpp"
5static Eigen::Vector3d read_point(
const geometry_msgs::msg::Point& message) {
6 return {message.x, message.y, message.z};
9static Eigen::Vector3d read_vector3(
const geometry_msgs::msg::Vector3& message) {
10 return {message.x, message.y, message.z};
13static Eigen::Quaterniond read_quaternion(
const geometry_msgs::msg::Quaternion& message) {
14 return {message.w, message.x, message.y, message.z};
17void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::Accel& message) {
18 state.set_linear_acceleration(read_vector3(message.linear));
19 state.set_angular_acceleration(read_vector3(message.angular));
22void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::AccelStamped& message) {
23 state.set_reference_frame(message.header.frame_id);
27void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::Pose& message) {
28 state.set_position(read_point(message.position));
29 state.set_orientation(read_quaternion(message.orientation));
32void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::PoseStamped& message) {
33 state.set_reference_frame(message.header.frame_id);
37void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::Transform& message) {
38 state.set_position(read_vector3(message.translation));
39 state.set_orientation(read_quaternion(message.rotation));
42void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::TransformStamped& message) {
43 state.set_reference_frame(message.header.frame_id);
44 state.set_name(message.child_frame_id);
48void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::Twist& message) {
49 state.set_linear_velocity(read_vector3(message.linear));
50 state.set_angular_velocity(read_vector3(message.angular));
53void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::TwistStamped& message) {
54 state.set_reference_frame(message.header.frame_id);
58void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::Wrench& message) {
59 state.set_force(read_vector3(message.force));
60 state.set_torque(read_vector3(message.torque));
63void read_message(state_representation::CartesianState& state,
const geometry_msgs::msg::WrenchStamped& message) {
64 state.set_reference_frame(message.header.frame_id);
68void read_message(state_representation::JointState& state,
const sensor_msgs::msg::JointState& message) {
70 state.set_names(message.name);
71 if (!message.position.empty()) {
72 state.set_positions(Eigen::VectorXd::Map(message.position.data(), message.position.size()));
74 if (!message.velocity.empty()) {
75 state.set_velocities(Eigen::VectorXd::Map(message.velocity.data(), message.velocity.size()));
77 if (!message.effort.empty()) {
78 state.set_torques(Eigen::VectorXd::Map(message.effort.data(), message.effort.size()));
80 }
catch (
const std::exception& ex) {
85void read_message(
bool& state,
const std_msgs::msg::Bool& message) {
89void read_message(
double& state,
const std_msgs::msg::Float64& message) {
93void read_message(std::vector<double>& state,
const std_msgs::msg::Float64MultiArray& message) {
97void read_message(
int& state,
const std_msgs::msg::Int32& message) {
101void read_message(std::string& state,
const std_msgs::msg::String& message) {
102 state = message.data;
An exception class to notify that the translation of a ROS message failed.
Modulo Core translation module for converting between ROS2 and state_representation data types.
void read_message(state_representation::CartesianState &state, const geometry_msgs::msg::Accel &message)
Convert a ROS geometry_msgs::msg::Accel to a CartesianState.