PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
mission.hpp
1 /****************************************************************************
2  * Copyright (c) 2024 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <optional>
9 #include <thread>
10 #include <filesystem>
11 #include <utility>
12 #include <variant>
13 #include <Eigen/Eigen>
14 
15 #include <px4_ros2/third_party/nlohmann/json_fwd.hpp>
16 
17 #include <rclcpp/rclcpp.hpp>
18 
19 namespace px4_ros2
20 {
29 {
30 public:
32 
33  explicit ActionArguments(const nlohmann::json & json);
34 
35  const nlohmann::json & json() const
36  {
37  return *_data;
38  }
39 
40  bool contains(const std::string & key) const;
41 
42  template<typename T>
43  T at(const std::string & key) const;
44 
50  bool resuming() const;
51 
52 private:
53  std::shared_ptr<nlohmann::json> _data;
54 };
55 
56 enum class MissionFrame
57 {
58  Global,
59 };
60 
61 static inline std::string missionFrameStr(MissionFrame frame)
62 {
63  switch (frame) {
64  case MissionFrame::Global:
65  return "global";
66  }
67  return "unknown";
68 }
69 
70 struct Waypoint
71 {
72  Waypoint() = default;
73  explicit Waypoint(Eigen::Vector3d a_coordinate, MissionFrame a_frame = MissionFrame::Global)
74  : coordinate(std::move(a_coordinate)), frame(a_frame) {}
75 
76  Eigen::Vector3d coordinate;
77 
78  MissionFrame frame{MissionFrame::Global};
79 
80  friend void from_json(const nlohmann::json & j, Waypoint & o); // NOLINT
81  friend void to_json(nlohmann::json & j, const Waypoint & o); // NOLINT
82 };
83 
84 enum class NavigationItemType
85 {
86  Waypoint,
87 };
88 
90 {
91  NavigationItem() = default;
92  NavigationItem(const Waypoint & waypoint) // NOLINT(google-explicit-constructor)
93  : data(waypoint) {}
94 
95  std::string id;
96  std::variant<Waypoint> data;
97 
98  friend void from_json(const nlohmann::json & j, NavigationItem & o); // NOLINT
99  friend void to_json(nlohmann::json & j, const NavigationItem & o); // NOLINT
100 };
101 
102 
104 {
105  ActionItem() = default;
106  explicit ActionItem(std::string a_name, ActionArguments a_arguments = {})
107  : name(std::move(a_name)), arguments(std::move(a_arguments)) {}
108 
109  std::string name;
110  std::string id;
111 
112  ActionArguments arguments;
113 
114  friend void from_json(const nlohmann::json & j, ActionItem & o); // NOLINT
115  friend void to_json(nlohmann::json & j, const ActionItem & o); // NOLINT
116 };
117 
118 using MissionItem = std::variant<NavigationItem, ActionItem>;
119 
121 {
122  std::optional<float> horizontal_velocity;
123  std::optional<float> vertical_velocity;
124  std::optional<float> max_heading_rate;
125 
126  void combineWith(const TrajectoryOptions & options)
127  {
128  if (options.horizontal_velocity) {
130  }
131  if (options.vertical_velocity) {
133  }
134  if (options.max_heading_rate) {
136  }
137  }
138 
139  friend void from_json(const nlohmann::json & j, TrajectoryOptions & o); // NOLINT
140  friend void to_json(nlohmann::json & j, const TrajectoryOptions & o); // NOLINT
141 };
142 
144 {
145  TrajectoryOptions trajectory_options;
146 
147  friend void from_json(const nlohmann::json & j, MissionDefaults & o); // NOLINT
148  friend void to_json(nlohmann::json & j, const MissionDefaults & o); // NOLINT
149 };
150 
157 class Mission
158 {
159 public:
160  Mission() = default;
161 
162  explicit Mission(
163  std::vector<MissionItem> mission_items,
164  MissionDefaults mission_defaults = {})
165  : _mission_defaults(mission_defaults), _mission_items(std::move(mission_items)) {}
166 
167  const MissionDefaults & defaults() const {return _mission_defaults;}
168 
169  const std::vector<MissionItem> & items() const {return _mission_items;}
170 
171  bool indexValid(int index) const
172  {
173  return index >= 0 && index < static_cast<int>(_mission_items.size());
174  }
175 
176  friend void from_json(const nlohmann::json & j, Mission & o); // NOLINT
177  friend void to_json(nlohmann::json & j, const Mission & o); // NOLINT
178 
179  std::string checksum() const;
180 
181 private:
182  MissionDefaults _mission_defaults;
183  std::vector<MissionItem> _mission_items;
184 };
185 
191 {
192 public:
194  std::shared_ptr<rclcpp::Node> node, std::filesystem::path filename,
195  std::function<void(std::shared_ptr<Mission>)> on_mission_update);
197 
198 private:
199  void run();
200  void fileUpdated();
201 
202  std::shared_ptr<rclcpp::Node> _node;
203  const std::filesystem::path _filename;
204  const std::function<void(std::shared_ptr<Mission>)> _on_mission_update;
205  std::thread _thread;
206  int _event_fd{-1};
207  rclcpp::TimerBase::SharedPtr _update_timer;
208 };
209 
210 
212 } /* namespace px4_ros2 */
Arguments passed to an action from the mission JSON definition.
Definition: mission.hpp:29
bool resuming() const
Check if the action is being resumed.
File monitor to load a mission from a JSON file.
Definition: mission.hpp:191
Mission definition.
Definition: mission.hpp:158
Definition: mission.hpp:104
Definition: mission.hpp:144
Definition: mission.hpp:90
Definition: mission.hpp:121
std::optional< float > max_heading_rate
[rad/s]
Definition: mission.hpp:124
std::optional< float > vertical_velocity
[m/s]
Definition: mission.hpp:123
std::optional< float > horizontal_velocity
[m/s]
Definition: mission.hpp:122
Definition: mission.hpp:71