# ros2_tutorials **Repository Path**: heguangchuan/ros2_tutorials ## Basic Information - **Project Name**: ros2_tutorials - **Description**: 《ROS2入门21讲 · 古月》课程代码 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: https://class.guyuehome.com - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 107 - **Created**: 2025-06-04 - **Last Updated**: 2025-06-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README - 课程视频:[https://class.guyuehome.com/detail/p_628f4288e4b01c509ab5bc7a/6](https://class.guyuehome.com/detail/p_628f4288e4b01c509ab5bc7a/6) - 图文教程:[https://book.guyuehome.com/](https://book.guyuehome.com/) - 课程问答:[https://www.guyuehome.com/Bubble/circleDetail/id/90/](https://www.guyuehome.com/Bubble/circleDetail/id/90/) - 博客泡圈:[https://www.guyuehome.com/](https://www.guyuehome.com/) ![课程大纲](docs/课程大纲.png) ### 终端设置 ```shell cmd.exe /k ""E:\miniconda\miniconda3\Scripts\activate.bat" "E:\miniconda\envs\ros2"" ``` ### 环境信息 ```shell ros2@ros2-virtual-machine:~$ cat /etc/os-release PRETTY_NAME="Ubuntu 22.04.5 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.5 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy ``` ```shell ros2@ros2-virtual-machine:~$ uname -a Linux ros2-virtual-machine 6.8.0-59-generic #61~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 15 17:03:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux ``` ### 设置编码 ```shell sudo cp /etc/apt/sources.list /etc/apt/sources_default.list sudo sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list sudo apt update && sudo apt install locales sudo locale-gen en_US en_US.UTF-8 sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 export LANG=en_US.UTF-8 ``` ### 添加源 ```shell sudo apt update && sudo apt install curl gnupg2 lsb-release sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null ``` 如果遇到 https://raw.githubusercontent.com 访问不了,请使用 https://raw.gitmirror.com/ros/rosdistro/master/ros.key 替换 ### 安装Ros2 ```shell sudo apt update sudo apt upgrade sudo apt install ros-humble-desktop ``` 文件将会被安装到 /opt/ros/humble/ 目录 ### 设置环境变量 ```shell source /opt/ros/humble/setup.bash echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc ``` ### 测试Ros2 ```shell ros2 run demo_nodes_cpp talker ros2 run demo_nodes_py listener ros2 run turtlesim turtlesim_node ros2 run turtlesim turtle_teleop_key ``` ### 工作空间与功能包 工作空间是一个代码大本营,存放项目相关的开发文件夹,包含: - src:存放功能包,是代码空间 - build:存放编译后的文件 - install:存放安装后的文件 - log:存放日志文件 ```shell sudo apt install python3-pip sudo pip3 install rosdepc heguangchuan@heguangchuan-virtual-machine:~/workspace/src$ cd ~/workspace/code/src/ heguangchuan@heguangchuan-virtual-machine:~/workspace/src$ sudo rosdepc init heguangchuan@heguangchuan-virtual-machine:~/workspace/src$ sudo rosdepc update heguangchuan@heguangchuan-virtual-machine:~/workspace/src$ cd .. heguangchuan@heguangchuan-virtual-machine:~/workspace$ colcon build ``` 如果没有安装 colcon 请使用 sudo apt install python3-colcon-common-extensions 安装,执行完成后在工作空间的根目录下会生成如下的结构: ```shell heguangchuan@heguangchuan-virtual-machine:~/workspace/code$ tree . ├── build │ └── COLCON_IGNORE ├── install │ ├── COLCON_IGNORE │ ├── local_setup.bash │ ├── local_setup.ps1 │ ├── local_setup.sh │ ├── _local_setup_util_ps1.py │ ├── _local_setup_util_sh.py │ ├── local_setup.zsh │ ├── setup.bash │ ├── setup.ps1 │ ├── setup.sh │ └── setup.zsh ├── log │ ├── build_2025-06-04_09-30-23 │ │ ├── events.log │ │ └── logger_all.log │ ├── COLCON_IGNORE │ ├── latest -> latest_build │ └── latest_build -> build_2025-06-04_09-30-23 └── src ``` 为了在执行 ros2 run 命令时方便的找到功能包,请将工作空间(通过 colcon build 命令生成的 install 目录)添加到环境变量中: ```shell source /home/heguangchuan/workspace/code/install/local_setup.bash echo "source /home/heguangchuan/workspace/code/install/local_setup.bash" >> ~/.bashrc source ~/.bashrc ``` ### 创建功能包 功能包是 ROS2 项目的基本单元,功能包的创建在工作空间下的 src 目录下进行,创建的语法如下: ```shell ros2 pkg create --build-type ament_cmake my_package ros2 pkg create --build-type ament_python my_package ``` ament_cmake 代表的是使用cmake进行编译,ament_python 代表的是使用python进行编译,my_package是功能包的名称,创建完成后会在工作空间下的,src 目录下生成如下结构: ```shell heguangchuan@heguangchuan-virtual-machine:~/workspace/code$ cd src/ heguangchuan@heguangchuan-virtual-machine:~/workspace/code/src$ ros2 pkg create --build-type ament_python first_pkg going to create a new package package name: first_pkg destination directory: /home/heguangchuan/workspace/code/src package format: 3 version: 0.0.0 description: TODO: Package description maintainer: ['heguangchuan '] licenses: ['TODO: License declaration'] build type: ament_python dependencies: [] creating folder ./first_pkg creating ./first_pkg/package.xml creating source folder creating folder ./first_pkg/first_pkg creating ./first_pkg/setup.py creating ./first_pkg/setup.cfg creating folder ./first_pkg/resource creating ./first_pkg/resource/first_pkg creating ./first_pkg/first_pkg/__init__.py creating folder ./first_pkg/test creating ./first_pkg/test/test_copyright.py creating ./first_pkg/test/test_flake8.py creating ./first_pkg/test/test_pep257.py [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 heguangchuan@heguangchuan-virtual-machine:~/workspace/code/src$ tree first_pkg/ first_pkg/ ├── first_pkg │ └── __init__.py ├── package.xml ├── resource │ └── first_pkg ├── setup.cfg ├── setup.py └── test ├── test_copyright.py ├── test_flake8.py └── test_pep257.py 3 directories, 8 files ``` ### 节点 节点是机器人的工作细胞,节点具有以下的特点: 1. 执行具体任务的进程 2. 独立可运行的可执行文件 3. 可以使用不同的编程语言 4. 可以分布式的部署在不同的主机 5. 通过节点名称进行管理 ### 执行节点 以本项目中 [node_helloworld.py](learning_node/learning_node/node_helloworld.py) 为例,需要先(colcon build)执行节点 ```shell heguangchuan@heguangchuan-virtual-machine:~/workspace/code/src/ros2_tutorials/learning_node$ ros2 run learning_node node_helloworld [INFO] [1749006630.190521303] [node_helloworld]: Hello World [INFO] [1749006630.693644108] [node_helloworld]: Hello World [INFO] [1749006631.194889501] [node_helloworld]: Hello World [INFO] [1749006631.697436672] [node_helloworld]: Hello World [INFO] [1749006632.199129959] [node_helloworld]: Hello World ``` 可以看到程序在每隔1秒打印一次Hello World,其代码具体如下: ```python import rclpy # ROS2 Python接口库 from rclpy.node import Node # ROS2 节点类 import time def main(args=None): # ROS2节点主入口main函数 rclpy.init(args=args) # ROS2 Python接口初始化 node = Node("node_helloworld") # 创建ROS2节点对象并进行初始化 while rclpy.ok(): # ROS2系统是否正常运行 node.get_logger().info("Hello World") # ROS2日志输出 time.sleep(0.5) # 休眠控制循环时间 node.destroy_node() # 销毁节点对象 rclpy.shutdown() # 关闭ROS2 Python接口 ``` 对于更适合面向对象的开发习惯,代码改进如下: ```python import rclpy # ROS2 Python接口库 from rclpy.node import Node # ROS2 节点类 import time """ 创建一个HelloWorld节点, 初始化时输出“hello world”日志 """ class HelloWorldNode(Node): def __init__(self, name): super().__init__(name) # ROS2节点父类初始化 while rclpy.ok(): # ROS2系统是否正常运行 self.get_logger().info("Hello World") # ROS2日志输出 time.sleep(0.5) # 休眠控制循环时间 def main(args=None): # ROS2节点主入口main函数 rclpy.init(args=args) # ROS2 Python接口初始化 node = HelloWorldNode("node_helloworld_class") # 创建ROS2节点对象并进行初始化 node.destroy_node() # 销毁节点对象 rclpy.shutdown() # 关闭ROS2 Python接口 ``` ### 话题 话题是ROS2中用于数据传输的机制,ROS2中提供了很多话题,具体代码参考 [topic_helloworld_pub.py](learning_topic/learning_topic/topic_helloworld_pub.py) 和 [topic_helloworld_sub.py](learning_topic/learning_topic/topic_helloworld_sub.py)