Modulo 4.2.2
Loading...
Searching...
No Matches
PublisherInterface.cpp
1#include "modulo_core/communication/PublisherInterface.hpp"
2
3#include <utility>
4
5#include <rclcpp/publisher.hpp>
6#include <rclcpp_lifecycle/lifecycle_publisher.hpp>
7#include <std_msgs/msg/bool.hpp>
8#include <std_msgs/msg/float64.hpp>
9#include <std_msgs/msg/float64_multi_array.hpp>
10#include <std_msgs/msg/int32.hpp>
11#include <std_msgs/msg/string.hpp>
12
13#include "modulo_core/communication/PublisherHandler.hpp"
14
16
17PublisherInterface::PublisherInterface(PublisherType type, std::shared_ptr<MessagePairInterface> message_pair)
18 : message_pair_(std::move(message_pair)), type_(type) {}
19
22 "The derived publisher handler is required to override this function to handle activation");
23}
24
27 "The derived publisher handler is required to override this function to handle deactivation");
28}
29
31 try {
32 if (this->message_pair_ == nullptr) {
33 throw exceptions::NullPointerException("Message pair is not set, nothing to publish");
34 }
35 switch (this->message_pair_->get_type()) {
36 case MessageType::BOOL:
37 this->publish(this->message_pair_->write<std_msgs::msg::Bool, bool>());
38 break;
39 case MessageType::FLOAT64:
40 this->publish(this->message_pair_->write<std_msgs::msg::Float64, double>());
41 break;
42 case MessageType::FLOAT64_MULTI_ARRAY:
43 this->publish(this->message_pair_->write<std_msgs::msg::Float64MultiArray, std::vector<double>>());
44 break;
45 case MessageType::INT32:
46 this->publish(this->message_pair_->write<std_msgs::msg::Int32, int>());
47 break;
48 case MessageType::STRING:
49 this->publish(this->message_pair_->write<std_msgs::msg::String, std::string>());
50 break;
51 case MessageType::ENCODED_STATE:
52 if (!this->message_pair_->get_message_pair<EncodedState, state_representation::State>()
53 ->get_data()
54 ->is_empty()) {
55 this->publish(this->message_pair_->write<EncodedState, state_representation::State>());
56 }
57 break;
58 default:
59 break;
60 }
61 } catch (const exceptions::CoreException& ex) {
62 throw;
63 }
64}
65
66template<typename MsgT>
67void PublisherInterface::publish(const MsgT& message) {
68 switch (this->get_type()) {
69 case PublisherType::PUBLISHER:
70 this->template get_handler<rclcpp::Publisher<MsgT>, MsgT>()->publish(message);
71 break;
72 case PublisherType::LIFECYCLE_PUBLISHER:
73 this->template get_handler<rclcpp_lifecycle::LifecyclePublisher<MsgT>, MsgT>()->publish(message);
74 break;
75 }
76}
77
78std::shared_ptr<MessagePairInterface> PublisherInterface::get_message_pair() const {
79 return this->message_pair_;
80}
81
82void PublisherInterface::set_message_pair(const std::shared_ptr<MessagePairInterface>& message_pair) {
83 if (message_pair == nullptr) {
84 throw exceptions::NullPointerException("Provide a valid pointer");
85 }
86 this->message_pair_ = message_pair;
87}
88
90 return this->type_;
91}
92}// namespace modulo_core::communication
virtual void deactivate()
Deactivate ROS publisher of a derived PublisherHandler instance through the PublisherInterface pointe...
PublisherType get_type() const
Get the type of the publisher interface.
virtual void publish()
Publish the data stored in the message pair through the ROS publisher of a derived PublisherHandler i...
std::shared_ptr< MessagePairInterface > get_message_pair() const
Get the pointer to the message pair of the PublisherInterface.
std::shared_ptr< MessagePairInterface > message_pair_
The pointer to the stored MessagePair instance.
PublisherInterface(PublisherType type, std::shared_ptr< MessagePairInterface > message_pair=nullptr)
Constructor with the message type and message pair.
void set_message_pair(const std::shared_ptr< MessagePairInterface > &message_pair)
Set the pointer to the message pair of the PublisherInterface.
virtual void activate()
Activate ROS publisher of a derived PublisherHandler instance through the PublisherInterface pointer.
A base class for all core exceptions.
An exception class to notify that a certain pointer is null.
Modulo Core communication module for handling messages on publication and subscription interfaces.
PublisherType
Enum of supported ROS publisher types for the PublisherInterface.