Ubuntu20.04 + ROS Noetic + Gazebo 实现 UR5 机械臂视觉抓取乐高积木仿真
在 Ubuntu 20.04 环境下配置 ROS Noetic 和 Gazebo,结合 YOLOv5 视觉算法实现 UR5 机械臂抓取乐高积木仿真的完整流程。涵盖环境安装、源码编译、依赖配置及四节点协同运行步骤,并提供了常见问题解决方案。

在 Ubuntu 20.04 环境下配置 ROS Noetic 和 Gazebo,结合 YOLOv5 视觉算法实现 UR5 机械臂抓取乐高积木仿真的完整流程。涵盖环境安装、源码编译、依赖配置及四节点协同运行步骤,并提供了常见问题解决方案。

| 软件 | 版本 |
|---|---|
| 虚拟机 | VirtualBox |
| 操作系统 | Ubuntu 20.04 |
| ROS 版本 | ROS Noetic |
| Gazebo 版本 | Gazebo 11 |
| Python 版本 | Python 3.8 |
本项目来自 GitHub:https://github.com/pietrolechthaler/UR5-Pick-and-Place-Simulation
主要功能:
打开终端(快捷键 Ctrl + Alt + T),输入以下命令检查 ROS 是否安装:
rosversion -d
如果输出 noetic,说明 ROS Noetic 已安装。
如果没有安装,请参考 ROS 官方教程安装:http://wiki.ros.org/noetic/Installation/Ubuntu
gazebo --version
如果输出类似 Gazebo multi-robot simulator, version 11.x.x,说明 Gazebo 已安装。
本项目使用 catkin build 而不是 catkin_make,需要安装 catkin-tools:
sudo apt update && sudo apt install python3-catkin-tools -y
验证安装:
catkin --version
应该输出类似 catkin_tools 0.9.x。
sudo apt install ros-noetic-gazebo-ros-pkgs ros-noetic-gazebo-ros-control -y
sudo apt install ros-noetic-joint-state-publisher ros-noetic-joint-state-publisher-gui -y
sudo apt install ros-noetic-robot-state-publisher -y
sudo apt install ros-noetic-controller-manager ros-noetic-joint-trajectory-controller -y
sudo apt install ros-noetic-cv-bridge ros-noetic-image-transport -y
sudo apt install python3-pip -y
按 Ctrl + Alt + T 打开终端。
我们在用户主目录下创建一个名为 UR5-Pick-and-Place 的文件夹:
cd ~
mkdir UR5-Pick-and-Place
cd UR5-Pick-and-Place
pwd
应该输出:/home/你的用户名/UR5-Pick-and-Place
cd ~/UR5-Pick-and-Place
git clone https://github.com/pietrolechthaler/UR5-Pick-and-Place-Simulation.git
⚠️ 如果网络不好,可能会克隆失败,请使用方法二。
Code 按钮Download ZIPUR5-Pick-and-Place-Simulation-main.zip 文件复制到 ~/UR5-Pick-and-Place/ 目录cd ~/UR5-Pick-and-Place
unzip UR5-Pick-and-Place-Simulation-main.zip
ls ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/
应该看到以下内容:
catkin_ws/ intro.gif LICENSE main.png README.md
cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
source /opt/ros/noetic/setup.bash
catkin build
编译过程大约需要 2-3 分钟,你会看到类似以下输出:
---------------------------------------------------------------- Profile: default Extending: [cached] /opt/ros/noetic Workspace: /home/xxx/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws ---------------------------------------------------------------- ... [build] Summary: All 21 packages succeeded!
⚠️ 如果出现警告(Warning)是正常的,只要最后显示
All xx packages succeeded!就表示编译成功。
source devel/setup.bash
为了避免每次打开终端都要手动 source,可以将其添加到 ~/.bashrc:
echo "source ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws/devel/setup.bash" >> ~/.bashrc
cd ~
git clone https://github.com/ultralytics/yolov5
⚠️ 如果网络不好,可以手动下载 ZIP 文件:访问 https://github.com/ultralytics/yolov5 下载 ZIP 文件解压到
~/yolov5/目录(注意:目录名必须是yolov5,不能是yolov5-master)
如果下载的是 ZIP 文件,解压后需要重命名:
cd ~
unzip yolov5-master.zip
mv yolov5-master yolov5
ls ~/yolov5/
应该看到 hubconf.py、requirements.txt 等文件。
由于我们在虚拟机中运行,使用 CPU 版本的 PyTorch 即可:
pip3 install torch==1.10.0+cpu torchvision==0.11.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html --no-cache-dir
⚠️ 这个下载可能需要一些时间,请耐心等待。
如果下载速度太慢,可以使用国内镜像:pip3 install torch==1.10.0+cpu torchvision==0.11.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple
python3 -c "import torch; print('PyTorch version:', torch.__version__)"
应该输出:PyTorch version: 1.10.0+cpu
pip3 install opencv-python pandas seaborn tqdm pyquaternion ultralytics --no-cache-dir
pip3 install numpy==1.23.5 --no-cache-dir
pip3 install networkx==3.1
python3 -c "import torch; import cv2; import pandas; print('All dependencies OK!')"
如果输出 All dependencies OK!,说明所有依赖安装成功。
按 Ctrl + Alt + T 打开终端,输入:
cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
source /opt/ros/noetic/setup.bash
source devel/setup.bash
roslaunch levelManager lego_world.launch paused:=false
📝 参数说明:
paused:=false:启动后仿真立即运行,不暂停。如果不加这个参数,Gazebo 会处于暂停状态,需要手动点击播放按钮。
等待 Gazebo 窗口打开,你会看到:

