Modulo 5.0.0
Loading...
Searching...
No Matches
message_writers.cpp
1#include "modulo_core/translators/message_writers.hpp"
2
3#include <state_representation/space/cartesian/CartesianPose.hpp>
4
5using namespace state_representation;
6
8
9static void write_point(geometry_msgs::msg::Point& message, const Eigen::Vector3d& vector) {
10 message.x = vector.x();
11 message.y = vector.y();
12 message.z = vector.z();
13}
14
15static void write_vector3(geometry_msgs::msg::Vector3& message, const Eigen::Vector3d& vector) {
16 message.x = vector.x();
17 message.y = vector.y();
18 message.z = vector.z();
19}
20
21static void write_quaternion(geometry_msgs::msg::Quaternion& message, const Eigen::Quaterniond& quat) {
22 message.w = quat.w();
23 message.x = quat.x();
24 message.y = quat.y();
25 message.z = quat.z();
26}
27
28void write_message(geometry_msgs::msg::Accel& message, const CartesianState& state, const rclcpp::Time&) {
29 if (!state) {
30 throw exceptions::MessageTranslationException(
31 state.get_name() + " state is empty while attempting to write it to message");
32 }
33 write_vector3(message.linear, state.get_linear_acceleration());
34 write_vector3(message.angular, state.get_angular_acceleration());
35}
36
37void write_message(geometry_msgs::msg::AccelStamped& message, const CartesianState& state, const rclcpp::Time& time) {
38 write_message(message.accel, state, time);
39 message.header.stamp = time;
40 message.header.frame_id = state.get_reference_frame();
41}
42
43void write_message(geometry_msgs::msg::Pose& message, const CartesianState& state, const rclcpp::Time&) {
44 if (!state) {
45 throw exceptions::MessageTranslationException(
46 state.get_name() + " state is empty while attempting to write it to message");
47 }
48 write_point(message.position, state.get_position());
49 write_quaternion(message.orientation, state.get_orientation());
50}
51
52void write_message(geometry_msgs::msg::PoseStamped& message, const CartesianState& state, const rclcpp::Time& time) {
53 write_message(message.pose, state, time);
54 message.header.stamp = time;
55 message.header.frame_id = state.get_reference_frame();
56}
57
58void write_message(geometry_msgs::msg::Transform& message, const CartesianState& state, const rclcpp::Time&) {
59 if (!state) {
60 throw exceptions::MessageTranslationException(
61 state.get_name() + " state is empty while attempting to write it to message");
62 }
63 write_vector3(message.translation, state.get_position());
64 write_quaternion(message.rotation, state.get_orientation());
65}
66
67void write_message(
68 geometry_msgs::msg::TransformStamped& message, const CartesianState& state, const rclcpp::Time& time) {
69 write_message(message.transform, state, time);
70 message.header.stamp = time;
71 message.header.frame_id = state.get_reference_frame();
72 message.child_frame_id = state.get_name();
73}
74
75void write_message(geometry_msgs::msg::Twist& message, const CartesianState& state, const rclcpp::Time&) {
76 if (!state) {
77 throw exceptions::MessageTranslationException(
78 state.get_name() + " state is empty while attempting to write it to message");
79 }
80 write_vector3(message.linear, state.get_linear_velocity());
81 write_vector3(message.angular, state.get_angular_velocity());
82}
83
84void write_message(geometry_msgs::msg::TwistStamped& message, const CartesianState& state, const rclcpp::Time& time) {
85 write_message(message.twist, state, time);
86 message.header.stamp = time;
87 message.header.frame_id = state.get_reference_frame();
88}
89
90void write_message(geometry_msgs::msg::Wrench& message, const CartesianState& state, const rclcpp::Time&) {
91 if (!state) {
92 throw exceptions::MessageTranslationException(
93 state.get_name() + " state is empty while attempting to write it to message");
94 }
95 write_vector3(message.force, state.get_force());
96 write_vector3(message.torque, state.get_torque());
97}
98
99void write_message(geometry_msgs::msg::WrenchStamped& message, const CartesianState& state, const rclcpp::Time& time) {
100 write_message(message.wrench, state, time);
101 message.header.stamp = time;
102 message.header.frame_id = state.get_reference_frame();
103}
104
105void write_message(sensor_msgs::msg::JointState& message, const JointState& state, const rclcpp::Time& time) {
106 if (!state) {
107 throw exceptions::MessageTranslationException(
108 state.get_name() + " state is empty while attempting to write it to message");
109 }
110 message.header.stamp = time;
111 message.name = state.get_names();
112 message.position =
113 std::vector<double>(state.get_positions().data(), state.get_positions().data() + state.get_positions().size());
114 message.velocity =
115 std::vector<double>(state.get_velocities().data(), state.get_velocities().data() + state.get_velocities().size());
116 message.effort =
117 std::vector<double>(state.get_torques().data(), state.get_torques().data() + state.get_torques().size());
118}
119
120void write_message(tf2_msgs::msg::TFMessage& message, const CartesianState& state, const rclcpp::Time& time) {
121 if (!state) {
122 throw exceptions::MessageTranslationException(
123 state.get_name() + " state is empty while attempting to write it to message");
124 }
125 geometry_msgs::msg::TransformStamped transform;
126 write_message(transform, state, time);
127 message.transforms.push_back(transform);
128}
129
130template<typename U, typename T>
131void write_message(U& message, const Parameter<T>& state, const rclcpp::Time&) {
132 if (!state) {
133 throw exceptions::MessageTranslationException(
134 state.get_name() + " state is empty while attempting to write it to message");
135 }
136 message.data = state.get_value();
137}
138
139template void write_message<std_msgs::msg::Float64, double>(
140 std_msgs::msg::Float64& message, const Parameter<double>& state, const rclcpp::Time&);
141
142template void write_message<std_msgs::msg::Float64MultiArray, std::vector<double>>(
143 std_msgs::msg::Float64MultiArray& message, const Parameter<std::vector<double>>& state, const rclcpp::Time&);
144
145template void write_message<std_msgs::msg::Bool, bool>(
146 std_msgs::msg::Bool& message, const Parameter<bool>& state, const rclcpp::Time&);
147
148template void write_message<std_msgs::msg::String, std::string>(
149 std_msgs::msg::String& message, const Parameter<std::string>& state, const rclcpp::Time&);
150
151template<>
152void write_message(
153 geometry_msgs::msg::Transform& message, const Parameter<CartesianPose>& state, const rclcpp::Time& time) {
154 write_message(message, state.get_value(), time);
155}
156
157template<>
158void write_message(
159 geometry_msgs::msg::TransformStamped& message, const Parameter<CartesianPose>& state, const rclcpp::Time& time) {
160 write_message(message, state.get_value(), time);
161}
162
163template<>
164void write_message(tf2_msgs::msg::TFMessage& message, const Parameter<CartesianPose>& state, const rclcpp::Time& time) {
165 write_message(message, state.get_value(), time);
166}
167
168void write_message(std_msgs::msg::Bool& message, const bool& state, const rclcpp::Time&) {
169 message.data = state;
170}
171
172void write_message(std_msgs::msg::Float64& message, const double& state, const rclcpp::Time&) {
173 message.data = state;
174}
175
176void write_message(std_msgs::msg::Float64MultiArray& message, const std::vector<double>& state, const rclcpp::Time&) {
177 message.data = state;
178}
179
180void write_message(std_msgs::msg::Int32& message, const int& state, const rclcpp::Time&) {
181 message.data = state;
182}
183
184void write_message(std_msgs::msg::String& message, const std::string& state, const rclcpp::Time&) {
185 message.data = state;
186}
187}// namespace modulo_core::translators
Modulo Core translation module for converting between ROS2 and state_representation data types.
void write_message(geometry_msgs::msg::Accel &message, const state_representation::CartesianState &state, const rclcpp::Time &time)
Convert a CartesianState to a ROS geometry_msgs::msg::Accel.