C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
yasmin_node.hpp
Go to the documentation of this file.
1// Copyright (C) 2024 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__YASMIN_NODE_HPP_
17#define YASMIN_ROS__YASMIN_NODE_HPP_
18
19#include <memory>
20#include <string>
21#include <thread>
22
23#include "rclcpp/rclcpp.hpp"
24
25#include "yasmin/blackboard.hpp"
26#include "yasmin/state.hpp"
28
29namespace yasmin_ros {
30
38class YasminNode : public rclcpp::Node {
39
40protected:
44 explicit YasminNode();
45
46public:
53 YasminNode(YasminNode &other) = delete;
54
59
65 void operator=(const YasminNode &) = delete;
66
74 static YasminNode::SharedPtr get_instance() {
75 // Ensure ROS 2 is initialized
76 if (!rclcpp::ok()) {
77 rclcpp::init(0, nullptr);
78 }
79
80 // Create the singleton instance if it doesn't exist
81 static YasminNode::SharedPtr instance =
82 YasminNode::SharedPtr(new YasminNode());
83 return instance;
84 }
85
86private:
88#if __has_include("rclcpp/version.h")
89#include "rclcpp/version.h"
90#if RCLCPP_VERSION_GTE(29, 1, 1) // Jazzy, Kilted and Rolling
91 rclcpp::experimental::executors::EventsExecutor executor;
92#else // Humble, Iron and Jazzy
93 rclcpp::executors::MultiThreadedExecutor executor;
94#endif
95#else // Foxy and Galactic
96 rclcpp::executors::MultiThreadedExecutor executor;
97#endif
99 std::unique_ptr<std::thread> spin_thread;
100};
101
102} // namespace yasmin_ros
103
104#endif // YASMIN_ROS__YASMIN_NODE_HPP_
static YasminNode::SharedPtr get_instance()
Provides access to the singleton instance of YasminNode.
Definition yasmin_node.hpp:74
YasminNode()
Default constructor. Initializes the node with a unique name.
Definition yasmin_node.cpp:45
std::unique_ptr< std::thread > spin_thread
Thread for spinning the node.
Definition yasmin_node.hpp:99
YasminNode(YasminNode &other)=delete
Deleted copy constructor to prevent copying of the singleton instance.
~YasminNode()
Destructor. Cleans up resources.
Definition yasmin_node.hpp:58
rclcpp::executors::MultiThreadedExecutor executor
Executor for managing multiple threads.
Definition yasmin_node.hpp:96
void operator=(const YasminNode &)=delete
Deleted assignment operator to enforce singleton pattern.
Definition action_state.hpp:37