Skip to content

Commit 9769a9e

Browse files
authored
test(bpp_common): add unit test for traffic light utils (#9441)
* add test data for traffic light utils Signed-off-by: Go Sakayori <gsakayori@gmail.com> * add unit test function Signed-off-by: Go Sakayori <go.sakayori@tier4.jp> * fix style Signed-off-by: Go Sakayori <gsakayori@gmail.com> * use test_utils::resolve_plg_share_uri for map path Signed-off-by: Go Sakayori <gsakayori@gmail.com> --------- Signed-off-by: Go Sakayori <gsakayori@gmail.com> Signed-off-by: Go Sakayori <go.sakayori@tier4.jp>
1 parent 685f1aa commit 9769a9e

File tree

5 files changed

+465
-3
lines changed

5 files changed

+465
-3
lines changed

planning/behavior_path_planner/autoware_behavior_path_planner_common/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ if(BUILD_TESTING)
3535
ament_add_ros_isolated_gmock(test_${PROJECT_NAME}_utilities
3636
test/test_utils.cpp
3737
test/test_path_utils.cpp
38+
test/test_traffic_light_utils.cpp
3839
)
3940

4041
target_link_libraries(test_${PROJECT_NAME}_utilities
@@ -93,4 +94,6 @@ if(BUILD_TESTING)
9394
)
9495
endif()
9596

96-
ament_auto_package()
97+
ament_auto_package(INSTALL_TO_SHARE
98+
test_data
99+
)

planning/behavior_path_planner/autoware_behavior_path_planner_common/src/utils/traffic_light_utils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool isStoppedAtRedTrafficLightWithinDistance(
130130
return false;
131131
}
132132

133-
return (distance_to_red_traffic_light < distance_threshold);
133+
return (distance_to_red_traffic_light.value() < distance_threshold);
134134
}
135135

