PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
local_position.hpp
1 /****************************************************************************
2  * Copyright (c) 2023 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <Eigen/Eigen>
9 #include <px4_msgs/msg/vehicle_local_position.hpp>
10 #include <px4_ros2/common/context.hpp>
11 #include <px4_ros2/utils/subscription.hpp>
12 
13 namespace px4_ros2 {
21 class OdometryLocalPosition : public Subscription<px4_msgs::msg::VehicleLocalPosition> {
22  public:
29  explicit OdometryLocalPosition(Context& context, bool local_position_is_optional = false);
30 
31  bool positionXYValid() const { return lastValid() && last().xy_valid; }
32 
33  bool positionZValid() const { return lastValid() && last().z_valid; }
34 
35  Eigen::Vector3f positionNed() const
36  {
37  const px4_msgs::msg::VehicleLocalPosition& pos = last();
38  return {pos.x, pos.y, pos.z};
39  }
40 
41  bool velocityXYValid() const { return lastValid() && last().v_xy_valid; }
42 
43  bool velocityZValid() const { return lastValid() && last().v_z_valid; }
44  Eigen::Vector3f velocityNed() const
45  {
46  const px4_msgs::msg::VehicleLocalPosition& pos = last();
47  return {pos.vx, pos.vy, pos.vz};
48  }
49 
50  Eigen::Vector3f accelerationNed() const
51  {
52  const px4_msgs::msg::VehicleLocalPosition& pos = last();
53  return {pos.ax, pos.ay, pos.az};
54  }
55 
61  float heading() const
62  {
63  const px4_msgs::msg::VehicleLocalPosition& pos = last();
64  return pos.heading;
65  }
66 
67  float distanceGround() const
68  {
69  const px4_msgs::msg::VehicleLocalPosition& pos = last();
70  return pos.dist_bottom;
71  }
72 };
73 
75 } /* namespace px4_ros2 */
Definition: context.hpp:18
Provides access to the vehicle's local position estimate.
Definition: local_position.hpp:21
float heading() const
Get the vehicle's heading relative to NED earth-fixed frame.
Definition: local_position.hpp:61
OdometryLocalPosition(Context &context, bool local_position_is_optional=false)
Provides a subscription to arbitrary ROS topics.
Definition: subscription.hpp:27
bool lastValid(const std::chrono::duration< int64_t, DurationT > max_delay=500ms) const
Check whether the last message is still valid. To be valid, the message must have been received withi...
Definition: subscription.hpp:80
const px4_msgs::msg::VehicleLocalPosition & last() const
Get the last-received message.
Definition: subscription.hpp:57