|
| 1 | +// Copyright (c) 2023 Open Navigation LLC |
| 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 <string> |
| 16 | +#include <memory> |
| 17 | + |
| 18 | +#include "nav2_util/base_footprint_publisher.hpp" |
| 19 | +#include "gtest/gtest.h" |
| 20 | +#include "tf2/exceptions.h" |
| 21 | + |
| 22 | +class RclCppFixture |
| 23 | +{ |
| 24 | +public: |
| 25 | + RclCppFixture() {rclcpp::init(0, nullptr);} |
| 26 | + ~RclCppFixture() {rclcpp::shutdown();} |
| 27 | +}; |
| 28 | +RclCppFixture g_rclcppfixture; |
| 29 | + |
| 30 | +TEST(TestBaseFootprintPublisher, TestBaseFootprintPublisher) |
| 31 | +{ |
| 32 | + auto node = std::make_shared<nav2_util::BaseFootprintPublisher>(); |
| 33 | + rclcpp::spin_some(node->get_node_base_interface()); |
| 34 | + |
| 35 | + auto tf_broadcaster = std::make_shared<tf2_ros::TransformBroadcaster>(node); |
| 36 | + auto buffer = std::make_shared<tf2_ros::Buffer>(node->get_clock()); |
| 37 | + auto timer_interface = std::make_shared<tf2_ros::CreateTimerROS>( |
| 38 | + node->get_node_base_interface(), |
| 39 | + node->get_node_timers_interface()); |
| 40 | + buffer->setCreateTimerInterface(timer_interface); |
| 41 | + auto listener = std::make_shared<tf2_ros::TransformListener>(*buffer, true); |
| 42 | + |
| 43 | + std::string base_link = "base_link"; |
| 44 | + std::string base_footprint = "base_footprint"; |
| 45 | + |
| 46 | + // Publish something to TF, should fail, doesn't exist |
| 47 | + geometry_msgs::msg::TransformStamped transform; |
| 48 | + transform.header.stamp = node->now(); |
| 49 | + transform.header.frame_id = "test1_1"; |
| 50 | + transform.child_frame_id = "test1"; |
| 51 | + tf_broadcaster->sendTransform(transform); |
| 52 | + rclcpp::spin_some(node->get_node_base_interface()); |
| 53 | + EXPECT_THROW( |
| 54 | + buffer->lookupTransform(base_link, base_footprint, tf2::TimePointZero), |
| 55 | + tf2::TransformException); |
| 56 | + |
| 57 | + // This is valid, should work now and communicate the Z-removed info |
| 58 | + transform.header.stamp = node->now(); |
| 59 | + transform.header.frame_id = "odom"; |
| 60 | + transform.child_frame_id = "base_link"; |
| 61 | + transform.transform.translation.x = 1.0; |
| 62 | + transform.transform.translation.y = 1.0; |
| 63 | + transform.transform.translation.z = 1.0; |
| 64 | + tf_broadcaster->sendTransform(transform); |
| 65 | + rclcpp::Rate r(1.0); |
| 66 | + r.sleep(); |
| 67 | + rclcpp::spin_some(node->get_node_base_interface()); |
| 68 | + auto t = buffer->lookupTransform(base_link, base_footprint, tf2::TimePointZero); |
| 69 | + EXPECT_EQ(t.transform.translation.x, 1.0); |
| 70 | + EXPECT_EQ(t.transform.translation.y, 1.0); |
| 71 | + EXPECT_EQ(t.transform.translation.z, 0.0); |
| 72 | +} |
0 commit comments