SMACC2
Loading...
Searching...
No Matches
cb_circular_pivot_motion.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#pragma once
21
22#include <tf2/transform_datatypes.h>
23#include <tf2_ros/transform_listener.h>
25#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
27
28using namespace std::chrono_literals;
29
30namespace cl_moveit2z
31{
32// executes a circular trajectory of the tipLink around one axis defined
33// by the z-axis of the plaenePivotPose
35{
36public:
37 std::optional<double> angularSpeed_rad_s_;
38 std::optional<double> linearSpeed_m_s_;
39
40 // if not specified it would be used the current robot pose
41 std::optional<geometry_msgs::msg::Pose> relativeInitialPose_;
42
43 CbCircularPivotMotion(std::optional<std::string> tipLink = std::nullopt)
45 {
46 }
47
49 const geometry_msgs::msg::PoseStamped & planePivotPose, double deltaRadians,
50 std::optional<std::string> tipLink = std::nullopt)
52 planePivotPose_(planePivotPose),
53 deltaRadians_(deltaRadians)
54 {
55 if (tipLink_) planePivotPose_.header.frame_id = *tipLink_;
56 }
57
59 const geometry_msgs::msg::PoseStamped & planePivotPose,
60 const geometry_msgs::msg::Pose & relativeInitialPose, double deltaRadians,
61 std::optional<std::string> tipLink = std::nullopt)
63 relativeInitialPose_(relativeInitialPose),
64 planePivotPose_(planePivotPose),
65 deltaRadians_(deltaRadians)
66 {
67 }
68
69 virtual void generateTrajectory() override
70 {
72 {
74 }
75
76 // The transform lookup can fail and leave the pose unset: no trajectory, and the behavior fails.
77 if (!relativeInitialPose_)
78 {
79 RCLCPP_ERROR_STREAM(
80 getLogger(),
81 "[" << getName()
82 << "] no end effector pose relative to the pivot, no trajectory generated");
83 this->postMotionFailure();
84 return;
85 }
86 const geometry_msgs::msg::Pose & initialPose = *relativeInitialPose_;
87
88 // project offset into the xy-plane
89 // get the radius
90 double radius = sqrt(
91 initialPose.position.z * initialPose.position.z +
92 initialPose.position.y * initialPose.position.y);
93 double initialAngle = atan2(initialPose.position.z, initialPose.position.y);
94
95 double totallineardist = fabs(radius * deltaRadians_);
96 double totalangulardist = fabs(deltaRadians_);
97
98 // at least 1 sample per centimeter (average)
99 // at least 1 sample per ~1.1 degrees (average)
100
101 const double RADS_PER_SAMPLE = 0.02;
102 const double METERS_PER_SAMPLE = 0.01;
103
104 int totalSamplesCount = static_cast<int>(
105 std::max(totallineardist / METERS_PER_SAMPLE, totalangulardist / RADS_PER_SAMPLE));
106
107 double linearSecondsPerSample;
108 double angularSecondsPerSamples;
109 double secondsPerSample;
110
112 {
113 linearSecondsPerSample = METERS_PER_SAMPLE / (*linearSpeed_m_s_);
114 }
115 else
116 {
117 linearSecondsPerSample = std::numeric_limits<double>::max();
118 }
119
121 {
122 angularSecondsPerSamples = RADS_PER_SAMPLE / (*angularSpeed_rad_s_);
123 }
124 else
125 {
126 angularSecondsPerSamples = std::numeric_limits<double>::max();
127 }
128
130 {
131 secondsPerSample = 0.5;
132 }
133 else
134 {
135 secondsPerSample = std::min(linearSecondsPerSample, angularSecondsPerSamples);
136 }
137
138 double currentAngle = initialAngle;
139
140 double angleStep = deltaRadians_ / (double)totalSamplesCount;
141
142 tf2::Transform tfBasePose;
143 tf2::fromMsg(planePivotPose_.pose, tfBasePose);
144
145 RCLCPP_INFO_STREAM(
146 getLogger(),
147 "[" << getName() << "] generated trajectory, total samples: " << totalSamplesCount);
148 for (int i = 0; i < totalSamplesCount; i++)
149 {
150 // relativePose i
151 double y = radius * cos(currentAngle);
152 double z = radius * sin(currentAngle);
153
154 geometry_msgs::msg::Pose relativeCurrentPose;
155
156 relativeCurrentPose.position.x = initialPose.position.x;
157 relativeCurrentPose.position.y = y;
158 relativeCurrentPose.position.z = z;
159
160 tf2::Quaternion localquat;
161 localquat.setEuler(currentAngle, 0, 0);
162
163 relativeCurrentPose.orientation.w = 1;
164
165 tf2::Transform tfRelativeCurrentPose;
166 tf2::fromMsg(relativeCurrentPose, tfRelativeCurrentPose);
167
168 tf2::Transform tfGlobalPose = tfRelativeCurrentPose * tfBasePose;
169
170 tfGlobalPose.setRotation(tfGlobalPose.getRotation() * localquat);
171
172 geometry_msgs::msg::PoseStamped globalPose;
173 tf2::toMsg(tfGlobalPose, globalPose.pose);
174 globalPose.header.frame_id = planePivotPose_.header.frame_id;
175 globalPose.header.stamp = rclcpp::Time(planePivotPose_.header.stamp) +
176 rclcpp::Duration::from_seconds(i * secondsPerSample);
177 RCLCPP_INFO_STREAM(
178 getLogger(),
179 "[" << getName() << "]" << rclcpp::Time(globalPose.header.stamp).nanoseconds());
180
181 this->endEffectorTrajectory_.push_back(globalPose);
182 currentAngle += angleStep;
183 }
184 }
185
186 virtual void createMarkers() override
187 {
189
190 tf2::Transform localdirection;
191 localdirection.setIdentity();
192 localdirection.setOrigin(tf2::Vector3(0.12, 0, 0));
193 auto frameid = planePivotPose_.header.frame_id;
194
195 visualization_msgs::msg::Marker marker;
196 marker.header.frame_id = frameid;
197 marker.header.stamp = getNode()->now();
198 marker.ns = "trajectory";
199 marker.id = static_cast<int>(beahiorMarkers_.markers.size());
200 marker.type = visualization_msgs::msg::Marker::ARROW;
201 marker.action = visualization_msgs::msg::Marker::ADD;
202 marker.scale.x = 0.01;
203 marker.scale.y = 0.02;
204 marker.scale.z = 0.02;
205 marker.color.a = 1.0;
206 marker.color.r = 0.0;
207 marker.color.g = 0;
208 marker.color.b = 1.0;
209
210 geometry_msgs::msg::Point start, end;
211 start.x = planePivotPose_.pose.position.x;
212 start.y = planePivotPose_.pose.position.y;
213 start.z = planePivotPose_.pose.position.z;
214
215 tf2::Transform basetransform;
216 tf2::fromMsg(planePivotPose_.pose, basetransform);
217 tf2::Transform endarrow = localdirection * basetransform;
218
219 end.x = endarrow.getOrigin().x();
220 end.y = endarrow.getOrigin().y();
221 end.z = endarrow.getOrigin().z();
222
223 marker.pose.orientation.w = 1;
224 marker.points.push_back(start);
225 marker.points.push_back(end);
226
227 beahiorMarkers_.markers.push_back(marker);
228 }
229
230protected:
231 // the rotation axis (z-axis) and the center of rotation in the space
232 geometry_msgs::msg::PoseStamped planePivotPose_;
233
234 double deltaRadians_ = 0.0;
235
236private:
238 {
239 // components resolved in onStateOrthogonalAllocation (base chain)
240 CpTfListener * tfListener = cpTfListener_;
241
242 tf2::Stamped<tf2::Transform> endEffectorInPivotFrame;
243
244 try
245 {
246 if (!tipLink_ || *tipLink_ == "")
247 {
248 tipLink_ = this->cpMoveGroup_->moveGroupClientInterface->getEndEffectorLink();
249 }
250
251 RCLCPP_INFO_STREAM(
252 getLogger(), "[" << getName() << "] waiting transform, pivot: '"
253 << planePivotPose_.header.frame_id << "' tipLink: '" << *tipLink_ << "'");
254
255 if (tfListener != nullptr)
256 {
257 // Use component-based TF listener (preferred)
258 auto transformOpt =
259 tfListener->lookupTransform(planePivotPose_.header.frame_id, *tipLink_, rclcpp::Time());
260
261 if (transformOpt)
262 {
263 tf2::fromMsg(transformOpt.value(), endEffectorInPivotFrame);
264 }
265 else
266 {
267 RCLCPP_ERROR_STREAM(
268 getLogger(), "[" << getName() << "] Failed to lookup transform from " << *tipLink_
269 << " to " << planePivotPose_.header.frame_id);
270 return;
271 }
272 }
273 else
274 {
275 // Fallback to legacy TF2 usage if component not available
276 RCLCPP_WARN_STREAM(
277 getLogger(), "[" << getName()
278 << "] CpTfListener component not available, using legacy TF2 (consider "
279 "adding CpTfListener component)");
280 tf2_ros::Buffer tfBuffer(getNode()->get_clock());
281 tf2_ros::TransformListener tfListenerLegacy(tfBuffer);
282
283 tf2::fromMsg(
284 tfBuffer.lookupTransform(
285 planePivotPose_.header.frame_id, *tipLink_, rclcpp::Time(), rclcpp::Duration(10s)),
286 endEffectorInPivotFrame);
287 }
288 }
289 catch (const std::exception & e)
290 {
291 RCLCPP_ERROR_STREAM(
292 getLogger(),
293 "[" << getName()
294 << "] Exception in computeCurrentEndEffectorPoseRelativeToPivot: " << e.what());
295 return;
296 }
297
298 // now pivot and EndEffector share a common reference frame (let say map)
299 // now get the current pose from the pivot reference frame with inverse composition
300 tf2::Transform pivotTransform;
301 tf2::fromMsg(planePivotPose_.pose, pivotTransform);
302 tf2::Transform invertedNewReferenceFrame = pivotTransform.inverse();
303
304 tf2::Transform currentPoseRelativeToPivot = invertedNewReferenceFrame * endEffectorInPivotFrame;
305
306 geometry_msgs::msg::Pose finalEndEffectorRelativePose;
307 tf2::toMsg(currentPoseRelativeToPivot, finalEndEffectorRelativePose);
308 relativeInitialPose_ = finalEndEffectorRelativePose;
309 }
310};
311
312} // namespace cl_moveit2z
CbCircularPivotMotion(std::optional< std::string > tipLink=std::nullopt)
std::optional< geometry_msgs::msg::Pose > relativeInitialPose_
CbCircularPivotMotion(const geometry_msgs::msg::PoseStamped &planePivotPose, const geometry_msgs::msg::Pose &relativeInitialPose, double deltaRadians, std::optional< std::string > tipLink=std::nullopt)
geometry_msgs::msg::PoseStamped planePivotPose_
CbCircularPivotMotion(const geometry_msgs::msg::PoseStamped &planePivotPose, double deltaRadians, std::optional< std::string > tipLink=std::nullopt)
std::vector< geometry_msgs::msg::PoseStamped > endEffectorTrajectory_
std::shared_ptr< moveit::planning_interface::MoveGroupInterface > moveGroupClientInterface
Component for shared TF2 transform management across all behaviors.
std::optional< geometry_msgs::msg::TransformStamped > lookupTransform(const std::string &target_frame, const std::string &source_frame, const rclcpp::Time &time=rclcpp::Time(0))
Thread-safe transform lookup.
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const