1 ROS2介绍与安装
- 1.1 Ubuntu配置与ROS2安装
- 1.1.1 Ubuntu22.04安装
- 1.1.2 下载安装ROS2
- 1.1.3 配置ROS2环境并测试
- 1.2 使用VSCode搭建ROS2开发环境
- 1.2.1 安装并配置VSCode
- 1.2.2 创建ROS2工程的方法
- 1.2.3 使用VSCode创建ROS2的C/C++项目
- 1.2.4 使用VSCode创建ROS2的Python项目
- 1.3 ROS2的基本体系
- 1.3.1 ROS2架构
- 1.3.2 ROS2和ROS1的区别
- 1.3.3 ROS2应用方向
- 1.4 总结和参考内容
- 1.4.1 总结
- 1.4.2 参考内容
- 其他ROS2学习笔记: ROS2学习笔记
- 代码仓库:Github连接地址
- 欢迎各位互相学习交流
1.1 Ubuntu配置与ROS2安装
1.1.1 Ubuntu22.04安装
- 下载Ubuntu22.04:本文选择 中科大镜像网 ,选择Ubuntu22.04镜像下载,后续放入VMware进行 断网安装
VMware15链接:
链接:https://pan.baidu.com/s/1BbaGtDhjVXCWeS2vuk3bRw
提取码:7lzk 复制这段内容后打开百度网盘手机App,操作更方便哦
- Ubuntu22.04 换源:将桌面版本的Ubuntu的软件更新设置来源为国内的镜像,或者直接输入命令
sudo gedit /etc/apt/sources.list
将内容改为如下所示:
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
1.1.2 下载安装ROS2
- 输入命令
sudo apt-get update -y
对软件源进行更新,输入sudo apt-get upgrade -y
对软件进行更新:
sudo apt-get update -y && sudo apt-get upgrade -y
- 获取ROS2的秘钥,并ROS2的存储库添加到源列表:
sudo apt install curl gnupg 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 $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
- 如果出现关于
raw.githubusercontent.com
连接失败的处理
-
- 访问
https://tool.lu/ip/
并输入域名raw.githubusercontent.com
,查询 ip 地址,这里查询到的是185.199.108.133
- 访问
-
- 修改
sudo gedit /etc/hosts
文件,并手动添加DNS解析:
- 修改
- 下载ROS2-humble:更新软件到最新版本,然后下载ROS2-humble:
sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt install ros-humble-desktop
- 安装colcon构建工具: 不同于之前ROS1的catkin工具,ROS2用colcon进行包的构建:
sudo apt install python3-colcon-common-extensions
1.1.3 配置ROS2环境并测试
-
配置ROS2环境: 默认ROS2-humble安装在
/opt/ros/humble/
下,将ROS2的环境添加到用户环境文件~/.bashrc
中,输入echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
-
测试ROS2:
ros2 run turtlesim turtlesim_node
和ros2 run turtlesim turtle_teleop_key
运行小乌龟
1.2 使用VSCode搭建ROS2开发环境
1.2.1 安装并配置VSCode
-
安装VSCode: VSCode下载地址,持续下一步即可安装完成。
-
下载完成后安装VSCode插件:插件主要包括
C/C++
和Python
以及CMake
的插件,如下图所示:
1.2.2 创建ROS2工程的方法
- ROS2 create命令:通过
ros2 pkg create --help
熟悉创建ROS2项目的方法
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter$ ros2 pkg create --help
usage: ros2 pkg create [-h] [--package-format {2,3}] [--description DESCRIPTION] [--license LICENSE]
[--destination-directory DESTINATION_DIRECTORY] [--build-type {cmake,ament_cmake,ament_python}]
[--dependencies DEPENDENCIES [DEPENDENCIES ...]] [--maintainer-email MAINTAINER_EMAIL]
[--maintainer-name MAINTAINER_NAME] [--node-name NODE_NAME] [--library-name LIBRARY_NAME]
package_name
Create a new ROS 2 package
positional arguments:
package_name The package name
options:
-h, --help show this help message and exit
--package-format {2,3}, --package_format {2,3}
The package.xml format.
--description DESCRIPTION
The description given in the package.xml
--license LICENSE The license attached to this package; this can be an arbitrary string, but a LICENSE file will only
be generated if it is one of the supported licenses (pass '?' to get a list)
--destination-directory DESTINATION_DIRECTORY
Directory where to create the package directory
--build-type {cmake,ament_cmake,ament_python}
The build type to process the package with
--dependencies DEPENDENCIES [DEPENDENCIES ...]
list of dependencies
--maintainer-email MAINTAINER_EMAIL
email address of the maintainer of this package
--maintainer-name MAINTAINER_NAME
name of the maintainer of this package
--node-name NODE_NAME
name of the empty executable
--library-name LIBRARY_NAME
name of the empty library
下面对上诉的命令进行简单的介绍:
提示当中的
[]
的内容表示命令关键字,{}
的内容表示可以携带的参数示例,例如创建--build-type
后面就可以接着ament_cmake
参数
[--destination-directory DESTINATION_DIRECTORY]
:ROS2的项目的位置,即在哪里创建你的ROS2项目
[--build-type {cmake,ament_cmake,ament_python}]
:ROS2编译项目的方式,有cmake
ament_camke
和ament_python
三种可选,ament_cmake
是基于cmake的一个cmake升级工具, 了解更多的ROS2 ament工具:(https://blog.csdn.net/gongdiwudu/article/details/126192244)
[--dependencies]
:ROS2项目的依赖项,这部分内容后面可以手动增加,其中常见的rclpy 是 python
的节点依赖项,rclcpp是C/C++节点的依赖项
,(rcl
表示ros2 client
)
[--node-name]
:预先设置的ROS2节点名字
- 因此可以使用命令:
ros2 pkg create <你的项目名字> --build-type <选择cmake/ament_camke/ament_python三者之一作为项目的编译工具> --node-name <节点名称,这一项可以不写,后续手动配置> --dependencies <依赖项名字,例如在ROS1中常用的rclpy rclcpp std_msgs sensor_msgs等>
,如:
ros2 pkg create demo --build-type ament_cmake --node-name demo_node --dependencies rclcpp
1.2.3 使用VSCode创建ROS2的C/C++项目
-
创建ROS2 C++工程:
ros2 pkg create vscodeCppDemo --build-type ament_cmake --node-name vscodeCppDemoNode --dependencies rclcpp std_msgs
-
查看工程目录结构:
tree .
,可以发现在src
目录下 存在了节点名称的cpp文件 ,即ROS2项目已经构建了配置好节点内容的工程
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ros2 pkg create vscodeCppDemo --build-type ament_cmake --node-name vscodeCppDemoNode --dependencies rclcpp std_msgs
going to create a new package
package name: vscodeCppDemo
destination directory: /mnt/hgfs/VMware/ROS2_DEMO/1_Chapter/code
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['pldz <pldz@R7000.com>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp', 'std_msgs']
node_name: vscodeCppDemoNode
creating folder ./vscodeCppDemo
creating ./vscodeCppDemo/package.xml
creating source and include folder
creating folder ./vscodeCppDemo/src
creating folder ./vscodeCppDemo/include/vscodeCppDemo
creating ./vscodeCppDemo/CMakeLists.txt
creating ./vscodeCppDemo/src/vscodeCppDemoNode.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
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ tree .
.
└── vscodeCppDemo
├── CMakeLists.txt
├── include
│ └── vscodeCppDemo
├── package.xml
└── src
└── vscodeCppDemoNode.cpp
4 directories, 3 files
- 在
<ROS2工程目录下>/src/<节点cpp>.cpp
进行节点编程,ROS2是C++11的编程风格:
#include "rclcpp/rclcpp.hpp"
// 继承ROS2的客户端节点
class VSCodeCppDemo: public rclcpp::Node{
public:
// ROS2节点的构造函数
VSCodeCppDemo(const char* nodeName):Node(nodeName){
RCLCPP_INFO(this->get_logger(),"hello world!");
}
};
int main(int argc, char *argv[])
{
rclcpp::init(argc,argv);
// 生成自定义的ROS2客户端对象, node是一个对象指针
auto node = std::make_shared<VSCodeCppDemo>("vscodeCppDemoNode");
rclcpp::shutdown();
return 0;
}
- 针对VSCode的
settings.json
进行配置:在项目下面新建.vscode
文件夹,然后在在该文件夹下新建settings.json
, 添加下列内容:
{
// Settings.json的配置,主要在于添加/opt/ros/humble文件夹下的ros2的include路径
// pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code/.vscode$ tree .
// .
// └── settings.json
// 0 directories, 1 file
"files.associations": {
"cstdio": "cpp"
},
"C_Cpp.default.includePath": ["/opt/ros/humble/include/**"]
}
- 配置ROS2项目的
package.xml
文件:package.xml
文件是ROS2项目的功能包管理文件,下面添加了一点注释:
<?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>vscodeCppDemo</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<!-- 包的作者信息,主要来自Git的配置 -->
<maintainer email="pldz@R7000.com">pldz</maintainer>
<license>TODO: License declaration</license>
<!-- 编译工程的工具 -->
<buildtool_depend>ament_cmake</buildtool_depend>
<!-- 编译需要的依赖项,可以手动添加 -->
<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>
- 配置
CMakeLists.txt
文件:CMakeLists.txt
是ROS2项目的编译配置文件,下面添加了一点注释:
cmake_minimum_required(VERSION 3.8)
project(vscodeCppDemo)
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)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
# 节点(也叫可执行文件)的映射
add_executable(vscodeCppDemoNode src/vscodeCppDemoNode.cpp)
# Include文件的位置
target_include_directories(vscodeCppDemoNode PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
# 目标依赖库
target_compile_features(vscodeCppDemoNode PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(
vscodeCppDemoNode
"rclcpp"
"std_msgs"
)
# 安装规则
install(TARGETS vscodeCppDemoNode
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()
- 利用
colcon
工具构建项目:在项目的工程目录下,输入colcon build
进行构建
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ colcon build
WARNING: Package name "vscodeCppDemo" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes.
Starting >>> vscodeCppDemo
--- stderr: vscodeCppDemo
WARNING: Package name "vscodeCppDemo" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes.
---
Finished <<< vscodeCppDemo [8.27s]
Summary: 1 package finished [8.62s]
1 package had stderr output: vscodeCppDemo
- 执行该项目节点文件:
-
- 尝试手动执行,在项目目录下的
build/<ROS2项目名称>
下存放着项目节点ROS2项目节点
,直接运行该文件:如下所示
- 尝试手动执行,在项目目录下的
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ./build/vscodeCppDemo/vscodeCppDemoNode
[INFO] [1682264164.879460585] [helloworld_node]: hello world!
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ file ./build/vscodeCppDemo/vscodeCppDemoNode
./build/vscodeCppDemo/vscodeCppDemoNode: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=53b5061fab0864a3b53f587b0cbc8a6be1d342f1, for GNU/Linux 3.2.0, not stripped
-
- 通过ROS2运行节点:首先需要添加项目的install环境,即
source <项目目录>/install/setup.bash
,然后可以通过ros2 run <项目名称> <节点名称>
运行节点
- 通过ROS2运行节点:首先需要添加项目的install环境,即
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ source ./install/setup.bash
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ros2 run vscodeCppDemo vscodeCppDemoNode
[INFO] [1682264459.635230302] [helloworld_node]: hello world!
- ROS2的C/C++项目的目录结构:在项目目录下输入
tree -L 3
查看最多三级文件结构,其中:
-
build
文件夹:存储编译的文件和可执行的ROS2节点
-
install
文件夹:直接反应叫安装目录,包括能够通过ros2 run ...
指令运行ROS2节点的环境
-
log
文件夹:存储日志文件
-
<项目名的文件夹>
:存放ROS2项目的源码,其中的package.xml
配置包信息(包名、版本、作者、依赖项);CMakeLists.txt
用于配置编译规则(源文件位置,编译所要连接的依赖项等等);
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ tree -L 3
.
├── build
│ ├── COLCON_IGNORE
│ └── vscodeCppDemo
│ ├── ament_cmake_core
│ ├── ament_cmake_environment_hooks
│ ├── ... 这里手动省略
│ ├── Makefile
│ └── vscodeCppDemoNode
├── install
│ ├── COLCON_IGNORE
│ ├── local_setup.bash
│ ├── ... 这里手动省略
│ ├── setup.bash
│ └── vscodeCppDemo
│ ├── lib
│ └── share
├── log
│ ├── build_2023-04-23_23-31-44
│ │ ├── events.log
│ │ ├── logger_all.log
│ │ └── vscodeCppDemo
│ └── COLCON_IGNORE
└── vscodeCppDemo
├── CMakeLists.txt
├── include
│ └── vscodeCppDemo
├── package.xml
└── src
└── vscodeCppDemoNode.cpp
23 directories, 30 files
1.2.4 使用VSCode创建ROS2的Python项目
- 创建ROS2的Python项目:
ros2 pkg create vscodePythonDemo --build-type ament_python --dependencies rclpy std_msgs --node-name vscodePythonDemoNode
,其中依赖项与C/C++的节点不同是rclpy
,此时在项目的同名目录下,已经有了节点.py
文件
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ros2 pkg create vscodePythonDemo --build-type ament_python --dependencies rclpy std_msgs --node-name vscodePythonDemoNode
going to create a new package
package name: vscodePythonDemo
destination directory: /mnt/hgfs/VMware/ROS2_DEMO/1_Chapter/code
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['pldz <pldz@R7000.com>']
licenses: ['TODO: License declaration']
build type: ament_python
dependencies: ['rclpy', 'std_msgs']
node_name: vscodePythonDemoNode
creating folder ./vscodePythonDemo
creating ./vscodePythonDemo/package.xml
creating source folder
creating folder ./vscodePythonDemo/vscodePythonDemo
creating ./vscodePythonDemo/setup.py
creating ./vscodePythonDemo/setup.cfg
creating folder ./vscodePythonDemo/resource
creating ./vscodePythonDemo/resource/vscodePythonDemo
creating ./vscodePythonDemo/vscodePythonDemo/__init__.py
creating folder ./vscodePythonDemo/test
creating ./vscodePythonDemo/test/test_copyright.py
creating ./vscodePythonDemo/test/test_flake8.py
creating ./vscodePythonDemo/test/test_pep257.py
creating ./vscodePythonDemo/vscodePythonDemo/vscodePythonDemoNode.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
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ cd vscodePythonDemo/
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code/vscodePythonDemo$ tree .
.
├── package.xml
├── resource
│ └── vscodePythonDemo
├── setup.cfg
├── setup.py
├── test
│ ├── test_copyright.py
│ ├── test_flake8.py
│ └── test_pep257.py
└── vscodePythonDemo
├── __init__.py
└── vscodePythonDemoNode.py
3 directories, 9 files
- 创建一个简单的ROS2的Python节点:代码如下,基本内容见注释
import rclpy
from rclpy.node import Node
# 继承Node类,定义VSCodePythonDemo类
class VSCodePythonDemo(Node):
# 初始化Python构造函数
def __init__(self, nodeName:str):
super().__init__(nodeName)
def printHello(self):
self.get_logger().info("hello world!")
def main():
rclpy.init()
# 创建VSCodePythonDemo对象
node = VSCodePythonDemo("vscodePythonDemo")
# 调用成员函数
node.printHello()
rclpy.shutdown()
- Python的VSCode环境配置:默认情况下VSCode的Python解析器,能够定位到
rclpy.py
的位置在/opt/ros/humble/local/lib/python3.10/dist-packages/
下,如果无法找到,可以手动配置.vscode
文件夹下的settings.json
文件,加入"python.analysis.extraPaths": ["/opt/ros/humble/local/lib/python3.10/dist-packages/"],
,这样的配置同样能够将自己安装的的Python依赖项加入到vscode的开发环境中
- ROS2的Python项目的简单配置:与C/C++项目不同,Python项目主要配置
packages.xml
文件来管理依赖项和Python包,setup.py
主要给Python项目进行打包配置
-
packages.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>vscodePythonDemo</name>
<version>0.0.0</version>
<!-- 包的作者信息,主要来自Git的配置 -->
<description>TODO: Package description</description>
<maintainer email="pldz@R7000.com">pldz</maintainer>
<license>TODO: License declaration</license>
<!-- 编译需要的依赖项,可以手动添加 -->
<depend>rclpy</depend>
<depend>std_msgs</depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
-
setup.py
文件的简单注释:
from setuptools import setup
# 项目包名
package_name = 'vscodePythonDemo'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
# 依赖的打包工具
install_requires=['setuptools'],
zip_safe=True,
# 项目用户信息
maintainer='pldz',
maintainer_email='pldz@R7000.com',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
# 可执行文件的入口
'vscodePythonDemoNode = vscodePythonDemo.vscodePythonDemoNode:main'
],
},
)
- 编译ROS2的Python项目:在项目目录下,输入:
colcon build
,利用ament_python
工具构建Python项目
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ colcon build
WARNING: Package name "vscodeCppDemo" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes.
WARNING: Package name "vscodePythonDemo" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes.
Starting >>> vscodeCppDemo
Starting >>> vscodePythonDemo
--- stderr: vscodePythonDemo
/usr/lib/python3/dist-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
---
Finished <<< vscodePythonDemo [1.41s]
Finished <<< vscodeCppDemo [1.79s]
Summary: 2 packages finished [2.10s]
1 package had stderr output: vscodePythonDemo
- 运行ROS2的Python项目:
.py
文件本身就是一个可执行的脚本,如果Python解析器的环境依赖都能够被找到的话,可以直接输入python3 xx.py
运行文件,同样通过激活./install/setup.bash
文件,利用ROS2运行Python节点
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ python3 ./build/vscodePythonDemo/build/lib/vscodePythonDemo/vscodePythonDemoNode.py
[INFO] [1682341866.812861345] [helloworld_py_node]: hello world!
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ python3 ./vscodePythonDemo/vscodePythonDemo/vscodePythonDemoNode.py
[INFO] [1682341890.895240693] [helloworld_py_node]: hello world!
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ros2 run vscodePythonDemo/ --prefix
build/ install/ log/ .vscode/ vscodeCppDemo/ vscodePythonDemo/
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ source ./install/setup.bash
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ros2 run vscodePythonDemo
--prefix vscodePythonDemoNode
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ ros2 run vscodePythonDemo vscodePythonDemoNode
[INFO] [1682341946.345360821] [helloworld_py_node]: hello world!
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$
- ROS2的Python项目的简单文件结构:在项目文件夹下输入
tree -L 3
查看项目三级目录;其中的setup.cfg是功能包基本配置文件
,它的作用会影响项目.build
文件夹下对Python项目的配置。
pldz@pldz-pc:~/share/ROS2_DEMO/1_Chapter/code$ tree -L 3
.
├── build
│ ├── COLCON_IGNORE
│ ├── vscodeCppDemo
| ... 手动删除了C/C++项目的内容
└── vscodePythonDemo
├── package.xml
├── resource
│ └── vscodePythonDemo
├── setup.cfg
├── setup.py
├── test
│ ├── test_copyright.py
│ ├── test_flake8.py
│ └── test_pep257.py
└── vscodePythonDemo
├── __init__.py
└── vscodePythonDemoNode.py
1.3 ROS2的基本体系
1.3.1 ROS2架构
ROS2的介绍中,有这么一张图,习惯划分说ROS2分为三层来源于B站UP:
-
操作系统层(OS Layer):如前所述,ROS虽然称之为机器人操作系统,但实质只是构建机器人应用程序的软件开发工具包,ROS必须依赖于传统意义的操作系统,目前ROS2可以运行在Linux、Windows、Mac或RTOS上。
-
中间层(Middleware Layer):主要由数据分发服务DDS与ROS2封装的关于机器人开发的中间件组成。DDS是一种去中心化的数据通讯方式,ROS2还引入了服务质量管理 (Quality of Service)机制,借助该机制可以保证在某些较差网络环境下也可以具备良好的通讯效果。ROS2中间件则主要由客户端库、DDS抽象层与进程内通讯API构成。
-
应用层(Application Layer):是指开发者构建的应用程序,在应用程序中是以功能包为核心的,在功能包中可以包含源码、数据定义、接口等内容。
1.3.2 ROS2和ROS1的区别
老生常谈的一个话题了,总结来说:
-
协议不一样:ROS1用的是TCP和UDP协议,而ROS2用DDS协议;DDS是ROS2的一个很重要的概念,DDS是专用总线协议,速度更快更可靠;
-
架构不一样,ROS1通过ROS Master管理节点,而ROS2是分布式通讯架构,不需要ROS Master
-
ROS2支持的平台更多,能够支撑嵌入式开发板,实时操作系统RTOS等平台
1.3.3 ROS2应用方向
-
Nav2项目:Nav2项目继承自ROS Navigation Stack。该项目旨在可以让移动机器人从A点安全的移动到B点。它也可以应用于涉及机器人导航的其他应用,例如跟随动态点。Nav2将用于实现路径规划、运动控制、动态避障和恢复行为等一系列功能
-
microROS:在基于ROS的机器人应用中,micro-ROS正在弥合性能有限的微控制器和一般处理器之间的差距。micro-ROS在各种嵌入式硬件上运行,使ROS能直接应用于机器人硬件
1.4 总结和参考内容
1.4.1 总结
- 创建ROS2项目的流程:
- 创建功能包:
ros2 pkg create <项目名> --build-type <cmake/ament_camke/ament_python> --node-name <节点名> --dependencies <rclpy/rclcpp ...>
- 编辑源文件:采用继承
rcl
节点发方式,创建ROS2节点
- 编辑配置文件: C/C++ 配置
packages.xml
和CMakeLists.txt
分别进行依赖项管理和编译配置,Python项目配置packages.xml
和setup.py
分别进行依赖项和编译安装配置
- 编译:安装
colcon
工具,sudo apt install python3-colcon-common-extensions
,并且在项目目录下,进行colcon build
- 执行:激活项目环境
source <项目>/install/setup.bash
,然后输入ros2 run <package> <node>
1.4.2 参考内容
-
- 【ROS2原理3】:构建系统“ament_cmake”和构建工具“ament_tools”
-
- ROS2学习笔记(五)-- ROS2命令行操作常用指令总结(一)
-
- Developing a ROS 2 package
-
- C++日志(三十一)类继承时的构造函数
-
- ROS2 API
-
- ROS2中文网
-
- ROS2.0整体架构说明
-
- ROS 2 Design