136136
bool isTrafficSignalStop(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright 2024 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 "ament_index_cpp/get_package_share_directory.hpp"
16+
#include "autoware/behavior_path_planner_common/data_manager.hpp"
17+
#include "autoware/behavior_path_planner_common/utils/traffic_light_utils.hpp"
18+
#include "autoware_test_utils/autoware_test_utils.hpp"
19+
#include "autoware_test_utils/mock_data_parser.hpp"
20+
21+
#include <autoware/universe_utils/geometry/geometry.hpp>
22+
23+
#include <autoware_perception_msgs/msg/detail/traffic_light_group__struct.hpp>
24+
#include <autoware_planning_msgs/msg/detail/lanelet_route__struct.hpp>
25+
#include <geometry_msgs/msg/detail/pose__struct.hpp>
26+
#include <geometry_msgs/msg/detail/twist__struct.hpp>
27+
#include <tier4_planning_msgs/msg/detail/path_with_lane_id__struct.hpp>
28+
29+
#include <gtest/gtest.h>
30+
#include <lanelet2_core/Forward.h>
31+
#include <yaml-cpp/node/node.h>
32+
33+
#include <limits>
34+
#include <memory>
35+
#include <string>
36+
37+
using autoware::behavior_path_planner::PlannerData;
38+
using autoware::behavior_path_planner::TrafficSignalStamped;
39+
using autoware_perception_msgs::msg::TrafficLightGroupArray;
40+
using autoware_planning_msgs::msg::LaneletRoute;
41+
using geometry_msgs::msg::Pose;
42+
43+
const double epsilon = 1e-06;
44+
45+
class TrafficLightTest : public ::testing::Test
46+
{
47+
protected:
48+
void SetUp() override
49+
{
50+
planner_data_ = std::make_shared<PlannerData>();
51+
const auto test_data_file =
52+
ament_index_cpp::get_package_share_directory("autoware_behavior_path_planner_common") +
53+
"/test_data/test_traffic_light.yaml";
54+
YAML::Node config = YAML::LoadFile(test_data_file);
55+
56+
set_current_pose(config);
57+
set_route_handler(config);
58+
set_traffic_signal(config);
59+
}
60+
61+
void set_current_pose(YAML::Node config)
62+
{
63+
const auto self_odometry =
64+
autoware::test_utils::parse<nav_msgs::msg::Odometry>(config["self_odometry"]);
65+
planner_data_->self_odometry = std::make_shared<const nav_msgs::msg::Odometry>(self_odometry);
66+
}
67+
68+
void set_route_handler(YAML::Node config)
69+
{
70+
const auto route = autoware::test_utils::parse<LaneletRoute>(config["route"]);
71+
const auto map_path =
72+
autoware::test_utils::resolve_pkg_share_uri(config["map_path_uri"].as<std::string>());
73+
if (!map_path.has_value()) return;
74+
const auto intersection_map = autoware::test_utils::make_map_bin_msg(map_path.value());
75+
planner_data_->route_handler->setMap(intersection_map);
76+
planner_data_->route_handler->setRoute(route);
77+
78+
for (const auto & segment : route.segments) {
79+
lanelets.push_back(
80+
planner_data_->route_handler->getLaneletsFromId(segment.preferred_primitive.id));
81+
}
82+
}
83+
84+
void set_traffic_signal(YAML::Node config)
85+
{
86+
const auto traffic_light =
87+
autoware::test_utils::parse<TrafficLightGroupArray>(config["traffic_signal"]);
88+
for (const auto & signal : traffic_light.traffic_light_groups) {
89+
TrafficSignalStamped traffic_signal;
90+
traffic_signal.stamp = rclcpp::Clock{RCL_ROS_TIME}.now();
91+
traffic_signal.signal = signal;
92+
planner_data_->traffic_light_id_map[signal.traffic_light_group_id] = traffic_signal;
93+
}
94+
}
95+
96+
void set_zero_velocity()
97+
{
98+
nav_msgs::msg::Odometry odometry;
99+
odometry.pose.pose = planner_data_->self_odometry->pose.pose;
100+
odometry.twist.twist.linear = autoware::universe_utils::createVector3(0.0, 0.0, 0.0);
101+
planner_data_->self_odometry = std::make_shared<const nav_msgs::msg::Odometry>(odometry);
102+
}
103+
104+
lanelet::ConstLanelets lanelets;
105+
std::shared_ptr<PlannerData> planner_data_;
106+
};
107+
108+
TEST_F(TrafficLightTest, getDistanceToNextTrafficLight)
109+
{
110+
using autoware::behavior_path_planner::utils::traffic_light::getDistanceToNextTrafficLight;
111+
112+
{
113+
const Pose pose = autoware::test_utils::createPose(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
114+
const lanelet::ConstLanelets empty_lanelets;
115+
EXPECT_DOUBLE_EQ(
116+
getDistanceToNextTrafficLight(pose, empty_lanelets), std::numeric_limits<double>::infinity());
117+
}
118+
{
119+
EXPECT_NEAR(
120+
getDistanceToNextTrafficLight(planner_data_->self_odometry->pose.pose, lanelets), 117.1599371,
121+
epsilon);
122+
}
123+
}
124+
125+
TEST_F(TrafficLightTest, calcDistanceToRedTrafficLight)
126+
{
127+
using autoware::behavior_path_planner::utils::traffic_light::calcDistanceToRedTrafficLight;
128+
129+
{
130+
const tier4_planning_msgs::msg::PathWithLaneId path;
131+
const lanelet::ConstLanelets empty_lanelets;
132+
EXPECT_FALSE(calcDistanceToRedTrafficLight(empty_lanelets, path, planner_data_).has_value());
133+
}
134+
{
135+
const auto path = planner_data_->route_handler->getCenterLinePath(lanelets, 0.0, 300.0);
136+
const auto distance = calcDistanceToRedTrafficLight(lanelets, path, planner_data_);
137+
ASSERT_TRUE(distance.has_value());
138+
EXPECT_NEAR(distance.value(), 117.1096960, epsilon);
139+
}
140+
}
141+
142+
TEST_F(TrafficLightTest, isStoppedAtRedTrafficLightWithinDistance)
143+
{
144+
using autoware::behavior_path_planner::utils::traffic_light::
145+
isStoppedAtRedTrafficLightWithinDistance;
146+
const auto distance_threshold = 10.0;
147+
const auto path = planner_data_->route_handler->getCenterLinePath(lanelets, 0.0, 300.0);
148+
{
149+
EXPECT_FALSE(
150+
isStoppedAtRedTrafficLightWithinDistance(lanelets, path, planner_data_, distance_threshold));
151+
}
152+
{
153+
set_zero_velocity();
154+
EXPECT_FALSE(
155+
isStoppedAtRedTrafficLightWithinDistance(lanelets, path, planner_data_, distance_threshold));
156+
}
157+
{
158+
set_zero_velocity();
159+
EXPECT_TRUE(isStoppedAtRedTrafficLightWithinDistance(lanelets, path, planner_data_));
160+
}
161+
}
162+
163+
TEST_F(TrafficLightTest, isTrafficSignalStop)
164+
{
165+
using autoware::behavior_path_planner::utils::traffic_light::isTrafficSignalStop;
166+
167+
{
168+
const lanelet::ConstLanelets empty_lanelets;
169+
EXPECT_FALSE(isTrafficSignalStop(empty_lanelets, planner_data_));
170+
}
171+
{
172+
EXPECT_TRUE(isTrafficSignalStop(lanelets, planner_data_));
173+
}
174+
}

planning/behavior_path_planner/autoware_behavior_path_planner_common/test/test_utils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Tier IV, Inc. All rights reserved.
1+
// Copyright 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.

0 commit comments

Comments
 (0)