Skip to content

Commit 1a9e20c

Browse files
committed
Add validation tests
Signed-off-by: zz990099 <771647586@qq.com>
1 parent da8ac8e commit 1a9e20c

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

nav2_behavior_tree/test/plugins/action/test_remove_in_collision_goals_action.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,75 @@ TEST_F(RemoveInCollisionGoalsTestFixture, test_tick_remove_in_collision_goals_su
181181
EXPECT_EQ(output_poses.goals[2], poses.goals[2]);
182182
}
183183

184+
TEST_F(RemoveInCollisionGoalsTestFixture,
185+
test_tick_remove_in_collision_goals_success_and_output_waypoint_statues)
186+
{
187+
// create tree
188+
std::string xml_txt =
189+
R"(
190+
<root BTCPP_format="4">
191+
<BehaviorTree ID="MainTree">
192+
<RemoveInCollisionGoals service_name="/global_costmap/get_cost_global_costmap"
193+
input_goals="{goals}" output_goals="{goals}"
194+
cost_threshold="253"
195+
input_waypoint_statuses="{waypoint_statuses}"
196+
output_waypoint_statuses="{waypoint_statuses}"/>
197+
</BehaviorTree>
198+
</root>)";
199+
200+
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard));
201+
202+
// create new goal and set it on blackboard
203+
nav_msgs::msg::Goals poses;
204+
poses.goals.resize(4);
205+
poses.goals[0].pose.position.x = 0.0;
206+
poses.goals[0].pose.position.y = 0.0;
207+
208+
poses.goals[1].pose.position.x = 0.5;
209+
poses.goals[1].pose.position.y = 0.0;
210+
211+
poses.goals[2].pose.position.x = 1.0;
212+
poses.goals[2].pose.position.y = 0.0;
213+
214+
poses.goals[3].pose.position.x = 2.0;
215+
poses.goals[3].pose.position.y = 0.0;
216+
217+
config_->blackboard->set("goals", poses);
218+
219+
// create waypoint_statuses and set it on blackboard
220+
std::vector<nav2_msgs::msg::WaypointStatus> waypoint_statuses(poses.goals.size());
221+
for (size_t i = 0 ; i < waypoint_statuses.size() ; ++i) {
222+
waypoint_statuses[i].waypoint_pose = poses.goals[i];
223+
waypoint_statuses[i].waypoint_index = i;
224+
}
225+
config_->blackboard->set("waypoint_statuses", waypoint_statuses);
226+
227+
// tick until node is not running
228+
tree_->rootNode()->executeTick();
229+
while (tree_->rootNode()->status() == BT::NodeStatus::RUNNING) {
230+
tree_->rootNode()->executeTick();
231+
}
232+
233+
EXPECT_EQ(tree_->rootNode()->status(), BT::NodeStatus::SUCCESS);
234+
// check that it removed the point in range
235+
nav_msgs::msg::Goals output_poses;
236+
EXPECT_TRUE(config_->blackboard->get("goals", output_poses));
237+
238+
EXPECT_EQ(output_poses.goals.size(), 3u);
239+
EXPECT_EQ(output_poses.goals[0], poses.goals[0]);
240+
EXPECT_EQ(output_poses.goals[1], poses.goals[1]);
241+
EXPECT_EQ(output_poses.goals[2], poses.goals[2]);
242+
243+
// check the waypoint_statuses
244+
std::vector<nav2_msgs::msg::WaypointStatus> output_waypoint_statuses;
245+
EXPECT_TRUE(config_->blackboard->get("waypoint_statuses", output_waypoint_statuses));
246+
EXPECT_EQ(output_waypoint_statuses.size(), 4u);
247+
EXPECT_EQ(output_waypoint_statuses[0].waypoint_status, nav2_msgs::msg::WaypointStatus::PENDING);
248+
EXPECT_EQ(output_waypoint_statuses[1].waypoint_status, nav2_msgs::msg::WaypointStatus::PENDING);
249+
EXPECT_EQ(output_waypoint_statuses[2].waypoint_status, nav2_msgs::msg::WaypointStatus::PENDING);
250+
EXPECT_EQ(output_waypoint_statuses[3].waypoint_status, nav2_msgs::msg::WaypointStatus::MISSED);
251+
}
252+
184253
TEST_F(RemoveInCollisionGoalsTestFixture, test_tick_remove_in_collision_goals_fail)
185254
{
186255
// create tree

nav2_behavior_tree/test/plugins/action/test_remove_passed_goals_action.cpp

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class RemovePassedGoalsTestFixture : public ::testing::Test
3939

4040
config_ = new BT::NodeConfiguration();
4141
transform_handler_ = std::make_shared<nav2_behavior_tree::TransformHandler>(node_);
42+
transform_handler_->activate();
4243

4344
// Create the blackboard that will be shared by all of the nodes in the tree
4445
config_->blackboard = BT::Blackboard::create();
@@ -98,7 +99,6 @@ TEST_F(RemovePassedGoalsTestFixture, test_tick)
9899
pose.position.x = 0.25;
99100
pose.position.y = 0.0;
100101

101-
transform_handler_->activate();
102102
transform_handler_->waitForTransform();
103103
transform_handler_->updateRobotPose(pose);
104104

@@ -144,6 +144,77 @@ TEST_F(RemovePassedGoalsTestFixture, test_tick)
144144
EXPECT_EQ(output_poses.goals[1], poses.goals[3]);
145145
}
146146

