Skip to content

ROS2 安装与基本使用#

基于 Ubuntu 22.04 + ROS2 Humble

安装#

换清华源#

参考:https://mirrors.tuna.tsinghua.edu.cn/help/ros2/

# Ubuntu 22.04 (jammy) + ROS2 Humble
sudo apt update && sudo apt install curl gnupg lsb-release
sudo curl -sSL https://mirrors.tuna.tsinghua.edu.cn/ros2/ros2.gpg | sudo tee /etc/apt/trusted.gpg.d/ros2.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/trusted.gpg.d/ros2.gpg] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ros2.list

sudo apt update
sudo apt install ros-humble-desktop
# 或 minimal 版:ros-humble-ros-base

环境初始化#

# 每次使用前 source
source /opt/ros/humble/setup.bash

# 持久化到 ~/.bashrc
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc

基础使用#

rqt 工具#

# 启动 turtlesim 演示
ros2 run turtlesim turtlesim_node
# 另一个终端
ros2 run turtlesim turtle_teleop_key

# 图形化显示节点关系
rqt_graph

# rqt 综合工具
rqt

查看系统状态#

ros2 node list      # 查看节点
ros2 topic list    # 查看话题
ros2 service list  # 查看服务
ros2 action list   # 查看动作

rosbag 录制与回放#

# 录制
ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose

# 查看信息
ros2 bag info subset

# 回放
ros2 bag play subset

创建项目#

# 创建 Python 包
ros2 pkg create --build-type ament_python py_pubsub

# 添加发布者代码
# 参考: https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html

# 运行
cd py_pubsub/py_pubsub
python3 publisher_member_function.py

参考#