简介:如何建立ros2的工作空间和功能包。
1. 创建新的工作空间
mkdir -p ~/dev_ws/src
cd ~/dev_ws/src
2. 新建功能包
ros2 pkg create --build-type ament_cmake cpp_header
功能包名:cpp_header
编译类型:build-type
编译工具:ament_cmake
3. 新建节点头文件
//minimal_publisher.hpp
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "rclcpp/rclcpp.hpp" //包含rclcpp.hpp
#include "std_msgs/msg/string.hpp" //包含string.hpp
//上面两个外部包的使用,除包含外,还需要在配置包依赖
using namespace std::chrono_literals;
class MinimalPublisher : public rclcpp::Node //类MinimalPublisher声明
{
public:
MinimalPublisher();
private:
void timer_callback();
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};
4. 头文件中类的定义
//minimal_publisher.cpp
#include "cpp_header/minimal_publisher.hpp"
MinimalPublisher::MinimalPublisher () //类MinimalPublisher定义
: Node("minimal_publisher"), count_(0) //初始化计数器
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10); //构建发布器
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}//定时绑定回调函数
void MinimalPublisher::timer_callback()//回调函数
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);//发布消息
}
5. 新建ROS发布节点
//minimal_publisher_node.cpp
#include "cpp_header/minimal_publisher.hpp"
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
6. 修改 package.xml文件
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cpp_header</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="wanghq2020@yeah.net">wanghq2023</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
#添加依赖包rclcpp、std_msgs
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
7. 修改配置文件 CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(cpp_header)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
#发现依赖包rclcpp、std_msgs
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
include_directories(include)
#生成talker,两个源文件在src中
add_executable(talker src/minimal_publisher_node.cpp src/minimal_publisher.cpp)
#添加目标依赖
ament_target_dependencies(talker rclcpp std_msgs)
#添加目标talker的安装路径
install(TARGETS
talker
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
8. 安装相关依赖
cd ~/dev_ws/
rosdep install -i --from-path src --rosdistro humble -y
参数rosdistro 根据自己的ROS版本设置,这里是humble。
9. 编译功能包
cd ~/dev_ws
~/dev_ws$ colcon build --symlink-install --packages-select cpp_header
在编译时,使用参数–packages-select,只编译特定的功能包
编译命令:colcon build,不再是ROS的catkin_make。
10. 环境设置
~/dev_ws$ source ./install/setup.bash
11. VS code 中包含路径设置
在使用code时,如果包含路径设置不完整,常常提示找不到源函数。
设置方式:修改c_cpp_properties.json文件中的 includePATH标签。这里是:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/ros/humble/include/**",
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
- 节点运行
~/dev_ws$ ros2 run cpp_header talker
[INFO] [1682586792.656552787] [minimal_publisher]: Publishing: 'Hello, world! 0'
[INFO] [1682586793.156567447] [minimal_publisher]: Publishing: 'Hello, world! 1'
[INFO] [1682586793.656552445] [minimal_publisher]: Publishing: 'Hello, world! 2'
参考文献:
【1】ROS2与C++入门教程-新建ros2包