SMACC2
Loading...
Searching...
No Matches
cb_pouring_motion.cpp
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
24
26{
28 const geometry_msgs::msg::Point & relativePivotPoint, double deltaHeight, std::string tipLink,
29 std::string globalFrame)
31 relativePivotPoint_(relativePivotPoint),
32 deltaHeight_(deltaHeight),
33 globalFrame_(globalFrame)
34
35{
36}
37
38#ifndef ROS_ROLLING
39
40geometry_msgs::msg::Point & toMsg(const tf2::Vector3 & in, geometry_msgs::msg::Point & out)
41{
42 out.x = in.getX();
43 out.y = in.getY();
44 out.z = in.getZ();
45 return out;
46}
47
48void toMsg(const tf2::Transform & in, geometry_msgs::msg::Pose & out)
49{
50 out.position.x = in.getOrigin().getX();
51 out.position.y = in.getOrigin().getY();
52 out.position.z = in.getOrigin().getZ();
53 out.orientation = toMsg(in.getRotation());
54}
55
56void fromMsg(const geometry_msgs::msg::Point & in, tf2::Vector3 & out)
57{
58 out = tf2::Vector3(in.x, in.y, in.z);
59}
60#endif
61
63{
64 // at least 1 sample per centimeter (average)
65 const double METERS_PER_SAMPLE = 0.001;
66
67 float dist_meters = 0;
68 float totallineardist = fabs(this->deltaHeight_);
69
70 int totalSamplesCount = totallineardist / METERS_PER_SAMPLE;
71 int steps = totallineardist / METERS_PER_SAMPLE;
72
73 float interpolation_factor_step = 1.0 / totalSamplesCount;
74
75 double secondsPerSample;
76
78 {
79 secondsPerSample = METERS_PER_SAMPLE / (*linearSpeed_m_s_);
80 }
81 else
82 {
83 secondsPerSample = std::numeric_limits<double>::max();
84 }
85
86 tf2::Stamped<tf2::Transform> currentEndEffectorTransform;
87
88 this->getCurrentEndEffectorPose(globalFrame_, currentEndEffectorTransform);
89
90 tf2::Transform lidEndEffectorTransform;
91 tf2::fromMsg(this->pointerRelativePose_, lidEndEffectorTransform);
92
93 tf2::Vector3 v0, v1;
94 v0 = (currentEndEffectorTransform * lidEndEffectorTransform).getOrigin();
95
96 tf2::Vector3 pivotPoint;
97
98#ifdef ROS_ROLLING
99 tf2::fromMsg(this->relativePivotPoint_, pivotPoint);
100#else
101 fromMsg(this->relativePivotPoint_, pivotPoint);
102
103#endif
104
105 tf2::Vector3 pivot = (currentEndEffectorTransform * pivotPoint);
106
107 v0 = v0 - pivot;
108 v1 = v0;
109 v1.setZ(v1.z() + this->deltaHeight_);
110
111 tf2::Vector3 vp1(v1);
112 tf2::Vector3 vp0(v0);
113 tf2::Quaternion rotation = tf2::shortestArcQuatNormalize2(vp0, vp1);
114
115 tf2::Quaternion initialEndEffectorOrientation = currentEndEffectorTransform.getRotation();
116 auto finalEndEffectorOrientation = initialEndEffectorOrientation * rotation;
117
118 tf2::Quaternion initialPointerOrientation =
119 initialEndEffectorOrientation * lidEndEffectorTransform.getRotation();
120 tf2::Quaternion finalPointerOrientation =
121 finalEndEffectorOrientation * lidEndEffectorTransform.getRotation();
122
123 // auto shortestAngle =
124 // tf2::angleShortestPath(initialEndEffectorOrientation, finalEndEffectorOrientation);
125
126 v0 += pivot;
127 v1 += pivot;
128
129 float linc = deltaHeight_ / steps; // METERS_PER_SAMPLE with sign
130 float interpolation_factor = 0;
131 tf2::Vector3 vi = v0;
132
133 tf2::Transform invertedLidTransform = lidEndEffectorTransform.inverse();
134 rclcpp::Time startTime = getNode()->now();
135
136 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] trajectory steps: " << steps);
137 for (float i = 0; i < steps; i++)
138 {
139 // auto currentEndEffectorOrientation =
140 // tf2::slerp(initialEndEffectorOrientation, finalEndEffectorOrientation, interpolation_factor);
141 auto currentPointerOrientation =
142 tf2::slerp(initialPointerOrientation, finalPointerOrientation, interpolation_factor);
143
144 interpolation_factor += interpolation_factor_step;
145 dist_meters += linc;
146
147 vi = v0;
148 vi.setZ(vi.getZ() + dist_meters);
149
150 tf2::Transform pose;
151 pose.setOrigin(vi);
152 pose.setRotation(currentPointerOrientation);
153
154 geometry_msgs::msg::PoseStamped pointerPose;
155 tf2::toMsg(pose, pointerPose.pose);
156 pointerPose.header.frame_id = globalFrame_;
157 pointerPose.header.stamp = startTime + rclcpp::Duration::from_seconds(i * secondsPerSample);
158 this->pointerTrajectory_.push_back(pointerPose);
159
160 tf2::Transform poseEndEffector = pose * invertedLidTransform;
161
162 geometry_msgs::msg::PoseStamped globalEndEffectorPose;
163 tf2::toMsg(poseEndEffector, globalEndEffectorPose.pose);
164 globalEndEffectorPose.header.frame_id = globalFrame_;
165 globalEndEffectorPose.header.stamp =
166 startTime + rclcpp::Duration::from_seconds(i * secondsPerSample);
167
168 this->endEffectorTrajectory_.push_back(globalEndEffectorPose);
169 RCLCPP_INFO_STREAM(
170 getLogger(), "[" << getName() << "] " << i << " - " << globalEndEffectorPose);
171 }
172 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "]trajectory generated, size: " << steps);
173}
174
176{
178
179 tf2::Stamped<tf2::Transform> currentEndEffectorTransform;
180 this->getCurrentEndEffectorPose(globalFrame_, currentEndEffectorTransform);
181 tf2::Vector3 pivotPoint;
182
183#ifdef ROS_ROLLING
184 tf2::fromMsg(this->relativePivotPoint_, pivotPoint);
185#else
186 fromMsg(this->relativePivotPoint_, pivotPoint);
187
188#endif
189 tf2::Vector3 pivot = (currentEndEffectorTransform * pivotPoint);
190
191 visualization_msgs::msg::Marker marker;
192
193 marker.ns = "trajectory";
194 marker.id = beahiorMarkers_.markers.size();
195 marker.type = visualization_msgs::msg::Marker::SPHERE;
196 marker.action = visualization_msgs::msg::Marker::ADD;
197 marker.scale.x = 0.01;
198 marker.scale.y = 0.01;
199 marker.scale.z = 0.01;
200
201 marker.color.a = 1.0;
202 marker.color.r = 0.0;
203 marker.color.g = 0;
204 marker.color.b = 1.0;
205
206#ifdef ROS_ROLLING
207 tf2::toMsg(pivot, marker.pose.position);
208#else
209 toMsg(pivot, marker.pose.position);
210#endif
211 marker.header.frame_id = globalFrame_;
212 marker.header.stamp = getNode()->now();
213
214 beahiorMarkers_.markers.push_back(marker);
215
216 tf2::Transform localdirection;
217 localdirection.setIdentity();
218 localdirection.setOrigin(tf2::Vector3(0.05, 0, 0));
219 auto frameid = this->pointerTrajectory_.front().header.frame_id;
220
221 for (auto & pose : this->pointerTrajectory_)
222 {
223 visualization_msgs::msg::Marker marker;
224 marker.header.frame_id = frameid;
225 marker.header.stamp = getNode()->now();
226 marker.ns = "trajectory";
227 marker.id = this->beahiorMarkers_.markers.size();
228 marker.type = visualization_msgs::msg::Marker::ARROW;
229 marker.action = visualization_msgs::msg::Marker::ADD;
230 marker.scale.x = 0.005;
231 marker.scale.y = 0.01;
232 marker.scale.z = 0.01;
233 marker.color.a = 0.8;
234 marker.color.r = 0.0;
235 marker.color.g = 1;
236 marker.color.b = 0.0;
237
238 geometry_msgs::msg::Point start, end;
239 start.x = 0;
240 start.y = 0;
241 start.z = 0;
242
243 tf2::Transform basetransform;
244 tf2::fromMsg(pose.pose, basetransform);
245 // tf2::Transform endarrow = localdirection * basetransform;
246
247 end.x = localdirection.getOrigin().x();
248 end.y = localdirection.getOrigin().y();
249 end.z = localdirection.getOrigin().z();
250
251 marker.pose.position = pose.pose.position;
252 marker.pose.orientation = pose.pose.orientation;
253 marker.points.push_back(start);
254 marker.points.push_back(end);
255
256 beahiorMarkers_.markers.push_back(marker);
257 }
258}
259
260} // namespace cl_move_group_interface
CbCircularPouringMotion(const geometry_msgs::msg::Point &pivotPoint, double deltaHeight, std::string tipLink, std::string globalFrame)
std::vector< geometry_msgs::msg::PoseStamped > pointerTrajectory_
void getCurrentEndEffectorPose(std::string globalFrame, tf2::Stamped< tf2::Transform > &currentEndEffectorTransform)
std::vector< geometry_msgs::msg::PoseStamped > endEffectorTrajectory_
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
geometry_msgs::msg::Point & toMsg(const tf2::Vector3 &in, geometry_msgs::msg::Point &out)
void fromMsg(const geometry_msgs::msg::Point &in, tf2::Vector3 &out)