Common
common packages for kyubic
 
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
10#ifndef _UTILS_HPP
11#define _UTILS_HPP
12
13#include <common_msgs/msg/status.hpp>
14#include <mutex>
15#include <rclcpp/rclcpp.hpp>
16#include <timer/timeout.hpp>
17
19{
20
30template <typename MsgT>
32 rclcpp::Node * node, std::mutex & mutex, std::shared_ptr<timer::Timeout> & timeout,
33 typename rclcpp::Publisher<MsgT>::SharedPtr & pub, const std::string & sensor_name)
34{
35 bool is_timeout;
36 int64_t timeout_ns;
37 int64_t elapsed_time;
38 {
39 std::lock_guard<std::mutex> lock(mutex);
40 is_timeout = timeout->is_timeout(node->get_clock()->now());
41 timeout_ns = timeout->get_timeout();
42 elapsed_time = timeout->get_elapsed_time();
43 }
44
45 if (is_timeout) {
46 auto msg = std::make_unique<MsgT>();
47 msg->header.stamp = node->get_clock()->now();
48 msg->status.id = common_msgs::msg::Status::ERROR;
49
50 pub->publish(std::move(msg));
51
52 RCLCPP_ERROR_THROTTLE(
53 node->get_logger(), *node->get_clock(), timeout_ns * 1e-6, "%s driver timeout: %lu [ms]",
54 sensor_name.c_str(), (uint64_t)(timeout->get_elapsed_time() * 1e-6));
55 } else if (elapsed_time > timeout_ns * 0.5) {
56 RCLCPP_WARN_THROTTLE(
57 node->get_logger(), *node->get_clock(), timeout_ns * 1e-6 / 2, "Failed to get %s data",
58 sensor_name.c_str());
59 return;
60 }
61}
62
63} // namespace driver::sensors_esp32_driver
64
65#endif // !_UTILS_HPP
driver for sensors_esp32
void check_timeout(rclcpp::Node *node, std::mutex &mutex, std::shared_ptr< timer::Timeout > &timeout, typename rclcpp::Publisher< MsgT >::SharedPtr &pub, const std::string &sensor_name)
Template function for checking timeouts.
Definition: utils.hpp:31
timeout library