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(const geometry_msgs::msg::PoseStamped & target_pose, std::string tip_link = "")
44 : targetPose(target_pose), tip_link_(std::move(tip_link))
45 {
46 }
47
48 virtual void onEntry() override
49 {
50 // components resolved in onStateOrthogonalAllocation (base class)
51 if (this->group_)
52 {
53 RCLCPP_DEBUG(
54 getLogger(), "[CbMoveEndEfector] new thread started to move absolute end effector");
55 moveit::planning_interface::MoveGroupInterface move_group(getNode(), *group_);
56 this->moveToAbsolutePose(move_group, targetPose);
57 RCLCPP_DEBUG(
58 getLogger(), "[CbMoveEndEfector] to move absolute end effector thread destroyed");
59 }
60 else
61 {
62 RCLCPP_DEBUG(
63 getLogger(), "[CbMoveEndEfector] new thread started to move absolute end effector");
65 RCLCPP_DEBUG(
66 getLogger(), "[CbMoveEndEfector] to move absolute end effector thread destroyed");
67 }
68 }
69
70protected:
72 moveit::planning_interface::MoveGroupInterface & moveGroupInterface,
73 geometry_msgs::msg::PoseStamped & targetObjectPose)
74 {
75 RCLCPP_DEBUG(getLogger(), "[CbMoveEndEffector] Synchronous sleep of 1 seconds");
76 rclcpp::sleep_for(500ms);
77
78 // Try to use CpMotionPlanner component (preferred)
79 CpMotionPlanner * motionPlanner = cpMotionPlanner_;
80
81 bool success = false;
82 moveit::planning_interface::MoveGroupInterface::Plan computedMotionPlan;
83
84 RCLCPP_INFO_STREAM(
85 getLogger(), "[CbMoveEndEffector] Target End effector Pose: " << targetObjectPose);
86
87 if (motionPlanner != nullptr)
88 {
89 // Use component-based motion planner (preferred)
90 RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] Using CpMotionPlanner component for planning");
91
92 PlanningOptions options;
93 options.planningTime = 1.0;
94 options.poseReferenceFrame = targetObjectPose.header.frame_id;
95
96 std::optional<std::string> tipLinkOpt;
97 if (!tip_link_.empty())
98 {
99 tipLinkOpt = tip_link_;
100 }
101
102 auto result = motionPlanner->planToPose(targetObjectPose, tipLinkOpt, options);
103
104 success = result.success;
105 if (success)
106 {
107 computedMotionPlan = result.plan;
108 RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] Planning succeeded (via CpMotionPlanner)");
109 }
110 else
111 {
112 RCLCPP_WARN(
113 getLogger(), "[CbMoveEndEffector] Planning failed (via CpMotionPlanner): %s",
114 result.errorMessage.c_str());
115 }
116 }
117 else
118 {
119 // Fallback to legacy direct API calls
120 RCLCPP_WARN(
121 getLogger(),
122 "[CbMoveEndEffector] CpMotionPlanner component not available, using legacy planning "
123 "(consider adding CpMotionPlanner component)");
124
125 moveGroupInterface.setPlanningTime(1.0);
126 moveGroupInterface.setPoseTarget(targetObjectPose, tip_link_);
127 moveGroupInterface.setPoseReferenceFrame(targetObjectPose.header.frame_id);
128
129 success =
130 (moveGroupInterface.plan(computedMotionPlan) == moveit::core::MoveItErrorCode::SUCCESS);
131 RCLCPP_INFO(
132 getLogger(), "[CbMoveEndEffector] Planning %s (legacy mode)",
133 success ? "succeeded" : "FAILED");
134 }
135
136 // Execution
137 if (success)
138 {
139 // Try to use CpTrajectoryExecutor component (preferred)
140 CpTrajectoryExecutor * trajectoryExecutor = cpTrajectoryExecutor_;
141
142 bool executionSuccess = false;
143
144 if (trajectoryExecutor != nullptr)
145 {
146 // Use component-based trajectory executor (preferred)
147 RCLCPP_INFO(
148 getLogger(), "[CbMoveEndEffector] Using CpTrajectoryExecutor component for execution");
149
150 ExecutionOptions execOptions;
151 execOptions.trajectoryName = this->getName();
152
153 auto execResult = trajectoryExecutor->executePlan(computedMotionPlan, execOptions);
154 executionSuccess = execResult.success;
155
156 if (executionSuccess)
157 {
158 RCLCPP_INFO(
159 getLogger(), "[CbMoveEndEffector] Execution succeeded (via CpTrajectoryExecutor)");
160 }
161 else
162 {
163 RCLCPP_WARN(
164 getLogger(), "[CbMoveEndEffector] Execution failed (via CpTrajectoryExecutor): %s",
165 execResult.errorMessage.c_str());
166 }
167 }
168 else
169 {
170 // Fallback to legacy direct execution
171 RCLCPP_WARN(
172 getLogger(),
173 "[CbMoveEndEffector] CpTrajectoryExecutor component not available, using legacy "
174 "execution "
175 "(consider adding CpTrajectoryExecutor component)");
176
177 auto executionResult = moveGroupInterface.execute(computedMotionPlan);
178 executionSuccess = (executionResult == moveit_msgs::msg::MoveItErrorCodes::SUCCESS);
179
180 RCLCPP_INFO(
181 getLogger(), "[CbMoveEndEffector] Execution %s (legacy mode)",
182 executionSuccess ? "succeeded" : "failed");
183 }
184
185 // Post events
186 if (executionSuccess)
187 {
188 this->postMotionSuccess();
189 }
190 else
191 {
192 this->postMotionFailure();
193 }
194 }
195 else
196 {
197 RCLCPP_INFO(getLogger(), "[CbMoveEndEffector] planning failed, skipping execution");
198 this->postMotionFailure();
199 }
200
201 RCLCPP_DEBUG(getLogger(), "[CbMoveEndEffector] Synchronous sleep of 1 seconds");
202 rclcpp::sleep_for(500ms);
203
204 return success;
205 }
206};
207} // namespace cl_moveit2z
bool moveToAbsolutePose(moveit::planning_interface::MoveGroupInterface &moveGroupInterface, geometry_msgs::msg::PoseStamped &targetObjectPose)
CbMoveEndEffector(const geometry_msgs::msg::PoseStamped &target_pose, std::string tip_link="")
std::optional< std::string > group_
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