【ROS2系列】 02 ROS2 HelloWorld

一、案例演示

本篇笔记实现用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
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值