C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
publisher_state.hpp
Go to the documentation of this file.
1// Copyright (C) 2025 Miguel Ángel González Santamarta
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN_ROS__PUBLISHER_STATE_HPP_
17#define YASMIN_ROS__PUBLISHER_STATE_HPP_
18
19#include <functional>
20#include <memory>
21#include <string>
22
23#include "rclcpp/rclcpp.hpp"
24
25#include "yasmin/blackboard.hpp"
26#include "yasmin/logs.hpp"
27#include "yasmin/state.hpp"
28#include "yasmin/types.hpp"
32
33namespace yasmin_ros {
34
43template <typename MsgT> class PublisherState : public yasmin::State {
44
47 std::function<MsgT(yasmin::Blackboard::SharedPtr)>;
48
49public:
54
55
62 PublisherState(const std::string &topic_name,
64 rclcpp::QoS qos = 10,
65 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr)
67 callback_group) {}
68
77 PublisherState(const rclcpp::Node::SharedPtr &node,
78 const std::string &topic_name,
80 rclcpp::QoS qos = 10,
81 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr)
84
85 if (node == nullptr) {
87 } else {
88 this->node_ = node;
89 }
90
91 // create subscriber
93 this->node_, topic_name, qos, callback_group);
94
95 if (this->create_message_handler == nullptr) {
96 throw std::invalid_argument("create_message_handler is needed");
97 }
98 }
99
106 std::string execute(yasmin::Blackboard::SharedPtr blackboard) override {
107
108 YASMIN_LOG_DEBUG("Publishing to topic '%s'", this->topic_name.c_str());
109 MsgT msg = this->create_message_handler(blackboard);
110 this->pub->publish(msg);
112 }
113
114protected:
116 rclcpp::Node::SharedPtr node_;
117
118private:
120 std::shared_ptr<rclcpp::Publisher<MsgT>> pub;
122 std::string topic_name;
125};
126
127} // namespace yasmin_ros
128
129#endif // YASMIN_ROS__MONITOR_STATE_HPP_
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:85
Represents a state in a state machine.
Definition state.hpp:46
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:32
std::shared_ptr< rclcpp::Publisher< MsgT > > pub
Publisher to the ROS 2 topic.
Definition publisher_state.hpp:120
std::string execute(yasmin::Blackboard::SharedPtr blackboard) override
Execute the publishing operation.
Definition publisher_state.hpp:106
std::string topic_name
Name of the topic to monitor.
Definition publisher_state.hpp:122
PublisherState(const std::string &topic_name, CreateMessageHandler create_message_handler, rclcpp::QoS qos=10, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Shared pointer type for PublisherState.
Definition publisher_state.hpp:62
CreateMessageHandler create_message_handler
Callback handler to create messages.
Definition publisher_state.hpp:124
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition publisher_state.hpp:116
std::function< MsgT(yasmin::Blackboard::SharedPtr)> CreateMessageHandler
Function type for creating messages for topic.
Definition publisher_state.hpp:46
PublisherState(const rclcpp::Node::SharedPtr &node, const std::string &topic_name, CreateMessageHandler create_message_handler, rclcpp::QoS qos=10, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Construct a new PublisherState with ROS 2 node and specific QoS.
Definition publisher_state.hpp:77
static rclcpp::Publisher< MsgT >::SharedPtr get_or_create_publisher(rclcpp::Node::SharedPtr node, const std::string &topic_name, const rclcpp::QoS &qos_profile=rclcpp::QoS(10), rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Get an existing publisher from the cache or create a new one.
Definition ros_clients_cache.hpp:174
static YasminNode::SharedPtr get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:74
#define YASMIN_LOG_DEBUG(text,...)
Definition logs.hpp:174
constexpr char SUCCEED[]
Constant representing a successful action outcome.
Definition basic_outcomes.hpp:39
Definition action_state.hpp:37
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52