moveit_cpp::PlanningComponent 默认只做规划,不执行轨迹 ------ 因为它没有对接控制器 。要让机械臂真的动起来 ,必须使用 TrajectoryExecutionComponent 轨迹执行组件。
我会给你最简可运行修复版代码 + 原理一句话说明。
🔥 核心问题(一句话)
planning_components->execute() 不是执行到真实机械臂 / 控制器,它只是在内部模拟播放,不会发指令给硬件。
真正执行必须用:
cpp
运行
moveit_cpp::TrajectoryExecutionComponent execution_component(PLANNING_GROUP, moveit_cpp_ptr);
execution_component.execute(plan_solution.trajectory);
✅ 修复后完整可执行代码(关键部分)
我只改执行部分,其他不变,你直接替换即可。
1. 增加头文件
cpp
运行
#include <moveit/moveit_cpp/trajectory_execution_component.h> // <-- 加这个
2. 声明执行器(在 planning_components 下面加)
cpp
运行
auto planning_components = std::make_shared<moveit_cpp::PlanningComponent>(PLANNING_GROUP, moveit_cpp_ptr);
auto execution_components = std::make_shared<moveit_cpp::TrajectoryExecutionComponent>(PLANNING_GROUP, moveit_cpp_ptr); // <-- 加这行
3. 把所有
cpp
运行
planning_components->execute();
替换成:
cpp
运行
if (!execution_components->execute(plan_solution1.trajectory))
{
RCLCPP_ERROR(LOGGER, "Execution failed!");
}
✔ 示例(Plan #1 修复后)
cpp
运行
if (plan_solution1)
{
visual_tools.publishAxisLabeled(robot_start_state->getGlobalLinkTransform("panda_link8"), "start_pose");
visual_tools.publishAxisLabeled(target_pose1.pose, "target_pose");
visual_tools.publishText(text_pose, "setStartStateToCurrentState", rvt::WHITE, rvt::XLARGE);
visual_tools.publishTrajectoryLine(plan_solution1.trajectory, joint_model_group_ptr);
visual_tools.trigger();
// ---------------- 真正执行 ----------------
execution_components->execute(plan_solution1.trajectory);
}
所有 5 个 plan 都这样改,机械臂就会真的动了。
🧠 为什么 moveit_cpp 要分两个组件?
- PlanningComponent :只负责算路径
- TrajectoryExecutionComponent :负责把路径发给控制器
MoveItCpp 架构就是这样设计的:规划 / 执行分离。
🚀 最快可运行修复总结
-
加头文件: cpp
运行
#include <moveit/moveit_cpp/trajectory_execution_component.h> -
创建执行器: cpp
运行
auto execution_components = std::make_shared<moveit_cpp::TrajectoryExecutionComponent>(PLANNING_GROUP, moveit_cpp_ptr); -
执行轨迹: cpp
运行
execution_components->execute(plan_solution1.trajectory);