|
23 | 23 | #include <tf2/impl/utils.h> |
24 | 24 | #include <cl_moveit2z/cl_moveit2z.hpp> |
25 | 25 | #include <cl_moveit2z/common.hpp> |
| 26 | +#include <cl_moveit2z/components/cp_motion_planner.hpp> |
| 27 | +#include <cl_moveit2z/components/cp_trajectory_executor.hpp> |
26 | 28 | #include <future> |
27 | 29 | #include <smacc2/smacc_asynchronous_client_behavior.hpp> |
28 | 30 | #include <tf2_geometry_msgs/tf2_geometry_msgs.hpp> |
@@ -77,41 +79,130 @@ class CbMoveEndEffector : public smacc2::SmaccAsyncClientBehavior |
77 | 79 | RCLCPP_DEBUG(getLogger(), "[CbMoveEndEffector] Synchronous sleep of 1 seconds"); |
78 | 80 | rclcpp::sleep_for(500ms); |
79 | 81 |
|
80 | | - moveGroupInterface.setPlanningTime(1.0); |
| 82 | + // Try to use CpMotionPlanner component (preferred) |
| 83 | + CpMotionPlanner * motionPlanner = nullptr; |
| 84 | + this->requiresComponent(motionPlanner, false); // Optional component |
| 85 | + |
| 86 | + bool success = false; |
| 87 | + moveit::planning_interface::MoveGroupInterface::Plan computedMotionPlan; |
81 | 88 |
|
82 | 89 | RCLCPP_INFO_STREAM( |
83 | | - getLogger(), "[CbMoveEndEffector] Target End efector Pose: " << targetObjectPose); |
| 90 | + getLogger(), "[CbMoveEndEffector] Target End effector Pose: " << targetObjectPose); |
84 | 91 |
|
85 | | - moveGroupInterface.setPoseTarget(targetObjectPose, tip_link_); |
86 | | - moveGroupInterface.setPoseReferenceFrame(targetObjectPose.header.frame_id); |
| 92 | + if (motionPlanner != nullptr) |
| 93 | + { |
| 94 | + // Use component-based motion planner (preferred) |
| 95 | + RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] Using CpMotionPlanner component for planning"); |
87 | 96 |
|
88 | | - moveit::planning_interface::MoveGroupInterface::Plan computedMotionPlan; |
89 | | - bool success = |
90 | | - (moveGroupInterface.plan(computedMotionPlan) == moveit::core::MoveItErrorCode::SUCCESS); |
91 | | - RCLCPP_INFO( |
92 | | - getLogger(), "[CbMoveEndEffector] Success Visualizing plan 1 (pose goal) %s", |
93 | | - success ? "" : "FAILED"); |
| 97 | + PlanningOptions options; |
| 98 | + options.planningTime = 1.0; |
| 99 | + options.poseReferenceFrame = targetObjectPose.header.frame_id; |
| 100 | + |
| 101 | + std::optional<std::string> tipLinkOpt; |
| 102 | + if (!tip_link_.empty()) |
| 103 | + { |
| 104 | + tipLinkOpt = tip_link_; |
| 105 | + } |
94 | 106 |
|
| 107 | + auto result = motionPlanner->planToPose(targetObjectPose, tipLinkOpt, options); |
| 108 | + |
| 109 | + success = result.success; |
| 110 | + if (success) |
| 111 | + { |
| 112 | + computedMotionPlan = result.plan; |
| 113 | + RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] Planning succeeded (via CpMotionPlanner)"); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + RCLCPP_WARN( |
| 118 | + getLogger(), "[CbMoveEndEffector] Planning failed (via CpMotionPlanner): %s", |
| 119 | + result.errorMessage.c_str()); |
| 120 | + } |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + // Fallback to legacy direct API calls |
| 125 | + RCLCPP_WARN( |
| 126 | + getLogger(), |
| 127 | + "[CbMoveEndEffector] CpMotionPlanner component not available, using legacy planning " |
| 128 | + "(consider adding CpMotionPlanner component)"); |
| 129 | + |
| 130 | + moveGroupInterface.setPlanningTime(1.0); |
| 131 | + moveGroupInterface.setPoseTarget(targetObjectPose, tip_link_); |
| 132 | + moveGroupInterface.setPoseReferenceFrame(targetObjectPose.header.frame_id); |
| 133 | + |
| 134 | + success = |
| 135 | + (moveGroupInterface.plan(computedMotionPlan) == moveit::core::MoveItErrorCode::SUCCESS); |
| 136 | + RCLCPP_INFO( |
| 137 | + getLogger(), "[CbMoveEndEffector] Planning %s (legacy mode)", |
| 138 | + success ? "succeeded" : "FAILED"); |
| 139 | + } |
| 140 | + |
| 141 | + // Execution |
95 | 142 | if (success) |
96 | 143 | { |
97 | | - auto executionResult = moveGroupInterface.execute(computedMotionPlan); |
| 144 | + // Try to use CpTrajectoryExecutor component (preferred) |
| 145 | + CpTrajectoryExecutor * trajectoryExecutor = nullptr; |
| 146 | + this->requiresComponent(trajectoryExecutor, false); // Optional component |
| 147 | + |
| 148 | + bool executionSuccess = false; |
| 149 | + |
| 150 | + if (trajectoryExecutor != nullptr) |
| 151 | + { |
| 152 | + // Use component-based trajectory executor (preferred) |
| 153 | + RCLCPP_INFO( |
| 154 | + getLogger(), "[CbMoveEndEffector] Using CpTrajectoryExecutor component for execution"); |
| 155 | + |
| 156 | + ExecutionOptions execOptions; |
| 157 | + execOptions.trajectoryName = this->getName(); |
| 158 | + |
| 159 | + auto execResult = trajectoryExecutor->executePlan(computedMotionPlan, execOptions); |
| 160 | + executionSuccess = execResult.success; |
| 161 | + |
| 162 | + if (executionSuccess) |
| 163 | + { |
| 164 | + RCLCPP_INFO( |
| 165 | + getLogger(), "[CbMoveEndEffector] Execution succeeded (via CpTrajectoryExecutor)"); |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + RCLCPP_WARN( |
| 170 | + getLogger(), "[CbMoveEndEffector] Execution failed (via CpTrajectoryExecutor): %s", |
| 171 | + execResult.errorMessage.c_str()); |
| 172 | + } |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + // Fallback to legacy direct execution |
| 177 | + RCLCPP_WARN( |
| 178 | + getLogger(), |
| 179 | + "[CbMoveEndEffector] CpTrajectoryExecutor component not available, using legacy " |
| 180 | + "execution " |
| 181 | + "(consider adding CpTrajectoryExecutor component)"); |
| 182 | + |
| 183 | + auto executionResult = moveGroupInterface.execute(computedMotionPlan); |
| 184 | + executionSuccess = (executionResult == moveit_msgs::msg::MoveItErrorCodes::SUCCESS); |
| 185 | + |
| 186 | + RCLCPP_INFO( |
| 187 | + getLogger(), "[CbMoveEndEffector] Execution %s (legacy mode)", |
| 188 | + executionSuccess ? "succeeded" : "failed"); |
| 189 | + } |
98 | 190 |
|
99 | | - if (executionResult == moveit_msgs::msg::MoveItErrorCodes::SUCCESS) |
| 191 | + // Post events |
| 192 | + if (executionSuccess) |
100 | 193 | { |
101 | | - RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] motion execution succeeded"); |
102 | 194 | movegroupClient_->postEventMotionExecutionSucceded(); |
103 | 195 | this->postSuccessEvent(); |
104 | 196 | } |
105 | 197 | else |
106 | 198 | { |
107 | | - RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] motion execution failed"); |
108 | 199 | movegroupClient_->postEventMotionExecutionFailed(); |
109 | 200 | this->postFailureEvent(); |
110 | 201 | } |
111 | 202 | } |
112 | 203 | else |
113 | 204 | { |
114 | | - RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] motion execution failed"); |
| 205 | + RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] planning failed, skipping execution"); |
115 | 206 | movegroupClient_->postEventMotionExecutionFailed(); |
116 | 207 | this->postFailureEvent(); |
117 | 208 | } |
|
0 commit comments