1、首先创建1个ros程序包,这个程序包相当于普通的c++工程的工程名:
cd ~/catkin_ws/src
catkin_create_pkg 工程名 std_msgs rospy roscpp (在这里我用的工程名字为multicol_slam)
此时,会自动生成package.xml和CMakeLists.txt这2个文件。
2、将原先c++工程的include、src、thirdparty、config、example、build等文件直接拷贝进入新建的ros程序包。对于thirdparty,里面需要编译的文件我们要重新编译。
3、其次,我们需要修改下ros程序包中的cmakelists.txt.在这里我选择的做法时吧原先c++程序包的cmakelists。txt中的大多数内容拷贝进入先建的cmakelist.txt。拷贝的过程中,需要注意以下几个地方:
a、工程名称采用ros程序包的工程名字
b、对include、src、thirdparty,config,example文件的处理可以直接吧原先的cmakelists中配置复制到新建的cmakelists中
c、如果需要消息传递方面的内容,也可以在新建的cmakelists中添加,关于这点,大家可以在我的另外一篇博客中查阅。
d、在生成可执行文件的时候,与之前的cmakelist有所不同:举例说明如下
c++工程:
# Build examples
MESSAGE("${PROJECT_NAME}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/Examples/Lafida)
add_executable(multi_col_slam_lafida Examples/Lafida/mult_col_slam_lafida.cpp)
target_link_libraries(multi_col_slam_lafida ${PROJECT_NAME})
ros工程:
MESSAGE("${PROJECT_NAME}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/Examples/Lafida)
add_executable(mcs Examples/Lafida/mult_col_slam_lafida.cpp)
target_link_libraries(mcs ${PROJECT_NAME} ${catkin_LIBRARIES})
相信已经看到区别了,catkin_LIBRARIES是我们需要改变的地方。
在不创建新的话题,服务或动作类型时
add_executable(bizhang src/bizhang.cpp src/bizhangUAV.cpp src/spacePoint.cpp)
target_link_libraries(bizhang ${catkin_LIBRARIES})
即为主要修改的地方
4、至此,应该可以吧c++工程转移到ros环境下了,catkin_make之后,使用rosrun命令即可。
通过话题通信
这里我们从Robosense速腾得到雷达数据,cpp项目为点云处理项目
int main(int argc,char ** argv){
ros::init(argc,argv,"stairsdetection");
ros::NodeHandle nh;
ros::Subscriber subLaserCloud = nh.subscribe<sensor_msgs::PointCloud2>("/rslidar_points", 1, laserCloudHandler);
ros::MultiThreadedSpinner spinner(1); // Use x threads
spinner.spin(); // spin() will not return until the node has been shutdown
return 0;
}
其中laserCloudHandler为点云处理函数
void laserCloudHandler(const sensor_msgs::PointCloud2ConstPtr &laserCloudMsg)
{
double loadS = pcl::getTime();
//以PointXYZ点云形式处理
pcl::PointCloud<pcl::PointXYZ>::Ptr laserCloudIn(new pcl::PointCloud<pcl::PointXYZ>);
//msg中的数据传递给laserCloudIn
pcl::fromROSMsg(*laserCloudMsg, *laserCloudIn);
/***************
点云处理
***********************/
}
cmakelist
#修改项目名
cmake_minimum_required(VERSION 3.0.2)
project(stairsDetection)
##下方为cpp原项目中的cmakelist中的内容
## 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(PCL 1.7 REQUIRED)
if( NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Release CACHE STRING
"Choosing Release build type - the fastest option."
FORCE )
endif()
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES stairsDetection
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
include_directories (${PCL_INCLUDE_DIRS})
include_directories ("${PROJECT_SOURCE_DIR}/include")
link_directories (${PCL_LIBRARY_DIRS})
add_definitions (${PCL_DEFINITIONS})
#修改链接库及执行文件
add_executable (stair_det src/stairDetectionMain.cpp src/preanalysis.cpp src/regions.cpp src/segmentPatch.cpp src/regiongrowing.cpp src/voxSAC.cpp src/splitmerge.cpp src/planeshape.cpp src/Stairs.cpp src/StairVector.cpp src/recognition.cpp src/main.cpp)
target_link_libraries (stair_det ${catkin_LIBRARIES} ${PCL_LIBRARIES})
参考: