Common
common packages for kyubic
 
Loading...
Searching...
No Matches
check_sensors_status.hpp
Go to the documentation of this file.
1
10#ifndef _CHECK_SENSORS_STATUS_HPP
11#define _CHECK_SENSORS_STATUS_HPP
12
13#include <behaviortree_cpp/condition_node.h>
14
15#include <common_msgs/msg/status.hpp>
16#include <driver_msgs/msg/bool_stamped.hpp>
17#include <driver_msgs/msg/depth.hpp>
18#include <driver_msgs/msg/dvl.hpp>
19#include <driver_msgs/msg/imu.hpp>
20#include <mutex>
21#include <rclcpp/rclcpp.hpp>
22#include <std_msgs/msg/string.hpp>
23#include <timer/timeout.hpp>
24
28class CheckSensorsStatus : public BT::ConditionNode
29{
30public:
38 const std::string & name, const BT::NodeConfig & config,
39 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr logger_pub,
40 rclcpp::Node::SharedPtr ros_node);
41
46 static BT::PortsList providedPorts();
47
53 BT::NodeStatus tick() override;
54
55private:
56 rclcpp::Node::SharedPtr ros_node_;
57 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr logger_pub_;
58 rclcpp::Subscription<driver_msgs::msg::IMU>::SharedPtr imu_sub_;
59 rclcpp::Subscription<driver_msgs::msg::Depth>::SharedPtr depth_sub_;
60 rclcpp::Subscription<driver_msgs::msg::DVL>::SharedPtr dvl_sub_;
61 rclcpp::Subscription<driver_msgs::msg::BoolStamped>::SharedPtr leak_sub_;
62
63 void logger(rclcpp::Time now);
64
68 struct SensorMonitor
69 {
70 std::mutex mutex;
71 std::shared_ptr<timer::Timeout> timer;
72 uint8_t status;
73
74 SensorMonitor(rclcpp::Time now, int64_t timeout_ns)
75 : status(common_msgs::msg::Status::ERROR) // Default is error
76 {
77 timer = std::make_shared<timer::Timeout>(now, timeout_ns);
78 }
79
83 void update(uint8_t status, rclcpp::Time now)
84 {
85 std::lock_guard<std::mutex> lock(mutex);
86 if (status != common_msgs::msg::Status::ERROR) {
87 timer->reset(now);
88 }
89 }
90
94 bool is_healthy(rclcpp::Time now)
95 {
96 std::lock_guard<std::mutex> lock(mutex);
97 if (timer->is_timeout(now)) {
98 return false;
99 }
100 return true;
101 }
102 };
103
109 template <typename MsgT, typename ExtraOps>
110 auto create_cb(std::shared_ptr<SensorMonitor> monitor_ptr, ExtraOps extra_process)
111 {
112 return [this, monitor_ptr, extra_process](const MsgT::ConstSharedPtr & msg) {
113 monitor_ptr->update(msg->status.id, this->ros_node_->get_clock()->now());
114 extra_process(msg);
115 };
116 }
117
118 std::shared_ptr<SensorMonitor> imu_monitor_;
119 std::shared_ptr<SensorMonitor> depth_monitor_;
120 std::shared_ptr<SensorMonitor> dvl_monitor_;
121 std::shared_ptr<SensorMonitor> leak_monitor_;
122};
123
124#endif // _CHECK_SENSORS_STATUS_HPP
Condition node to check the health status of robot sensors.
Definition: check_sensors_status.hpp:29
static BT::PortsList providedPorts()
Defines the input and output ports for this node.
Definition: check_sensors_status.cpp:38
BT::NodeStatus tick() override
Checks the current status of the sensors.
Definition: check_sensors_status.cpp:54
For timer.
timeout library