一、案例演示
本篇笔记实现用ROS2的接口输出hello world!,演示效果如下:
二、准备工作
在ROS2中所有的程序都依赖于工作空间,工作空间可以理解为存放ROS代码、编译结果、执行文件、日志的一个文件夹,创建工作空间实际上就是创建一个特殊结构的文件夹。通过如下指令创建一个工作空间:
cd ~
mkdir -p WS/ws00_helloworld/src
cd ~/WS/ws00_helloworld/src
colcon build
指令执行完毕后,ws00_helloworld会多出build、install、log文件夹,加上原来创建的src文件夹,一共有4个文件夹。
三、创建功能包
创建功能包的时候一定不要在工作空间下直接创建,一定要在src目录中创建
cd ~/WS/ws00_helloworld/src
ros2 pkg create pkg01_helloworld_cpp --build-type ament_cmake --dependencies rclcpp --node-name helloworld
输出如下:
yxc@MS:~/WS/ws00_helloworld/src$ ros2 pkg create pkg01_helloworld_cpp --build-type ament_cmake --dependencies rclcpp --node-name helloworld
going to create a new package
package name: pkg01_helloworld_cpp
destination directory: /home/yxc/WS/ws00_helloworld/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['yxc <yxc@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp']
node_name: helloworld
creating folder ./pkg01_helloworld_cpp
creating ./pkg01_helloworld_cpp/package.xml
creating source and include folder
creating folder ./pkg01_helloworld_cpp/src
creating folder ./pkg01_helloworld_cpp/include/pkg01_helloworld_cpp
creating ./pkg01_helloworld_cpp/CMakeLists.txt
creating ./pkg01_helloworld_cpp/src/helloworld.cpp
[WARNING]: Unknown license 'TODO: License declaration'. This has been set in the package.xml, but no LICENSE file has been created.
It is recommended to use one of the ament license identitifers:
Apache-2.0
BSL-1.0
BSD-2.0
BSD-2-Clause
BSD-3-Clause
GPL-3.0-only
LGPL-3.0-only
MIT
MIT-0
可以看到在src目录下创建了一个名为pkg01_helloworld_cpp的目录,其中的内容为
yxc@MS:~/WS/ws00_helloworld/src/pkg01_helloworld_cpp$ tree
.
├── CMakeLists.txt
├── include
│ └── pkg01_helloworld_cpp
├── package.xml
└── src
└── helloworld.cpp
3 directories, 3 files
四、编译
cd ~/WS/ws00_helloworld
colcon build
执行编译后,若编译成功,在屏幕上方中间会弹出一个带有绿色图标的提示,如下图:
终端输出如下:
yxc@MS:~/WS/ws00_helloworld$ colcon build
Starting >>> pkg01_helloworld_cpp
Finished <<< pkg01_helloworld_cpp [0.14s]
Summary: 1 package finished [0.31s]
并且在~/WS/ws00_helloworld/install/pkg01_helloworld_cpp/lib/pkg01_helloworld_cpp目录下,会出现一个helloworld执行文件
yxc@MS:~/WS/ws00_helloworld/install/pkg01_helloworld_cpp/lib/pkg01_helloworld_cpp$ ls
helloworld
五、执行
在执行前,要执行以下setup.bash文件,设置环境变量
source ~/WS/ws00_helloworld/install/setup.bash
ros2 run pkg01_helloworld_cpp helloworld
六、修改cpp代码
在src/pkg01_helloworld_cpp/src下的helloworld.cpp文件中做修改,修改后的内容为:
/*
需求:在终端输出hello world。
流程:
1. 包含头文件;
2. 初始化ROS2客户端;
3. 创建节点指针;
4. 输出日志;
5. 释放资源。
*/
#include "rclcpp/rclcpp.hpp"
int main(int argc, char **argv){
// 2. 初始化ROS2客户端;
rclcpp::init(argc, argv);
// 3. 创建节点指针;
auto node = rclcpp::Node::make_shared("helloworld_node_cpp");
// 4. 输出日志;
RCLCPP_INFO(node->get_logger(), "hello world!");
// 5. 释放资源。
rclcpp::shutdown();
return 0;
}
七、再次编译
修改好代码后,再次来到工作空间根目录下进行编译
cd ~/WS/ws00_helloworld
colcon build
编译成功后终端输出如下:
yxc@MS:~/WS/ws00_helloworld$ colcon build
Starting >>> pkg01_helloworld_cpp
Finished <<< pkg01_helloworld_cpp [2.23s]
Summary: 1 package finished [2.39s]
八、再次运行
前面已经进行过环境变量配置,因此现在可以直接运行
ros2 run pkg01_helloworld_cpp helloworld