1#include "modulo_core/translators/message_writers.hpp"
3#include <state_representation/space/cartesian/CartesianPose.hpp>
5using namespace state_representation;
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();
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();
21static void write_quaternion(geometry_msgs::msg::Quaternion& message,
const Eigen::Quaterniond& quat) {
28void write_message(geometry_msgs::msg::Accel& message,
const CartesianState& state,
const rclcpp::Time&) {
30 throw exceptions::MessageTranslationException(
31 state.get_name() +
" state is empty while attempting to write it to message");
33 write_vector3(message.linear, state.get_linear_acceleration());
34 write_vector3(message.angular, state.get_angular_acceleration());
37void write_message(geometry_msgs::msg::AccelStamped& message,
const CartesianState& state,
const rclcpp::Time& time) {
39 message.header.stamp = time;
40 message.header.frame_id = state.get_reference_frame();
43void write_message(geometry_msgs::msg::Pose& message,
const CartesianState& state,
const rclcpp::Time&) {
45 throw exceptions::MessageTranslationException(
46 state.get_name() +
" state is empty while attempting to write it to message");
48 write_point(message.position, state.get_position());
49 write_quaternion(message.orientation, state.get_orientation());
52void write_message(geometry_msgs::msg::PoseStamped& message,
const CartesianState& state,
const rclcpp::Time& time) {
54 message.header.stamp = time;
55 message.header.frame_id = state.get_reference_frame();
58void write_message(geometry_msgs::msg::Transform& message,
const CartesianState& state,
const rclcpp::Time&) {
60 throw exceptions::MessageTranslationException(
61 state.get_name() +
" state is empty while attempting to write it to message");
63 write_vector3(message.translation, state.get_position());
64 write_quaternion(message.rotation, state.get_orientation());
68 geometry_msgs::msg::TransformStamped& message,
const CartesianState& state,
const rclcpp::Time& time) {
70 message.header.stamp = time;
71 message.header.frame_id = state.get_reference_frame();
72 message.child_frame_id = state.get_name();
75void write_message(geometry_msgs::msg::Twist& message,
const CartesianState& state,
const rclcpp::Time&) {
77 throw exceptions::MessageTranslationException(
78 state.get_name() +
" state is empty while attempting to write it to message");
80 write_vector3(message.linear, state.get_linear_velocity());
81 write_vector3(message.angular, state.get_angular_velocity());
84void write_message(geometry_msgs::msg::TwistStamped& message,
const CartesianState& state,
const rclcpp::Time& time) {
86 message.header.stamp = time;
87 message.header.frame_id = state.get_reference_frame();
90void write_message(geometry_msgs::msg::Wrench& message,
const CartesianState& state,
const rclcpp::Time&) {
92 throw exceptions::MessageTranslationException(
93 state.get_name() +
" state is empty while attempting to write it to message");
95 write_vector3(message.force, state.get_force());
96 write_vector3(message.torque, state.get_torque());
99void write_message(geometry_msgs::msg::WrenchStamped& message,
const CartesianState& state,
const rclcpp::Time& time) {
101 message.header.stamp = time;
102 message.header.frame_id = state.get_reference_frame();
105void write_message(sensor_msgs::msg::JointState& message,
const JointState& state,
const rclcpp::Time& time) {
107 throw exceptions::MessageTranslationException(
108 state.get_name() +
" state is empty while attempting to write it to message");
110 message.header.stamp = time;
111 message.name = state.get_names();
113 std::vector<double>(state.get_positions().data(), state.get_positions().data() + state.get_positions().size());
115 std::vector<double>(state.get_velocities().data(), state.get_velocities().data() + state.get_velocities().size());
117 std::vector<double>(state.get_torques().data(), state.get_torques().data() + state.get_torques().size());
120void write_message(tf2_msgs::msg::TFMessage& message,
const CartesianState& state,
const rclcpp::Time& time) {
122 throw exceptions::MessageTranslationException(
123 state.get_name() +
" state is empty while attempting to write it to message");
125 geometry_msgs::msg::TransformStamped transform;
127 message.transforms.push_back(transform);
130template<
typename U,
typename T>
131void write_message(U& message,
const Parameter<T>& state,
const rclcpp::Time&) {
133 throw exceptions::MessageTranslationException(
134 state.get_name() +
" state is empty while attempting to write it to message");
136 message.data = state.get_value();
139template void write_message<std_msgs::msg::Float64, double>(
140 std_msgs::msg::Float64& message,
const Parameter<double>& state,
const rclcpp::Time&);
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&);
145template void write_message<std_msgs::msg::Bool, bool>(
146 std_msgs::msg::Bool& message,
const Parameter<bool>& state,
const rclcpp::Time&);
148template void write_message<std_msgs::msg::String, std::string>(
149 std_msgs::msg::String& message,
const Parameter<std::string>& state,
const rclcpp::Time&);
153 geometry_msgs::msg::Transform& message,
const Parameter<CartesianPose>& state,
const rclcpp::Time& time) {
159 geometry_msgs::msg::TransformStamped& message,
const Parameter<CartesianPose>& state,
const rclcpp::Time& time) {
164void write_message(tf2_msgs::msg::TFMessage& message,
const Parameter<CartesianPose>& state,
const rclcpp::Time& time) {
168void write_message(std_msgs::msg::Bool& message,
const bool& state,
const rclcpp::Time&) {
169 message.data = state;
172void write_message(std_msgs::msg::Float64& message,
const double& state,
const rclcpp::Time&) {
173 message.data = state;
176void write_message(std_msgs::msg::Float64MultiArray& message,
const std::vector<double>& state,
const rclcpp::Time&) {
177 message.data = state;
180void write_message(std_msgs::msg::Int32& message,
const int& state,
const rclcpp::Time&) {
181 message.data = state;
184void write_message(std_msgs::msg::String& message,
const std::string& state,
const rclcpp::Time&) {
185 message.data = state;
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.