-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix fake GPS velocity frame and COG units #2261
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
Open
Daniiiil1
wants to merge
2
commits into
mavlink:ros2
Choose a base branch
from
Daniiiil1:codex/fix-fake-gps-ned-velocity
base: ros2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,74 @@ | ||
| /* | ||
| * Copyright 2026 Daniil Mordanov. | ||
| * | ||
| * This file is part of the mavros package and subject to the license terms | ||
| * in the top-level LICENSE file of the mavros repository. | ||
| * https://github.com/mavlink/mavros/tree/master/LICENSE.md | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
|
|
||
| #include <Eigen/Core> | ||
|
|
||
| #include "mavros/frame_tf.hpp" | ||
|
|
||
| namespace mavros | ||
| { | ||
| namespace extra_plugins | ||
| { | ||
| namespace fake_gps | ||
| { | ||
|
|
||
| /** | ||
| * @brief Derive local NED velocity from two ECEF positions. | ||
| * | ||
| * @param current_ecef Current ECEF position [m]. | ||
| * @param previous_ecef Previous ECEF position [m]. | ||
| * @param dt Elapsed time [s]. | ||
| * @param map_origin Geodetic map origin [latitude, longitude, altitude]. | ||
| * @return Velocity in the local North-East-Down frame [m/s]. | ||
| */ | ||
| inline Eigen::Vector3d calculate_velocity_ned( | ||
| const Eigen::Vector3d & current_ecef, | ||
| const Eigen::Vector3d & previous_ecef, | ||
| const double dt, | ||
| const Eigen::Vector3d & map_origin) | ||
| { | ||
| if (dt <= 0.0) { | ||
| return Eigen::Vector3d::Zero(); | ||
| } | ||
|
|
||
| const Eigen::Vector3d velocity_ecef = (current_ecef - previous_ecef) / dt; | ||
| const Eigen::Vector3d velocity_enu = ftf::transform_frame_ecef_enu( | ||
| velocity_ecef, map_origin); | ||
| return ftf::transform_frame_enu_ned(velocity_enu); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Compute course over ground from a NED velocity vector. | ||
| * | ||
| * @return Course clockwise from north in centidegrees in the range [0, 35999]. | ||
| */ | ||
| inline uint16_t course_over_ground_cdeg(const Eigen::Vector3d & velocity_ned) | ||
| { | ||
| const double north = velocity_ned.x(); | ||
| const double east = velocity_ned.y(); | ||
|
|
||
| if (north == 0.0 && east == 0.0) { | ||
| return 0; | ||
| } | ||
|
|
||
| double course_deg = std::atan2(east, north) * 180.0 / M_PI; | ||
| if (course_deg < 0.0) { | ||
| course_deg += 360.0; | ||
| } | ||
|
|
||
| return static_cast<uint16_t>(std::lround(course_deg * 100.0)) % 36000; | ||
| } | ||
|
|
||
| } // namespace fake_gps | ||
| } // namespace extra_plugins | ||
| } // namespace mavros | ||
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,71 @@ | ||
| /* | ||
| * Copyright 2026 Daniil Mordanov. | ||
| * | ||
| * This file is part of the mavros package and subject to the license terms | ||
| * in the top-level LICENSE file of the mavros repository. | ||
| * https://github.com/mavlink/mavros/tree/master/LICENSE.md | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <Eigen/Core> | ||
|
|
||
| #include "fake_gps_utils.hpp" | ||
|
|
||
| namespace fake_gps = mavros::extra_plugins::fake_gps; | ||
|
|
||
| static constexpr double epsilon = 1e-9; | ||
|
|
||
| TEST(FakeGpsUtils, ConvertsEcefVelocityToNed) | ||
| { | ||
| const Eigen::Vector3d map_origin(0.0, 0.0, 0.0); | ||
| const Eigen::Vector3d previous_ecef = Eigen::Vector3d::Zero(); | ||
|
|
||
| const auto north = fake_gps::calculate_velocity_ned( | ||
| Eigen::Vector3d(0.0, 0.0, 2.0), previous_ecef, 2.0, map_origin); | ||
| EXPECT_NEAR(north.x(), 1.0, epsilon); | ||
| EXPECT_NEAR(north.y(), 0.0, epsilon); | ||
| EXPECT_NEAR(north.z(), 0.0, epsilon); | ||
|
|
||
| const auto east = fake_gps::calculate_velocity_ned( | ||
| Eigen::Vector3d(0.0, 2.0, 0.0), previous_ecef, 2.0, map_origin); | ||
| EXPECT_NEAR(east.x(), 0.0, epsilon); | ||
| EXPECT_NEAR(east.y(), 1.0, epsilon); | ||
| EXPECT_NEAR(east.z(), 0.0, epsilon); | ||
|
|
||
| const auto up = fake_gps::calculate_velocity_ned( | ||
| Eigen::Vector3d(2.0, 0.0, 0.0), previous_ecef, 2.0, map_origin); | ||
| EXPECT_NEAR(up.x(), 0.0, epsilon); | ||
| EXPECT_NEAR(up.y(), 0.0, epsilon); | ||
| EXPECT_NEAR(up.z(), -1.0, epsilon); | ||
| } | ||
|
|
||
| TEST(FakeGpsUtils, RejectsNonIncreasingTimestamps) | ||
| { | ||
| const Eigen::Vector3d current_ecef(1.0, 2.0, 3.0); | ||
| const Eigen::Vector3d previous_ecef = Eigen::Vector3d::Zero(); | ||
| const Eigen::Vector3d map_origin(0.0, 0.0, 0.0); | ||
|
|
||
| EXPECT_TRUE( | ||
| fake_gps::calculate_velocity_ned( | ||
| current_ecef, previous_ecef, 0.0, map_origin).isZero()); | ||
| EXPECT_TRUE( | ||
| fake_gps::calculate_velocity_ned( | ||
| current_ecef, previous_ecef, -1.0, map_origin).isZero()); | ||
| } | ||
|
|
||
| TEST(FakeGpsUtils, ComputesCourseClockwiseFromNorth) | ||
| { | ||
| EXPECT_EQ( | ||
| fake_gps::course_over_ground_cdeg(Eigen::Vector3d(0.0, 0.0, 0.0)), 0); | ||
| EXPECT_EQ( | ||
| fake_gps::course_over_ground_cdeg(Eigen::Vector3d(1.0, 0.0, 0.0)), 0); | ||
| EXPECT_EQ( | ||
| fake_gps::course_over_ground_cdeg(Eigen::Vector3d(1.0, 1.0, 0.0)), 4500); | ||
| EXPECT_EQ( | ||
| fake_gps::course_over_ground_cdeg(Eigen::Vector3d(0.0, 1.0, 0.0)), 9000); | ||
| EXPECT_EQ( | ||
| fake_gps::course_over_ground_cdeg(Eigen::Vector3d(-1.0, 0.0, 0.0)), 18000); | ||
| EXPECT_EQ( | ||
| fake_gps::course_over_ground_cdeg(Eigen::Vector3d(0.0, -1.0, 0.0)), 27000); | ||
| } |
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.
Maybe worth to move to generic frame_tf.hpp?