1 ROS的安装及其配置
先行安装 ubuntu22.04
1.1 UTF-8 环境检查
请先检查本地语言环境是否支持 UTF-8 编码(中文环境下一般都是支持的)
locale
如果不支持,则进行安装
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
1.2 启动 Ubuntu universe 存储库
打开软件和更新,重要的是勾选社区维护的自由和开源软件
PS:可以把显卡驱动也装上
1.3 设置软件源
先将 ROS 2 apt 存储库添加到系统,用 apt 授权我们的 GPG 密钥:
sudo apt update && 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
授权密钥失败
点击链接 https://www.ipaddress.com/
输入域名:raw.githubusercontent.com 查询 IP 地址(可能能查到多个,任选一个就行)
修改文件:
sudo gedit /etc/hosts
1.4 安装 ROS2
首先更新 apt 存储库缓存:
sudo apt update
然后升级已安装的软件( ROS2 软件包建立在经常更新的 Ubuntu 系统上,在安装新软件包之前请确保您的系统是最新的):
sudo apt upgrade
安装桌面版 ROS2(建议),包含:ROS、RViz、示例与教程,安装命令如下:
sudo apt install ros-humble-desktop
1.5 配置环境
终端下,执行 ROS2 程序时,需要调用如下命令配置环境:
source /opt/ros/humble/setup.bash
每次新开终端时,都得执行上述命令,或者也可以执行如下命令,将配置环境指令写
入 ”~/.bashrc“ 文件,那么每次新启动终端时,不需要在手动配置环境:
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
PS:关于 ROS2 的卸载
ROS2 安装完毕之后,如果想卸载 ROS2,可以执行如下命令:
sudo apt remove ~nros-humble-* && sudo apt autoremove
还可以再删除 ROS2 对应的存储库:
sudo rm /etc/apt/sources.list.d/ros2.list
sudo apt update
sudo apt autoremove
# Consider upgrading for packages previously shadowed.
sudo apt upgrade
1.6 测试 ROS2
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
ROS2 里要通过上下左右来控制乌龟运动
1.7 安装 colcon 构建工具
sudo apt install python3-colcon-common-extensions
1.8 ROS2 体验
1.8.1 C++
mkdir -p ws00_helloworld/src
cd ws00_helloworld
colcon build #编译
ROS2 中的编译命令为colcon build
ros2 pkg create pkg01_helloworld_cpp --build-type ament_cmake --dependencies rclcpp --node-name helloworld
意为创建一个名为 pkg01_helloworld_cpp 的功能包,类型为 cpp (–build-type ament_cmake),依赖:rclcpp (ros2client)(–dependencies rclcpp)为目录下添加一个叫 helloworld 的 cpp 包 (–node-name helloworld)
刷新环境变量
source install/setup.bash
运行文件
ros2 run pkg01_helloworld_cpp helloworld
格式还是指令 + 包名 + 文件名
编写cpp文件
#include "rclcpp/rclcpp.hpp"
int main(int argc,char** argv)
{
//初始化
rclcpp::init(argc,argv);
//创建节点
auto node = rclcpp::Node::make_shared("helloworld_node");
//编辑输出内容(日志输出的对象,内容)
RCLCPP_INFO(node->get_logger(),"hello world!");
//释放资源
rclcpp::shutdown();
return 0;
}
编译执行即可
package.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>pkg01_helloworld_cpp</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="ros2@todo.todo">ros2</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<!-- 所需要依赖 -->
<depend>rclcpp</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
cmake_minimum_required(VERSION 3.8)
project(pkg01_helloworld_cpp)
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)
# 映射源文件与可执行文件
add_executable(helloworld src/helloworld.cpp)
# 设置目标依赖库
ament_target_dependencies(
helloworld
"rclcpp"
)
# 定义安装规则
install(TARGETS helloworld
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()
配置
add_executable(helloworld src/helloworld.cpp)
add_executable(helloworld2 src/helloworld2.cpp)
ament_target_dependencies(
helloworld
"rclcpp"
)
ament_target_dependencies(
helloworld2
"rclcpp"
)
install(TARGETS helloworld
DESTINATION lib/${PROJECT_NAME})
install(TARGETS helloworld2
DESTINATION lib/${PROJECT_NAME})
中间有几行暂时不需要,被删除了
配置的思路和 ros1 一样
Python
创建功能包(和 c++ 类似)
ros2 pkg create pkg02_helloworld_py --build-type ament_python --dependencies rclpy --node-name helloworld
编辑python文件
import rclpy
def main():
# 初始化 ROS2
rclpy.init()
# 创建节点
node = rclpy.create_node("helloworld_py_node")
# 输出文本
node.get_logger().info("hello world!")
# 释放资源
rclpy.shutdown()
if __name__ == '__main__':
main()
setup.py
from setuptools import setup
package_name = 'pkg02_helloworld_py'
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='ros2',
maintainer_email='ros2@todo.todo',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
# 映射源文件与可执行文件
'helloworld = pkg02_helloworld_py.helloworld:main'
],
},
)
运行优化
每次终端中执行工作空间下的节点时,都需要调用. install/setup.bash 指令,使用不便,优化策略是,可以将该指令的调用添加进~/setup.bash,操作格式如下:
echo "source /{工作空间路径}/install/setup.bash" >> ~/.bashrc
示例:
echo "source /home/ros2/ws00_helloworld/install/setup.bash" >> ~/.bashrc
路径的获取可以直接 pwd
不建议每个都这么做,可能会导致工作空间混乱
1.9 vscode
要使用的插件
VScode 配置
编写 cpp 时,vscode 可能会因为找不到头文件而报错,这个时候就要修改 cpp 的包含路径
本质上是修改了 .vscode 配置文件
编译优化
在编译时,使用colcon build
会将目录下所有文件都编译一遍,这样效率很低,可以使用命令进行定向编译
colcon build --packages-select pkg01_helloworld_cpp pkg02_helloworld_py
1.10 安装 git
sudo apt install git
可以使用git help
查看使用说明
1.11 ROS2 体系框架
该部分部分内容暂时不做笔记
B站传送门
推荐实例化风格
之前的代码中一直使用的是直接实例化的方式,事实上这种方式在进行多节点通信的时候效率不高,推荐使用通过构建类继承 Node 进行实例化的办法
#include "rclcpp/rclcpp.hpp"
class MyNode: public rclcpp::Node{
public:
MyNode():Node("rclcpp_node"){
RCLCPP_INFO(this->get_logger(),"hello_node");
}
};
int main(int argc, char const *argv[])
{
rclcpp::init(argc,argv);
auto node = std::make_shared<MyNode>();
rclcpp::shutdown();
return 0;
}
import rclpy
from rclpy.node import Node
import rclpy.node
class MyNode(Node):
def __init__(self):
super().__init__("hello_world_py")
self.get_logger().info("hello_python")
def main():
rclpy.init()
node = MyNode()
rclpy.shutdown()
pass
if __name__=='__main__':
main()
配置包
package
不管是何种类型的功能包,package.xml 的格式都是类似的,在该文件中包含了包名、版本、作者、依赖项的信息,package.xml 可以为 colcon 构建工具确定功能包的编译顺序。
<?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>pkg02_helloworld_py</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="herrscher@todo.todo">herrscher</maintainer>
<license>TODO: License declaration</license>
<depend>rclpy</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>
- 根标签
:该标签为整个 xml 文件的根标签,format 属性用来声明文件的格式版本。 - 元信息标签
:包名;
:包的版本号;
:包的描述信息;
:维护者信息;
:软件协议;
:包的介绍网址;
:包的作者信息。 - 依赖项
<buildtool_depend>:声明编译工具依赖;
<build_depend>:声明编译依赖;
<build_export_depend>:声明根据此包构建库所需依赖;
<exec_depend>:声明执行时依赖;
:相当于<build_depend>、<build_export_depend>、<exec_depend>三者的集成;
<test_depend>:声明测试依赖;
<doc_depend>:声明构建文档依赖。
CMakeLists
# 声明 cmake 的最低版本
cmake_minimum_required(VERSION 3.8)
# 包名,需要与 package.xml 中的包名一致
project(pkg01_helloworld_cpp)
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)
# 映射源文件与可执行文件
add_executable(helloworld src/helloworld.cpp)
# 设置目标依赖库
ament_target_dependencies(
helloworld
"rclcpp"
)
# 定义安装规则
install(TARGETS helloworld
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()
setup.py
from setuptools import setup
package_name = 'pkg02_helloworld_py'
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='ros2', # 维护者
maintainer_email='ros2@todo.todo', # 维护者 email
description='TODO: Package description', # 包描述
license='TODO: License declaration', # 软件协议
tests_require=['pytest'], # 测试依赖
entry_points={
'console_scripts': [
# 映射源文件与可执行文件
'helloworld = pkg02_helloworld_py.helloworld:main'
],
},
)
操作命令
新建功能包
ros2 pkg create 包名 --build-type 构建类型 --dependencies 依赖列表 --node-name 可执行程序名称
–build-type:是指功能包的构建类型,有 cmake、ament_cmake、ament_python 三种类型可选;
–dependencies:所依赖的功能包列表;
–node-name:可执行程序的名称,会自动生成对应的源文件并生成配置文件。
编译
colcon build
或者 colcon build --packages-select 功能包列表
前者会构建工作空间下的所有功能包,后者可以构建指定功能包。
查找
ros2 pkg executables [包名] # 输出所有功能包或指定功能包下的可执行程序。
ros2 pkg list # 列出所有功能包
ros2 pkg prefix 包名 # 列出功能包路径
ros2 pkg xml # 输出功能包的 package.xml 内容
执行
ros2 run 功能包 可执行程序 参数
二进制安装
sudo apt install ros-ROS2 版本代号-功能包名称
PS:可以调用 apt search ros-ROS2 版本代号-* | grep -i 关键字格式的命令,根据关键字查找所需的功能包。
源码安装
git clone 仓库地址
其他内容查看赵老师的PDF