SMACC2
Loading...
Searching...
No Matches
cb_move_end_effector.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 *****************************************************************************************************************/
20
21#pragma once
22
23#include <tf2/impl/utils.h>
27#include <future>
28#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
29
30using namespace std::chrono_literals;
31
32namespace cl_moveit2z
33{
35{
36public:
37 geometry_msgs::msg::PoseStamped targetPose;
38 std::string tip_link_;
39 std::optional<std::string> group_;
40
42
43 CbMoveEndEffector(geometry_msgs::msg::PoseStamped target_pose, std::string tip_link = "")
44 : targetPose(target_pose)
45 {
46 tip_link_ = tip_link;
47 }
48
49 virtual void onEntry() override
50 {
51 // components resolved in onStateOrthogonalAllocation (base class)
52 if (this->group_)
53 {
54 RCLCPP_DEBUG(
55 getLogger(), "[CbMoveEndEfector] new thread started to move absolute end effector");
56 moveit::planning_interface::MoveGroupInterface move_group(getNode(), *group_);
57 this->moveToAbsolutePose(move_group, targetPose);
58 RCLCPP_DEBUG(
59 getLogger(), "[CbMoveEndEfector] to move absolute end effector thread destroyed");
60 }
61 else
62 {
63 RCLCPP_DEBUG(
64 getLogger(), "[CbMoveEndEfector] new thread started to move absolute end effector");
66 RCLCPP_DEBUG(
67 getLogger(), "[CbMoveEndEfector] to move absolute end effector thread destroyed");
68 }
69 }
70
71protected:
73 moveit::planning_interface::MoveGroupInterface & moveGroupInterface,
74 geometry_msgs::msg::PoseStamped & targetObjectPose)
75 {
76 RCLCPP_DEBUG(getLogger(), "[CbMoveEndEffector] Synchronous sleep of 1 seconds");
77 rclcpp::sleep_for(500ms);
78
79 // Try to use CpMotionPlanner component (preferred)
80 CpMotionPlanner * motionPlanner = cpMotionPlanner_;
81
82 bool success = false;
83 moveit::planning_interface::MoveGroupInterface::Plan computedMotionPlan;
84
85 RCLCPP_INFO_STREAM(
86 getLogger(), "[CbMoveEndEffector] Target End effector Pose: " << targetObjectPose);
87
88 if (motionPlanner != nullptr)
89 {
90 // Use component-based motion planner (preferred)
91 RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] Using CpMotionPlanner component for planning");
92
93 PlanningOptions options;
94 options.planningTime = 1.0;
95 options.poseReferenceFrame = targetObjectPose.header.frame_id;
96
97 std::optional<std::string> tipLinkOpt;
98 if (!tip_link_.empty())
99 {
100 tipLinkOpt = tip_link_;
101 }
102
103 auto result = motionPlanner->planToPose(targetObjectPose, tipLinkOpt, options);
104
105 success = result.success;
106 if (success)
107 {
108 computedMotionPlan = result.plan;
109 RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] Planning succeeded (via CpMotionPlanner)");
110 }
111 else
112 {
113 RCLCPP_WARN(
114 getLogger(), "[CbMoveEndEffector] Planning failed (via CpMotionPlanner): %s",
115 result.errorMessage.c_str());
116 }
117 }
118 else
119 {
120 // Fallback to legacy direct API calls
121 RCLCPP_WARN(
122 getLogger(),
123 "[CbMoveEndEffector] CpMotionPlanner component not available, using legacy planning "
124 "(consider adding CpMotionPlanner component)");
125
126 moveGroupInterface.setPlanningTime(1.0);
127 moveGroupInterface.setPoseTarget(targetObjectPose, tip_link_);
128 moveGroupInterface.setPoseReferenceFrame(targetObjectPose.header.frame_id);
129
130 success =
131 (moveGroupInterface.plan(computedMotionPlan) == moveit::core::MoveItErrorCode::SUCCESS);
132 RCLCPP_INFO(
133 getLogger(), "[CbMoveEndEffector] Planning %s (legacy mode)",
134 success ? "succeeded" : "FAILED");
135 }
136
137 // Execution
138 if (success)
139 {
140 // Try to use CpTrajectoryExecutor component (preferred)
141 CpTrajectoryExecutor * trajectoryExecutor = cpTrajectoryExecutor_;
142
143 bool executionSuccess = false;
144
145 if (trajectoryExecutor != nullptr)
146 {
147 // Use component-based trajectory executor (preferred)
148 RCLCPP_INFO(
149 getLogger(), "[CbMoveEndEffector] Using CpTrajectoryExecutor component for execution");
150
151 ExecutionOptions execOptions;
152 execOptions.trajectoryName = this->getName();
153
154 auto execResult = trajectoryExecutor->executePlan(computedMotionPlan, execOptions);
155 executionSuccess = execResult.success;
156
157 if (executionSuccess)
158 {
159 RCLCPP_INFO(
160 getLogger(), "[CbMoveEndEffector] Execution succeeded (via CpTrajectoryExecutor)");
161 }
162 else
163 {
164 RCLCPP_WARN(
165 getLogger(), "[CbMoveEndEffector] Execution failed (via CpTrajectoryExecutor): %s",
166 execResult.errorMessage.c_str());
167 }
168 }
169 else
170 {
171 // Fallback to legacy direct execution
172 RCLCPP_WARN(
173 getLogger(),
174 "[CbMoveEndEffector] CpTrajectoryExecutor component not available, using legacy "
175 "execution "
176 "(consider adding CpTrajectoryExecutor component)");
177
178 auto executionResult = moveGroupInterface.execute(computedMotionPlan);
179 executionSuccess = (executionResult == moveit_msgs::msg::MoveItErrorCodes::SUCCESS);
180
181 RCLCPP_INFO(
182 getLogger(), "[CbMoveEndEffector] Execution %s (legacy mode)",
183 executionSuccess ? "succeeded" : "failed");
184 }
185
186 // Post events
187 if (executionSuccess)
188 {
189 this->postMotionSuccess();
190 }
191 else
192 {
193 this->postMotionFailure();
194 }
195 }
196 else
197 {
198 RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] planning failed, skipping execution");
199 this->postMotionFailure();
200 }
201
202 RCLCPP_DEBUG(getLogger(), "[CbMoveEndEffector] Synchronous sleep of 1 seconds");
203 rclcpp::sleep_for(500ms);
204
205 return success;
206 }
207};
208} // namespace cl_moveit2z
bool moveToAbsolutePose(moveit::planning_interface::MoveGroupInterface &moveGroupInterface, geometry_msgs::msg::PoseStamped &targetObjectPose)
std::optional< std::string > group_
CbMoveEndEffector(geometry_msgs::msg::PoseStamped target_pose, std::string tip_link="")
geometry_msgs::msg::PoseStamped targetPose
Component for centralized motion planning operations.
PlanningResult planToPose(const geometry_msgs::msg::PoseStamped &target, const std::optional< std::string > &tipLink=std::nullopt, const PlanningOptions &options={})
Plan to a Cartesian pose.
std::shared_ptr< moveit::planning_interface::MoveGroupInterface > moveGroupClientInterface
Component for centralized trajectory execution.
ExecutionResult executePlan(const moveit::planning_interface::MoveGroupInterface::Plan &plan, const ExecutionOptions &options={})
Execute a motion plan synchronously.
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
Configuration options for trajectory execution.
std::optional< std::string > trajectoryName
Configuration options for motion planning.
std::optional< std::string > poseReferenceFrame
std::optional< double > planningTime