初始化ros工作空间,1个工作空间可以有多个ros工程
#!/bin/bash
#创建
rwp=$(pwd)/../ad706
mkdir -p ${rwp}/src
cd ${rwp}/src
catkin_init_workspace
#编译
cd ${rwp}
catkin_make
source devel/setup.bash
source /opt/ros/kinetic/setup.bash
创建ros工程
#!/bin/bash
source /opt/ros/kinetic/setup.bash
node_name=grabber_lidar
catkin_create_pkg ${node_name} roscpp std_msgs geometry_msgs sensor_msgs pcl_conversions pcl_ros pcl_msgs
vscode配置:
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/opt/ros/kinetic/include/**",
"/home/wx/Workspace_ros/ad706/src/grabber_lidar/include/**",
"/home/wx/Workspace_ros/ad706/src/grabber_lidar/thirdparty/**",
"/usr/include/**"
],
"name": "ROS",
"cppStandard": "c++11"
}
],
"version": 4
}
ros自定义点云发布:
自定义点云 转pcl点云 toROSMsg转ros点云发布
void demo_pcl(){
ros::init(argc,argv, "grabber_lidar");
ros::NodeHandle _nh;
ros::Publisher _pub = _nh.advertise<sensor_msgs::PointCloud2>("grabber_lidar_pub", 10);
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
cloud->width = 2000;
cloud->height = 1;
cloud->points.resize(cloud->width * cloud->height);
ros::Rate loop_rate(10);
//按下Crtl+C组合或ROS停止所有节点,ros::ok()停止
while(ros::ok())
{
for (int i = 0; i < cloud->points.size(); ++ i){
//
pcl::PointXYZI* p = &(cloud->points.at(i));
p->x = 1024 * rand () / (RAND_MAX + 1.0f);
p->y = 1024 * rand () / (RAND_MAX + 1.0f);
p->z = 1024 * rand () / (RAND_MAX + 1.0f);
p->intensity = i % 255;
}
int fid = 0;
cloud->header.frame_id = fid++;
sensor_msgs::PointCloud2 rosCloud;
pcl::toROSMsg(*cloud, rosCloud);
rosCloud.header.frame_id = "odom";
_pub.publish(rosCloud);
//spinOnce()函数负责处理所有ROS内部时间和操作,例如读取订阅的消息。
ros::spinOnce();
//按照10HZ频率将程序挂起
loop_rate.sleep();
}
}