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  // Use node clock for sim time support
94  _timer = rclcpp::create_timer(&_node, _node.get_clock(),
95  rclcpp::Duration::from_seconds(duration_s), [this, on_completed] {
96  _timer.reset();
97  on_completed();
98  });
99  }
100 
101  void deactivate() override { _timer.reset(); }
102 
103  private:
104  rclcpp::Node& _node;
105  std::shared_ptr<rclcpp::TimerBase> _timer;
106 };
107 
112 class OnResume : public ActionInterface {
113  public:
114  explicit OnResume(ModeBase& mode, std::shared_ptr<LandDetected> land_detected)
115  : _node(mode.node()), _landed(std::move(land_detected))
116  {
117  }
118 
119  ~OnResume() override = default;
120  std::string name() const override { return "onResume"; }
121 
122  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
123  const std::function<void()>& on_completed) override;
124 
125  private:
126  void resumeFromUnexpectedLanding(const std::shared_ptr<ActionHandler>& handler,
127  const std::function<void()>& on_completed,
128  int current_mission_index);
129  void navigateToPreviousWaypoint(const std::shared_ptr<ActionHandler>& handler,
130  const std::function<void()>& on_completed,
131  int current_mission_index);
132 
133  rclcpp::Node& _node;
134  std::shared_ptr<LandDetected> _landed;
135 };
136 
141 class OnFailure : public ActionInterface {
142  public:
143  ~OnFailure() override = default;
144  std::string name() const override { return "onFailure"; }
145 
146  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
147  const std::function<void()>& on_completed) override;
148 };
149 
155  public:
156  ~ChangeSettings() override = default;
157  std::string name() const override { return "changeSettings"; }
158 
159  bool shouldStopAtWaypoint(const ActionArguments& arguments) override { return false; }
160 
161  void run(const std::shared_ptr<ActionHandler>& handler, const ActionArguments& arguments,
162  const std::function<void()>& on_completed) override;
163 
164  void deactivate() override;
165 
166  private:
167  std::unique_ptr<ActionStateKeeper> _state;
168 };
169 
171 } /* 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:154
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:159
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:101
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:141
Default action to resume missions.
Definition: default_actions.hpp:112
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