欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > ROS第四梯:ROS项目中添加自定义类

ROS第四梯:ROS项目中添加自定义类

2025/6/16 22:46:28 来源:https://blog.csdn.net/qq625924821/article/details/142367751  浏览:    关键词:ROS第四梯:ROS项目中添加自定义类

第一步,ROS项目结构介绍

工作空间中包含一个名为pcl_ros_test的功能包,其中main.cpp是原有项目自带的,接下来以CommonAlg自定义类添加为例进行介绍。

第二步:头文件CommonAlg.h创建和编写,并保存在include/pcl_ros_test下:

//系统
#include <string>#ifndef COMMON_H
#define COMMON_Hnamespace test
{
    class CommonAlg
    {
        public:
        /* 
        @brief: 判断文件是否存在
        @param[in]: 文件绝对路径
        @return: 存在为True,否则为False
        */
        static bool existFile(const std::string& filePath);
    };
};
#endif //COMMON_H

第三步,源文件CommonAlg.cpp创建和编写,并保存在src下:

#include "pcl_ros_test/CommonAlg.h"//系统
#include <string.h> 
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <unistd.h>
#include <sys/stat.h>   // 注意:这个头文件在sys下面
#include <cstdio>
#include <dirent.h>/* 
@brief: 判断文件是否存在
@param[in]: 文件绝对路径
@return: 存在为True,否则为False
*/
bool CommonAlg::existFile(const std::string& filePath)
{
    std::ifstream infile(filePath);
    if(infile.is_open())
    {
        infile.close();
        return true;
    }
    else
    {
        infile.close();
        return false;
    }   
}

第四步,环境配置

功能包下的CMakeLists.txt文件修改:

①:

// 配置catkin编译器的编译规则  
catkin_package(  
 INCLUDE_DIRS include  
#  LIBRARIES test  
 CATKIN_DEPENDS roscpp std_msgs // 加载链接所需的共享库  
#  DEPENDS system_lib  
)

②:

//自定义包含路径
include_directories(include${catkin_INCLUDE_DIRS}
)

③:

//将自定义文件添加到可执行文件路径下
add_executable(main_node src/main.cpp include/pcl_ros_test/CommonAlg.h src/CommonAlg.cpp)
target_link_libraries(main_node ${catkin_LIBRARIES})

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词