C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
monitor_state.hpp
Go to the documentation of this file.
1// Copyright (C) 2023 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__MONITOR_STATE_HPP
17#define YASMIN_ROS__MONITOR_STATE_HPP
18
19#include <chrono>
20#include <condition_variable>
21#include <functional>
22#include <memory>
23#include <set>
24#include <string>
25#include <vector>
26
27#include "rclcpp/rclcpp.hpp"
28
29#include "yasmin/blackboard.hpp"
30#include "yasmin/logs.hpp"
31#include "yasmin/state.hpp"
34
35using std::placeholders::_1;
36
37namespace yasmin_ros {
38
48template <typename MsgT> class MonitorState : public yasmin::State {
49
51 using MonitorHandler = std::function<std::string(
52 std::shared_ptr<yasmin::Blackboard>, std::shared_ptr<MsgT>)>;
53
54public:
68 MonitorState(const std::string &topic_name,
69 const std::set<std::string> &outcomes,
70 MonitorHandler monitor_handler, rclcpp::QoS qos = 10,
71 int msg_queue = 10, int timeout = -1, int maximum_retry = 3)
73 nullptr, msg_queue, timeout, maximum_retry) {}
74
90 MonitorState(const std::string &topic_name,
91 const std::set<std::string> &outcomes,
92 MonitorHandler monitor_handler, rclcpp::QoS qos = 10,
93 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr,
94 int msg_queue = 10, int timeout = -1, int maximum_retry = 3)
96 callback_group, msg_queue, timeout, maximum_retry) {}
97
112 MonitorState(const rclcpp::Node::SharedPtr &node,
113 const std::string &topic_name,
114 const std::set<std::string> &outcomes,
115 MonitorHandler monitor_handler, rclcpp::QoS qos = 10,
116 rclcpp::CallbackGroup::SharedPtr callback_group = nullptr,
117 int msg_queue = 10, int timeout = -1, int maximum_retry = 3)
121
122 // set outcomes
123 if (timeout > 0) {
124 this->outcomes.insert(basic_outcomes::TIMEOUT);
125 }
126
127 if (outcomes.size() > 0) {
128 for (const std::string &outcome : outcomes) {
129 this->outcomes.insert(outcome);
130 }
131 }
132
133 if (node == nullptr) {
135 } else {
136 this->node_ = node;
137 }
138
139 // Create subscription
140 rclcpp::SubscriptionOptions options;
141 options.callback_group = callback_group;
142 this->sub = this->node_->create_subscription<MsgT>(
143 this->topic_name, this->qos,
144 std::bind(&MonitorState::callback, this, _1), options);
145 }
146
154 std::string execute(std::shared_ptr<yasmin::Blackboard> blackboard) override {
155 int retry_count = 0;
156
157 while (this->msg_list.empty()) {
158 std::unique_lock<std::mutex> lock(this->msg_mutex);
159
160 std::cv_status wait_status = std::cv_status::no_timeout;
161 if (this->timeout > 0) {
162 wait_status =
163 this->msg_cond.wait_for(lock, std::chrono::seconds(this->timeout));
164 } else {
165 this->msg_cond.wait(lock);
166 }
167
168 if (this->is_canceled()) {
170 }
171
172 while (this->timeout > 0 && wait_status == std::cv_status::timeout) {
173 YASMIN_LOG_WARN("Timeout reached, topic '%s' is not available",
174 this->topic_name.c_str());
175
176 if (retry_count < this->maximum_retry) {
177 retry_count++;
178 YASMIN_LOG_WARN("Retrying to wait for topic '%s' (%d/%d)",
179 this->topic_name.c_str(), retry_count,
180 this->maximum_retry);
181 wait_status = this->msg_cond.wait_for(
182 lock, std::chrono::seconds(this->timeout));
183 } else {
185 }
186
187 if (this->is_canceled()) {
189 }
190 }
191 }
192
193 YASMIN_LOG_INFO("Processing msg from topic '%s'", this->topic_name.c_str());
194 std::string outcome =
195 this->monitor_handler(blackboard, this->msg_list.at(0));
196 this->msg_list.erase(this->msg_list.begin());
197
198 return outcome;
199 }
200
207 this->msg_cond.notify_one();
209 }
210
211protected:
213 rclcpp::Node::SharedPtr node_;
214
215private:
217 std::shared_ptr<rclcpp::Subscription<MsgT>> sub;
218
220 std::string topic_name;
222 rclcpp::QoS qos;
223
225 std::vector<std::shared_ptr<MsgT>> msg_list;
234
236 std::condition_variable msg_cond;
238 std::mutex msg_mutex;
239
248 void callback(const typename MsgT::SharedPtr msg) {
249
250 this->msg_list.push_back(msg);
251
252 if ((int)this->msg_list.size() > this->msg_queue) {
253 this->msg_list.erase(this->msg_list.begin());
254 }
255
256 this->msg_cond.notify_one();
257 }
258};
259
260} // namespace yasmin_ros
261
262#endif // YASMIN_ROS__MONITOR_STATE_HPP
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