PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
Loading...
Searching...
No Matches
health_and_arming_checks.hpp
1/****************************************************************************
2 * Copyright (c) 2023 PX4 Development Team.
3 * SPDX-License-Identifier: BSD-3-Clause
4 ****************************************************************************/
5
6#pragma once
7
8#include <rclcpp/rclcpp.hpp>
9#include <px4_msgs/msg/arming_check_reply.hpp>
10#include <px4_msgs/msg/arming_check_request.hpp>
11#include <px4_ros2/common/requirement_flags.hpp>
12
13#include "events.hpp"
14
15#include <memory>
16#include <functional>
17
18class Registration;
19
20namespace px4_ros2
21{
22
24{
25public:
26 explicit HealthAndArmingCheckReporter(px4_msgs::msg::ArmingCheckReply & arming_check_reply)
27 : _arming_check_reply(arming_check_reply) {}
28
29 template<typename ... Args>
30 void armingCheckFailureExt(
31 uint32_t event_id,
32 events::Log log_level, const char * message, Args... args)
33 {
34 const uint16_t navigation_mode_groups{};
35 const uint8_t health_component_index{};
36
37 _arming_check_reply.can_arm_and_run = false;
38
39 if (!addEvent(
40 event_id, log_level, message, navigation_mode_groups,
41 health_component_index, args ...))
42 {
43 printf("Error: too many events\n");
44 }
45 }
46
47 void setHealth(uint8_t health_component_index, bool is_present, bool warning, bool error);
48
49private:
50 template<typename ... Args>
51 bool addEvent(
52 uint32_t event_id, const events::LogLevels & log_levels, const char * message,
53 Args... args);
54
55 px4_msgs::msg::ArmingCheckReply & _arming_check_reply;
56};
57
58
59template<typename ... Args>
60bool HealthAndArmingCheckReporter::addEvent(
61 uint32_t event_id, const events::LogLevels & log_levels,
62 const char * message, Args... args)
63{
64 if (_arming_check_reply.num_events >= _arming_check_reply.events.size()) {
65 return false;
66 }
67
68 events::EventType & e = _arming_check_reply.events[_arming_check_reply.num_events];
69 e.log_levels =
70 (static_cast<uint8_t>(log_levels.internal) << 4) | static_cast<uint8_t>(log_levels.external);
71 e.id = event_id;
72 static_assert(
73 events::util::sizeofArguments(args ...) <= sizeof(e.arguments),
74 "Too many arguments");
75 events::util::fillEventArguments(static_cast<uint8_t *>(e.arguments.data()), args ...);
76 ++_arming_check_reply.num_events;
77 return true;
78}
79
80
81inline void HealthAndArmingCheckReporter::setHealth(
82 uint8_t health_component_index, bool is_present, bool warning,
83 bool error)
84{
85 _arming_check_reply.health_component_index = health_component_index;
86 _arming_check_reply.health_component_is_present = is_present;
87 _arming_check_reply.health_component_warning = warning;
88 _arming_check_reply.health_component_error = error;
89}
90
91
93{
94public:
95 using CheckCallback = std::function<void (HealthAndArmingCheckReporter &)>;
96
98 rclcpp::Node & node, CheckCallback check_callback,
99 const std::string & topic_namespace_prefix = "");
101
107 bool doRegister(const std::string & name);
108
109 void setModeRequirements(const RequirementFlags & mode_requirements)
110 {
111 _mode_requirements = mode_requirements;
112 }
113
114 RequirementFlags & modeRequirements() {return _mode_requirements;}
115
116private:
117 friend class ModeBase;
118 friend class ModeExecutorBase;
119 void overrideRegistration(const std::shared_ptr<Registration> & registration);
120
121 void watchdogTimerUpdate();
122
123 rclcpp::Node & _node;
124 std::shared_ptr<Registration> _registration;
125 CheckCallback _check_callback;
126 bool _check_triggered{true};
127
128 rclcpp::Subscription<px4_msgs::msg::ArmingCheckRequest>::SharedPtr _arming_check_request_sub;
129 rclcpp::Publisher<px4_msgs::msg::ArmingCheckReply>::SharedPtr _arming_check_reply_pub;
130
131 RequirementFlags _mode_requirements{};
132 rclcpp::TimerBase::SharedPtr _watchdog_timer;
133 bool _shutdown_on_timeout{true};
134};
135
136} // namespace px4_ros2
Definition health_and_arming_checks.hpp:24
Definition health_and_arming_checks.hpp:93
bool doRegister(const std::string &name)
Requirement flags used by modes.
Definition requirement_flags.hpp:17
Definition events.hpp:51