mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
error_publisher.h
1#pragma once
2
3#include <type_traits>
4
5#include <rclcpp/rclcpp.hpp>
6
7#include <mrs_lib/errorgraph/node_id.h>
8#include <mrs_msgs/msg/errorgraph_element.hpp>
9#include <mrs_lib/timer_handler.h>
10
11namespace mrs_lib
12{
13 namespace errorgraph
14 {
15
23 {
24 public:
28 using error_id_t = uint16_t;
29
30 private:
31 struct error_wrapper_t
32 {
33 std::optional<error_id_t> id;
34 mrs_msgs::msg::ErrorgraphError msg;
35 };
36
37 public:
47 ErrorPublisher(const rclcpp::Node::SharedPtr node, const rclcpp::Clock::SharedPtr clock, const std::string& node_name, const std::string& component_name,
48 const rclcpp::Rate& publish_period = rclcpp::Rate(1.0));
49
56 void flushAndShutdown();
57
67 void addGeneralError(const error_id_t id, const std::string& description);
68
77 template <typename enum_T>
78 requires(std::is_enum_v<enum_T> && sizeof(std::underlying_type_t<enum_T>) <= sizeof(error_id_t))
79 void addGeneralError(const enum_T id, const std::string& description)
80 {
81 addGeneralError(static_cast<error_id_t>(id), description);
82 }
83
91 void addOneshotError(const std::string& description);
92
102 void addWaitingForNodeError(const node_id_t& node_id);
103
112 void addWaitingForTopicError(const std::string& topic_name);
113
114 private:
115 rclcpp::Node::SharedPtr node_;
116 rclcpp::Clock::SharedPtr clock_;
117 std::string node_name_;
118 std::string component_name_;
119
120 std::mutex errors_mtx_;
121 std::vector<error_wrapper_t> errors_;
122
123 rclcpp::CallbackGroup::SharedPtr cbkgrp_timers_;
124
125 rclcpp::Publisher<mrs_msgs::msg::ErrorgraphElement>::SharedPtr publisher_;
126
127 std::unique_ptr<mrs_lib::MRSTimer> timer_publisher_;
128
129 void publishErrors();
130 };
131
132 } // namespace errorgraph
133} // namespace mrs_lib
A helper class for aggregating and publishing errors to the Errorgraph. Report errors preventing your...
Definition error_publisher.h:23
void addWaitingForNodeError(const node_id_t &node_id)
Add a special error type waiting_for_node. Use this whenever your node is blocked because it waits fo...
Definition error_publisher.cpp:92
uint16_t error_id_t
Type of the ID used to avoid duplication of errors when using addGeneralError.
Definition error_publisher.h:28
void addGeneralError(const enum_T id, const std::string &description)
A convenience overload for custom enumeration types. This overload just casts the id parameter to the...
Definition error_publisher.h:79
void flushAndShutdown()
Publishes all aggregated errors and calls rclcpp::shutdown().
Definition error_publisher.cpp:35
void addGeneralError(const error_id_t id, const std::string &description)
Add a custom error to the list of aggregated errors to be published in the next period....
Definition error_publisher.cpp:44
void addOneshotError(const std::string &description)
Add an error that only appears once. This overload assumes that the error is unique,...
Definition error_publisher.cpp:63
void addWaitingForTopicError(const std::string &topic_name)
Add a special error type waiting_for_topic. You should prioritize using the addWaitingForNodeError() ...
Definition error_publisher.cpp:73
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Identifies a specific component within a ROS node for error reporting.
Definition node_id.h:21