完整记录 ROS2 MoveIt! 项目编写过程

发布时间 2023-07-30 20:44:19作者: BuckyI

目的是编写一个简单的项目,根据一个特定的机器人模型生成 moveit 配置文件,并根据配置文件模拟控制机器人,以学习为目的。这里使用的环境为 moveit2 humble。

创建一个名叫 ur5_project 的文件夹作为工作区,后续工作都在其中进行。

创建软件包 ur_description

没有找到现成能用的 UR5 Description 软件包,比较有名的 ros-industrial/universal_robot 为 ROS1 版本,不能直接使用,但是可以借用核心文件自己生成。

git clone https://github.com/ros-industrial/universal_robot.git

克隆仓库到本地,其中有个 ur_description 的文件夹即包含了各种模型。

创建一个新的 package,由于只是用来存放 urdf 等文件,没有代码,因此不需要添加依赖项,也不需要添加节点。

cd ur5_project/src
source /ros_entrypoint.sh
ros2 pkg create --build-type ament_cmake ur_description

注意

  • 如果反悔了想要修改包名,必须同时在 CMakeLists.txtpackage.xml 中修改,不可以只修改文件夹名称。

  • 另外,源软件包的名字为 ur_description,这里为了省事自己的软件包名也设为 ur_description,xacro 文件中有很多 $(find ur_description),如果软件包名不一致,需要进行调整。

    `<xacro:include filename="$(find ur_description)/urdf/inc/ur3e_macro.xacro"/>`
    

CMakeLists.txt 添加下面代码

install(
  DIRECTORY config meshes urdf
  DESTINATION share/${PROJECT_NAME} 
)

编译及验证

$ cd ur5_project
$ colcon build --packages-select ur_description
Starting >>> ur_description
Finished <<< ur_description [1.45s]                  

Summary: 1 package finished [1.59s]

可以到 install 下进行检查,如果没问题的话,会看到 ur_description 包及其中的各项文件。
image

创建软件包 ur5_robot_moveit_config

可参考 Integration with a New Robot — MoveIt Documentation

首先确认需要的工具都已经安装好:

$ ros2 pkg list | grep moveit_setup_assistant
moveit_setup_assistant # 说明已安装
# 如果没有安装,执行命令安装需要的版本
$ sudo apt install  ros-humble-moveit ros-humble-moveit-setup-assistant -y
$ cd ur5_project
$ source ./install/setup.bash  # 这里保证前面生成的 description 包可以被找到
$ ros2 run moveit_setup_assistant moveit_setup_assistant

image
这里选择左侧按钮创建新的配置包,选择 ur_description/urdf/ur5.xacro 作为 urdf 路径,最后点击加载。
image

接下来是具体的设定,不详细解释,大概记录一下设定内容[1]

  • Self-Collisions -- Generate Collison Matrix
  • Virtual Joints -- Add Virtual Joint
    image
  • Planning Groups -- Add Group
    这里我选择用 Chain 的方式设定,直接从最开头到最末尾把所有的都选中
    image
  • ros2_controll URDF Modifications -- Add interfaces
  • ROS 2 Controllers -- Auto Add
  • MoveIt Controllers -- Auto Add
  • Author Information
  • 最后 Configuration Files 部分,路径设为 ur5_project/src/ur5_robot_moveit_config,Generate Package
    image

注:

  • 最佳的包名为 robot_name + "_moveit_config",在查看 xacro 文件后确认 robot_name 为 ur5_robot
  • 这里只是一个简单的实现,有些设置略过了。

image

编译及验证

$ cd ur5_project
$ colcon build --packages-select ur5_robot_moveit_config
Starting >>> ur5_robot_moveit_config
Finished <<< ur5_robot_moveit_config [1.20s]

Summary: 1 package finished [1.34s]
$ source ./install/setup.bash
$ ros2 pkg list | grep ur5_robot
ur5_robot_moveit_config  # 确认可以找到
$ ros2 launch ur5_robot_moveit_config demo.launch.py

运行默认生成的示例 launch file[2], 打开 Rviz 窗口验证。
image

创建自己的功能包 movemove

这里复现官方教程中的 Visualizing In RViz,但是用前面生成的 ur5_robot 模型。

$ cd ur5_project/src
$ ros2 pkg create \
	--build-type ament_cmake \
	--dependencies moveit_ros_planning_interface rclcpp moveit_visual_tools \
	--node-name move 
	movemove

包创建完成后添加依赖项:

  • package.xml: <depend>moveit_visual_tools</depend>
  • CMakeLists.txt:
    • find_package(moveit_visual_tools REQUIRED)
    • ament_target_dependencies 添加 moveit_visual_tools

src/move.cpp 中填充代码,调用 moveit interface 操作 ur5 模型,这里不详细介绍,可以直接复制官方教程中提供的代码[3],不过需要注意代码中的节点名以及机器人 planning group, link 相关的名称需要改掉。
添加 launch/run_launch.py,用于启动所有涉及到的节点。
添加 config/moveit.rviz,用于预设 Rviz,没有这个也没关系,可以先随便放一个,最后运行成功后按照需要调整,最后覆盖即可。

别忘了在 CMakeLists.txt 中添加:

install(
  DIRECTORY config launch
  DESTINATION share/${PROJECT_NAME} 
)

编译及验证

$ cd ur5_project
$ colcon build
Starting >>> ur_description
Starting >>> movemove
Finished <<< ur_description [0.32s]                                                      
Starting >>> ur5_robot_moveit_config
Finished <<< ur5_robot_moveit_config [0.28s]                 
[Processing: movemove]                             
Finished <<< movemove [36.2s]                       

Summary: 3 packages finished [36.4s]

新开一个终端,运行 launch file, 启动所有必要的节点:

$ cd ur5_project/
$ source ./install/setup.bash
$ ros2 launch movemove run_launch.py


打开了 Rviz 窗口,显示 ur5 模型,这里主要是确认 Displays 中有 Marker Array, Planning Scene, Trajectory 插件,以及 RvizVisualToolsGUI 打开。

另外打开一个终端,运行 move 节点(其实这个也可以一块写到 launch file 中)

$ cd ur5_project/
$ source ./install/setup.bash 
$ ros2 run movemove move


完结撒花 ??????
源代码:BuckyI/simp_ros_project


  1. 参考 【学习笔记】ROS2纯小白 - MoveIt! (humble) 引入新的机器人模型 - 知乎 ↩︎

  2. 代码解析 https://gist.github.com/BuckyI/ed673a982d73b693148060a00e6b54c2 ↩︎

  3. 参考 https://gist.github.com/BuckyI/0ebf9b68e5384feda40f3067f572a7c3 ↩︎