Skip to content

Commit

Permalink
HeadingSmoothing: fix angle wrapping and add unit tests
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Grob <[email protected]>
  • Loading branch information
bresch and MaEtUgR authored Sep 10, 2024
1 parent a5729da commit 03aec2e
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/motion_planning/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ target_include_directories(motion_planning PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

px4_add_unit_gtest(SRC VelocitySmoothingTest.cpp LINKLIBS motion_planning)
px4_add_unit_gtest(SRC PositionSmoothingTest.cpp LINKLIBS motion_planning)
px4_add_unit_gtest(SRC HeadingSmoothingTest.cpp LINKLIBS motion_planning)
2 changes: 1 addition & 1 deletion src/lib/motion_planning/HeadingSmoothing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

HeadingSmoothing::HeadingSmoothing()
{
_velocity_smoothing.setMaxVel(M_PI_F); // smoothed "velocity" is heading [-pi, pi]
_velocity_smoothing.setMaxVel(M_TWOPI_F); // "velocity" is heading. 2Pi limit is needed for correct angle wrapping
}

void HeadingSmoothing::reset(const float heading, const float heading_rate)
Expand Down
147 changes: 147 additions & 0 deletions src/lib/motion_planning/HeadingSmoothingTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/****************************************************************************
*
* Copyright (C) 2024 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* Test code for the Heading Smoothing library
* Run exclusively this test with the command "make tests TESTFILTER=HeadingSmoothing"
*/

#include "mathlib/math/Limits.hpp"
#include <gtest/gtest.h>

#include <motion_planning/HeadingSmoothing.hpp>

using namespace matrix;

class HeadingSmoothingTest : public ::testing::Test
{
public:
HeadingSmoothingTest()
{
_smoothing.setMaxHeadingRate(15.f);
_smoothing.setMaxHeadingAccel(1.f);
}

HeadingSmoothing _smoothing;
float _dt{.1f};
};

TEST_F(HeadingSmoothingTest, convergence)
{
const float heading_current = math::radians(0.f);
const float heading_target = math::radians(5.f);
_smoothing.reset(heading_current, 0.f);

const int nb_steps = ceilf(1.f / _dt);

for (int i = 0; i < nb_steps; i++) {
_smoothing.update(heading_target, _dt);
}

const float heading = _smoothing.getSmoothedHeading();
const float heading_rate = _smoothing.getSmoothedHeadingRate();
EXPECT_EQ(heading, heading_target) << "heading (deg): " << math::degrees(heading);
EXPECT_EQ(heading_rate, 0.f);
}

TEST_F(HeadingSmoothingTest, zero_crossing)
{
const float heading_current = math::radians(-95.f);
const float heading_target = math::radians(5.f);
_smoothing.reset(heading_current, 0.f);

const int nb_steps = ceilf(4.f / _dt);

for (int i = 0; i < nb_steps; i++) {
_smoothing.update(heading_target, _dt);
}

const float heading = _smoothing.getSmoothedHeading();
const float heading_rate = _smoothing.getSmoothedHeadingRate();
EXPECT_EQ(heading, heading_target) << "heading (deg): " << math::degrees(heading);
EXPECT_EQ(heading_rate, 0.f);
}

TEST_F(HeadingSmoothingTest, wrap_pi)
{
const float heading_current = math::radians(-170.f);
const float heading_target = math::radians(170.f);
_smoothing.reset(heading_current, 0.f);

const int nb_steps = ceilf(2.f / _dt);

for (int i = 0; i < nb_steps; i++) {
_smoothing.update(heading_target, _dt);
}

const float heading = _smoothing.getSmoothedHeading();
const float heading_rate = _smoothing.getSmoothedHeadingRate();
EXPECT_EQ(heading, heading_target) << "heading (deg): " << math::degrees(heading);
EXPECT_EQ(heading_rate, 0.f);
}

TEST_F(HeadingSmoothingTest, positive_rate)
{
const float max_heading_rate = .15f;
_smoothing.setMaxHeadingRate(max_heading_rate);
const float heading_current = math::radians(-170.f);
const float heading_target = math::radians(-20.f);
_smoothing.reset(heading_current, 0.f);

const int nb_steps = ceilf(2.f / _dt);

for (int i = 0; i < nb_steps; i++) {
_smoothing.update(heading_target, _dt);
}

const float heading_rate = _smoothing.getSmoothedHeadingRate();
EXPECT_EQ(heading_rate, max_heading_rate);
}

TEST_F(HeadingSmoothingTest, negative_rate)
{
const float max_heading_rate = 0.15;
_smoothing.setMaxHeadingRate(max_heading_rate);
const float heading_current = math::radians(-20.f);
const float heading_target = math::radians(-140.f);
_smoothing.reset(heading_current, 0.f);

const int nb_steps = ceilf(2.f / _dt);

for (int i = 0; i < nb_steps; i++) {
_smoothing.update(heading_target, _dt);
}

const float heading_rate = _smoothing.getSmoothedHeadingRate();
EXPECT_EQ(heading_rate, -max_heading_rate);
}

0 comments on commit 03aec2e

Please sign in to comment.