Skip to content

Commit f50d6c1

Browse files
committed
Add unittest for costmap activation
Signed-off-by: Fabian König <fabiankoenig@gmail.com>
1 parent 1951faa commit f50d6c1

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

nav2_costmap_2d/test/unit/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ ament_add_gtest(denoise_layer_test denoise_layer_test.cpp image_test.cpp image_p
5555
target_link_libraries(denoise_layer_test
5656
nav2_costmap_2d_core layers
5757
)
58+
59+
ament_add_gtest_executable(lifecycle_test
60+
lifecycle_test.cpp
61+
)
62+
target_link_libraries(lifecycle_test
63+
nav2_costmap_2d_core
64+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
#include <memory>
14+
15+
#include "gtest/gtest.h"
16+
17+
#include "nav2_costmap_2d/costmap_2d_ros.hpp"
18+
#include "rclcpp/rclcpp.hpp"
19+
#include "lifecycle_msgs/msg/state.hpp"
20+
21+
22+
TEST(LifecylceTest, CheckInitialTfTimeout) {
23+
rclcpp::init(0, nullptr);
24+
25+
auto costmap = std::make_shared<nav2_costmap_2d::Costmap2DROS>("test_costmap");
26+
costmap->set_parameter({"initial_transform_timeout", 0.0});
27+
28+
std::thread spin_thread{[costmap]() {rclcpp::spin(costmap->get_node_base_interface());}};
29+
30+
{
31+
const auto state_after_configure = costmap->configure();
32+
ASSERT_EQ(state_after_configure.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
33+
// Without providing the transform from global to robot base the activation should fail
34+
// and the costmap should transition into the inactive state.
35+
const auto state_after_activate = costmap->activate();
36+
ASSERT_EQ(state_after_activate.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE);
37+
}
38+
39+
// Set a dummy transform from global to robot base
40+
geometry_msgs::msg::TransformStamped transform_global_to_robot{};
41+
transform_global_to_robot.header.frame_id = costmap->getGlobalFrameID();
42+
transform_global_to_robot.child_frame_id = costmap->getBaseFrameID();
43+
costmap->getTfBuffer()->setTransform(transform_global_to_robot, "test", true);
44+
// Now the costmap should successful transition into the active state
45+
{
46+
const auto state_after_activate = costmap->activate();
47+
ASSERT_EQ(state_after_activate.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE);
48+
}
49+
50+
rclcpp::shutdown();
51+
if (spin_thread.joinable()) {
52+
spin_thread.join();
53+
}
54+
}

0 commit comments

Comments
 (0)