Skip to content

Commit ce1b276

Browse files
authored
fix(motion_velocity_smoother): curvature calculation (autowarefoundation#1460)
* fix(motion_velocity_smoother): curvature calculation Signed-off-by: Takamasa Horibe <horibe.takamasa@gmail.com> * fix cppcheck for namespace Signed-off-by: Takamasa Horibe <horibe.takamasa@gmail.com> * remove debug code Signed-off-by: Takamasa Horibe <horibe.takamasa@gmail.com>
1 parent 5c24ed0 commit ce1b276

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

planning/motion_velocity_smoother/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ rclcpp_components_register_node(motion_velocity_smoother_node
4646
EXECUTABLE motion_velocity_smoother
4747
)
4848

49+
if(BUILD_TESTING)
50+
ament_add_ros_isolated_gmock(test_smoother_functions
51+
test/test_smoother_functions.cpp
52+
)
53+
target_link_libraries(test_smoother_functions
54+
smoother
55+
)
56+
endif()
57+
58+
4959
ament_auto_package(
5060
INSTALL_TO_SHARE
5161
launch

planning/motion_velocity_smoother/package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<depend>tier4_debug_msgs</depend>
2929
<depend>tier4_planning_msgs</depend>
3030

31+
<test_depend>ament_cmake_ros</test_depend>
3132
<test_depend>ament_lint_auto</test_depend>
3233
<test_depend>autoware_lint_common</test_depend>
3334

planning/motion_velocity_smoother/src/trajectory_utils.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,17 @@ std::vector<double> calcTrajectoryCurvatureFrom3Points(
199199
using tier4_autoware_utils::calcCurvature;
200200
using tier4_autoware_utils::getPoint;
201201

202+
if (trajectory.size() < 3) {
203+
const std::vector<double> k_arr(trajectory.size(), 0.0);
204+
return k_arr;
205+
}
206+
202207
// if the idx size is not enough, change the idx_dist
203-
if (trajectory.size() < 2 * idx_dist + 1) {
204-
idx_dist = std::ceil((trajectory.size() - 1) / 2.0);
208+
const auto max_idx_dist = static_cast<size_t>(std::floor((trajectory.size() - 1) / 2.0));
209+
idx_dist = std::max(1ul, std::min(idx_dist, max_idx_dist));
210+
211+
if (idx_dist < 1) {
212+
throw std::logic_error("idx_dist less than 1 is not expected");
205213
}
206214

207215
// calculate curvature by circle fitting from three points
@@ -217,14 +225,6 @@ std::vector<double> calcTrajectoryCurvatureFrom3Points(
217225
}
218226
}
219227

220-
// for debug
221-
if (k_arr.empty()) {
222-
RCLCPP_ERROR(
223-
rclcpp::get_logger("motion_velocity_smoother").get_child("trajectory_utils"),
224-
"k_arr.size() = 0, something wrong. pls check.");
225-
return {};
226-
}
227-
228228
// first and last curvature is copied from next value
229229
for (size_t i = 0; i < idx_dist; ++i) {
230230
k_arr.insert(k_arr.begin(), k_arr.front());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2022 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "motion_velocity_smoother/trajectory_utils.hpp"
16+
17+
#include <gtest/gtest.h>
18+
19+
#include <vector>
20+
21+
using autoware_auto_planning_msgs::msg::TrajectoryPoint;
22+
using motion_velocity_smoother::trajectory_utils::TrajectoryPoints;
23+
24+
TrajectoryPoints genStraightTrajectory(const size_t size)
25+
{
26+
double x = 0.0;
27+
double dx = 1.0;
28+
geometry_msgs::msg::Quaternion o;
29+
o.x = 0.0;
30+
o.y = 0.0;
31+
o.z = 0.0;
32+
o.w = 1.0;
33+
34+
TrajectoryPoints tps;
35+
TrajectoryPoint p;
36+
for (size_t i = 0; i < size; ++i) {
37+
p.pose.position.x = x;
38+
p.pose.orientation = o;
39+
x += dx;
40+
tps.push_back(p);
41+
}
42+
return tps;
43+
}
44+
45+
TEST(TestTrajectoryUtils, CalcTrajectoryCurvatureFrom3Points)
46+
{
47+
// the output curvature vector should have the same size of the input trajectory.
48+
const auto checkOutputSize = [](const size_t trajectory_size, const size_t idx_dist) {
49+
const auto trajectory_points = genStraightTrajectory(trajectory_size);
50+
const auto curvatures =
51+
motion_velocity_smoother::trajectory_utils::calcTrajectoryCurvatureFrom3Points(
52+
trajectory_points, idx_dist);
53+
EXPECT_EQ(curvatures.size(), trajectory_size) << ", idx_dist = " << idx_dist;
54+
};
55+
56+
const auto trajectory_size_arr = {0, 1, 2, 3, 4, 5, 10, 100};
57+
const auto idx_dist_arr = {0, 1, 2, 3, 4, 5, 10, 100};
58+
for (const auto trajectory_size : trajectory_size_arr) {
59+
for (const auto idx_dist : idx_dist_arr) {
60+
checkOutputSize(trajectory_size, idx_dist);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)