-
Notifications
You must be signed in to change notification settings - Fork 75
Add odometry subscription helper #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /**************************************************************************** | ||
| * Copyright (c) 2023 PX4 Development Team. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| ****************************************************************************/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <Eigen/Eigen> | ||
| #include <px4_msgs/msg/vehicle_odometry.hpp> | ||
| #include <px4_ros2/common/context.hpp> | ||
| #include <px4_ros2/utils/geometry.hpp> | ||
| #include <px4_ros2/utils/subscription.hpp> | ||
|
|
||
| namespace px4_ros2 { | ||
| /** \ingroup odometry | ||
| * @{ | ||
| */ | ||
|
|
||
| /** | ||
| * @brief Provides access to the vehicle's odometry estimate | ||
| * @details Use this wrapper when you need position, velocity, attitude, and angular rates | ||
| * to be strictly time-aligned from the same odometry message. | ||
| * This class consolidates the odometry snapshot so advanced control (e.g. MPC) or end-to-end | ||
| * reinforcement learning pipelines can consume a consistent state vector at a single timestamp. | ||
| */ | ||
| class Odometry : public Subscription<px4_msgs::msg::VehicleOdometry> { | ||
| public: | ||
| explicit Odometry(Context& context); | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's position in the North-East-Down (NED) local frame. | ||
| * * @return The [North, East, Down] position in meters. | ||
| */ | ||
| Eigen::Vector3f positionNed() const | ||
| { | ||
| const px4_msgs::msg::VehicleOdometry& odom = last(); | ||
| return {odom.position[0], odom.position[1], odom.position[2]}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's linear velocity in the North-East-Down (NED) frame. | ||
| * * @note By default, PX4 uses VELOCITY_FRAME_NED for odometry updates. | ||
| * @return the velocity. | ||
| */ | ||
| Eigen::Vector3f velocityNed() const | ||
| { | ||
| const px4_msgs::msg::VehicleOdometry& odom = last(); | ||
| return {odom.velocity[0], odom.velocity[1], odom.velocity[2]}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's angular velocity in FRD frame. | ||
| * | ||
| * @return the angular velocity | ||
| */ | ||
| Eigen::Vector3f angularVelocityFrd() const | ||
| { | ||
| const px4_msgs::msg::VehicleOdometry& odom = last(); | ||
| return {odom.angular_velocity[0], odom.angular_velocity[1], odom.angular_velocity[2]}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's attitude. | ||
| * | ||
| * @return the attitude quaternion | ||
| */ | ||
| Eigen::Quaternionf attitude() const | ||
| { | ||
| const px4_msgs::msg::VehicleOdometry& odom = last(); | ||
| return Eigen::Quaternionf{odom.q[0], odom.q[1], odom.q[2], odom.q[3]}; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's roll in extrinsic RPY order. | ||
| * | ||
| * @return the attitude roll in radians within [-pi, pi] | ||
| */ | ||
| float roll() const { return quaternionToRoll(attitude()); } | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's pitch in extrinsic RPY order. | ||
| * | ||
| * @return the attitude pitch in radians within [-pi, pi] | ||
| */ | ||
| float pitch() const { return quaternionToPitch(attitude()); } | ||
|
|
||
| /** | ||
| * @brief Get the vehicle's yaw in extrinsic RPY order. | ||
| * | ||
| * @return the attitude yaw in radians within [-pi, pi] | ||
| */ | ||
| float yaw() const { return quaternionToYaw(attitude()); } | ||
| }; | ||
|
|
||
| /** @}*/ | ||
| } /* namespace px4_ros2 */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /**************************************************************************** | ||
| * Copyright (c) 2023 PX4 Development Team. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| ****************************************************************************/ | ||
|
|
||
| #include <px4_ros2/odometry/odometry.hpp> | ||
| #include <px4_ros2/utils/message_version.hpp> | ||
|
|
||
| namespace px4_ros2 { | ||
|
|
||
| Odometry::Odometry(Context& context) | ||
| : Subscription<px4_msgs::msg::VehicleOdometry>( | ||
| context, "fmu/out/vehicle_odometry" + | ||
| px4_ros2::getMessageNameVersion<px4_msgs::msg::VehicleOdometry>()) | ||
| { | ||
| RequirementFlags requirements{}; | ||
| requirements.local_position = true; | ||
| requirements.local_alt = true; | ||
| requirements.attitude = true; | ||
| requirements.angular_velocity = true; | ||
|
|
||
| context.setRequirement(requirements); | ||
| } | ||
|
|
||
| } // namespace px4_ros2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document why and when you would want to use this over e.g. https://github.com/rocketSzw/px4-ros2-interface-lib/blob/915ccd5db66bf7d4fc2accf44f5cafaab8dbd76a/px4_ros2_cpp/include/px4_ros2/odometry/attitude.hpp?
Just to avoid confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary motivation is to ensure strict time synchronization of the full state vector.
In the default PX4 dds configuration,
local_positionand attitude are published at 50Hz, but angular velocity is often omitted. For advanced control scenarios—such as MPC or End-to-End Reinforcement Learning (mapping states directly to motor throttles)—we require position, velocity, attitude, and angular rates to be aligned at the exact same timestamp.While
attitude.hpphandles orientation, it does not inherently bundle the necessary odometry/velocity data synchronously. This wrapper fills that gap by providing a consolidated, consistent state snapshot, which is critical for the stability of my RL controller.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I'm aware of that :)
But someone looking at the code might not be, so can you add these explanations as a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I have added explanations and resolved the conflict.