Skip to content

Commit 33c0a39

Browse files
committed
Reenable sensor_msgs cpp test
1 parent cf61272 commit 33c0a39

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

tf2_sensor_msgs/CMakeLists.txt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@ find_package(Eigen3 REQUIRED)
2424

2525
ament_export_dependencies(eigen3_cmake_module)
2626
ament_export_dependencies(Eigen3)
27-
28-
# TODO enable tests
29-
#if(BUILD_TESTING)
30-
# catkin_add_nosetests(test/test_tf2_sensor_msgs.py)
31-
32-
#find_package(catkin REQUIRED COMPONENTS
33-
# sensor_msgs
34-
# rostest
35-
# tf2_ros
36-
# tf2
37-
#)
38-
#include_directories(${EIGEN_INCLUDE_DIRS})
39-
#add_executable(test_tf2_sensor_msgs_cpp EXCLUDE_FROM_ALL test/test_tf2_sensor_msgs.cpp)
40-
#target_link_libraries(test_tf2_sensor_msgs_cpp ${catkin_LIBRARIES} ${GTEST_LIBRARIES})
41-
#if(TARGET tests)
42-
# add_dependencies(tests test_tf2_sensor_msgs_cpp)
43-
#endif()
44-
#add_rostest(${CMAKE_CURRENT_SOURCE_DIR}/test/test.launch)
45-
#endif()
27+
ament_export_dependencies(tf2)
28+
29+
if(BUILD_TESTING)
30+
find_package(ament_cmake_gtest REQUIRED)
31+
find_package(ament_cmake_pytest REQUIRED)
32+
33+
# TODO(ros2/orocos_kinematics_dynamics): reenable when PyKDL is ready to use
34+
#ament_add_pytest_test(test_tf2_sensor_msgs_py test/test_tf2_sensor_msgs.py)
35+
36+
ament_add_gtest(test_tf2_sensor_msgs_cpp test/test_tf2_sensor_msgs.cpp)
37+
if(TARGET test_tf2_sensor_msgs_cpp)
38+
target_include_directories(test_tf2_sensor_msgs_cpp PUBLIC include)
39+
ament_target_dependencies(test_tf2_sensor_msgs_cpp
40+
"Eigen3"
41+
"rclcpp"
42+
"sensor_msgs"
43+
"tf2"
44+
"tf2_ros"
45+
)
46+
endif()
47+
endif()
4648

4749
ament_auto_package()

tf2_sensor_msgs/test/test_tf2_sensor_msgs.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
*/
2929

3030

31-
#include <tf2_sensor_msgs/tf2_sensor_msgs.hpp>
32-
#include <geometry_msgs/PoseStamped.h>
33-
#include <tf2_ros/transform_listener.h>
34-
#include <ros/ros.h>
31+
#include <builtin_interfaces/msg/time.hpp>
32+
#include <geometry_msgs/msg/pose_stamped.hpp>
3533
#include <gtest/gtest.h>
34+
#include <sensor_msgs/point_cloud2_iterator.hpp>
3635
#include <tf2_ros/buffer.h>
36+
#include <tf2_ros/transform_listener.h>
37+
#include <tf2_sensor_msgs/tf2_sensor_msgs.hpp>
3738

