ROS2 入门应用 工作空间
ROS2 入门应用 工作空间
- 1. 创建
- 2. 检查
- 3. 编译
- 4. 加载
1. 创建
前提条件是在ROS2的环境下,即有:
source /opt/ros/humble/setup.bash
或者已经按《ROS2官网安装教程补充》写入~/.bashrc
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc
假设新的工作空间名称为ros2_ws
,创建文件夹ros2_ws
及其子文件夹src
,然后导航到其中
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
后续将工作空间中的功能包放入src
目录
先克隆一个海龟功能包做个例子说明先:
git clone https://github.com/ros/ros_tutorials.git -b humble-devel
2. 检查
在构建工作空间之前,需要解决包依赖关系
虽然可能已经拥有所有依赖项,但最好还是在每次克隆后检查依赖项
cd ..
rosdep install -i --from-path src --rosdistro humble -y
3. 编译
从工作空间的根目录(ros2_ws
),现在可以使用以下命令编译功能包:
colcon build --symlink-install
# Summary: 1 package finished [19.1s]
colcon构建的其他有用参数:
–packages-up-to:构建所需的包及其所有依赖项,但不构建整个工作空间(节省时间)
–symlink-install:无需在每次调整python脚本时重新构建
–event-handlers console_direct+:在生成时显示控制台输出(否则可以在log文件夹中找到)
构建好了可以看到新建了3个文件夹:
build install log src
4. 加载
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc
这样新的ros2_ws
工作空间下的相关包会覆盖原ros2
底层的相关包
如果需要验证一下
可以修改~/ros2_ws/src/ros_tutorials/turtlesim/src/turtle_frame.cpp
的代码
例如在setWindowTitle
标题显示内容前面加上 “ROS2 ”:
重新编译下工作空间
colcon build --symlink-install
打开一个新终端,运行节点,可以看到标题已改变
ros2 run turtlesim turtlesim_node
谢谢