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
26#include "yasmin/logs.hpp"
27#include "yasmin/state.hpp"
31
32namespace yasmin_ros {
33
42template <typename MsgT> class PublisherState : public yasmin::State {
43
46 std::function<MsgT(std::shared_ptr<yasmin::blackboard::Blackboard>)>;
47
48public:
56 PublisherState(const std::string &topic_name,
58 rclcpp::QoS qos = 10,
59 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr)
61 callback_group) {}
62
71 PublisherState(const rclcpp::Node::SharedPtr &node,
72 const std::string &topic_name,
74 rclcpp::QoS qos = 10,
75 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr)
78
79 if (node == nullptr) {
81 } else {
82 this->node_ = node;
83 }
84
85 // create subscriber
87 this->node_, topic_name, qos, callback_group);
88
89 if (this->create_message_handler == nullptr) {
90 throw std::invalid_argument("create_message_handler is needed");
91 }
92 }
93
100 std::string
101 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) override {
102
103 YASMIN_LOG_DEBUG("Publishing to topic '%s'", this->topic_name.c_str());
104 MsgT msg = this->create_message_handler(blackboard);
105 this->pub->publish(msg);
107 }
108
109protected:
111 rclcpp::Node::SharedPtr node_;
112
113private:
114 std::shared_ptr<rclcpp::Publisher<MsgT>>
116
117 std::string topic_name;
120};
121
122} // namespace yasmin_ros
123
124#endif // YASMIN_ROS__MONITOR_STATE_HPP
Represents a state in a state machine.
Definition state.hpp:53
State(const std::set< std::string > &outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
std::shared_ptr< rclcpp::Publisher< MsgT > > pub
Definition publisher_state.hpp:115
std::string topic_name
Definition publisher_state.hpp:117
PublisherState(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:56
CreateMessageHandler create_message_handler
Definition publisher_state.hpp:119
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition publisher_state.hpp:111
std::function< MsgT(std::shared_ptr< yasmin::blackboard::Blackboard >)> CreateMessageHandler
Function type for creating messages for topic.
Definition publisher_state.hpp:45
std::string execute(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard) override
Execute the publishing operation.
Definition publisher_state.hpp:101
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:71
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 std::shared_ptr< YasminNode > get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:73
#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