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/mission/actions/action.hpp>
9 #include <px4_ros2/mission/mission_executor.hpp>
10 #include <px4_ros2/components/mode.hpp>
11 #include <px4_ros2/vehicle_state/land_detected.hpp>
12 #include <rclcpp/rclcpp.hpp>
13 
14 
15 namespace px4_ros2::default_actions
16 {
25 class Rtl : public ActionInterface
26 {
27 public:
28  ~Rtl() override = default;
29  std::string name() const override {return "rtl";}
30 
31  bool shouldStopAtWaypoint(const ActionArguments & arguments) override {return false;}
32  void run(
33  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
34  const std::function<void()> & on_completed) override
35  {
36  handler->runMode(ModeBase::kModeIDRtl, on_completed);
37  }
38 };
39 
44 class Land : public ActionInterface
45 {
46 public:
47  ~Land() override = default;
48  std::string name() const override {return "land";}
49 
50  bool shouldStopAtWaypoint(const ActionArguments & arguments) override {return true;}
51  void run(
52  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
53  const std::function<void()> & on_completed) override
54  {
55  handler->runMode(ModeBase::kModeIDLand, on_completed);
56  }
57 };
58 
63 class Takeoff : public ActionInterface
64 {
65 public:
66  ~Takeoff() override = default;
67  std::string name() const override {return "takeoff";}
68 
69  void run(
70  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
71  const std::function<void()> & on_completed) override
72  {
73  float altitude = NAN;
74  if (arguments.contains("altitude")) {
75  altitude = arguments.at<float>("altitude");
76  }
77  handler->runModeTakeoff(altitude, NAN, on_completed);
78  }
79 };
80 
85 class Hold : public ActionInterface
86 {
87 public:
88  explicit Hold(ModeBase & mode)
89  : _node(mode.node())
90  {
91  }
92  ~Hold() override = default;
93  std::string name() const override {return "hold";}
94 
95  bool shouldStopAtWaypoint(const ActionArguments & arguments) override {return true;}
96 
97  void run(
98  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
99  const std::function<void()> & on_completed) override
100  {
101  float duration_s = 1.0f;
102  if (arguments.contains("duration")) {
103  duration_s = arguments.at<float>("duration");
104  }
105  handler->runMode(ModeBase::kModeIDLoiter, [] {});
106  _timer = _node.create_wall_timer(
107  std::chrono::duration<float>(duration_s), [this, on_completed] {
108  _timer.reset();
109  on_completed();
110  });
111  }
112 
113  void deactivate() override
114  {
115  _timer.reset();
116  }
117 
118 private:
119  rclcpp::Node & _node;
120  std::shared_ptr<rclcpp::TimerBase> _timer;
121 };
122 
127 class OnResume : public ActionInterface
128 {
129 public:
130  explicit OnResume(ModeBase & mode, std::shared_ptr<LandDetected> land_detected)
131  : _node(mode.node()), _landed(std::move(land_detected))
132  {
133  }
134 
135  ~OnResume() override = default;
136  std::string name() const override {return "onResume";}
137 
138  void run(
139  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
140  const std::function<void()> & on_completed) override;
141 
142 private:
143  void resumeFromUnexpectedLanding(
144  const std::shared_ptr<ActionHandler> & handler,
145  const std::function<void()> & on_completed, int current_mission_index);
146  void navigateToPreviousWaypoint(
147  const std::shared_ptr<ActionHandler> & handler,
148  const std::function<void()> & on_completed, int current_mission_index);
149 
150  rclcpp::Node & _node;
151  std::shared_ptr<LandDetected> _landed;
152 };
153 
159 {
160 public:
161  ~OnFailure() override = default;
162  std::string name() const override {return "onFailure";}
163 
164  void run(
165  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
166  const std::function<void()> & on_completed) override;
167 };
168 
174 {
175 public:
176  ~ChangeSettings() override = default;
177  std::string name() const override {return "changeSettings";}
178 
179  bool shouldStopAtWaypoint(const ActionArguments & arguments) override {return false;}
180 
181  void run(
182  const std::shared_ptr<ActionHandler> & handler, const ActionArguments & arguments,
183  const std::function<void()> & on_completed) override;
184 
185  void deactivate() override;
186 
187 private:
188  std::unique_ptr<ActionStateKeeper> _state;
189 };
190 
192 } /* namespace px4_ros2::default_actions */
Arguments passed to an action from the mission JSON definition.
Definition: mission.hpp:29
Interface class for an action.
Definition: action.hpp:26
Base class for a mode.
Definition: mode.hpp:71
Action to change mission settings (e.g. velocity)
Definition: default_actions.hpp:174
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:179
Default action to trigger the Hold (Loiter) mode.
Definition: default_actions.hpp:86
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:95
void deactivate() override
Definition: default_actions.hpp:113
Default action to trigger the Land mode.
Definition: default_actions.hpp:45
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:50
Default action to handle failures.
Definition: default_actions.hpp:159
Default action to resume missions.
Definition: default_actions.hpp:128
Default action to trigger the RTL mode.
Definition: default_actions.hpp:26
bool shouldStopAtWaypoint(const ActionArguments &arguments) override
Definition: default_actions.hpp:31
Default action to trigger the Takeoff mode.
Definition: default_actions.hpp:64