3839
std::unique_ptr<tf2_ros::Buffer> tf_buffer = nullptr;
3940
static const double EPS = 1e-3;
@@ -42,45 +43,48 @@ static const double EPS = 1e-3;
4243
TEST(Tf2Sensor, PointCloud2)
4344
{
4445
sensor_msgs::msg::PointCloud2 cloud;
45-
sensor_msgs::msg::PointCloud2Modifier modifier(cloud);
46+
sensor_msgs::PointCloud2Modifier modifier(cloud);
4647
modifier.setPointCloud2FieldsByString(2, "xyz", "rgb");
4748
modifier.resize(1);
4849

49-
sensor_msgs::msg::PointCloud2Iterator<float> iter_x(cloud, "x");
50-
sensor_msgs::msg::PointCloud2Iterator<float> iter_y(cloud, "y");
51-
sensor_msgs::msg::PointCloud2Iterator<float> iter_z(cloud, "z");
50+
sensor_msgs::PointCloud2Iterator<float> iter_x(cloud, "x");
51+
sensor_msgs::PointCloud2Iterator<float> iter_y(cloud, "y");
52+
sensor_msgs::PointCloud2Iterator<float> iter_z(cloud, "z");
5253

5354
*iter_x = 1;
5455
*iter_y = 2;
5556
*iter_z = 3;
5657

57-
cloud.header.stamp = builtin_interfaces::msg::Time(2);
58+
cloud.header.stamp.sec = 2;
59+
cloud.header.stamp.nanosec = 0;
5860
cloud.header.frame_id = "A";
5961

6062
// simple api
61-
sensor_msgs::msg::PointCloud2 cloud_simple = tf_buffer->transform(cloud, "B", tf2::durationFromSec(2.0));
62-
sensor_msgs::msg::PointCloud2Iterator<float> iter_x_after(cloud_simple, "x");
63-
sensor_msgs::msg::PointCloud2Iterator<float> iter_y_after(cloud_simple, "y");
64-
sensor_msgs::msg::PointCloud2Iterator<float> iter_z_after(cloud_simple, "z");
63+
sensor_msgs::msg::PointCloud2 cloud_simple = tf_buffer->transform(
64+
cloud, "B", tf2::durationFromSec(
65+
2.0));
66+
sensor_msgs::PointCloud2Iterator<float> iter_x_after(cloud_simple, "x");
67+
sensor_msgs::PointCloud2Iterator<float> iter_y_after(cloud_simple, "y");
68+
sensor_msgs::PointCloud2Iterator<float> iter_z_after(cloud_simple, "z");
6569
EXPECT_NEAR(*iter_x_after, -9, EPS);
6670
EXPECT_NEAR(*iter_y_after, 18, EPS);
6771
EXPECT_NEAR(*iter_z_after, 27, EPS);
6872

6973
// advanced api
70-
sensor_msgs::msg::PointCloud2 cloud_advanced = tf_buffer->transform(cloud, "B", builtin_interfaces::msg::Time(2.0),
71-
"A", tf2::durationFromSec(3.0));
72-
sensor_msgs::msg::PointCloud2Iterator<float> iter_x_advanced(cloud_advanced, "x");
73-
sensor_msgs::msg::PointCloud2Iterator<float> iter_y_advanced(cloud_advanced, "y");
74-
sensor_msgs::msg::PointCloud2Iterator<float> iter_z_advanced(cloud_advanced, "z");
74+
sensor_msgs::msg::PointCloud2 cloud_advanced = tf_buffer->transform(
75+
cloud, "B", tf2::timeFromSec(2.0),
76+
"A", tf2::durationFromSec(3.0));
77+
sensor_msgs::PointCloud2Iterator<float> iter_x_advanced(cloud_advanced, "x");
78+
sensor_msgs::PointCloud2Iterator<float> iter_y_advanced(cloud_advanced, "y");
79+
sensor_msgs::PointCloud2Iterator<float> iter_z_advanced(cloud_advanced, "z");
7580
EXPECT_NEAR(*iter_x_advanced, -9, EPS);
7681
EXPECT_NEAR(*iter_y_advanced, 18, EPS);
7782
EXPECT_NEAR(*iter_z_advanced, 27, EPS);
7883
}
7984

80-
int main(int argc, char **argv){
85+
int main(int argc, char ** argv)
86+
{
8187
testing::InitGoogleTest(&argc, argv);
82-
ros::init(argc, argv, "test");
83-
ros::NodeHandle n;
8488

8589
rclcpp::Clock::SharedPtr clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
8690
tf_buffer = std::make_unique<tf2_ros::Buffer>(clock);
@@ -94,7 +98,8 @@ int main(int argc, char **argv){
9498
t.transform.rotation.y = 0;
9599
t.transform.rotation.z = 0;
96100
t.transform.rotation.w = 0;
97-
t.header.stamp = builtin_interfaces::msg::Time(2.0);
101+
t.header.stamp.sec = 2;
102+
t.header.stamp.nanosec = 0;
98103
t.header.frame_id = "A";
99104
t.child_frame_id = "B";
100105
tf_buffer->setTransform(t, "test");

0 commit comments

Comments
 (0)