Skip to content

Commit 7ab3061

Browse files
authored
test(obstacle_collision_checker): refactor and add tests (autowarefoundation#8989)
Signed-off-by: Maxime CLEMENT <maxime.clement@tier4.jp>
1 parent 4f97001 commit 7ab3061

File tree

5 files changed

+323
-165
lines changed

5 files changed

+323
-165
lines changed

control/obstacle_collision_checker/include/obstacle_collision_checker/obstacle_collision_checker.hpp

+21-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Tier IV, Inc. All rights reserved.
1+
// Copyright 2020-2024 Tier IV, Inc. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
#ifndef OBSTACLE_COLLISION_CHECKER__OBSTACLE_COLLISION_CHECKER_HPP_
1616
#define OBSTACLE_COLLISION_CHECKER__OBSTACLE_COLLISION_CHECKER_HPP_
1717

18-
#include <autoware/universe_utils/geometry/boost_geometry.hpp>
1918
#include <autoware_vehicle_info_utils/vehicle_info_utils.hpp>
2019

2120
#include <autoware_planning_msgs/msg/trajectory.hpp>
@@ -24,8 +23,6 @@
2423
#include <geometry_msgs/msg/twist.hpp>
2524
#include <sensor_msgs/msg/point_cloud2.hpp>
2625

27-
#include <boost/optional.hpp>
28-
2926
#include <pcl/point_cloud.h>
3027
#include <pcl/point_types.h>
3128

@@ -54,6 +51,8 @@ struct Input
5451
geometry_msgs::msg::TransformStamped::ConstSharedPtr obstacle_transform;
5552
autoware_planning_msgs::msg::Trajectory::ConstSharedPtr reference_trajectory;
5653
autoware_planning_msgs::msg::Trajectory::ConstSharedPtr predicted_trajectory;
54+
autoware::vehicle_info_utils::VehicleInfo vehicle_info;
55+
Param param;
5756
};
5857

5958
struct Output
@@ -65,43 +64,31 @@ struct Output
6564
std::vector<LinearRing2d> vehicle_passing_areas;
6665
};
6766

68-
class ObstacleCollisionChecker
69-
{
70-
public:
71-
explicit ObstacleCollisionChecker(rclcpp::Node & node);
72-
Output update(const Input & input);
73-
74-
void setParam(const Param & param) { param_ = param; }
67+
Output check_for_collisions(const Input & input);
7568

76-
private:
77-
Param param_;
78-
autoware::vehicle_info_utils::VehicleInfo vehicle_info_;
69+
//! This function assumes the input trajectory is sampled dense enough
70+
autoware_planning_msgs::msg::Trajectory resample_trajectory(
71+
const autoware_planning_msgs::msg::Trajectory & trajectory, const double interval);
7972

80-
//! This function assumes the input trajectory is sampled dense enough
81-
static autoware_planning_msgs::msg::Trajectory resampleTrajectory(
82-
const autoware_planning_msgs::msg::Trajectory & trajectory, const double interval);
73+
autoware_planning_msgs::msg::Trajectory cut_trajectory(
74+
const autoware_planning_msgs::msg::Trajectory & trajectory, const double length);
8375

84-
static autoware_planning_msgs::msg::Trajectory cutTrajectory(
85-
const autoware_planning_msgs::msg::Trajectory & trajectory, const double length);
76+
std::vector<LinearRing2d> create_vehicle_footprints(
77+
const autoware_planning_msgs::msg::Trajectory & trajectory, const Param & param,
78+
const autoware::vehicle_info_utils::VehicleInfo & vehicle_info);
8679

87-
static std::vector<LinearRing2d> createVehicleFootprints(
88-
const autoware_planning_msgs::msg::Trajectory & trajectory, const Param & param,
89-
const autoware::vehicle_info_utils::VehicleInfo & vehicle_info);
80+
std::vector<LinearRing2d> create_vehicle_passing_areas(
81+
const std::vector<LinearRing2d> & vehicle_footprints);
9082

91-
static std::vector<LinearRing2d> createVehiclePassingAreas(
92-
const std::vector<LinearRing2d> & vehicle_footprints);
83+
LinearRing2d create_hull_from_footprints(const LinearRing2d & area1, const LinearRing2d & area2);
9384

94-
static LinearRing2d createHullFromFootprints(
95-
const LinearRing2d & area1, const LinearRing2d & area2);
85+
bool will_collide(
86+
const pcl::PointCloud<pcl::PointXYZ> & obstacle_pointcloud,
87+
const std::vector<LinearRing2d> & vehicle_footprints);
9688

97-
static bool willCollide(
98-
const pcl::PointCloud<pcl::PointXYZ> & obstacle_pointcloud,
99-
const std::vector<LinearRing2d> & vehicle_footprints);
100-
101-
static bool hasCollision(
102-
const pcl::PointCloud<pcl::PointXYZ> & obstacle_pointcloud,
103-
const LinearRing2d & vehicle_footprint);
104-
};
89+
bool has_collision(
90+
const pcl::PointCloud<pcl::PointXYZ> & obstacle_pointcloud,
91+
const LinearRing2d & vehicle_footprint);
10592
} // namespace obstacle_collision_checker
10693