按 Ctrl + Alt + T 打开新终端,输入:
cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
source /opt/ros/noetic/setup.bash
source devel/setup.bash
rosrun levelManager levelManager.py -l 4
📝 参数说明:
-l 1:加载 Level 1(1 块积木);-l 2:加载 Level 2(3 块积木);-l 3:加载 Level 3(5 块积木);-l 4:加载 Level 4(6 块积木,最多)。
你会看到输出:
Added 6 bricks All done. Ready to start.
此时在 Gazebo 中可以看到工作台上出现了乐高积木。

按 Ctrl + Alt + T 打开新终端,输入:
cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
source /opt/ros/noetic/setup.bash
source devel/setup.bash
rosrun motion_planning motion_planning.py
你会看到输出:
Initializing node of kinematics Waiting for action of gripper controller Waiting for detection of the models
此时机械臂在等待视觉检测结果。
等待显示 Waiting for detection of the models 后...
按 Ctrl + Alt + T 打开新终端,输入:
cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
source /opt/ros/noetic/setup.bash
source devel/setup.bash
rosrun vision lego-vision.py
你会看到 YOLOv5 模型加载过程:
Loading model best.pt YOLOv5 🚀 ... Python-3.8.10 torch-1.10.0+cpu CPU Fusing layers... Model summary: 213 layers, 7039792 parameters, 0 gradients, 15.8 GFLOPs Adding AutoShape... Loading model orientation.pt ... Starting Node Vision 1.0 Subscribing to camera images Localization is starting.. Time: x.xxx
视觉检测完成后,回到第三个终端(motion_planning),你会看到:
Lego is facing (0, 0, 1) Angle of approaching measures -3.76 deg Moving model X1-Y2-Z2-TWINFILLET to 0.103547 -0.501132 0.8095 X1-Y2-Z2-TWINFILLET_1 TO HOME Moving to Default Position
同时在 Gazebo 窗口中,你可以看到:

🎉 恭喜!你已经成功复现了 UR5 视觉抓取仿真!
现象:Gazebo 窗口打开了,但机械臂不动,时间停止。
原因:launch 文件默认 paused:=true。
解决方法:
paused:=false现象:编译时报错。
解决方法:
cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
catkin clean -y
source /opt/ros/noetic/setup.bash
catkin build
现象:运行 vision 节点时报错 No module named 'ultralytics' 或找不到 yolov5。
解决方法:
确认 YOLOv5 在正确位置:
ls ~/yolov5/hubconf.py
如果文件不存在,重新下载 YOLOv5(参考第 6 节)。
现象:pip 安装 torch 时报错或被 kill。
原因:虚拟机内存不足。
解决方法:
--no-cache-dir 参数减少内存占用现象:vision 节点一直等待,不输出检测结果。
解决方法:检查仿真是否在运行:
rostopic echo /clock -n 3
如果没有输出,说明仿真暂停了,需要在 Gazebo 中点击播放按钮。
现象:在 Conda 环境下运行时报错。
解决方法:不要在 Conda 环境中运行,使用系统 Python:
conda deactivate
本教程完整介绍了如何在 Ubuntu 20.04 + ROS Noetic 环境下复现 UR5 机械臂视觉抓取仿真项目。
主要步骤:
启动顺序(重要):
# 1. 启动世界
roslaunch levelManager lego_world.launch paused:=false
# 2. 加载积木
rosrun levelManager levelManager.py -l 1
# 3. 启动运动规划
rosrun motion_planning motion_planning.py
# 4. 启动视觉检测
rosrun vision lego-vision.py

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online