
1.urdf 准备
首先从 solidworks 中导出机器人的 urdf,并可以在 rviz 中正常显示,joint_state_publisher_gui 节点可以正常控制每个关节的转动。这一部分在这里不多赘述。
2.环境配置
示例环境:x86 ubuntu22.04,32G,ROS2 humble。
2.1Moveit2 安装
按照“鱼香 ros”一键安装配置好“ROS2 humble”和“rosdepc”(小鱼定制 rosdep)
wget http://fishros.com/install -O fishros && . fishros
更新软件包:
使用 mixin
安装 Colcon ROS 2 构建系统:
sudo apt install python3-colcon-common-extensions
sudo apt install python3-colcon-mixin
colcon mixin add default https://raw.githubusercontent.com/colcon/colcon-mixin-repository/master/index.yaml
colcon mixin update default
安装 vcstool :
sudo apt install python3-vcstool
2.2 创建 Colcon 工作区并下载教程
对于教程,您需要有一个 colcon 工作区设置。:
mkdir -p ~/ws_moveit/src
2.3 下载 MoveIt 和教程的源代码
下载 humble 分支源码(需要梯子)
cd ~/ws_moveit/src
git clone -b humble https://github.com/moveit/moveit2_tutorials
下载 MoveIt 其余部分的源代码:
vcs import --recursive < moveit2_tutorials/moveit2_tutorials.repos
导入命令可能会要求您提供 GitHub 凭据。 您只需按 Enter 即可继续(忽略“身份验证失败”错误)。
3.构建 Colcon 工作区
首先删除所有先前安装的 moveit 二进制文件:
sudo apt remove ros-$ROS_DISTRO-moveit*
安装依赖
sudo apt update && rosdepc install -r --from-paths . --ignore-src --rosdistro $ROS_DISTRO -y
配置工作区
cd ~/ws_moveit
colcon build --mixin release
#如果会卡死的话使用这条命令:colcon build ----executor sequential --mixin release
编译完成后应该是 55 个包编译成功。
4.设置 Colcon 工作区
echo 'source ~/ws_moveit/install/setup.bash' >> ~/.bashrc
该命令允许你的工作区在打开终端时自动查找到该包。
3.Moveit Setup Assistant 配置助手
https://www.ncnynl.com/ros2docs/cn/moveit2/doc/examples/setup_assistant/setup_assistant_tutorial.html
注:需要先在你 urdf 工作空间中 source 到你的 urdf 包才行
4.驱动规划关键函数
4.1ROS2 和 MoveIt 核心部分
ROS2 和 MoveIt 头文件
#include <rclcpp/rclcpp.hpp> // ROS2 C++ 客户端库
#include <moveit/move_group_interface/move_group_interface.h> // MoveIt 运动规划接口
#include <moveit_msgs/msg/robot_trajectory.hpp> // 机器人轨迹消息类型
MoveGroupInterface 初始化
using moveit::planning_interface::MoveGroupInterface;
auto move_group_interface = MoveGroupInterface(node, "left_arm_plan_group");//规划组名称
-
功能: 创建 MoveIt 运动规划接口
-
参数: ROS 节点和规划组名称
-
用途: 所有运动规划和执行的核心接口
设置规划参数
move_group_interface.setMaxVelocityScalingFactor(1.0); // 最大速度比例,默认为0.1
move_group_interface.setMaxAccelerationScalingFactor(1.0); // 最大加速度比例,默认为0.1
move_group_interface.setPlanningTime(10.0); // 规划时间限制,默认为5.0
move_group_interface.setNumPlanningAttempts(20); // 规划尝试次数
坐标系设置
move_group_interface.setPoseReferenceFrame("base_link"); // 设置参考坐标系
move_group_interface.setEndEffectorLink("Left_Arm_Link_6"); // 设置末端执行器
笛卡尔路径规划
double fraction = move_group_interface.computeCartesianPath(
waypoints, // 路径点向量
eef_step, // 末端执行器步长
jump_threshold, // 跳跃阈值
trajectory // 输出轨迹
);
-
返回值: 路径规划完成度(0.0-1.0)
-
参数说明:
-
waypoints
: 包含起点和终点的姿态向量 -
eef_step
: 0.0005(0.5mm)- 路径插值精度 -
jump_threshold
: 0.0 - 禁用跳跃检测 -
trajectory
: 计算得到的机器人轨迹
-
轨迹执行
moveit::planning_interface::MoveGroupInterface::Plan plan;
plan.trajectory_ = trajectory;
auto result = move_group_interface.execute(plan);
5.讨论
一起学习,有疑问的可以联系我邮箱francistian@foxmail.com