Modulo 5.0.0
Loading...
Searching...
No Matches
message_readers.cpp
1#include "modulo_core/translators/message_readers.hpp"
2
4
5static Eigen::Vector3d read_point(const geometry_msgs::msg::Point& message) {
6 return {message.x, message.y, message.z};
7}
8
9static Eigen::Vector3d read_vector3(const geometry_msgs::msg::Vector3& message) {
10 return {message.x, message.y, message.z};
11}
12
13static Eigen::Quaterniond read_quaternion(const geometry_msgs::msg::Quaternion& message) {
14 return {message.w, message.x, message.y, message.z};
15}
16
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));
20}
21
22void read_message(state_representation::CartesianState& state, const geometry_msgs::msg::AccelStamped& message) {
23 state.set_reference_frame(message.header.frame_id);
24 read_message(state, message.accel);
25}
26
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));
30}
31
32void read_message(state_representation::CartesianState& state, const geometry_msgs::msg::PoseStamped& message) {
33 state.set_reference_frame(message.header.frame_id);
34 read_message(state, message.pose);
35}
36
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));
40}
41
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);
45 read_message(state, message.transform);
46}
47
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));
51}
52
53void read_message(state_representation::CartesianState& state, const geometry_msgs::msg::TwistStamped& message) {
54 state.set_reference_frame(message.header.frame_id);
55 read_message(state, message.twist);
56}
57
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));
61}
62
63void read_message(state_representation::CartesianState& state, const geometry_msgs::msg::WrenchStamped& message) {
64 state.set_reference_frame(message.header.frame_id);
65 read_message(state, message.wrench);
66}
67
68void read_message(state_representation::JointState& state, const sensor_msgs::msg::JointState& message) {
69 try {
70 state.set_names(message.name);
71 if (!message.position.empty()) {
72 state.set_positions(Eigen::VectorXd::Map(message.position.data(), message.position.size()));
73 }
74 if (!message.velocity.empty()) {
75 state.set_velocities(Eigen::VectorXd::Map(message.velocity.data(), message.velocity.size()));
76 }
77 if (!message.effort.empty()) {
78 state.set_torques(Eigen::VectorXd::Map(message.effort.data(), message.effort.size()));
79 }
80 } catch (const std::exception& ex) {
82 }
83}
84
85void read_message(bool& state, const std_msgs::msg::Bool& message) {
86 state = message.data;
87}
88
89void read_message(double& state, const std_msgs::msg::Float64& message) {
90 state = message.data;
91}
92
93void read_message(std::vector<double>& state, const std_msgs::msg::Float64MultiArray& message) {
94 state = message.data;
95}
96
97void read_message(int& state, const std_msgs::msg::Int32& message) {
98 state = message.data;
99}
100
101void read_message(std::string& state, const std_msgs::msg::String& message) {
102 state = message.data;
103}
104}// namespace modulo_core::translators
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.