10794
#endif // OBSTACLE_COLLISION_CHECKER__OBSTACLE_COLLISION_CHECKER_HPP_

control/obstacle_collision_checker/include/obstacle_collision_checker/obstacle_collision_checker_node.hpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <autoware/universe_utils/ros/processing_time_publisher.hpp>
2323
#include <autoware/universe_utils/ros/self_pose_listener.hpp>
2424
#include <autoware/universe_utils/ros/transform_listener.hpp>
25+
#include <autoware_vehicle_info_utils/vehicle_info.hpp>
2526
#include <diagnostic_updater/diagnostic_updater.hpp>
2627
#include <rclcpp/rclcpp.hpp>
2728

@@ -38,7 +39,7 @@ namespace obstacle_collision_checker
3839
{
3940
struct NodeParam
4041
{
41-
double update_rate;
42+
double update_rate{};
4243
};
4344

4445
class ObstacleCollisionCheckerNode : public rclcpp::Node
@@ -64,46 +65,45 @@ class ObstacleCollisionCheckerNode : public rclcpp::Node
6465
geometry_msgs::msg::TransformStamped::ConstSharedPtr obstacle_transform_;
6566
autoware_planning_msgs::msg::Trajectory::ConstSharedPtr reference_trajectory_;
6667
autoware_planning_msgs::msg::Trajectory::ConstSharedPtr predicted_trajectory_;
68+
autoware::vehicle_info_utils::VehicleInfo vehicle_info_;
6769

6870
// Callback
69-
void onObstaclePointcloud(const sensor_msgs::msg::PointCloud2::SharedPtr msg);
70-
void onReferenceTrajectory(const autoware_planning_msgs::msg::Trajectory::SharedPtr msg);
71-
void onPredictedTrajectory(const autoware_planning_msgs::msg::Trajectory::SharedPtr msg);
72-
void onOdom(const nav_msgs::msg::Odometry::SharedPtr msg);
71+
void on_obstacle_pointcloud(const sensor_msgs::msg::PointCloud2::SharedPtr msg);
72+
void on_reference_trajectory(const autoware_planning_msgs::msg::Trajectory::SharedPtr msg);
73+
void on_predicted_trajectory(const autoware_planning_msgs::msg::Trajectory::SharedPtr msg);
74+
void on_odom(const nav_msgs::msg::Odometry::SharedPtr msg);
7375

7476
// Publisher
7577
std::shared_ptr<autoware::universe_utils::DebugPublisher> debug_publisher_;
7678
std::shared_ptr<autoware::universe_utils::ProcessingTimePublisher> time_publisher_;
7779

7880
// Timer
7981
rclcpp::TimerBase::SharedPtr timer_;
80-
void initTimer(double period_s);
82+
void init_timer(double period_s);
8183

82-
bool isDataReady();
83-
bool isDataTimeout();
84-
void onTimer();
84+
bool is_data_ready();
85+
bool is_data_timeout();
86+
void on_timer();
8587

8688
// Parameter
8789
NodeParam node_param_;
88-
Param param_;
8990

9091
// Dynamic Reconfigure
9192
OnSetParametersCallbackHandle::SharedPtr set_param_res_;
92-
rcl_interfaces::msg::SetParametersResult paramCallback(
93+
rcl_interfaces::msg::SetParametersResult param_callback(
9394
const std::vector<rclcpp::Parameter> & parameters);
9495

9596
// Core
9697
Input input_;
9798
Output output_;
98-
std::unique_ptr<ObstacleCollisionChecker> obstacle_collision_checker_;
9999

100100
// Diagnostic Updater
101101
diagnostic_updater::Updater updater_;
102102

103-
void checkLaneDeparture(diagnostic_updater::DiagnosticStatusWrapper & stat);
103+
void check_lane_departure(diagnostic_updater::DiagnosticStatusWrapper & stat);
104104

105105
// Visualization
106-
visualization_msgs::msg::MarkerArray createMarkerArray() const;
106+
visualization_msgs::msg::MarkerArray create_marker_array() const;
107107
};
108108
} // namespace obstacle_collision_checker
109109

