Modulo 5.0.0
Loading...
Searching...
No Matches
JointPositionsBroadcaster.hpp
1#pragma once
2
3#include <memory>
4#include <vector>
5
6#include <rclcpp/rclcpp.hpp>
7
8#include <modulo_interfaces/msg/joint_positions.hpp>
9#include <modulo_interfaces/msg/joint_positions_collection.hpp>
10#include <state_representation/space/joint/JointPositions.hpp>
11
12#include "modulo_core/joint_positions_options.hpp"
13
14namespace modulo_core {
15
22public:
26 template<class NodeT, class AllocatorT = std::allocator<void>>
28 NodeT&& node, const rclcpp::QoS& qos = rclcpp::QoS(1).transient_local(),
29 const rclcpp::PublisherOptionsWithAllocator<AllocatorT>& options =
30 detail::get_default_joint_positions_options<rclcpp::PublisherOptionsWithAllocator<AllocatorT>>())
32 rclcpp::node_interfaces::get_node_parameters_interface(node),
33 rclcpp::node_interfaces::get_node_topics_interface(node), qos, options) {}
34
38 template<class AllocatorT = std::allocator<void>>
40 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
41 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
42 const rclcpp::QoS& qos = rclcpp::QoS(1).transient_local(),
43 const rclcpp::PublisherOptionsWithAllocator<AllocatorT>& options =
44 detail::get_default_joint_positions_options<rclcpp::PublisherOptionsWithAllocator<AllocatorT>>()) {
45 publisher_ = rclcpp::create_publisher<modulo_interfaces::msg::JointPositionsCollection>(
46 node_parameters, node_topics, "/joint_positions", qos, options);
47 }
48
54 template<typename T>
55 void send(const T& joint_positions);
56
62 template<typename T>
63 void send(const std::vector<T>& joint_positions);
64
65private:
66 template<typename T>
67 void send(
68 const std::vector<T>& joint_positions, std::function<std::string(T)> get_name,
69 std::function<modulo_interfaces::msg::JointPositions(T)> translator);
70
71 std::shared_ptr<rclcpp::Publisher<modulo_interfaces::msg::JointPositionsCollection>> publisher_;
72 modulo_interfaces::msg::JointPositionsCollection net_message_;
73};
74
75template<typename T>
76inline void JointPositionsBroadcaster::send(const T& joint_positions) {
77 send(std::vector<T>{joint_positions});
78}
79
80template<typename T>
82 const std::vector<T>& joint_positions, std::function<std::string(T)> get_name,
83 std::function<modulo_interfaces::msg::JointPositions(T)> translator) {
84 for (auto it_in = joint_positions.begin(); it_in != joint_positions.end(); ++it_in) {
85 bool match_found = false;
86 for (auto it_msg = net_message_.joint_positions.begin(); it_msg != net_message_.joint_positions.end(); ++it_msg) {
87 if (get_name(*it_in) == it_msg->header.frame_id) {
88 *it_msg = translator(*it_in);
89 match_found = true;
90 break;
91 }
92 }
93 if (!match_found) {
94 net_message_.joint_positions.push_back(translator(*it_in));
95 }
96 }
97 publisher_->publish(net_message_);
98}
99}// namespace modulo_core
The JointPositionsBroadcaster is a TF2 style class that publishes a collection of JointPositions mess...
void send(const T &joint_positions)
Send a JointPositions object.
JointPositionsBroadcaster(rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters, rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics, const rclcpp::QoS &qos=rclcpp::QoS(1).transient_local(), const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options=detail::get_default_joint_positions_options< rclcpp::PublisherOptionsWithAllocator< AllocatorT > >())
Constructor of the JointPositionsBroadcaster with node interfaces.
JointPositionsBroadcaster(NodeT &&node, const rclcpp::QoS &qos=rclcpp::QoS(1).transient_local(), const rclcpp::PublisherOptionsWithAllocator< AllocatorT > &options=detail::get_default_joint_positions_options< rclcpp::PublisherOptionsWithAllocator< AllocatorT > >())
Constructor of the JointPositionsBroadcaster with a node.
void send(const std::vector< T > &joint_positions)
Send a vector of JointPositions objects.
Modulo Core.