目录
最终效果:
1.程序读取线速度与角速度
2.opencv显示小车相机数据
3.rviz显示的雷达点云与pcl显示的雷达点云
4.程序计算odom里程计移动距离
------------------------全文23856字53图一步一步完成大约耗时2h------------------------
所需硬件以及软件:
Agilex小车 | nomachine | bitvise | vscode |
松灵小车一辆 (传感器采集显示) | 远程连接软件 (远程桌面) | 远程连接软件 (文件管理与终端运行) | 代码编辑 (编写ROS节点等) |
一、远程连接与配置
1.1 nomachine软件
打开自己的笔记本电脑,在官网找到软件下载链接NoMachine - Free Remote Desktop for Everybody
根据电脑版本匹配软件,我的笔记本是64位的
在自己的笔记本上下载,下载完成后,点击安装nomachine
同意协议
自定义安装文件夹
然后安装完成就可以啦~我们在笔记本找到安装的Nomachine软件并打开
进入后进行初步的设置
然后进入这个界面就可以啦~
1.2 连接小车
打开小车的电源键,等待小车启动后,发出wifi信号(大概3min)然后在笔记本找到小车的Wifi
如同SCOUTMINI-xxxxxx-5G的Wifi名称,点击连接(记住自己小车的WIFI名称哦,别连错啦)
密码一般是:goodlife
然后回到我们的Nomachine软件就可以看到我们的小车电脑啦:
点击连接
验证登录信息,选择OK
然后输入小车用户名以及密码登录:
然后我们就轻松且愉快的连接上小车啦,输入agx登录小车~
可以看到笔记本通过Nomachine软件连接的小车桌面和小车自带的桌面一样,这样我们之后就可以通过Wifi连接小车,在自己笔记本上操作小车啦~
1.3 网口配置
连接小车后,打开笔记本浏览器,输入ip地址登录小车的路由器:192.168.1.1
密码1234678(也可能不是这个,参考手册)
设置为作为LAN口使用,这样可以通过HDMI线,连接显示屏使用哈
二、传感器测试
2.1 相机图像测试
登录小车后,新建终端输入指令:
使能gs _usb内核模块(重启小车后使用传感器前都需要使能哦~)
sudo modprobe gs_usb
打开can设备且设置波特率(重启小车后使用传感器前都需要使能哦~)
sudo ip link set can0 up type can bitrate 500000
如果在前面的步骤中没有发生错误,那么您现在应该可以通过使用command查看CAN
ifconfig -a
启动摄像头程序,并打开rviz可视化
roslaunch realsense2_camera rs_camera.launch
在Add找到image,添加
选择Fixed Frame为camera_link
然后点击image打开下拉栏,选择摄像头的话题image Topic
如果话题选择是color则可以看到彩色图像输出,如果是depth则可以看到是深度图像输出
2.2 激光雷达测试
新建终端输入指令开启激光雷达
roslaunch scout_bringup open_rslidar.launch
开启激光雷达
成功开启激光雷达传感器
2.3 控制小车运动
下载一个包之后编译
apt install libasio-dev # 需要小车联网下载此包
# 在/home/agilex/catkin_ws/src/ugv_sdk下新建build目录
cd build && cmake .. && make
编译完成之后,使能命令,执行demo程序
# 将遥控器SWB拨到最上方
# 在build/ugv_sdk/bin下执行
./app_tracer_robot can0
这时小车将执行自动化程序开关灯以及车轮转动
2.4 手柄控制运动
同时按住手柄左右两个POWER键开启手柄,然后将SWA|B|C|D四个拨片拨到最上方
然后启动基础节点
roslaunch scout_bringup scout_minimal.launch
操控手柄即可让小车前进后退
三、远程开发环境搭建
方案一:使用nomachine远程连接小车后,直接在小车PC端进行文件与代码编辑【不推荐】
方案二:使用Vscode搭建SSH连接小车,这样可以直接进入小车修改小车代码【推荐】
方案三:使用Bitvise远程SSH软件登录小车,可以远程进行文件管理以及代码查看【推荐】
本文采用方案三:使用Bitvise远程SSH软件登录小车
这里需要注意:Host是小车的ip地址,获取需要在小车终端输入:
ifconfig
在终端输出中,找到类似192.168.xx.xx的ip,这就是我们要远程连接的小车~
Host:192.168.xx.xx
Post端口默认:22
右边的登录选项按照小车登录进行设置:
User:agilex
Inital method:password
Password:agx
这样就通过Bitvise建立了远程连接~这样就能方便进行文件管理
四、线速度角速度读取显示
新建一个ROS工作空间,并创建工具包
cd ~ # 进入目录
mkdir cqu_ws # 创建自己的工作空间,我取名叫做cqu_ws
cd cqu_ws # 进入工作空间
mkdir src # 创建src目录
关于ROS工作空间与文件系统,我们需要满足ROS的规范哈:
关于ROS文件系统可以参考我的博客:2.第二次课—ROS基础【ROS智能车-自动驾驶入门与实战】科创竞赛兴趣班 | ROS环境搭建| 问题总结| ROS文件系统| Vscode开发| ROS接口| Python和C++| 保姆级教程_自动驾驶ros-CSDN博客
或者进一步在赵虚左老师课程进行深入学习:
003ROS概念_Chapter1-ROS概述与环境搭建_哔哩哔哩_bilibili
然后我们创建show_v功能包:
mkdir show_v # 创建功能包
cd show_v # 进入功能包
mkdir src # 创建src目录
然后在show_v功能包中我们新建一个c++程序也叫show_v.cpp
写入代码:
// #include <ros/ros.h>
// #include <nav_msgs/Odometry.h>
// void speed_callback(const nav_msgs::Odometry::ConstPtr &msg)
// {
// ROS_INFO("Car linear v: vx=%0.6f, vy=%0.6f,vz=%0.6f",msg->twist.twist.linear.x,msg->twist.twist.linear.y,msg->twist.twist.linear.z);
// ROS_INFO("Car angular v: ax=%0.6f, ay=%0.6f,az=%0.6f",msg->twist.twist.angular.x,msg->twist.twist.angular.y,msg->twist.twist.angular.z);
// return;
// }
// int main(int argc, char **argv)
// {
// ros::init(argc,argv,"speed_subscriber");
// ros::NodeHandle nh;
// ros::Subscriber speed_sub = nh.subscribe("/odom",10,speed_callback);
// ros::spin();
// return 0;
// }
#include "ros/ros.h"
#include "scout_msgs/ScoutStatus.h"
#include "string.h"
#include "iostream"
using namespace std;
void read(const scout_msgs::ScoutStatus::ConstPtr &msg)
{
cout << "linear_velocity=" << msg->linear_velocity << " angular_velocity=" << msg->angular_velocity << endl;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "speed_subscriber");
ros::NodeHandle n;
ros::Subscriber sub;
sub = n.subscribe("/scout_status", 1, read);
ros::spin();
return 0;
}
同时注意cmakelist.txt文件:
cmake_minimum_required(VERSION 3.0.2)
project(show_v)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
#find_package(catkin REQUIRED COMPONENTS
# roscpp
# rospy
# std_msgs
#)
find_package(catkin REQUIRED COMPONENTS
roslaunch
roscpp
sensor_msgs
std_msgs
geometry_msgs
tf2
tf2_ros
ugv_sdk
scout_msgs
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs
# )
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES show_v
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/show_v.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
add_executable(speed_subscriber src/show_v.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
target_link_libraries(speed_subscriber
${catkin_LIBRARIES}
)
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# catkin_install_python(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_show_v.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
包依赖:
编译节点:
回到cqu_ws进行catkin_make编译
cd cqu_ws # 回到工作空间目录
catkin_make # 进行编译
编译完成开两个终端,然后执行程序:
【指令】roslaunch scout_bringup scout_minimal.launch # 启动小车基础节点
【指令】rosrun show_v speed_subscriber # 启动读取线速度、角速度程序
如果出现找不到文件在哪报错,记得刷新一下环境变量哦:
source ./devel/setup.bash
然后我们打开手柄让小车轮子转起来(或者咱用手拨动一下轮子),完成后,终端输出:
可见终端输出了小车的线速度和角速度啦~
五、OpenCV显示相机输出
利用opencv库函数,通过编写ROS节点订阅小车发布的彩色RGB以及深度图像话题,因为ROS发布的图像数据不能被opencv所处理,所以需要引入cv_bridge进行数据转换,然后在回调函数中编写图像显示程序,最后在屏幕进行显示。
先看看小车有无opencv库:
python3 -c "import cv2;print(cv2.__version__)"
输入指令后发现咱小车的opencv版本是4.1.1~
然后我们创建show_v功能包:
mkdir show_img # 创建功能包
cd img # 进入功能包
mkdir src # 创建src目录
然后在show_img功能包中我们新建一个c++程序也叫show_img.cpp
源码:
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
cv::Mat image;
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
{
ROS_INFO_STREAM("Get Msg");
try
{
image = cv_bridge::toCvShare(msg, "16UC1")->image;
cv::imshow("view", image);
}
catch (cv_bridge::Exception &e)
{
ROS_ERROR("Could not convert from '%s' to '16UC1'.", msg->encoding.c_str());
}
cv::waitKey(1);
}
void imageCallback2(const sensor_msgs::ImageConstPtr &msg)
{
ROS_INFO_STREAM("Get Msg");
try
{
image = cv_bridge::toCvShare(msg, "bgr8")->image;
cv::imshow("view2", image);
}
catch (cv_bridge::Exception &e)
{
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
cv::waitKey(1);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_listener");
ros::NodeHandle nh;
cv::namedWindow("view");
cv::namedWindow("view2");
// cv::startWindowThread();
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe("/camera/depth/image_rect_raw", 1, imageCallback);
image_transport::Subscriber sub2 = it.subscribe("/camera/color/image_raw", 1, imageCallback2);
ros::spin();
// cv::destroyWindow("view");
cv::waitKey(0);
return 0;
}
同理也需要注意cmakelist.txt文件
cmake_minimum_required(VERSION 3.0.2)
project(show_img)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
set(OpenCV_DIR /usr/share/OpenCV)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
OpenCV
cv_bridge
image_transport
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs
# )
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES show_img
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/show_img.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
add_executable(image_listener src/show_img.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
target_link_libraries(image_listener
${catkin_LIBRARIES}
)
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# catkin_install_python(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_show_img.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
注意包依赖,节点名称与包下的cpp目录
然后回到cqu_ws目录下进行编译
cd cqu_ws # 回到工作空间
catkin_make # 编译
编程成功后,运行指令
【指令】roslaunch scout_bringup scout_minimal.launch # 开启小车基础节点
【指令】rosrun show_img image_listener # 启动opencv读取显示程序
可见小车打开opencv库,并打开两个窗口成功显示输出彩色图像与深度图像~
六、PCL显示激光雷达点云信息
订阅小车发布的雷达点云话题/rslidar_points数据,利用PCL库将雷达点云数据转换为PCL下格式点云数据,然后声明pcl可视化窗口,进行窗口可视化,编译后打开Rviz与可视化pcl点云进行对比
源码:
#include <ros/ros.h>
// PCL specific includes
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_plane.h>
#include <Eigen/Core>
#include <iostream>
using namespace std;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer1(new pcl::visualization::PCLVisualizer("realsense pcl"));
// ros::Publisher pub;
typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr &input)
{
// 声明存储原始数据与滤波后的数据的点云的格式
pcl::PCLPointCloud2 *cloud = new pcl::PCLPointCloud2; //原始的点云的数据格式
pcl::PCLPointCloud2ConstPtr cloudPtr(cloud);
// 转化为PCL中的点云的数据格式
pcl_conversions::toPCL(*input, *cloud);
// pub.publish (*cloud);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1;
cloud1.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::fromROSMsg(*input, *cloud1);
viewer1->setBackgroundColor(0, 0, 0);
viewer1->removeAllPointClouds(); // 移除当前所有点云
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZRGB> rgb(cloud1, "z");
viewer1->addPointCloud(cloud1, rgb, "realsense pcl");
viewer1->updatePointCloud(cloud1, rgb, "realsense pcl");
viewer1->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "realsense pcl");
viewer1->spinOnce(0.001);
}
int main(int argc, char **argv)
{
// Initialize ROS
ros::init(argc, argv, "pcl");
ros::NodeHandle nh;
// Create a ROS subscriber for the input point cloud
ros::Subscriber sub = nh.subscribe("/rslidar_points", 10, cloud_cb);
// Create a ROS publisher for the output point cloud
// pub = nh.advertise<pcl::PCLPointCloud2> ("output", 1);
ros::spin();
}
cmakelist.txt
set(PCL_INCLUDE_DIRS /usr/include/pcl-1.8) #指定pcl1.8路径
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
pcl_conversions
pcl_msgs
pcl_ros
sensor_msgs
)
project(show_pcl)
find_package(PCL REQUIRED)
add_definitions(${PCL_DEFINITIONS})
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PCL_LIBRARIES}
)
add_executable(pcl src/show_pcl.cpp)
target_link_libraries(pcl ${catkin_LIBRARIES} ${PCL_LIBRARIES})
补充:如果想要单独创建整个功能包需要:
进入src目录
【指令】catkin_create_pkg show_pcl pcl_conversions pcl_msgs pcl_ros sensor_msgs
创建一个show_pcl包
然后在src下创建一个show_pcl.cpp程序文件,写入程序后
修改Cmkelist.txt文件,注意构建依赖时指定ros节点和cpp文件,以及pcl库的相关依赖
然后回到cqu_ws空间进行catkin_make编译
在编译过程中遇到报错,就对应你的源程序以及Cmakelist.txt文件进行修改
最后编译通过,在小车打开终端进行测试
打开三个终端,分别启动程序:
【指令】roslaunch scout_bringup open_rslidar.launch
【指令】rviz
【指令】rosrun show_pcl pcl
依次运行,即可得到:
注意在打开rviz时要更换换话题
然后我们rosrun我们的show_pcl包节点
即可看到rviz显示的雷达点云与pcl显示的雷达点云,撒花~~~~
七、Odom里程计距离处理显示
同理上面的流程
源码:
#include <ros/ros.h>
// PCL specific includes
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_plane.h>
#include <Eigen/Core>
#include <iostream>
using namespace std;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer1(new pcl::visualization::PCLVisualizer("realsense pcl"));
// ros::Publisher pub;
typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr &input)
{
// 声明存储原始数据与滤波后的数据的点云的格式
pcl::PCLPointCloud2 *cloud = new pcl::PCLPointCloud2; //原始的点云的数据格式
pcl::PCLPointCloud2ConstPtr cloudPtr(cloud);
// 转化为PCL中的点云的数据格式
pcl_conversions::toPCL(*input, *cloud);
// pub.publish (*cloud);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1;
cloud1.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::fromROSMsg(*input, *cloud1);
viewer1->setBackgroundColor(0, 0, 0);
viewer1->removeAllPointClouds(); // 移除当前所有点云
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZRGB> rgb(cloud1, "z");
viewer1->addPointCloud(cloud1, rgb, "realsense pcl");
viewer1->updatePointCloud(cloud1, rgb, "realsense pcl");
viewer1->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "realsense pcl");
viewer1->spinOnce(0.001);
}
int main(int argc, char **argv)
{
// Initialize ROS
ros::init(argc, argv, "pcl");
ros::NodeHandle nh;
// Create a ROS subscriber for the input point cloud
ros::Subscriber sub = nh.subscribe("/rslidar_points", 10, cloud_cb);
// Create a ROS publisher for the output point cloud
// pub = nh.advertise<pcl::PCLPointCloud2> ("output", 1);
ros::spin();
}
cmakelist.txt
set(PCL_INCLUDE_DIRS /usr/include/pcl-1.8) #指定pcl1.8路径
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
pcl_conversions
pcl_msgs
pcl_ros
sensor_msgs
)
project(show_pcl)
find_package(PCL REQUIRED)
add_definitions(${PCL_DEFINITIONS})
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PCL_LIBRARIES}
)
add_executable(pcl src/show_pcl.cpp)
target_link_libraries(pcl ${catkin_LIBRARIES} ${PCL_LIBRARIES})
catkin_make进行编译
开启三个终端:
【启动基础节点】roslaunch scout_bringup scout_minimal.launch
【激光雷达节点】roslaunch scout_bringup open_rslidar.launch
【启动我们的节点】rosrun odom_d odom_d
可以看到右上角终端我们的输出小车里程计odom距离,成功啦~~~~~~~
八、其他信息
1.gitee仓库
整个工作空间与源码我都放在了gitee上,一键克隆下载编译后即可轻松运行:
git clone https://gitee.com/akaxi611/vehicle_software_development_class_task1.git
2.参考链接
车载软件开发基础-课后实践报告3: 车载软件开发基础-课后实践报告3
exercise1: 车载软件设计基础课后实践1,智能小车的信息录入及分配
【ROS】小车机器视觉巡线行驶_基于opencv的视觉巡线ros小车-CSDN博客
使用ros订阅点云并在pcl::visualization中实时显示_在ros中订阅点云并显示点云-CSDN博客
(六)ROS发布里程计(Odometry)消息并在rviz中显示_odemetry-CSDN博客
3.更多信息
----------------------------------------------------------------------------------------------------
相信读到这里的朋友,一定是坚持且优秀的
给博主一个免费的赞👍吧
扫描二维码进博主交流群,问题交流 | 吹吹水 | 一起变得更加优秀
————————————————
2024.10.28
两江重大卓工院