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::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>(
155 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard)
override {
159 std::unique_lock<std::mutex> lock(this->
msg_mutex);
161 std::cv_status wait_status = std::cv_status::no_timeout;
173 while (this->
timeout > 0 && wait_status == std::cv_status::timeout) {
181 this->maximum_retry);
182 wait_status = this->
msg_cond.wait_for(
183 lock, std::chrono::seconds(this->
timeout));
195 std::string outcome =
217 std::shared_ptr<rclcpp::Subscription<MsgT>>
223 std::vector<std::shared_ptr<MsgT>>
244 void callback(
const typename MsgT::SharedPtr msg) {
248 if ((
int)this->
msg_list.size() > this->msg_queue) {
252 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:133
std::vector< std::shared_ptr< MsgT > > msg_list
Definition monitor_state.hpp:224
rclcpp::Node::SharedPtr node_
Shared pointer to the ROS 2 node.
Definition monitor_state.hpp:214
rclcpp::QoS qos
Definition monitor_state.hpp:221
int timeout
Definition monitor_state.hpp:228
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
Definition monitor_state.hpp:226
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
Definition monitor_state.hpp:229
std::string topic_name
Definition monitor_state.hpp:220
std::shared_ptr< rclcpp::Subscription< MsgT > > sub
Definition monitor_state.hpp:218
void cancel_state()
Cancel the current monitor state.
Definition monitor_state.hpp:207
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::function< std::string( std::shared_ptr< yasmin::blackboard::Blackboard >, std::shared_ptr< MsgT >)> MonitorHandler
Function type for handling messages from topic.
Definition monitor_state.hpp:51
std::string execute(std::shared_ptr< yasmin::blackboard::Blackboard > blackboard) override
Execute the monitoring operation and process the first received message.
Definition monitor_state.hpp:155
std::mutex msg_mutex
Mutex for protecting action completion.
Definition monitor_state.hpp:234
void callback(const typename MsgT::SharedPtr msg)
Callback function for receiving messages from the subscribed topic.
Definition monitor_state.hpp:244
std::condition_variable msg_cond
Condition variable for action completion.
Definition monitor_state.hpp:232
int msg_queue
Definition monitor_state.hpp:227
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