PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
mode.hpp
1 /****************************************************************************
2  * Copyright (c) 2023 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <cstdint>
9 
10 #include <rclcpp/rclcpp.hpp>
11 #include <px4_msgs/msg/mode_completed.hpp>
12 #include <px4_msgs/msg/vehicle_control_mode.hpp>
13 #include <px4_msgs/msg/vehicle_status.hpp>
14 #include "health_and_arming_checks.hpp"
15 #include "overrides.hpp"
16 #include "manual_control_input.hpp"
17 #include <px4_ros2/common/setpoint_base.hpp>
18 #include <px4_ros2/common/context.hpp>
19 
20 class Registration;
21 struct RegistrationSettings;
22 class SharedVehicleStatusToken;
23 
24 namespace px4_ros2
25 {
30 enum class Result
31 {
32  Success = 0,
33  Rejected,
34  Interrupted,
35  Timeout,
36  Deactivated,
37 
38  // Mode-specific results
39  ModeFailureOther = 100,
40 };
41 
42 static_assert(
43  static_cast<int>(Result::ModeFailureOther) ==
44  static_cast<int>(px4_msgs::msg::ModeCompleted::RESULT_FAILURE_OTHER),
45  "definition mismatch");
46 
47 constexpr inline const char * resultToString(Result result) noexcept
48 {
49  switch (result) {
50  case Result::Success: return "Success";
51 
52  case Result::Rejected: return "Rejected";
53 
54  case Result::Interrupted: return "Interrupted";
55 
56  case Result::Timeout: return "Timeout";
57 
58  case Result::Deactivated: return "Deactivated";
59 
60  case Result::ModeFailureOther: return "Mode Failure (generic)";
61  }
62 
63  return "Unknown";
64 }
65 
70 class ModeBase : public Context
71 {
72 public:
73  using ModeID = uint8_t;
74  static constexpr ModeID kModeIDInvalid = 0xff;
75 
76  static constexpr ModeID kModeIDPosctl =
77  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_POSCTL;
78  static constexpr ModeID kModeIDTakeoff =
79  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_AUTO_TAKEOFF;
80  static constexpr ModeID kModeIDDescend =
81  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_DESCEND;
82  static constexpr ModeID kModeIDLand =
83  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_AUTO_LAND;
84  static constexpr ModeID kModeIDRtl =
85  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_AUTO_RTL;
86  static constexpr ModeID kModeIDPrecisionLand =
87  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_AUTO_PRECLAND;
88  static constexpr ModeID kModeIDLoiter =
89  px4_msgs::msg::VehicleStatus::NAVIGATION_STATE_AUTO_LOITER;
90 
91  struct Settings
92  {
93  // NOLINTNEXTLINE allow implicit conversion
94  Settings(
95  std::string mode_name, bool want_activate_even_while_disarmed = false,
96  ModeID request_replace_internal_mode = kModeIDInvalid)
97  : name(std::move(mode_name)), activate_even_while_disarmed(want_activate_even_while_disarmed),
98  replace_internal_mode(request_replace_internal_mode) {}
99  std::string name;
101  ModeID replace_internal_mode{kModeIDInvalid};
102  };
103 
104  ModeBase(
105  rclcpp::Node & node, Settings settings,
106  const std::string & topic_namespace_prefix = "");
107  ModeBase(const ModeBase &) = delete;
108  virtual ~ModeBase();
109 
114  bool doRegister();
115 
116 
121 
125  virtual void onActivate() = 0;
126 
130  virtual void onDeactivate() = 0;
131 
137  void setSetpointUpdateRate(float rate_hz);
138 
139  virtual void updateSetpoint(float dt_s) {}
140 
146  void completed(Result result);
147 
148 
149  // Properties & state
150 
151  ModeID id() const;
152 
153  bool isArmed() const {return _is_armed;}
154 
155  bool isActive() const {return _is_active;}
156 
157  ConfigOverrides & configOverrides() {return _config_overrides;}
158 
163  RequirementFlags & modeRequirements() {return _health_and_arming_checks.modeRequirements();}
164 
165 protected:
166  void setSkipMessageCompatibilityCheck() {_skip_message_compatibility_check = true;}
167  void overrideRegistration(const std::shared_ptr<Registration> & registration);
168 
169  void disableWatchdogTimer() {_health_and_arming_checks.disableWatchdogTimer();}
170 
171  bool defaultMessageCompatibilityCheck();
172 
173 private:
174  void addSetpointType(SetpointBase * setpoint) override;
175  void setRequirement(const RequirementFlags & requirement_flags) override;
176 
177  friend class ModeExecutorBase;
178  RegistrationSettings getRegistrationSettings() const;
179  void onAboutToRegister();
180  bool onRegistered();
181 
182  void unsubscribeVehicleStatus();
183  void vehicleStatusUpdated(
184  const px4_msgs::msg::VehicleStatus::UniquePtr & msg,
185  bool do_not_activate = false);
186 
187  void callOnActivate();
188  void callOnDeactivate();
189 
190  void updateSetpointUpdateTimer();
191 
192  void updateModeRequirementsFromSetpoints();
193  void setSetpointUpdateRateFromSetpointTypes();
194  void activateSetpointType(SetpointBase & setpoint);
195 
196  std::shared_ptr<Registration> _registration;
197 
198  const Settings _settings;
199  bool _skip_message_compatibility_check{false};
200 
201  HealthAndArmingChecks _health_and_arming_checks;
202 
203  rclcpp::Publisher<px4_msgs::msg::ModeCompleted>::SharedPtr _mode_completed_pub;
204  rclcpp::Publisher<px4_msgs::msg::VehicleControlMode>::SharedPtr _config_control_setpoints_pub;
205 
206  std::unique_ptr<SharedVehicleStatusToken> _vehicle_status_sub_token;
207 
208  bool _is_active{false};
209  bool _is_armed{false};
210  bool _completed{false};
211 
212  float _setpoint_update_rate_hz{0.f};
213  rclcpp::TimerBase::SharedPtr _setpoint_update_timer;
214  rclcpp::Time _last_setpoint_update{};
215 
216  ConfigOverrides _config_overrides;
217 
218  std::vector<std::shared_ptr<SetpointBase>> _setpoint_types;
219  std::vector<SetpointBase *> _new_setpoint_types;
220 };
221 
223 } // namespace px4_ros2
Definition: context.hpp:20
Definition: health_and_arming_checks.hpp:24
Base class for a mode.
Definition: mode.hpp:71
virtual void checkArmingAndRunConditions(HealthAndArmingCheckReporter &reporter)
Definition: mode.hpp:120
RequirementFlags & modeRequirements()
Definition: mode.hpp:163
void setSetpointUpdateRate(float rate_hz)
uint8_t ModeID
Mode ID, corresponds to nav_state.
Definition: mode.hpp:73
virtual void onDeactivate()=0
virtual void onActivate()=0
void completed(Result result)
Result
Definition: mode.hpp:31
@ Interrupted
Ctrl-C or another error (from ROS)
@ Deactivated
Mode or executor got deactivated.
@ Rejected
The request was rejected.
Definition: mode.hpp:92
std::string name
Name of the mode with length < 25 characters.
Definition: mode.hpp:99
bool activate_even_while_disarmed
If true, the mode is also activated while disarmed if selected.
Definition: mode.hpp:100
ModeID replace_internal_mode
Can be used to replace an fmu-internal mode.
Definition: mode.hpp:101
Requirement flags used by modes.
Definition: requirement_flags.hpp:17