PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
geodesic.hpp
1 /****************************************************************************
2  * Copyright (c) 2024 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
20 #pragma once
21 
22 #include <cmath>
23 #include <Eigen/Eigen>
24 #include <rclcpp/rclcpp.hpp>
25 #include <px4_msgs/msg/vehicle_local_position.hpp>
26 #include <px4_ros2/common/context.hpp>
27 
28 namespace px4_ros2
29 {
34 class MapProjectionImpl;
35 
47 {
48 public:
49  explicit MapProjection(Context & context);
50 
51  ~MapProjection();
52 
56  bool isInitialized() const;
57 
65  Eigen::Vector2f globalToLocal(const Eigen::Vector2d & global_position) const;
66 
74  Eigen::Vector3f globalToLocal(const Eigen::Vector3d & global_position) const;
75 
83  Eigen::Vector2d localToGlobal(const Eigen::Vector2f & local_position) const;
84 
92  Eigen::Vector3d localToGlobal(const Eigen::Vector3f & local_position) const;
93 
94 private:
98  void assertInitalized() const;
99 
105  void vehicleLocalPositionCallback(px4_msgs::msg::VehicleLocalPosition::UniquePtr msg);
106 
107  rclcpp::Node & _node;
108  std::unique_ptr<MapProjectionImpl> _map_projection_math;
109  rclcpp::Subscription<px4_msgs::msg::VehicleLocalPosition>::SharedPtr _vehicle_local_position_sub;
110 };
111 
122  const Eigen::Vector2d & global_position_now,
123  const Eigen::Vector2d & global_position_next);
124 
135  const Eigen::Vector3d & global_position_now,
136  const Eigen::Vector3d & global_position_next)
137 {
139  static_cast<Eigen::Vector2d>(global_position_now.head(2)),
140  static_cast<Eigen::Vector2d>(global_position_next.head(2)));
141 }
142 
153  const Eigen::Vector3d & global_position_now,
154  const Eigen::Vector3d & global_position_next);
155 
166  const Eigen::Vector2d & global_position_now,
167  const Eigen::Vector2d & global_position_next);
168 
178 static inline float headingToGlobalPosition(
179  const Eigen::Vector3d & global_position_now,
180  const Eigen::Vector3d & global_position_next)
181 {
183  static_cast<Eigen::Vector2d>(global_position_now.head(2)),
184  static_cast<Eigen::Vector2d>(global_position_next.head(2)));
185 }
186 
196 Eigen::Vector2f vectorToGlobalPosition(
197  const Eigen::Vector2d & global_position_now,
198  const Eigen::Vector2d & global_position_next);
199 
209 static inline Eigen::Vector3f vectorToGlobalPosition(
210  const Eigen::Vector3d & global_position_now,
211  const Eigen::Vector3d & global_position_next)
212 {
213  const Eigen::Vector2f vector_res =
215  static_cast<Eigen::Vector2d>(global_position_now.head(2)),
216  static_cast<Eigen::Vector2d>(global_position_next.head(2)));
217  const double d_alt = -(global_position_next.z() - global_position_now.z());
218  return Eigen::Vector3f {vector_res.x(), vector_res.y(), static_cast<float>(d_alt)};
219 }
220 
232  const Eigen::Vector2d & global_position_line_start,
233  const Eigen::Vector2d & global_position_line_end,
234  float dist_from_start);
235 
248  const Eigen::Vector2d & global_position_now,
249  float heading, float dist);
250 
262 static inline Eigen::Vector3d globalPositionFromHeadingAndDist(
263  const Eigen::Vector3d & global_position_now,
264  float heading, float dist)
265 {
266  const Eigen::Vector2d global_position_res = globalPositionFromHeadingAndDist(
267  static_cast<Eigen::Vector2d>(global_position_now.head(2)), heading, dist);
268  return Eigen::Vector3d {
269  global_position_res.x(),
270  global_position_res.y(),
271  global_position_now.z()};
272 }
273 
284  const Eigen::Vector2d & global_position,
285  const Eigen::Vector2f & vector_ne);
286 
296 static inline Eigen::Vector3d addVectorToGlobalPosition(
297  const Eigen::Vector3d & global_position,
298  const Eigen::Vector3f & vector_ned)
299 {
300  const Eigen::Vector2d global_position_res = addVectorToGlobalPosition(
301  static_cast<Eigen::Vector2d>(global_position.head(2)),
302  static_cast<Eigen::Vector2f>(vector_ned.head(2)));
303  const double d_alt = -vector_ned.z();
304  return Eigen::Vector3d {global_position_res.x(), global_position_res.y(),
305  global_position.z() + d_alt};
306 }
307 
309 } // namespace px4_ros2
Definition: context.hpp:20
Provides methods to convert between the geographical coordinate system ("global") and the local azimu...
Definition: geodesic.hpp:47
bool isInitialized() const
Eigen::Vector2f globalToLocal(const Eigen::Vector2d &global_position) const
Transform a point in the geographic coordinate system to the local azimuthal equidistant plane using ...
Eigen::Vector2d localToGlobal(const Eigen::Vector2f &local_position) const
Transform a point in the local azimuthal equidistant plane to the geographic coordinate system using ...
Eigen::Vector3d localToGlobal(const Eigen::Vector3f &local_position) const
Transform a point in the local azimuthal equidistant plane to the geographic coordinate system using ...
Eigen::Vector3f globalToLocal(const Eigen::Vector3d &global_position) const
Transform a point in the geographic coordinate system to the local azimuthal equidistant plane using ...
static float horizontalDistanceToGlobalPosition(const Eigen::Vector3d &global_position_now, const Eigen::Vector3d &global_position_next)
Compute the horizontal distance between two global positions in meters.
Definition: geodesic.hpp:134
static Eigen::Vector3d globalPositionFromHeadingAndDist(const Eigen::Vector3d &global_position_now, float heading, float dist)
Compute the global position given another global position, distance and heading see http://www....
Definition: geodesic.hpp:262
static float headingToGlobalPosition(const Eigen::Vector3d &global_position_now, const Eigen::Vector3d &global_position_next)
Compute the heading to the next global position in radians.
Definition: geodesic.hpp:178
static Eigen::Vector3d addVectorToGlobalPosition(const Eigen::Vector3d &global_position, const Eigen::Vector3f &vector_ned)
Compute the global position from adding a local frame vector to the current global position.
Definition: geodesic.hpp:296
static Eigen::Vector3f vectorToGlobalPosition(const Eigen::Vector3d &global_position_now, const Eigen::Vector3d &global_position_next)
Compute the vector to the next global position in meters.
Definition: geodesic.hpp:209
float distanceToGlobalPosition(const Eigen::Vector3d &global_position_now, const Eigen::Vector3d &global_position_next)
Compute the distance between two global positions in meters.
Eigen::Vector2d globalPositionFromLineAndDist(const Eigen::Vector2d &global_position_line_start, const Eigen::Vector2d &global_position_line_end, float dist_from_start)
Compute the global position on the line vector defined by two positions (start -> end) at a certain d...