foxy系统
工作空间站的创建
mkdir -p ros2_ws/src
功能包创建
cd src
ros2 pkg create --build-type ament_cmake ros2pcl --dependencies rclcpp
subscription_pcl.cpp 创建 移动到功能包src下
touch subscription_pcl.cpp
将以下内容复制进去,修改为自己所需
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/point_cloud2.hpp"
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloud;
class PclSub : public rclcpp::Node
{
public:
PclSub(std::string name)
: Node(name)
{
using std::placeholders::_1;
sub_novel = this->create_subscription<sensor_msgs::msg::PointCloud2>("//此处为所需订阅的点云话题", 1, std::bind(&PclSub::topic_callback, this, std::placeholders::_1));
}
private:
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr sub_novel;
void topic_callback(const sensor_msgs::msg::PointCloud2::SharedPtr msg)
{
//此处为需要对点云进行处理的操作
}
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
auto node = std::make_shared<PclSub>("pclsub");
rclcpp::spin(node);
//rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
Cmakelists.txt
cmake_minimum_required(VERSION 3.5)
project(ros2pcl_test)
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(PCL REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()
add_executable(ros2pcl_test_sub
src/subscription_pcl.cpp
)
ament_target_dependencies(ros2pcl_test_sub
rclcpp
sensor_msgs
PCL
)
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
target_link_libraries(ros2pcl_test_sub ${PCL_LIBRARIES})
install(TARGETS
ros2pcl_test_sub
DESTINATION lib/${PROJECT_NAME})
ament_package()
然后返回到ros2_ws目录下编译
colcon build
更新环境
. install/setup.bash
ok,生成文件
ros2 run 运行
补充一个定时器
rclcpp::Rate rate(0.1);
while (rclcpp::ok())
{
rate.sleep();
}
转载请表明出处