版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
機(jī)器人操作系統(tǒng)(ROS2)入門(mén)與實(shí)踐機(jī)器人操作系統(tǒng)(ROS2)入門(mén)與實(shí)踐第1章LinuxUbuntu入門(mén)基礎(chǔ)第2章ROS2安裝與系統(tǒng)架構(gòu)第3章ROS2編程基礎(chǔ)第4章ROS2機(jī)器人運(yùn)動(dòng)控制第5章激光雷達(dá)在ROS2中的使用第6章IMU在ROS2中的使用第7章ROS2中的SLAM環(huán)境建圖第8章ROS2中的NAV2自主導(dǎo)航第9章ROS2中的圖像視覺(jué)應(yīng)用第10章ROS2的三維視覺(jué)應(yīng)用第11章ROS2的機(jī)械臂應(yīng)用第12章基于ROS2的綜合應(yīng)用第10章10.2使用PCL進(jìn)行物品檢測(cè)
10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取10.3本章小結(jié)第10章ROS2的三維視覺(jué)應(yīng)用
三維點(diǎn)云數(shù)據(jù)的獲取是通過(guò)訂閱三維相機(jī)驅(qū)動(dòng)節(jié)點(diǎn)發(fā)布的話題,從話題中獲取相機(jī)發(fā)出的消息包來(lái)實(shí)現(xiàn)的。本實(shí)驗(yàn)中,使用的虛擬機(jī)器人配備的是KinectV2相機(jī),話題名稱是"/kinect2/sd/points"。話題中的消息包格式為sensor_msgs::PointCloud2。本實(shí)驗(yàn)將會(huì)實(shí)現(xiàn)一個(gè)訂閱者節(jié)點(diǎn),訂閱相機(jī)發(fā)布的話題"/kinect2/sd/points"。從此話題中接收sensor_msgs::PointCloud2類型的消息包,并將其中的點(diǎn)云數(shù)據(jù)轉(zhuǎn)換成PCL的格式,然后把所有三維點(diǎn)的坐標(biāo)值顯示在終端程序里。10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取
詳細(xì)操作步驟見(jiàn)教材P329-P338頁(yè)1、編寫(xiě)節(jié)點(diǎn)代碼在VSCode中找到pc_pkg軟件包,在
“src”文件夾新建文件,命名為“pc_data.cpp”。10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取10.1.1編寫(xiě)點(diǎn)云數(shù)據(jù)獲取程序打開(kāi)一個(gè)新的終端窗口,在工作空間中創(chuàng)建一個(gè)新的軟件包。cd~/ros2_ws/srcros2pkgcreatepc_pkg#include<rclcpp/rclcpp.hpp>#include<sensor_msgs/msg/point_cloud2.hpp>#include<pcl/point_types.h>#include<pcl/point_cloud.h>#include<pcl_conversions/pcl_conversions.h>
std::shared_ptr<rclcpp::Node>node;
voidPointcloudCallback(constsensor_msgs::msg::PointCloud2::SharedPtrmsg){pcl::PointCloud<pcl::PointXYZ>pointCloudIn;pcl::fromROSMsg(*msg,pointCloudIn);
intcloudSize=pointCloudIn.points.size();for(inti=0;i<cloudSize;i++){RCLCPP_INFO(node->get_logger(),"[i=%d](%.2f,%.2f,%.2f)",i,pointCloudIn.points[i].x,pointCloudIn.points[i].y,pointCloudIn.points[i].z);}}10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取intmain(intargc,char**argv){rclcpp::init(argc,argv);
node=std::make_shared<rclcpp::Node>("pointcloud_data_node");autopc_sub=node->create_subscription<sensor_msgs::msg::PointCloud2>("/kinect2/sd/points",1,PointcloudCallback);
rclcpp::spin(node);
rclcpp::shutdown();
return0;}10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取2、設(shè)置編譯規(guī)則find_package(rclcppREQUIRED)find_package(sensor_msgsREQUIRED)find_package(pcl_conversionsREQUIRED)find_package(pcl_rosREQUIRED)add_executable(pc_datasrc/pc_data.cpp)ament_target_dependencies(pc_data"rclcpp""sensor_msgs""pcl_conversions""pcl_ros")install(TARGETSpc_dataDESTINATIONlib/${PROJECT_NAME})3、修改軟件包信息<depend>rclcpp</depend><depend>sensor_msgs</depend><depend>pcl_conversions</depend><depend>pcl_ros</depend>10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取sourceinstall/setup.bashros2launchwpr_simulation2wpb_table.launch.py10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取4、編譯軟件包c(diǎn)d~/ros2_wscolconbuild10.1.2仿真運(yùn)行點(diǎn)云數(shù)據(jù)獲取程序sourceinstall/setup.bashros2runpc_pkgpc_data打開(kāi)第2個(gè)子窗口。10.1RGB-D相機(jī)的三維點(diǎn)云數(shù)據(jù)獲取
在10.1節(jié)的實(shí)驗(yàn)里,實(shí)現(xiàn)了從ROS機(jī)器人頭部的RGB-D相機(jī)獲取三維點(diǎn)云數(shù)據(jù)。這一次將繼續(xù)深入,使用PCL實(shí)現(xiàn)三維特征提取,并對(duì)桌面上的物體進(jìn)行檢測(cè)和定位。
具體實(shí)現(xiàn)步驟如下:1)將機(jī)器人頭部相機(jī)采集到的三維點(diǎn)云消息包進(jìn)行格式轉(zhuǎn)換。從ROS2的點(diǎn)云格式轉(zhuǎn)換為PCL點(diǎn)云格式,方便后面調(diào)用PCL的函數(shù)對(duì)點(diǎn)云數(shù)據(jù)進(jìn)行處理。2)先使用PCL函數(shù)對(duì)點(diǎn)云數(shù)據(jù)進(jìn)行平面提取,將桌面的高度確定下來(lái)。3)將桌面高度以下的點(diǎn)集剔除掉,僅保留桌面之上的物體點(diǎn)云。4)使用歐幾里德分割法對(duì)保留下來(lái)的物體點(diǎn)云進(jìn)行點(diǎn)云簇的提取,將桌面上的多個(gè)相隔較遠(yuǎn)的點(diǎn)云簇區(qū)分開(kāi)。這時(shí)可以認(rèn)為每個(gè)點(diǎn)云簇就表示一個(gè)物體的點(diǎn)云集合。計(jì)算每個(gè)物體點(diǎn)云集合的質(zhì)心坐標(biāo),用來(lái)表示物體的空間位置。10.2使用PCL進(jìn)行物品檢測(cè)
詳細(xì)操作步驟見(jiàn)教材P339-P356頁(yè)1、編寫(xiě)節(jié)點(diǎn)代碼在VSCode中找到pc_pkg軟件包,在
“src”文件夾中新建文件,命名為“pc_objects.cpp”。10.2使用PCL進(jìn)行物品檢測(cè)10.2.1編寫(xiě)物品檢測(cè)程序下面編寫(xiě)這個(gè)代碼文件內(nèi)容:#include<rclcpp/rclcpp.hpp>#include<sensor_msgs/msg/point_cloud2.hpp>#include<pcl/point_types.h>#include<pcl/point_cloud.h>#include<pcl_conversions/pcl_conversions.h>#include<tf2_ros/transform_listener.h>#include<pcl_ros/transforms.hpp>#include<pcl/filters/passthrough.h>#include<pcl/segmentation/sac_segmentation.h>#include<pcl/search/kdtree.h>#include<pcl/segmentation/extract_clusters.h>
std::shared_ptr<rclcpp::Node>node;tf2_ros::Buffer::SharedPtrtf_buffer_;std::shared_ptr<tf2_ros::TransformListener>tf_listener_;10.2使用PCL進(jìn)行物品檢測(cè)voidPointcloudCallback(constsensor_msgs::msg::PointCloud2::SharedPtrmsg){boolresult=tf_buffer_->canTransform("base_footprint",msg->header.frame_id,msg->header.stamp);if(!result){return;}sensor_msgs::msg::PointCloud2pc_footprint;pcl_ros::transformPointCloud("base_footprint",*msg,pc_footprint,*tf_buffer_);10.2使用PCL進(jìn)行物品檢測(cè)pcl::PointCloud<pcl::PointXYZ>cloud_src;pcl::fromROSMsg(pc_footprint,cloud_src);
pcl::PassThrough<pcl::PointXYZ>pass;pass.setInputCloud(cloud_src.makeShared());pass.setFilterFieldName("x");pass.setFilterLimits(0.5,1.5);pass.filter(cloud_src);pass.setInputCloud(cloud_src.makeShared());pass.setFilterFieldName("y");pass.setFilterLimits(-0.5,0.5);pass.filter(cloud_src);pass.setInputCloud(cloud_src.makeShared());pass.setFilterFieldName("z");pass.setFilterLimits(0.5,1.5);pass.filter(cloud_src);10.2使用PCL進(jìn)行物品檢測(cè)pcl::ModelCoefficients::Ptrcoefficients(newpcl::ModelCoefficients);pcl::SACSegmentation<pcl::PointXYZ>segmentation;segmentation.setInputCloud(cloud_src.makeShared());segmentation.setModelType(pcl::SACMODEL_PLANE);segmentation.setMethodType(pcl::SAC_RANSAC);segmentation.setDistanceThreshold(0.05);segmentation.setOptimizeCoefficients(true);pcl::PointIndices::PtrplaneIndices(newpcl::PointIndices);segmentation.segment(*planeIndices,*coefficients);intpoint_num=planeIndices->indices.size();floatpoints_z_sum=0;for(inti=0;i<point_num;i++){intpoint_index=planeIndices->indices[i];points_z_sum+=cloud_src.points[point_index].z;}10.2使用PCL進(jìn)行物品檢測(cè)floatplane_height=points_z_sum/point_num;RCLCPP_INFO(node->get_logger(),"plane_height=%.2f",plane_height);
pass.setInputCloud(cloud_src.makeShared());pass.setFilterFieldName("z");pass.setFilterLimits(plane_height+0.2,1.5);pass.filter(cloud_src);
pcl::search::KdTree<pcl::PointXYZ>::Ptrtree(newpcl::search::KdTree<pcl::PointXYZ>);tree->setInputCloud(cloud_src.makeShared());
pcl::EuclideanClusterExtraction<pcl::PointXYZ>ec;ec.setInputCloud(cloud_src.makeShared());ec.setMinClusterSize(100);ec.setMaxClusterSize(25000);ec.setClusterTolerance(0.1);ec.setSearchMethod(tree);std::vector<pcl::PointIndices>cluster_indices;ec.extract(cluster_indices);intobject_num=cluster_indices.size();10.2使用PCL進(jìn)行物品檢測(cè)RCLCPP_INFO(node->get_logger(),"object_num=%d",object_num);for(inti=0;i<object_num;i++){intpoint_num=cluster_indices[i].indices.size();floatpoints_x_sum=0;floatpoints_y_sum=0;floatpoints_z_sum=0;for(intj=0;j<point_num;j++){intpoint_index=cluster_indices[i].indices[j];points_x_sum+=cloud_src.points[point_index].x;points_y_sum+=cloud_src.points[point_index].y;points_z_sum+=cloud_src.points[point_index].z;}floatobject_x=points_x_sum/point_num;floatobject_y=points_y_sum/point_num;floatobject_z=points_z_sum/point_num;RCLCPP_INFO(node->get_logger(),"object%dpos=(%.2f,%.2f,%.2f)",i,object_x,object_y,object_z);}RCLCPP_INFO(node->get_logger(),"---------------------");}10.2使用PCL進(jìn)行物品檢測(cè)intmain(intargc,char**argv){rclcpp::init(argc,argv);
node=std::make_shared<rclcpp::Node>("pointcloud_objects_node");
tf_buffer_=std::make_shared<tf2_ros::Buffer>(node->get_clock());tf_listener_=std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);
autopc_sub=node->create_subscription<sensor_msgs::msg::PointCloud2>("/kinect2/sd/points",1,PointcloudCallback);
rclcpp::spin(node);
rclcpp::shutdown();
return0;}10.2使用PCL進(jìn)行物品檢測(cè)2、設(shè)置編譯規(guī)則find_package(rclcppREQUIRED)find_package(sensor_msgsREQUIRED)find_package(pcl_conversionsREQUIRED)f
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 45040-2024床上用品乳膠制品透氣性試驗(yàn)方法
- 易錯(cuò)題17 文言文閱讀之?dāng)嗑漕}-當(dāng)斷不斷不該斷卻斷【高考語(yǔ)文】備戰(zhàn)2025年高考易錯(cuò)題(新高考專用)含解析
- 愚人節(jié)活動(dòng)策劃方案 (15篇)
- 參觀圓明園的觀后感
- 智能大廈綜合布線的工程設(shè)計(jì)方案
- 青春追夢(mèng)人心共進(jìn)
- 多振源混疊的DAS目標(biāo)信號(hào)分離
- 智研咨詢發(fā)布:2024年中國(guó)美妝行業(yè)市場(chǎng)發(fā)展環(huán)境及前景研究報(bào)告
- DOPS基P-N-S協(xié)同阻燃劑的合成及其阻燃環(huán)氧樹(shù)脂的性能研究
- 二零二五版國(guó)際學(xué)校英語(yǔ)教師兼職外教聘請(qǐng)合同樣本3篇
- 房地產(chǎn)調(diào)控政策解讀
- 2024-2025學(xué)年八年級(jí)數(shù)學(xué)人教版上冊(cè)寒假作業(yè)(綜合復(fù)習(xí)能力提升篇)(含答案)
- 《AP內(nèi)容介紹》課件
- 醫(yī)生定期考核簡(jiǎn)易程序述職報(bào)告范文(10篇)
- 安全創(chuàng)新創(chuàng)效
- 鋼結(jié)構(gòu)工程施工(杜紹堂 第五版) 課件全套 單元1-3 緒論、材料與連接- 鋼結(jié)構(gòu)施工安全
- 門(mén)診診療指南及規(guī)范
- 2023《住院患者身體約束的護(hù)理》團(tuán)體標(biāo)準(zhǔn)解讀PPT
- 國(guó)外文化消費(fèi)研究述評(píng)
- 部編版語(yǔ)文四年級(jí)下冊(cè)第一單元 迷人的鄉(xiāng)村風(fēng)景 大單元整體教學(xué)設(shè)計(jì)
- 五年級(jí)行程問(wèn)題應(yīng)用題100道
評(píng)論
0/150
提交評(píng)論