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
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::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
155 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) override {
156 int retry_count = 0;
157
158 while (this->msg_list.empty()) {
159 std::unique_lock<std::mutex> lock(this->msg_mutex);
160
161 std::cv_status wait_status = std::cv_status::no_timeout;
162 if (this->timeout > 0) {
163 wait_status =
164 this->msg_cond.wait_for(lock, std::chrono::seconds(this->timeout));
165 } else {
166 this->msg_cond.wait(lock);
167 }
168
169 if (this->is_canceled()) {
171 }
172
173 while (this->timeout > 0 && wait_status == std::cv_status::timeout) {
174 YASMIN_LOG_WARN("Timeout reached, topic '%s' is not available",
175 this->topic_name.c_str());
176
177 if (retry_count < this->maximum_retry) {
178 retry_count++;
179 YASMIN_LOG_WARN("Retrying to wait for topic '%s' (%d/%d)",
180 this->topic_name.c_str(), retry_count,
181 this->maximum_retry);
182 wait_status = this->msg_cond.wait_for(
183 lock, std::chrono::seconds(this->timeout));
184 } else {
186 }
187
188 if (this->is_canceled()) {
190 }
191 }
192 }
193
194 YASMIN_LOG_INFO("Processing msg from topic '%s'", this->topic_name.c_str());
195 std::string outcome =
196 this->monitor_handler(blackboard, this->msg_list.at(0));
197 this->msg_list.erase(this->msg_list.begin());
198
199 return outcome;
200 }
201
208 this->msg_cond.notify_one();
210 }
211
212protected:
214 rclcpp::Node::SharedPtr node_;
215
216private:
217 std::shared_ptr<rclcpp::Subscription<MsgT>>
219
220 std::string topic_name;
221 rclcpp::QoS qos;
222
223 std::vector<std::shared_ptr<MsgT>>
230
232 std::condition_variable msg_cond;
234 std::mutex msg_mutex;
235
244 void callback(const typename MsgT::SharedPtr msg) {
245
246 this->msg_list.push_back(msg);
247
248 if ((int)this->msg_list.size() > this->msg_queue) {
249 this->msg_list.erase(this->msg_list.begin());
250 }
251
252 this->msg_cond.notify_one();
253 }
254};
255
256} // namespace yasmin_ros
257
258#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: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