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 <functional>
9#include <memory>
10#include <px4_msgs/msg/arming_check_reply.hpp>
11#include <px4_msgs/msg/arming_check_request.hpp>
12#include <px4_ros2/common/requirement_flags.hpp>
13#include <rclcpp/rclcpp.hpp>
14
15#include "events.hpp"
16
17class Registration;
18
19namespace px4_ros2 {
20
22 public:
23 explicit HealthAndArmingCheckReporter(px4_msgs::msg::ArmingCheckReply& arming_check_reply)
24 : _arming_check_reply(arming_check_reply)
25 {
26 }
27
28 template <typename... Args>
29 void armingCheckFailureExt(uint32_t event_id, events::Log log_level, const char* message,
30 Args... args)
31 {
32 const uint16_t navigation_mode_groups{};
33 const uint8_t health_component_index{};
34
35 _arming_check_reply.can_arm_and_run = false;
36
37 if (!addEvent(event_id, log_level, message, navigation_mode_groups, health_component_index,
38 args...)) {
39 printf("Error: too many events\n");
40 }
41 }
42
43 void setHealth(uint8_t health_component_index, bool is_present, bool warning, bool error);
44
45 private:
46 template <typename... Args>
47 bool addEvent(uint32_t event_id, const events::LogLevels& log_levels, const char* message,
48 Args... args);
49
50 px4_msgs::msg::ArmingCheckReply& _arming_check_reply;
51};
52
53template <typename... Args>
54bool HealthAndArmingCheckReporter::addEvent(uint32_t event_id, const events::LogLevels& log_levels,
55 const char* message, Args... args)
56{
57 if (_arming_check_reply.num_events >= _arming_check_reply.events.size()) {
58 return false;
59 }
60
61 events::EventType& e = _arming_check_reply.events[_arming_check_reply.num_events];
62 e.log_levels =
63 (static_cast<uint8_t>(log_levels.internal) << 4) | static_cast<uint8_t>(log_levels.external);
64 e.id = event_id;
65 static_assert(events::util::sizeofArguments(args...) <= sizeof(e.arguments),
66 "Too many arguments");
67 events::util::fillEventArguments(static_cast<uint8_t*>(e.arguments.data()), args...);
68 ++_arming_check_reply.num_events;
69 return true;
70}
71
72inline void HealthAndArmingCheckReporter::setHealth(uint8_t health_component_index, bool is_present,
73 bool warning, bool error)
74{
75 _arming_check_reply.health_component_index = health_component_index;
76 _arming_check_reply.health_component_is_present = is_present;
77 _arming_check_reply.health_component_warning = warning;
78 _arming_check_reply.health_component_error = error;
79}
80
82 public:
83 using CheckCallback = std::function<void(HealthAndArmingCheckReporter&)>;
84
85 HealthAndArmingChecks(rclcpp::Node& node, CheckCallback check_callback,
86 const std::string& topic_namespace_prefix = "");
88
95 bool doRegister(const std::string& name);
96
97 void setModeRequirements(const RequirementFlags& mode_requirements)
98 {
99 _mode_requirements = mode_requirements;
100 }
101
102 RequirementFlags& modeRequirements() { return _mode_requirements; }
103
104 void disableWatchdogTimer() { _watchdog_timer.reset(); }
105
106 private:
107 friend class ModeBase;
108 friend class ModeExecutorBase;
109 void overrideRegistration(const std::shared_ptr<Registration>& registration);
110
111 void watchdogTimerUpdate();
112
113 rclcpp::Node& _node;
114 std::shared_ptr<Registration> _registration;
115 CheckCallback _check_callback;
116 bool _check_triggered{true};
117 bool _first_request{true};
118
119 rclcpp::Subscription<px4_msgs::msg::ArmingCheckRequest>::SharedPtr _arming_check_request_sub;
120 rclcpp::Publisher<px4_msgs::msg::ArmingCheckReply>::SharedPtr _arming_check_reply_pub;
121
122 RequirementFlags _mode_requirements{};
123 rclcpp::TimerBase::SharedPtr _watchdog_timer;
124 bool _shutdown_on_timeout{true};
125};
126
127} // namespace px4_ros2
Definition health_and_arming_checks.hpp:21
Definition health_and_arming_checks.hpp:81
bool doRegister(const std::string &name)
Requirement flags used by modes.
Definition requirement_flags.hpp:15
Definition events.hpp:47