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 using WatchdogTimeoutCallback = std::function<void()>;
85
86 HealthAndArmingChecks(rclcpp::Node& node, CheckCallback check_callback,
87 const std::string& topic_namespace_prefix = "");
89
96 bool doRegister(const std::string& name);
97
98 void setModeRequirements(const RequirementFlags& mode_requirements)
99 {
100 _mode_requirements = mode_requirements;
101 }
102
103 RequirementFlags& modeRequirements() { return _mode_requirements; }
104
105 void disableWatchdogTimer() { _watchdog_timer = nullptr; }
106
113 void setWatchdogTimeoutCallback(WatchdogTimeoutCallback callback);
114
115 private:
116 friend class ModeBase;
117 friend class ModeExecutorBase;
118 void overrideRegistration(const std::shared_ptr<Registration>& registration);
119
120 void watchdogTimerUpdate();
121
122 rclcpp::Node& _node;
123 std::shared_ptr<Registration> _registration;
124 CheckCallback _check_callback;
125 bool _check_triggered{true};
126 bool _first_request{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 WatchdogTimeoutCallback _watchdog_timeout_callback;
135};
136
137} // namespace px4_ros2
Definition health_and_arming_checks.hpp:21
Definition health_and_arming_checks.hpp:81
void setWatchdogTimeoutCallback(WatchdogTimeoutCallback callback)
bool doRegister(const std::string &name)
Base class for a mode.
Definition mode.hpp:74
Base class for a mode executor.
Definition mode_executor.hpp:29
Requirement flags used by modes.
Definition requirement_flags.hpp:15
Definition events.hpp:47