SMACC2
Loading...
Searching...
No Matches
cb_move_joints.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 <algorithm>
24#include <future>
25#include <map>
26#include <sstream>
27#include <string>
28
33
34namespace cl_moveit2z
35{
37{
38public:
39 std::optional<double> scalingFactor_;
40 std::map<std::string, double> jointValueTarget_;
41 std::optional<std::string> group_;
42
44
45 CbMoveJoints(const std::map<std::string, double> & jointValueTarget)
46 : jointValueTarget_(jointValueTarget)
47 {
48 }
49
50 virtual void onEntry() override
51 {
53
54 if (this->group_)
55 {
56 moveit::planning_interface::MoveGroupInterface move_group(
57 getNode(), moveit::planning_interface::MoveGroupInterface::Options(*(this->group_)));
58 this->moveJoints(move_group);
59 }
60 else
61 {
63 }
64 }
65
66 virtual void onExit() override {}
67
68protected:
69 static std::string currentJointStatesToString(
70 moveit::planning_interface::MoveGroupInterface & moveGroupInterface,
71 std::map<std::string, double> & targetJoints)
72 {
73 auto state = moveGroupInterface.getCurrentState();
74
75 if (state == nullptr) return std::string();
76
77 auto vnames = state->getVariableNames();
78
79 std::stringstream ss;
80
81 for (auto & tgj : targetJoints)
82 {
83 auto it = std::find(vnames.begin(), vnames.end(), tgj.first);
84 auto index = std::distance(vnames.begin(), it);
85
86 ss << tgj.first << ":" << state->getVariablePosition(index) << std::endl;
87 }
88
89 return ss.str();
90 }
91
92 void moveJoints(moveit::planning_interface::MoveGroupInterface & moveGroupInterface)
93 {
94 if (jointValueTarget_.size() == 0)
95 {
96 RCLCPP_WARN(getLogger(), "[CbMoveJoints] No joint value specified. Skipping planning call.");
98 this->postFailureEvent();
99 return;
100 }
101
102 // Try to use CpMotionPlanner component (preferred)
103 CpMotionPlanner * motionPlanner = nullptr;
104 this->requiresComponent(
105 motionPlanner, smacc2::ComponentRequirement::SOFT); // Optional component
106
107 bool success = false;
108 moveit::planning_interface::MoveGroupInterface::Plan computedMotionPlan;
109
110 if (motionPlanner != nullptr)
111 {
112 // Use component-based motion planner (preferred)
113 RCLCPP_INFO(getLogger(), "[CbMoveJoints] Using CpMotionPlanner component for joint planning");
114
115 PlanningOptions options;
116 if (scalingFactor_)
117 {
119 }
120
121 auto result = motionPlanner->planToJointTarget(jointValueTarget_, options);
122
123 success = result.success;
124 if (success)
125 {
126 computedMotionPlan = result.plan;
127 RCLCPP_INFO(getLogger(), "[CbMoveJoints] Planning succeeded (via CpMotionPlanner)");
128 }
129 else
130 {
131 RCLCPP_WARN(
132 getLogger(), "[CbMoveJoints] Planning failed (via CpMotionPlanner): %s",
133 result.errorMessage.c_str());
134 }
135 }
136 else
137 {
138 // Fallback to legacy direct API calls
139 RCLCPP_WARN(
140 getLogger(),
141 "[CbMoveJoints] CpMotionPlanner component not available, using legacy planning "
142 "(consider adding CpMotionPlanner component)");
143
144 if (scalingFactor_) moveGroupInterface.setMaxVelocityScalingFactor(*scalingFactor_);
145
146 moveGroupInterface.setJointValueTarget(jointValueTarget_);
147
148 auto result = moveGroupInterface.plan(computedMotionPlan);
149
150 success = (result == moveit::core::MoveItErrorCode::SUCCESS);
151
152 RCLCPP_INFO(
153 getLogger(), "[CbMoveJoints] Planning %s (legacy mode, code: %d)",
154 success ? "SUCCESS" : "FAILED", result.val);
155 }
156
157 // Execution
158 if (success)
159 {
160 // Try to use CpTrajectoryExecutor component (preferred)
161 CpTrajectoryExecutor * trajectoryExecutor = nullptr;
162 this->requiresComponent(
163 trajectoryExecutor, smacc2::ComponentRequirement::SOFT); // Optional component
164
165 bool executionSuccess = false;
166
167 if (trajectoryExecutor != nullptr)
168 {
169 // Use component-based trajectory executor (preferred)
170 RCLCPP_INFO(
171 getLogger(), "[CbMoveJoints] Using CpTrajectoryExecutor component for execution");
172
173 ExecutionOptions execOptions;
174 execOptions.trajectoryName = this->getName();
175 if (scalingFactor_)
176 {
177 execOptions.maxVelocityScaling = *scalingFactor_;
178 }
179
180 auto execResult = trajectoryExecutor->executePlan(computedMotionPlan, execOptions);
181 executionSuccess = execResult.success;
182
183 if (executionSuccess)
184 {
185 RCLCPP_INFO(getLogger(), "[CbMoveJoints] Execution succeeded (via CpTrajectoryExecutor)");
186 }
187 else
188 {
189 RCLCPP_WARN(
190 getLogger(), "[CbMoveJoints] Execution failed (via CpTrajectoryExecutor): %s",
191 execResult.errorMessage.c_str());
192 }
193 }
194 else
195 {
196 // Fallback to legacy direct execution
197 RCLCPP_WARN(
198 getLogger(),
199 "[CbMoveJoints] CpTrajectoryExecutor component not available, using legacy execution "
200 "(consider adding CpTrajectoryExecutor component)");
201
202 auto executionResult = moveGroupInterface.execute(computedMotionPlan);
203 executionSuccess = (executionResult == moveit_msgs::msg::MoveItErrorCodes::SUCCESS);
204
205 RCLCPP_INFO(
206 getLogger(), "[CbMoveJoints] Execution %s (legacy mode)",
207 executionSuccess ? "succeeded" : "failed");
208 }
209
210 // Post events
211 if (executionSuccess)
212 {
213 RCLCPP_INFO_STREAM(
214 getLogger(),
215 "[" << this->getName() << "] motion execution succeeded. Throwing success event.");
217 this->postSuccessEvent();
218 }
219 else
220 {
221 RCLCPP_WARN_STREAM(
222 getLogger(), "[" << this->getName() << "] motion execution failed. Throwing fail event.");
224 this->postFailureEvent();
225 }
226 }
227 else
228 {
229 auto statestr = currentJointStatesToString(moveGroupInterface, jointValueTarget_);
230 RCLCPP_WARN_STREAM(
231 getLogger(), "[" << this->getName() << "] planning failed. Throwing fail event."
232 << std::endl
233 << statestr);
235 this->postFailureEvent();
236 }
237 }
238
240};
241} // namespace cl_moveit2z
virtual void onEntry() override
static std::string currentJointStatesToString(moveit::planning_interface::MoveGroupInterface &moveGroupInterface, std::map< std::string, double > &targetJoints)
CbMoveJoints(const std::map< std::string, double > &jointValueTarget)
std::map< std::string, double > jointValueTarget_
void moveJoints(moveit::planning_interface::MoveGroupInterface &moveGroupInterface)
virtual void onExit() override
std::optional< double > scalingFactor_
std::optional< std::string > group_
std::shared_ptr< moveit::planning_interface::MoveGroupInterface > moveGroupClientInterface
Component for centralized motion planning operations.
PlanningResult planToJointTarget(const std::map< std::string, double > &jointTargets, const PlanningOptions &options={})
Plan to joint values.
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
void requiresClient(SmaccClientType *&storage)
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
Configuration options for trajectory execution.
std::optional< double > maxVelocityScaling
std::optional< std::string > trajectoryName
Configuration options for motion planning.
std::optional< double > maxVelocityScaling