|
| 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