PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
default_actions.hpp
1 /****************************************************************************
2  * Copyright (c) 2024 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <px4_ros2/components/mode.hpp>
9 #include <px4_ros2/mission/actions/action.hpp>
10 #include <px4_ros2/mission/mission_executor.hpp>
11 #include <px4_ros2/vehicle_state/land_detected.hpp>
12 #include <rclcpp/rclcpp.hpp>
13 
14 namespace px4_ros2::default_actions {
23 class Rtl : public ActionInterface {
24  public:
25  ~Rtl() override = default;
26  std::string name() const override { return "rtl"; }
27 
28  bool shouldStopAtWaypoint(const ActionArguments& arguments) override { return false; }
29  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
30  const std::function<void()>& on_completed) override
31  {
32  handler->runMode(ModeBase::kModeIDRtl, on_completed);
33  }
34 };
35 
40 class Land : public ActionInterface {
41  public:
42  ~Land() override = default;
43  std::string name() const override { return "land"; }
44 
45  bool shouldStopAtWaypoint(const ActionArguments& arguments) override { return true; }
46  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
47  const std::function<void()>& on_completed) override
48  {
49  handler->runMode(ModeBase::kModeIDLand, on_completed);
50  }
51 };
52 
57 class Takeoff : public ActionInterface {
58  public:
59  ~Takeoff() override = default;
60  std::string name() const override { return "takeoff"; }
61 
62  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
63  const std::function<void()>& on_completed) override
64  {
65  float altitude = NAN;
66  if (arguments.contains("altitude")) {
67  altitude = arguments.at<float>("altitude");
68  }
69  handler->runModeTakeoff(altitude, NAN, on_completed);
70  }
71 };
72 
77 class Hold : public ActionInterface {
78  public:
79  explicit Hold(ModeBase& mode) : _node(mode.node()) {}
80  ~Hold() override = default;
81  std::string name() const override { return "hold"; }
82 
83  bool shouldStopAtWaypoint(const ActionArguments& arguments) override { return true; }
84 
85  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
86  const std::function<void()>& on_completed) override
87  {
88  float duration_s = 1.0f;
89  if (arguments.contains("duration")) {
90  duration_s = arguments.at<float>("duration");
91  }
92  handler->runMode(ModeBase::kModeIDLoiter, [] {});
93  _timer =
94  _node.create_wall_timer(std::chrono::duration<float>(duration_s), [this, on_completed] {
95  _timer.reset();
96  on_completed();
97  });
98  }
99 
100  void deactivate() override { _timer.reset(); }
101 
102  private:
103  rclcpp::Node& _node;
104  std::shared_ptr<rclcpp::TimerBase> _timer;
105 };
106 
111 class OnResume : public ActionInterface {
112  public:
113  explicit OnResume(ModeBase& mode, std::shared_ptr<LandDetected> land_detected)
114  : _node(mode.node()), _landed(std::move(land_detected))
115  {
116  }
117 
118  ~OnResume() override = default;
119  std::string name() const override { return "onResume"; }
120 
121  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
122  const std::function<void()>& on_completed) override;
123 
124  private:
125  void resumeFromUnexpectedLanding(const std::shared_ptr<ActionHandler>& handler,
126  const std::function<void()>& on_completed,
127  int current_mission_index);
128  void navigateToPreviousWaypoint(const std::shared_ptr<ActionHandler>& handler,
129  const std::function<void()>& on_completed,
130  int current_mission_index);
131 
132  rclcpp::Node& _node;
133  std::shared_ptr<LandDetected> _landed;
134 };
135 
140 class OnFailure : public ActionInterface {
141  public:
142  ~OnFailure() override = default;
143  std::string name() const override { return "onFailure"; }
144 
145  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
146  const std::function<void()>& on_completed) override;
147 };
148 
154  public:
155  ~ChangeSettings() override = default;
156  std::string name() const override { return "changeSettings"; }
157 
158  bool shouldStopAtWaypoint(const ActionArguments& arguments) override { return false; }
159 
160  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
161  const std::function<void()>& on_completed) override;
162 
163  void deactivate() override;
164 
165  private:
166  std::unique_ptr<ActionStateKeeper> _state;
167 };
168 
170 } /* namespace px4_ros2::default_actions */
Arguments passed to an action from the mission JSON definition.
Definition: mission.hpp:25
Interface class for an action.
Definition: action.hpp:24
Base class for a mode.
Definition: mode.hpp:73
Action to change mission settings (e.g. velocity)
Definition: default_actions.hpp:153
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:158
Default action to trigger the Hold (Loiter) mode.
Definition: default_actions.hpp:77
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:83
void deactivate() override
Definition: default_actions.hpp:100
Default action to trigger the Land mode.
Definition: default_actions.hpp:40
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:45
Default action to handle failures.
Definition: default_actions.hpp:140
Default action to resume missions.
Definition: default_actions.hpp:111
Default action to trigger the RTL mode.
Definition: default_actions.hpp:23
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:28
Default action to trigger the Takeoff mode.
Definition: default_actions.hpp:57