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 <functional>
21#include <memory>
22#include <set>
23#include <string>
24#include <vector>
25
26#include "rclcpp/rclcpp.hpp"
27
29#include "yasmin/state.hpp"
32
33using std::placeholders::_1;
34
35namespace yasmin_ros {
36
46template <typename MsgT> class MonitorState : public yasmin::State {
47
49 using MonitorHandler = std::function<std::string(
50 std::shared_ptr<yasmin::blackboard::Blackboard>, std::shared_ptr<MsgT>)>;
51
52public:
63 MonitorState(std::string topic_name, std::set<std::string> outcomes,
64 MonitorHandler monitor_handler, rclcpp::QoS qos, int msg_queue)
66 -1) {}
67
75 MonitorState(std::string topic_name, std::set<std::string> outcomes,
78
90 MonitorState(std::string topic_name, std::set<std::string> outcomes,
91 MonitorHandler monitor_handler, rclcpp::QoS qos, int msg_queue,
92 int timeout)
95
108 MonitorState(const rclcpp::Node::SharedPtr &node, std::string topic_name,
109 std::set<std::string> outcomes, MonitorHandler monitor_handler,
110 rclcpp::QoS qos, int msg_queue, int timeout)
113 time_to_wait(1000) {
114
115 // set outcomes
116 if (timeout > 0) {
118 } else {
119 this->outcomes = {};
120 }
121 this->outcomes.insert(basic_outcomes::CANCEL);
122
123 if (outcomes.size() > 0) {
124 for (std::string outcome : outcomes) {
125 this->outcomes.insert(outcome);
126 }
127 }
128
129 if (node == nullptr) {
131 } else {
132 this->node_ = node;
133 }
134
135 // create subscriber
136 this->sub = this->node_->template create_subscription<MsgT>(
137 topic_name, qos, std::bind(&MonitorState::callback, this, _1));
138 }
139
147 std::string
148 execute(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) override {
149
150 float elapsed_time = 0;
151 this->msg_list.clear();
152 this->monitoring = true;
153
154 while (this->msg_list.empty()) {
155 std::this_thread::sleep_for(
156 std::chrono::microseconds(this->time_to_wait));
157
158 if (is_canceled()) {
159 this->monitoring = false;
161 }
162
163 if (this->timeout > 0) {
164
165 if (elapsed_time / 1e6 >= this->timeout) {
166 this->monitoring = false;
167 RCLCPP_WARN(this->node_->get_logger(),
168 "Timeout reached, topic '%s' is not available",
169 this->topic_name.c_str());
171 }
172
173 elapsed_time += this->time_to_wait;
174 }
175 }
176
177 RCLCPP_INFO(this->node_->get_logger(), "Processing msg from topic '%s'",
178 this->topic_name.c_str());
179 std::string outcome =
180 this->monitor_handler(blackboard, this->msg_list.at(0));
181 this->msg_list.erase(this->msg_list.begin());
182
183 this->monitoring = false;
184 return outcome;
185 }
186
187private:
188 rclcpp::Node::SharedPtr node_;
189 std::shared_ptr<rclcpp::Subscription<MsgT>>
191 std::vector<std::shared_ptr<MsgT>>
193
194 std::string topic_name;
201
210 void callback(const typename MsgT::SharedPtr msg) {
211
212 if (this->monitoring) {
213 this->msg_list.push_back(msg);
214
215 if ((int)this->msg_list.size() >= this->msg_queue) {
216 this->msg_list.erase(this->msg_list.begin());
217 }
218 }
219 }
220};
221
222} // namespace yasmin_ros
223
224#endif // YASMIN_ROS__MONITOR_STATE_HPP
Represents a state in a state machine.
Definition state.hpp:42
bool is_canceled() const
Checks if the state has been canceled.
Definition state.cpp:71
std::set< std::string > outcomes
The possible outcomes of this state.
Definition state.hpp:46
State(std::set< std::string > outcomes)
Constructs a State with a set of possible outcomes.
Definition state.cpp:27
std::vector< std::shared_ptr< MsgT > > msg_list
Definition monitor_state.hpp:192
rclcpp::Node::SharedPtr node_
Definition monitor_state.hpp:188
MonitorState(const rclcpp::Node::SharedPtr &node, std::string topic_name, std::set< std::string > outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos, int msg_queue, int timeout)
Construct a new MonitorState with ROS 2 node, specific QoS, message queue, and timeout.
Definition monitor_state.hpp:108
int timeout
Definition monitor_state.hpp:198
MonitorState(std::string topic_name, std::set< std::string > outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos, int msg_queue, int timeout)
Construct a new MonitorState with specific QoS, message queue, and timeout.
Definition monitor_state.hpp:90
MonitorHandler monitor_handler
Definition monitor_state.hpp:196
std::string topic_name
Definition monitor_state.hpp:194
std::shared_ptr< rclcpp::Subscription< MsgT > > sub
Definition monitor_state.hpp:190
MonitorState(std::string topic_name, std::set< std::string > outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos, int msg_queue)
Construct a new MonitorState with specific QoS and message queue settings.
Definition monitor_state.hpp:63
int time_to_wait
Definition monitor_state.hpp:200
MonitorState(std::string topic_name, std::set< std::string > outcomes, MonitorHandler monitor_handler)
Construct a new MonitorState with default QoS and message queue.
Definition monitor_state.hpp:75
bool monitoring
Definition monitor_state.hpp:199
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:49
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:148
void callback(const typename MsgT::SharedPtr msg)
Callback function for receiving messages from the subscribed topic.
Definition monitor_state.hpp:210
int msg_queue
Definition monitor_state.hpp:197
static std::shared_ptr< YasminNode > get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:73
constexpr char CANCEL[]
Constant representing a canceled action outcome.
Definition basic_outcomes.hpp:55
constexpr char TIMEOUT[]
Constant representing a timed-out action outcome.
Definition basic_outcomes.hpp:63
Definition action_state.hpp:35