control/obstacle_collision_checker/src/obstacle_collision_checker_node/obstacle_collision_checker.cpp

+31-45
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,18 @@
2020
#include <autoware/universe_utils/system/stop_watch.hpp>
2121
#include <pcl_ros/transforms.hpp>
2222
#include <rclcpp/rclcpp.hpp>
23+
#include <tf2_eigen/tf2_eigen.hpp>
2324

2425
#include <boost/geometry.hpp>
2526

2627
#include <pcl_conversions/pcl_conversions.h>
2728
#include <tf2/utils.h>
2829

29-
#ifdef ROS_DISTRO_GALACTIC
30-
#include <tf2_eigen/tf2_eigen.h>
31-
#else
32-
#include <tf2_eigen/tf2_eigen.hpp>
33-
#endif
34-
35-
#include <iostream>
3630
#include <vector>
3731

3832
namespace
3933
{
40-
pcl::PointCloud<pcl::PointXYZ> getTransformedPointCloud(
34+
pcl::PointCloud<pcl::PointXYZ> get_transformed_point_cloud(
4135
const sensor_msgs::msg::PointCloud2 & pointcloud_msg,
4236
const geometry_msgs::msg::Transform & transform)
4337
{
@@ -52,7 +46,7 @@ pcl::PointCloud<pcl::PointXYZ> getTransformedPointCloud(
5246
return transformed_pointcloud;
5347
}
5448

55-
pcl::PointCloud<pcl::PointXYZ> filterPointCloudByTrajectory(
49+
pcl::PointCloud<pcl::PointXYZ> filter_point_cloud_by_trajectory(
5650
const pcl::PointCloud<pcl::PointXYZ> & pointcloud,
5751
const autoware_planning_msgs::msg::Trajectory & trajectory, const double radius)
5852
{
@@ -70,7 +64,7 @@ pcl::PointCloud<pcl::PointXYZ> filterPointCloudByTrajectory(
7064
return filtered_pointcloud;
7165
}
7266

73-
double calcBrakingDistance(
67+
double calc_braking_distance(
7468
const double abs_velocity, const double max_deceleration, const double delay_time)
7569
{
7670
const double idling_distance = abs_velocity * delay_time;
@@ -82,12 +76,7 @@ double calcBrakingDistance(
8276

8377
namespace obstacle_collision_checker
8478
{
85-
ObstacleCollisionChecker::ObstacleCollisionChecker(rclcpp::Node & node)
86-
: vehicle_info_(autoware::vehicle_info_utils::VehicleInfoUtils(node).getVehicleInfo())
87-
{
88-
}
89-
90-
Output ObstacleCollisionChecker::update(const Input & input)
79+
Output check_for_collisions(const Input & input)
9180
{
9281
Output output;
9382
autoware::universe_utils::StopWatch<std::chrono::milliseconds> stop_watch;
@@ -97,31 +86,32 @@ Output ObstacleCollisionChecker::update(const Input & input)
9786
const auto & raw_abs_velocity = std::abs(input.current_twist->linear.x);
9887
const auto abs_velocity = raw_abs_velocity < min_velocity ? 0.0 : raw_abs_velocity;
9988
const auto braking_distance =
100-
calcBrakingDistance(abs_velocity, param_.max_deceleration, param_.delay_time);
101-
output.resampled_trajectory = cutTrajectory(
102-
resampleTrajectory(*input.predicted_trajectory, param_.resample_interval), braking_distance);
89+
calc_braking_distance(abs_velocity, input.param.max_deceleration, input.param.delay_time);
90+
output.resampled_trajectory = cut_trajectory(
91+
resample_trajectory(*input.predicted_trajectory, input.param.resample_interval),
92+
braking_distance);
10393
output.processing_time_map["resampleTrajectory"] = stop_watch.toc(true);
10494

10595
// resample pointcloud
10696
const auto obstacle_pointcloud =
107-
getTransformedPointCloud(*input.obstacle_pointcloud, input.obstacle_transform->transform);
108-
const auto filtered_obstacle_pointcloud = filterPointCloudByTrajectory(
109-
obstacle_pointcloud, output.resampled_trajectory, param_.search_radius);
97+
get_transformed_point_cloud(*input.obstacle_pointcloud, input.obstacle_transform->transform);
98+
const auto filtered_obstacle_pointcloud = filter_point_cloud_by_trajectory(
99+
obstacle_pointcloud, output.resampled_trajectory, input.param.search_radius);
110100

111101
output.vehicle_footprints =
112-
createVehicleFootprints(output.resampled_trajectory, param_, vehicle_info_);
102+
create_vehicle_footprints(output.resampled_trajectory, input.param, input.vehicle_info);
113103
output.processing_time_map["createVehicleFootprints"] = stop_watch.toc(true);
114104

115-
output.vehicle_passing_areas = createVehiclePassingAreas(output.vehicle_footprints);
105+
output.vehicle_passing_areas = create_vehicle_passing_areas(output.vehicle_footprints);
116106
output.processing_time_map["createVehiclePassingAreas"] = stop_watch.toc(true);
117107

118-
output.will_collide = willCollide(filtered_obstacle_pointcloud, output.vehicle_passing_areas);
108+
output.will_collide = will_collide(filtered_obstacle_pointcloud, output.vehicle_passing_areas);
119109
output.processing_time_map["willCollide"] = stop_watch.toc(true);
120110

121111
return output;
122112
}
123113

124-
autoware_planning_msgs::msg::Trajectory ObstacleCollisionChecker::resampleTrajectory(
114+
autoware_planning_msgs::msg::Trajectory resample_trajectory(
125115
const autoware_planning_msgs::msg::Trajectory & trajectory, const double interval)
126116
{
127117
autoware_planning_msgs::msg::Trajectory resampled;
@@ -131,11 +121,9 @@ autoware_planning_msgs::msg::Trajectory ObstacleCollisionChecker::resampleTrajec
131121
for (size_t i = 1; i < trajectory.points.size() - 1; ++i) {
132122
const auto & point = trajectory.points.at(i);
133123

134-
const auto p1 =
135-
autoware::universe_utils::fromMsg(resampled.points.back().pose.position).to_2d();
136-
const auto p2 = autoware::universe_utils::fromMsg(point.pose.position).to_2d();
137-
138-
if (boost::geometry::distance(p1, p2) > interval) {
124+
const auto distance =
125+
autoware::universe_utils::calcDistance2d(resampled.points.back(), point.pose.position);
126+
if (distance > interval) {
139127
resampled.points.push_back(point);
140128
}
141129
}
@@ -144,7 +132,7 @@ autoware_planning_msgs::msg::Trajectory ObstacleCollisionChecker::resampleTrajec
144132
return resampled;
145133
}
146134

147-
autoware_planning_msgs::msg::Trajectory ObstacleCollisionChecker::cutTrajectory(
135+
autoware_planning_msgs::msg::Trajectory cut_trajectory(
148136
const autoware_planning_msgs::msg::Trajectory & trajectory, const double length)
149137
{
150138
autoware_planning_msgs::msg::Trajectory cut;
@@ -157,8 +145,8 @@ autoware_planning_msgs::msg::Trajectory ObstacleCollisionChecker::cutTrajectory(
157145

158146
const auto p1 = autoware::universe_utils::fromMsg(cut.points.back().pose.position);
159147
const auto p2 = autoware::universe_utils::fromMsg(point.pose.position);
160-
const auto points_distance = boost::geometry::distance(p1.to_2d(), p2.to_2d());
161148

149+
const auto points_distance = boost::geometry::distance(p1, p2);
162150
const auto remain_distance = length - total_length;
163151

164152
// Over length
@@ -187,7 +175,7 @@ autoware_planning_msgs::msg::Trajectory ObstacleCollisionChecker::cutTrajectory(
187175
return cut;
188176
}
189177

190-
std::vector<LinearRing2d> ObstacleCollisionChecker::createVehicleFootprints(
178+
std::vector<LinearRing2d> create_vehicle_footprints(
191179
const autoware_planning_msgs::msg::Trajectory & trajectory, const Param & param,
192180
const autoware::vehicle_info_utils::VehicleInfo & vehicle_info)
193181
{
@@ -205,22 +193,21 @@ std::vector<LinearRing2d> ObstacleCollisionChecker::createVehicleFootprints(
205193
return vehicle_footprints;
206194
}
207195

208-
std::vector<LinearRing2d> ObstacleCollisionChecker::createVehiclePassingAreas(
196+
std::vector<LinearRing2d> create_vehicle_passing_areas(
209197
const std::vector<LinearRing2d> & vehicle_footprints)
210198
{
211199
// Create hull from two adjacent vehicle footprints
212200
std::vector<LinearRing2d> areas;
213201
for (size_t i = 0; i < vehicle_footprints.size() - 1; ++i) {
214202
const auto & footprint1 = vehicle_footprints.at(i);
215203
const auto & footprint2 = vehicle_footprints.at(i + 1);
216-
areas.push_back(createHullFromFootprints(footprint1, footprint2));
204+
areas.push_back(create_hull_from_footprints(footprint1, footprint2));
217205
}
218206

219207
return areas;
220208
}
221209

222-
LinearRing2d ObstacleCollisionChecker::createHullFromFootprints(
223-
const LinearRing2d & area1, const LinearRing2d & area2)
210+
LinearRing2d create_hull_from_footprints(const LinearRing2d & area1, const LinearRing2d & area2)
224211
{
225212
autoware::universe_utils::MultiPoint2d combined;
226213
for (const auto & p : area1) {
@@ -234,33 +221,32 @@ LinearRing2d ObstacleCollisionChecker::createHullFromFootprints(
234221
return hull;
235222
}
236223

237-
bool ObstacleCollisionChecker::willCollide(
224+
bool will_collide(
238225
const pcl::PointCloud<pcl::PointXYZ> & obstacle_pointcloud,
239226
const std::vector<LinearRing2d> & vehicle_footprints)
240227
{
241228
for (size_t i = 1; i < vehicle_footprints.size(); i++) {
242229
// skip first footprint because surround obstacle checker handle it
243230
const auto & vehicle_footprint = vehicle_footprints.at(i);
244-
if (hasCollision(obstacle_pointcloud, vehicle_footprint)) {
245-
RCLCPP_WARN(
246-
rclcpp::get_logger("obstacle_collision_checker"), "ObstacleCollisionChecker::willCollide");
231+
if (has_collision(obstacle_pointcloud, vehicle_footprint)) {
232+
RCLCPP_WARN(rclcpp::get_logger("obstacle_collision_checker"), "willCollide");
247233
return true;
248234
}
249235
}
250236

251237
return false;
252238
}
253239

254-
bool ObstacleCollisionChecker::hasCollision(
240+
bool has_collision(
255241
const pcl::PointCloud<pcl::PointXYZ> & obstacle_pointcloud,
256242
const LinearRing2d & vehicle_footprint)
257243
{
258244
for (const auto & point : obstacle_pointcloud.points) {
259245
if (boost::geometry::within(
260246
autoware::universe_utils::Point2d{point.x, point.y}, vehicle_footprint)) {
261247
RCLCPP_WARN(
262-
rclcpp::get_logger("obstacle_collision_checker"),
263-
"[ObstacleCollisionChecker] Collide to Point x: %f y: %f", point.x, point.y);
248+
rclcpp::get_logger("obstacle_collision_checker"), "Collide to Point x: %f y: %f", point.x,
249+
point.y);
264250
return true;
265251
}
266252
}

0 commit comments

Comments
 (0)