跳到主要内容
Ubuntu20.04 + ROS Noetic + Gazebo 实现 UR5 机械臂视觉抓取乐高积木仿真 | 极客日志
Python AI 算法
Ubuntu20.04 + ROS Noetic + Gazebo 实现 UR5 机械臂视觉抓取乐高积木仿真 综述由AI生成 在 Ubuntu 20.04 环境下配置 ROS Noetic 和 Gazebo,结合 YOLOv5 视觉算法实现 UR5 机械臂抓取乐高积木仿真的完整流程。涵盖环境安装、源码编译、依赖配置及四节点协同运行步骤,并提供了常见问题解决方案。
修罗 发布于 2026/3/30 更新于 2026/5/28 27 浏览1. 环境说明
1.1 本教程使用的环境
软件 版本 虚拟机 VirtualBox 操作系统 Ubuntu 20.04 ROS 版本 ROS Noetic Gazebo 版本 Gazebo 11 Python 版本 Python 3.8
1.2 项目简介
本项目来自 GitHub:https://github.com/pietrolechthaler/UR5-Pick-and-Place-Simulation
主要功能:
在 Gazebo 中仿真 UR5 机械臂 + Robotiq85 夹爪
使用 Xbox Kinect 相机获取场景图像
使用 YOLOv5 识别 11 种不同类型的乐高积木
机械臂自动抓取积木并放置到指定位置
2. 前置准备
2.1 确认 ROS Noetic 已安装
打开终端(快捷键 Ctrl + Alt + T),输入以下命令检查 ROS 是否安装:
rosversion -d
如果输出 noetic,说明 ROS Noetic 已安装。
如果没有安装,请参考 ROS 官方教程安装:http://wiki.ros.org/noetic/Installation/Ubuntu
2.2 确认 Gazebo 已安装
gazebo --version
如果输出类似 Gazebo multi-robot simulator, version 11.x.x,说明 Gazebo 已安装。
2.3 安装 catkin 工具
本项目使用 catkin build 而不是 catkin_make,需要安装 catkin-tools:
sudo apt update && sudo apt install python3-catkin-tools -y
验证安装:
catkin --version
应该输出类似 catkin_tools 0.9.x。
2.4 安装其他必要的 ROS 包
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
apt install ros-noetic-robot-state-publisher -y
apt install ros-noetic-controller-manager ros-noetic-joint-trajectory-controller -y
apt install ros-noetic-cv-bridge ros-noetic-image-transport -y
sudo
sudo
sudo
2.5 安装 pip3 sudo apt install python3-pip -y
3. 创建工作目录
3.1 打开终端
3.2 创建项目文件夹 我们在用户主目录下创建一个名为 UR5-Pick-and-Place 的文件夹:
cd ~
mkdir UR5-Pick-and-Place
cd UR5-Pick-and-Place
3.3 确认当前位置 应该输出:/home/你的用户名/UR5-Pick-and-Place
4. 下载项目源码
4.1 方法一:使用 git clone(推荐) cd ~/UR5-Pick-and-Place
git clone https://github.com/pietrolechthaler/UR5-Pick-and-Place-Simulation.git
⚠️ 如果网络不好,可能会克隆失败,请使用方法二。
4.2 方法二:手动下载 ZIP 文件 cd ~/UR5-Pick-and-Place
unzip UR5-Pick-and-Place-Simulation-main.zip
4.3 确认文件结构 ls ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/
catkin_ws/ intro.gif LICENSE main .png README.md
5. 编译 catkin 工作空间
5.1 进入 catkin 工作空间 cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
5.2 设置 ROS 环境变量 source /opt/ros/noetic/setup.bash
5.3 编译工作空间 编译过程大约需要 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! 就表示编译成功。
5.4 设置工作空间环境变量
5.5 (可选)将环境变量添加到 bashrc 为了避免每次打开终端都要手动 source,可以将其添加到 ~/.bashrc:
echo "source ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws/devel/setup.bash" >> ~/.bashrc
6. 安装 YOLOv5
6.1 进入用户主目录
6.2 克隆 YOLOv5 仓库 git clone https://github.com/ultralytics/yolov5
cd ~
unzip yolov5-master.zip
mv yolov5-master yolov5
6.3 确认 YOLOv5 目录 应该看到 hubconf.py、requirements.txt 等文件。
7. 安装 Python 依赖
7.1 安装 PyTorch(CPU 版本) 由于我们在虚拟机中运行,使用 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
7.2 验证 PyTorch 安装 python3 -c "import torch; print('PyTorch version:', torch.__version__)"
应该输出:PyTorch version: 1.10.0+cpu
7.3 安装其他 Python 依赖 pip3 install opencv-python pandas seaborn tqdm pyquaternion ultralytics --no-cache-dir
7.4 升级 numpy pip3 install numpy==1.23.5 --no-cache-dir
7.5 安装 networkx(兼容版本) pip3 install networkx==3.1
7.6 验证所有依赖 python3 -c "import torch; import cv2; import pandas; print('All dependencies OK!')"
如果输出 All dependencies OK!,说明所有依赖安装成功。
8. 运行仿真
8.1 打开第一个终端 - 启动 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
roslaunch levelManager lego_world.launch paused:=false
📝 参数说明:paused:=false:启动后仿真立即运行,不暂停。如果不加这个参数,Gazebo 会处于暂停状态,需要手动点击播放按钮。
一个工作台
UR5 机械臂
Robotiq85 夹爪
Xbox Kinect 相机(在机械臂上方)
8.2 打开第二个终端 - 加载乐高积木 按 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 中可以看到工作台上出现了乐高积木。
8.3 打开第三个终端 - 启动运动规划 按 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 后...
8.4 打开第四个终端 - 启动视觉检测 按 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
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
8.5 观察机械臂抓取过程 视觉检测完成后,回到第三个终端(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
机械臂移动到积木上方
夹爪张开
机械臂下降
夹爪闭合抓取积木
机械臂抬起
移动到目标位置
夹爪张开放下积木
机械臂返回初始位置
🎉 恭喜!你已经成功复现了 UR5 视觉抓取仿真!
9. 常见问题解决
问题 1:Gazebo 启动后仿真不运行 现象 :Gazebo 窗口打开了,但机械臂不动,时间停止。
原因 :launch 文件默认 paused:=true。
方法一:启动时添加参数 paused:=false
方法二:在 Gazebo 窗口底部点击播放按钮 ▶️
问题 2:catkin build 失败 cd ~/UR5-Pick-and-Place/UR5-Pick-and-Place-Simulation-main/catkin_ws
catkin clean -y
source /opt/ros/noetic/setup.bash
catkin build
问题 3:找不到 YOLOv5 现象 :运行 vision 节点时报错 No module named 'ultralytics' 或找不到 yolov5。
如果文件不存在,重新下载 YOLOv5(参考第 6 节)。
问题 4:PyTorch 安装失败 现象 :pip 安装 torch 时报错或被 kill。
增加虚拟机内存到至少 4GB
使用 --no-cache-dir 参数减少内存占用
关闭其他程序释放内存
问题 5:相机话题没有数据 现象 :vision 节点一直等待,不输出检测结果。
rostopic echo /clock -n 3
如果没有输出,说明仿真暂停了,需要在 Gazebo 中点击播放按钮。
问题 6:libp11-kit.so.0: undefined symbol: ffi_type_pointer 解决方法 :不要在 Conda 环境中运行,使用系统 Python:
📚 参考链接
🎯 总结 本教程完整介绍了如何在 Ubuntu 20.04 + ROS Noetic 环境下复现 UR5 机械臂视觉抓取仿真项目。
安装必要的 ROS 包和工具
下载并编译项目源码
安装 YOLOv5 和 Python 依赖
按顺序启动 4 个节点完成仿真
roslaunch levelManager lego_world.launch paused:=false
rosrun levelManager levelManager.py -l 1
rosrun motion_planning motion_planning.py
rosrun vision lego-vision.py
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
RSA密钥对生成器 生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
Mermaid 预览与可视化编辑 基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
随机西班牙地址生成器 随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online
Gemini 图片去水印 基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
curl 转代码 解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online