147+
TEST_F(RemovePassedGoalsTestFixture,
148+
test_tick_remove_passed_goals_success_and_output_waypoint_statuses)
149+
{
150+
geometry_msgs::msg::Pose pose;
151+
pose.position.x = 0.25;
152+
pose.position.y = 0.0;
153+
154+
transform_handler_->waitForTransform();
155+
transform_handler_->updateRobotPose(pose);
156+
157+
// create tree
158+
std::string xml_txt =
159+
R"(
160+
<root BTCPP_format="4">
161+
<BehaviorTree ID="MainTree">
162+
<RemovePassedGoals radius="0.5" input_goals="{goals}" output_goals="{goals}"
163+
input_waypoint_statuses="{waypoint_statuses}"
164+
output_waypoint_statuses="{waypoint_statuses}"/>
165+
</BehaviorTree>
166+
</root>)";
167+
168+
tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard));
169+
170+
// create new goal and set it on blackboard
171+
nav_msgs::msg::Goals poses;
172+
poses.goals.resize(4);
173+
poses.goals[0].pose.position.x = 0.0;
174+
poses.goals[0].pose.position.y = 0.0;
175+
176+
poses.goals[1].pose.position.x = 0.5;
177+
poses.goals[1].pose.position.y = 0.0;
178+
179+
poses.goals[2].pose.position.x = 1.0;
180+
poses.goals[2].pose.position.y = 0.0;
181+
182+
poses.goals[3].pose.position.x = 2.0;
183+
poses.goals[3].pose.position.y = 0.0;
184+
185+
config_->blackboard->set("goals", poses);
186+
187+
// create waypoint_statuses and set it on blackboard
188+
std::vector<nav2_msgs::msg::WaypointStatus> waypoint_statuses(poses.goals.size());
189+
for (size_t i = 0 ; i < waypoint_statuses.size() ; ++i) {
190+
waypoint_statuses[i].waypoint_pose = poses.goals[i];
191+
waypoint_statuses[i].waypoint_index = i;
192+
}
193+
config_->blackboard->set("waypoint_statuses", waypoint_statuses);
194+
195+
// tick until node succeeds
196+
while (tree_->rootNode()->status() != BT::NodeStatus::SUCCESS) {
197+
tree_->rootNode()->executeTick();
198+
}
199+
200+
// check that it removed the point in range
201+
nav_msgs::msg::Goals output_poses;
202+
EXPECT_TRUE(config_->blackboard->get("goals", output_poses));
203+
204+
EXPECT_EQ(output_poses.goals.size(), 2u);
205+
EXPECT_EQ(output_poses.goals[0], poses.goals[2]);
206+
EXPECT_EQ(output_poses.goals[1], poses.goals[3]);
207+
208+
// check the waypoint_statuses
209+
std::vector<nav2_msgs::msg::WaypointStatus> output_waypoint_statuses;
210+
EXPECT_TRUE(config_->blackboard->get("waypoint_statuses", output_waypoint_statuses));
211+
EXPECT_EQ(output_waypoint_statuses.size(), 4u);
212+
EXPECT_EQ(output_waypoint_statuses[0].waypoint_status, nav2_msgs::msg::WaypointStatus::COMPLETED);
213+
EXPECT_EQ(output_waypoint_statuses[1].waypoint_status, nav2_msgs::msg::WaypointStatus::COMPLETED);
214+
EXPECT_EQ(output_waypoint_statuses[2].waypoint_status, nav2_msgs::msg::WaypointStatus::PENDING);
215+
EXPECT_EQ(output_waypoint_statuses[3].waypoint_status, nav2_msgs::msg::WaypointStatus::PENDING);
216+
}
217+
147218
int main(int argc, char ** argv)
148219
{
149220
::testing::InitGoogleTest(&argc, argv);

nav2_util/test/test_geometry_utils.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
using nav2_util::geometry_utils::euclidean_distance;
2323
using nav2_util::geometry_utils::calculate_path_length;
24+
using nav2_util::geometry_utils::find_next_matching_goal_in_waypoint_statuses;
2425

2526
TEST(GeometryUtils, euclidean_distance_point_3d)
2627
{
@@ -128,3 +129,30 @@ TEST(GeometryUtils, calculate_path_length)
128129
calculate_path_length(circle_path),
129130
2 * pi * polar_distance, 1e-1);
130131
}
132+
133+
TEST(GeometryUtils, find_next_matching_goal_in_waypoint_statuses)
134+
{
135+
size_t nb_waypoints = 10;
136+
float distance_between_waypoints = 2.0;
137+
138+
std::vector<nav2_msgs::msg::WaypointStatus> waypoint_statuses(nb_waypoints);
139+
for (size_t i = 0 ; i < nb_waypoints ; ++i) {
140+
waypoint_statuses[i].waypoint_index = i;
141+
waypoint_statuses[i].waypoint_pose.pose.position.x = distance_between_waypoints * i;
142+
}
143+
144+
// match success
145+
size_t matching_index = nb_waypoints / 2;
146+
ASSERT_EQ(find_next_matching_goal_in_waypoint_statuses(waypoint_statuses,
147+
waypoint_statuses[matching_index].waypoint_pose), matching_index);
148+
149+
// match failed due to mismatch of pose
150+
geometry_msgs::msg::PoseStamped fake_pose;
151+
fake_pose.pose.position.x = -1.0;
152+
ASSERT_EQ(find_next_matching_goal_in_waypoint_statuses(waypoint_statuses, fake_pose), -1);
153+
154+
// match failed due to waypoint_status is not PENDING
155+
waypoint_statuses[matching_index].waypoint_status = nav2_msgs::msg::WaypointStatus::COMPLETED;
156+
ASSERT_EQ(find_next_matching_goal_in_waypoint_statuses(waypoint_statuses,
157+
waypoint_statuses[matching_index].waypoint_pose), -1);
158+
}

0 commit comments

Comments
 (0)