|
| 1 | +#include <vortex/utils/math.hpp> |
| 2 | +#include "nav_msgs/msg/odometry.hpp" |
| 3 | +#include "rclcpp/rclcpp.hpp" |
| 4 | +#include "vortex_msgs/msg/reference_filter_quat.hpp" |
| 5 | +#include "vortex_msgs/msg/rpy.hpp" |
| 6 | +#include "vortex_msgs/msg/waypoint.hpp" |
| 7 | + |
| 8 | +#include <variant> |
| 9 | + |
| 10 | +class MessagePublisherNode : public rclcpp::Node { |
| 11 | + public: |
| 12 | + MessagePublisherNode() : Node("message_publisher_node") { |
| 13 | + this->declare_parameter<std::string>("input_type", "odometry"); |
| 14 | + this->declare_parameter<std::string>("topics.odom", "nautilus/odom"); |
| 15 | + this->declare_parameter<std::string>("topics.waypoint", |
| 16 | + "nautilus/waypoint"); |
| 17 | + this->declare_parameter<std::string>("topics.reference_filter", |
| 18 | + "nautilus/reference_filter"); |
| 19 | + this->declare_parameter<std::string>("topics.output", |
| 20 | + "utils/odometry/euler"); |
| 21 | + |
| 22 | + auto input_type = this->get_parameter("input_type").as_string(); |
| 23 | + auto output_topic = this->get_parameter("topics.output").as_string(); |
| 24 | + |
| 25 | + rpy_pub_ = |
| 26 | + this->create_publisher<vortex_msgs::msg::RPY>(output_topic, 2); |
| 27 | + |
| 28 | + if (input_type == "odometry") { |
| 29 | + auto topic = this->get_parameter("topics.odom").as_string(); |
| 30 | + sub_ = this->create_subscription<nav_msgs::msg::Odometry>( |
| 31 | + topic, 2, |
| 32 | + std::bind(&MessagePublisherNode::odom_callback, this, |
| 33 | + std::placeholders::_1)); |
| 34 | + RCLCPP_INFO(this->get_logger(), |
| 35 | + "Euler publisher [odometry]: %s -> %s", topic.c_str(), |
| 36 | + output_topic.c_str()); |
| 37 | + |
| 38 | + } else if (input_type == "waypoint") { |
| 39 | + auto topic = this->get_parameter("topics.waypoint").as_string(); |
| 40 | + sub_ = this->create_subscription<vortex_msgs::msg::Waypoint>( |
| 41 | + topic, 2, |
| 42 | + std::bind(&MessagePublisherNode::waypoint_callback, this, |
| 43 | + std::placeholders::_1)); |
| 44 | + RCLCPP_INFO(this->get_logger(), |
| 45 | + "Euler publisher [waypoint]: %s -> %s", topic.c_str(), |
| 46 | + output_topic.c_str()); |
| 47 | + |
| 48 | + } else if (input_type == "reference_filter") { |
| 49 | + auto topic = |
| 50 | + this->get_parameter("topics.reference_filter").as_string(); |
| 51 | + sub_ = this->create_subscription< |
| 52 | + vortex_msgs::msg::ReferenceFilterQuat>( |
| 53 | + topic, 2, |
| 54 | + std::bind(&MessagePublisherNode::ref_filter_callback, this, |
| 55 | + std::placeholders::_1)); |
| 56 | + RCLCPP_INFO(this->get_logger(), |
| 57 | + "Euler publisher [reference_filter]: %s -> %s", |
| 58 | + topic.c_str(), output_topic.c_str()); |
| 59 | + |
| 60 | + } else { |
| 61 | + RCLCPP_FATAL(this->get_logger(), |
| 62 | + "Unknown input_type: '%s'. Must be 'odometry', " |
| 63 | + "'waypoint', or 'reference_filter'.", |
| 64 | + input_type.c_str()); |
| 65 | + rclcpp::shutdown(); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private: |
| 71 | + Eigen::Vector3d convert_quat(double w, double x, double y, double z) { |
| 72 | + Eigen::Quaterniond eigen_q(w, x, y, z); |
| 73 | + return vortex::utils::math::quat_to_euler(eigen_q); |
| 74 | + } |
| 75 | + |
| 76 | + void publish_rpy(const std_msgs::msg::Header& header, |
| 77 | + const Eigen::Vector3d& euler) { |
| 78 | + vortex_msgs::msg::RPY msg; |
| 79 | + msg.header = header; |
| 80 | + msg.roll = euler.x(); |
| 81 | + msg.pitch = euler.y(); |
| 82 | + msg.yaw = euler.z(); |
| 83 | + rpy_pub_->publish(msg); |
| 84 | + } |
| 85 | + |
| 86 | + void odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg) { |
| 87 | + const auto& q = msg->pose.pose.orientation; |
| 88 | + auto euler = convert_quat(q.w, q.x, q.y, q.z); |
| 89 | + publish_rpy(msg->header, euler); |
| 90 | + } |
| 91 | + |
| 92 | + void waypoint_callback(const vortex_msgs::msg::Waypoint::SharedPtr msg) { |
| 93 | + const auto& q = msg->pose.orientation; |
| 94 | + auto euler = convert_quat(q.w, q.x, q.y, q.z); |
| 95 | + std_msgs::msg::Header header; |
| 96 | + header.stamp = this->now(); |
| 97 | + publish_rpy(header, euler); |
| 98 | + } |
| 99 | + |
| 100 | + void ref_filter_callback( |
| 101 | + const vortex_msgs::msg::ReferenceFilterQuat::SharedPtr msg) { |
| 102 | + auto euler = convert_quat(msg->qw, msg->qx, msg->qy, msg->qz); |
| 103 | + publish_rpy(msg->header, euler); |
| 104 | + } |
| 105 | + |
| 106 | + using SubVariant = std::variant< |
| 107 | + rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr, |
| 108 | + rclcpp::Subscription<vortex_msgs::msg::Waypoint>::SharedPtr, |
| 109 | + rclcpp::Subscription<vortex_msgs::msg::ReferenceFilterQuat>::SharedPtr>; |
| 110 | + |
| 111 | + SubVariant sub_; |
| 112 | + rclcpp::Publisher<vortex_msgs::msg::RPY>::SharedPtr rpy_pub_; |
| 113 | +}; |
| 114 | + |
| 115 | +int main(int argc, char** argv) { |
| 116 | + rclcpp::init(argc, argv); |
| 117 | + auto node = std::make_shared<MessagePublisherNode>(); |
| 118 | + rclcpp::spin(node); |
| 119 | + rclcpp::shutdown(); |
| 120 | + return 0; |
| 121 | +} |
0 commit comments