SMACC2
Loading...
Searching...
No Matches
cb_move_cartesian_relative2.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/transform_datatypes.h>
27#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::Vector3 offset_;
38
39 std::optional<double> linearSpeed_m_s_;
40
41 CbMoveCartesianRelative2(std::string referenceFrame, std::string tipLink)
42 : CbMoveEndEffectorTrajectory(std::move(tipLink)), globalFrame_(std::move(referenceFrame))
43 {
44 }
45
47 std::string referenceFrame, std::string tipLink, geometry_msgs::msg::Vector3 offset)
48 : CbMoveEndEffectorTrajectory(std::move(tipLink)),
49 offset_(offset),
50 globalFrame_(std::move(referenceFrame))
51 {
52 }
53
55
56 void generateTrajectory() override
57 {
58 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] generating trajectory");
59 // at least 1 sample per centimeter (average)
60 const double METERS_PER_SAMPLE = 0.001;
61
62 tf2::Vector3 voffset;
63 tf2::fromMsg(offset_, voffset);
64
65 float totallineardist = static_cast<float>(voffset.length());
66
67 int totalSamplesCount = static_cast<int>(totallineardist / METERS_PER_SAMPLE);
68 int steps = totalSamplesCount;
69
70 float interpolation_factor_step = static_cast<float>(1.0 / totalSamplesCount);
71
72 double secondsPerSample;
73
75 {
76 linearSpeed_m_s_ = 0.1;
77 }
78
79 secondsPerSample = METERS_PER_SAMPLE / (*linearSpeed_m_s_);
80
81 tf2::Stamped<tf2::Transform> currentEndEffectorTransform;
82 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] getting current end effector pose");
83 this->getCurrentEndEffectorPose(globalFrame_, currentEndEffectorTransform);
84
85 tf2::Transform finalEndEffectorTransform = currentEndEffectorTransform;
86 finalEndEffectorTransform.setOrigin(finalEndEffectorTransform.getOrigin() + voffset);
87
88 float interpolation_factor = 0;
89
90 rclcpp::Time startTime = getNode()->now();
91 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] trajectory steps: " << steps);
92
93 for (int i = 0; i < steps; i++)
94 {
95 interpolation_factor += interpolation_factor_step;
96
97 tf2::Vector3 vi = currentEndEffectorTransform.getOrigin().lerp(
98 finalEndEffectorTransform.getOrigin(), interpolation_factor);
99
100 tf2::Transform pose;
101 pose.setOrigin(vi);
102 pose.setRotation(currentEndEffectorTransform.getRotation());
103
104 geometry_msgs::msg::PoseStamped pointerPose;
105 tf2::toMsg(pose, pointerPose.pose);
106 pointerPose.header.frame_id = globalFrame_;
107 pointerPose.header.stamp = startTime + rclcpp::Duration::from_seconds(i * secondsPerSample);
108
109 this->endEffectorTrajectory_.push_back(pointerPose);
110 }
111
112 RCLCPP_INFO_STREAM(
113 getLogger(),
114 "[" << getName() << "] Target End efector Pose: " << this->endEffectorTrajectory_.back());
115 }
116
117private:
118 std::string globalFrame_;
119};
120} // namespace cl_moveit2z
CbMoveCartesianRelative2(std::string referenceFrame, std::string tipLink, geometry_msgs::msg::Vector3 offset)
CbMoveCartesianRelative2(std::string referenceFrame, std::string tipLink)
std::vector< geometry_msgs::msg::PoseStamped > endEffectorTrajectory_
void getCurrentEndEffectorPose(std::string globalFrame, tf2::Stamped< tf2::Transform > &currentEndEffectorTransform)
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const