16#ifndef YASMIN_ROS__MONITOR_STATE_HPP
17#define YASMIN_ROS__MONITOR_STATE_HPP
20#include <condition_variable>
27#include "rclcpp/rclcpp.hpp"
35using std::placeholders::_1;
52 std::shared_ptr<yasmin::Blackboard>, std::shared_ptr<MsgT>)>;
69 const std::set<std::string> &
outcomes,
91 const std::set<std::string> &
outcomes,
93 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr,
114 const std::set<std::string> &
outcomes,
116 rclcpp::CallbackGroup::SharedPtr callback_group =
nullptr,
128 for (
const std::string &outcome :
outcomes) {
129 this->outcomes.insert(outcome);
133 if (node ==
nullptr) {
140 rclcpp::SubscriptionOptions options;
141 options.callback_group = callback_group;
142 this->
sub = this->
node_->create_subscription<MsgT>(
154 std::string
execute(std::shared_ptr<yasmin::Blackboard> blackboard)
override {
158 std::unique_lock<std::mutex> lock(this->
msg_mutex);
160 std::cv_status wait_status = std::cv_status::no_timeout;
172 while (this->
timeout > 0 && wait_status == std::cv_status::timeout) {
180 this->maximum_retry);
181 wait_status = this->
msg_cond.wait_for(
182 lock, std::chrono::seconds(this->
timeout));
194 std::string outcome =
217 std::shared_ptr<rclcpp::Subscription<MsgT>>
sub;
248 void callback(
const typename MsgT::SharedPtr msg) {
250 this->msg_list.push_back(msg);
252 if ((
int)this->msg_list.size() > this->msg_queue) {
253 this->msg_list.erase(this->msg_list.begin());
256 this->msg_cond.notify_one();
Represents a state in a state machine.
Definition state.hpp:53
bool is_canceled() const
Checks if the state has been canceled.
Definition state.cpp:41
State(const std::set< std::string > &outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:57
virtual void cancel_state()
Cancels the current state execution.
Definition state.hpp:132
std::vector< std::shared_ptr< MsgT > > msg_list
List to store queued messages.
Definition monitor_state.hpp:225
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition monitor_state.hpp:213
std::string execute(std::shared_ptr< yasmin::Blackboard > blackboard) override
Execute the monitoring operation and process the first received message.
Definition monitor_state.hpp:154
rclcpp::QoS qos
Quality of Service settings for the topic.
Definition monitor_state.hpp:222
int timeout
Timeout in seconds for message reception.
Definition monitor_state.hpp:231
MonitorState(const std::string &topic_name, const std::set< std::string > &outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos=10, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr, int msg_queue=10, int timeout=-1, int maximum_retry=3)
Construct a new MonitorState with specific QoS, message queue, and timeout.
Definition monitor_state.hpp:90
MonitorHandler monitor_handler
Callback function to handle incoming messages.
Definition monitor_state.hpp:227
MonitorState(const rclcpp::Node::SharedPtr &node, const std::string &topic_name, const std::set< std::string > &outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos=10, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr, int msg_queue=10, int timeout=-1, int maximum_retry=3)
Construct a new MonitorState with ROS 2 node, specific QoS, message queue, and timeout.
Definition monitor_state.hpp:112
int maximum_retry
Maximum number of retries.
Definition monitor_state.hpp:233
std::string topic_name
Name of the topic to monitor.
Definition monitor_state.hpp:220
std::shared_ptr< rclcpp::Subscription< MsgT > > sub
Subscription to the ROS 2 topic.
Definition monitor_state.hpp:217
void cancel_state()
Cancel the current monitor state.
Definition monitor_state.hpp:206
MonitorState(const std::string &topic_name, const std::set< std::string > &outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos=10, int msg_queue=10, int timeout=-1, int maximum_retry=3)
Construct a new MonitorState with specific QoS, message queue, and timeout.
Definition monitor_state.hpp:68
std::mutex msg_mutex
Mutex for protecting action completion.
Definition monitor_state.hpp:238
std::function< std::string( std::shared_ptr< yasmin::Blackboard >, std::shared_ptr< MsgT >)> MonitorHandler
Function type for handling messages from topic.
Definition monitor_state.hpp:51
void callback(const typename MsgT::SharedPtr msg)
Callback function for receiving messages from the subscribed topic.
Definition monitor_state.hpp:248
std::condition_variable msg_cond
Condition variable for action completion.
Definition monitor_state.hpp:236
int msg_queue
Maximum number of messages to queue.
Definition monitor_state.hpp:229
static std::shared_ptr< YasminNode > get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:73
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
#define YASMIN_LOG_WARN(text,...)
Definition logs.hpp:164
constexpr char CANCEL[]
Constant representing a canceled action outcome.
Definition basic_outcomes.hpp:63
constexpr char TIMEOUT[]
Constant representing a timed-out action outcome.
Definition basic_outcomes.hpp:71
Definition action_state.hpp:37