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
86 "The message was published successfully");
87
88 if (node == nullptr) {
90 } else {
91 this->node_ = node;
92 }
93
94 // Create publisher
96 this->node_, topic_name, qos, callback_group);
97
98 if (this->create_message_handler == nullptr) {
99 throw std::invalid_argument("create_message_handler is needed");
100 }
101 }
102
109 std::string execute(yasmin::Blackboard::SharedPtr blackboard) override {
110
111 YASMIN_LOG_DEBUG("Publishing to topic '%s'", this->topic_name.c_str());
112 MsgT msg = this->create_message_handler(blackboard);
113 this->pub->publish(msg);
115 }
116
117protected:
119 rclcpp::Node::SharedPtr node_;
120
121private:
123 std::shared_ptr<rclcpp::Publisher<MsgT>> pub;
125 std::string topic_name;
128};
129
130} // namespace yasmin_ros
131
132#endif // YASMIN_ROS__MONITOR_STATE_HPP_
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:100
Represents a state in a state machine.
Definition state.hpp:49
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:64
void set_outcome_description(const std::string &outcome, const std::string &description)
Sets a human-readable description for an outcome.
Definition state.cpp:164
std::shared_ptr< rclcpp::Publisher< MsgT > > pub
Publisher to the ROS 2 topic.
Definition publisher_state.hpp:123
std::string execute(yasmin::Blackboard::SharedPtr blackboard) override
Execute the publishing operation.
Definition publisher_state.hpp:109
std::string topic_name
Name of the topic to monitor.
Definition publisher_state.hpp:125
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
std::function< MsgT(yasmin::Blackboard::SharedPtr)> CreateMessageHandler
Function type for creating messages for topic.
Definition publisher_state.hpp:46
CreateMessageHandler create_message_handler
Callback handler to create messages.
Definition publisher_state.hpp:127
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition publisher_state.hpp:119
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:175
static YasminNode::SharedPtr get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.cpp:58
#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