Easy Navigation
Loading...
Searching...
No Matches
SafetySupervisor.hpp
Go to the documentation of this file.
1// Copyright 2026 Intelligent Robotics Lab
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
17
18#ifndef EASYNAV_SYSTEM__SAFETY__SAFETYSUPERVISOR_HPP_
19#define EASYNAV_SYSTEM__SAFETY__SAFETYSUPERVISOR_HPP_
20
21#include <atomic>
22#include <mutex>
23#include <optional>
24#include <string>
25
26#include "easynav_interfaces/msg/heartbeat.hpp"
27#include "rclcpp/callback_group.hpp"
28#include "rclcpp/clock.hpp"
29#include "rclcpp/logger.hpp"
30#include "rclcpp/publisher.hpp"
31#include "rclcpp/subscription.hpp"
32#include "rclcpp_lifecycle/lifecycle_node.hpp"
33#include "std_msgs/msg/string.hpp"
34
41
42namespace easynav::safety
43{
44
79{
80public:
82 void declare_parameters(rclcpp_lifecycle::LifecycleNode & node);
83
86 bool check_system(
87 rclcpp_lifecycle::LifecycleNode & node, rclcpp::CallbackGroup::SharedPtr rt_group = nullptr);
88
90 bool check_controller(ControllerNode & controller) const;
91
93 void on_configured(const Nodes & nodes, NavState & nav_state);
94
96 bool allows_reconfiguration(const std::string & reason) const;
97
99 void on_activate();
100
104 bool cycle_rt(NavState & nav_state, RtMonitor::Clock::time_point now);
105
107 [[nodiscard]] const std::string & failure() const {return failure_;}
108
109 [[nodiscard]] const RtMonitor & get_rt_monitor() const {return rt_monitor_;}
110
112 [[nodiscard]] bool is_safety_status_enabled() const {return safety_status_sub_ != nullptr;}
113
115 [[nodiscard]] bool is_safety_mode() const {return safety_mode_;}
116
118 [[nodiscard]] bool is_memory_lock_requested() const {return lock_memory_;}
119
121 [[nodiscard]] std::string get_configuration_hash() const;
122
123private:
124 std::atomic<bool> safety_mode_ {false};
125 std::atomic<bool> lock_memory_ {false};
126 double max_linear_vel_ {0.0};
127 double max_angular_vel_ {0.0};
128 rclcpp::Logger logger_ {rclcpp::get_logger("system_node")};
129 std::string namespace_;
131 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr configuration_pub_;
132
134 void report_rt_status(NavState & nav_state, RtMonitor::Status status);
135
137 void check_pose_age(NavState & nav_state, const rclcpp::Time & now);
138
140 void report_safety_status(
141 NavState & nav_state, const SafetyChannelMonitor::Evaluation & evaluation);
142
143 ParameterFreezer freezer_;
144
145 // Real-time cycle: monitor and heartbeat, used only from the RT cycle once active.
146 RtMonitor rt_monitor_;
147 double max_period_factor_ {2.0};
148 std::optional<RtMonitor::Status> last_rt_report_;
149 std::string hardware_id_ {"system_node"};
150 std::string failure_;
151 rclcpp::Clock::SharedPtr clock_;
152 double heartbeat_period_ {0.0};
153 std::optional<RtMonitor::Clock::time_point> last_heartbeat_;
154 easynav_interfaces::msg::Heartbeat heartbeat_;
155 rclcpp::Publisher<easynav_interfaces::msg::Heartbeat>::SharedPtr heartbeat_pub_;
156 std::string configuration_hash_;
157
158 // Age of the robot pose, checked from the RT cycle.
159 double max_pose_age_ {1.0};
160 std::optional<bool> last_pose_stale_;
161
162 // Safety channel: received in the RT callback group, applied from the RT cycle.
163 double safety_status_timeout_ {0.0};
164 SafetyChannelMonitor safety_channel_;
165 rclcpp::Subscription<easynav_interfaces::msg::SafetyStatus>::SharedPtr safety_status_sub_;
166 std::optional<SafetyChannelMonitor::Evaluation> last_safety_report_;
167 mutable std::mutex configuration_hash_mutex_;
168};
169
170} // namespace easynav::safety
171
172#endif // EASYNAV_SYSTEM__SAFETY__SAFETYSUPERVISOR_HPP_
Fingerprint (dump and SHA-256) of the EasyNav configuration.
Declaration of the ControllerNode class, a ROS 2 lifecycle node for speed computation in Easy Navigat...
A blackboard-like structure to hold the current state of the navigation system.
Declaration of the ParameterFreezer class.
Declaration of the RtMonitor class.
Declaration of the SafetyChannelMonitor class.
ROS 2 lifecycle node that manages calculating speeds for the Easy Navigation system.
Definition ControllerNode.hpp:72
A generic, type-safe, thread-safe blackboard to hold runtime state.
Definition NavState.hpp:87
Detects real-time cycles that start late.
Definition RtMonitor.hpp:37
Status
Definition RtMonitor.hpp:41
What SystemNode does for safety, kept out of the navigation logic.
Definition SafetySupervisor.hpp:79
bool cycle_rt(NavState &nav_state, RtMonitor::Clock::time_point now)
At the start of each RT cycle, at now: monitors it, publishes the heartbeat when due,...
Definition SafetySupervisor.cpp:263
bool check_system(rclcpp_lifecycle::LifecycleNode &node, rclcpp::CallbackGroup::SharedPtr rt_group=nullptr)
Reads node's safety parameters and checks them, before the subnodes configure.
Definition SafetySupervisor.cpp:50
void on_configured(const Nodes &nodes, NavState &nav_state)
After a successful configure: fingerprints and, in safety mode, freezes nodes.
Definition SafetySupervisor.cpp:200
bool is_safety_mode() const
"safety.mode", as of the last configure.
Definition SafetySupervisor.hpp:115
const std::string & failure() const
Why cycle_rt() asked to stop.
Definition SafetySupervisor.hpp:107
void on_activate()
On activation: the RT monitor starts over.
Definition SafetySupervisor.cpp:253
bool check_controller(ControllerNode &controller) const
Checks controller against "safety.plc_limits" and what "safety.mode" requires.
Definition SafetySupervisor.cpp:162
bool is_safety_status_enabled() const
Whether the safety status is received ("safety.status.timeout" > 0).
Definition SafetySupervisor.hpp:112
bool allows_reconfiguration(const std::string &reason) const
Whether a reconfiguration may be requested; logs why not.
Definition SafetySupervisor.cpp:241
std::string get_configuration_hash() const
SHA-256 (hex) of every EasyNav parameter, as of the last configure.
Definition SafetySupervisor.cpp:469
bool is_memory_lock_requested() const
"safety.lock_memory", as of the last configure.
Definition SafetySupervisor.hpp:118
const RtMonitor & get_rt_monitor() const
Definition SafetySupervisor.hpp:109
void declare_parameters(rclcpp_lifecycle::LifecycleNode &node)
Declares the "safety.*" parameters in node (SystemNode).
Definition SafetySupervisor.cpp:35
Definition CommandGuard.hpp:36
std::map< std::string, rclcpp_lifecycle::LifecycleNode::SharedPtr > Nodes
EasyNav nodes by name.
Definition ConfigurationFingerprint.hpp:30