moveit2
The MoveIt Motion Planning Framework for ROS 2.
trajectory_blender_transition_window.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018 Pilz GmbH & Co. KG
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Pilz GmbH & Co. KG nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
36 
37 #include <algorithm>
38 #include <memory>
39 #include <math.h>
40 #include <tf2_eigen/tf2_eigen.hpp>
42 
43 namespace
44 {
45 static const rclcpp::Logger LOGGER =
46  rclcpp::get_logger("moveit.pilz_industrial_motion_planner.trajectory_blender_transition_window");
47 }
48 
50  const planning_scene::PlanningSceneConstPtr& planning_scene,
53 {
54  RCLCPP_INFO(LOGGER, "Start trajectory blending using transition window.");
55 
56  double sampling_time = 0.;
57  if (!validateRequest(req, sampling_time, res.error_code))
58  {
59  RCLCPP_ERROR(LOGGER, "Trajectory blend request is not valid.");
60  return false;
61  }
62 
63  // search for intersection points of the two trajectories with the blending
64  // sphere
65  // intersection points belongs to blend trajectory after blending
66  std::size_t first_intersection_index;
67  std::size_t second_intersection_index;
68  if (!searchIntersectionPoints(req, first_intersection_index, second_intersection_index))
69  {
70  RCLCPP_ERROR(LOGGER, "Blend radius to large.");
71  res.error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN;
72  return false;
73  }
74 
75  // Select blending period and adjust the start and end point of the blend
76  // phase
77  std::size_t blend_align_index;
78  determineTrajectoryAlignment(req, first_intersection_index, second_intersection_index, blend_align_index);
79 
80  // blend the trajectories in Cartesian space
81  pilz_industrial_motion_planner::CartesianTrajectory blend_trajectory_cartesian;
82  blendTrajectoryCartesian(req, first_intersection_index, second_intersection_index, blend_align_index, sampling_time,
83  blend_trajectory_cartesian);
84 
85  // generate the blending trajectory in joint space
86  std::map<std::string, double> initial_joint_position, initial_joint_velocity;
87  for (const std::string& joint_name :
88  req.first_trajectory->getFirstWayPointPtr()->getJointModelGroup(req.group_name)->getActiveJointModelNames())
89  {
90  initial_joint_position[joint_name] =
91  req.first_trajectory->getWayPoint(first_intersection_index - 1).getVariablePosition(joint_name);
92  initial_joint_velocity[joint_name] =
93  req.first_trajectory->getWayPoint(first_intersection_index - 1).getVariableVelocity(joint_name);
94  }
95  trajectory_msgs::msg::JointTrajectory blend_joint_trajectory;
96  moveit_msgs::msg::MoveItErrorCodes error_code;
97 
98  if (!generateJointTrajectory(planning_scene, limits_.getJointLimitContainer(), blend_trajectory_cartesian,
99  req.group_name, req.link_name, initial_joint_position, initial_joint_velocity,
100  blend_joint_trajectory, error_code, true))
101  {
102  // LCOV_EXCL_START
103  RCLCPP_INFO(LOGGER, "Failed to generate joint trajectory for blending trajectory.");
104  res.error_code.val = error_code.val;
105  return false;
106  // LCOV_EXCL_STOP
107  }
108 
109  res.first_trajectory = std::make_shared<robot_trajectory::RobotTrajectory>(req.first_trajectory->getRobotModel(),
110  req.first_trajectory->getGroup());
111  res.blend_trajectory = std::make_shared<robot_trajectory::RobotTrajectory>(req.first_trajectory->getRobotModel(),
112  req.first_trajectory->getGroup());
113  res.second_trajectory = std::make_shared<robot_trajectory::RobotTrajectory>(req.first_trajectory->getRobotModel(),
114  req.first_trajectory->getGroup());
115 
116  // set the three trajectories after blending in response
117  // erase the points [first_intersection_index, back()] from the first
118  // trajectory
119  for (size_t i = 0; i < first_intersection_index; ++i)
120  {
121  res.first_trajectory->insertWayPoint(i, req.first_trajectory->getWayPoint(i),
122  req.first_trajectory->getWayPointDurationFromPrevious(i));
123  }
124 
125  // append the blend trajectory
126  res.blend_trajectory->setRobotTrajectoryMsg(req.first_trajectory->getFirstWayPoint(), blend_joint_trajectory);
127  // copy the points [second_intersection_index, len] from the second trajectory
128  for (size_t i = second_intersection_index + 1; i < req.second_trajectory->getWayPointCount(); ++i)
129  {
130  res.second_trajectory->insertWayPoint(i - (second_intersection_index + 1), req.second_trajectory->getWayPoint(i),
131  req.second_trajectory->getWayPointDurationFromPrevious(i));
132  }
133 
134  // adjust the time from start
135  res.second_trajectory->setWayPointDurationFromPrevious(0, sampling_time);
136 
137  res.error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
138  return true;
139 }
140 
141 bool pilz_industrial_motion_planner::TrajectoryBlenderTransitionWindow::validateRequest(
142  const pilz_industrial_motion_planner::TrajectoryBlendRequest& req, double& sampling_time,
143  moveit_msgs::msg::MoveItErrorCodes& error_code) const
144 {
145  RCLCPP_DEBUG(LOGGER, "Validate the trajectory blend request.");
146 
147  // check planning group
148  if (!req.first_trajectory->getRobotModel()->hasJointModelGroup(req.group_name))
149  {
150  RCLCPP_ERROR_STREAM(LOGGER, "Unknown planning group: " << req.group_name);
151  error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_GROUP_NAME;
152  return false;
153  }
154 
155  // check link exists
156  if (!req.first_trajectory->getRobotModel()->hasLinkModel(req.link_name) &&
157  !req.first_trajectory->getLastWayPoint().hasAttachedBody(req.link_name))
158  {
159  RCLCPP_ERROR_STREAM(LOGGER, "Unknown link name: " << req.link_name);
160  error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_LINK_NAME;
161  return false;
162  }
163 
164  if (req.blend_radius <= 0)
165  {
166  RCLCPP_ERROR(LOGGER, "Blending radius must be positive");
167  error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN;
168  return false;
169  }
170 
171  // end position of the first trajectory and start position of second
172  // trajectory must be the same
174  req.first_trajectory->getLastWayPoint(), req.second_trajectory->getFirstWayPoint(), req.group_name, EPSILON))
175  {
176  RCLCPP_ERROR_STREAM(
177  LOGGER, "During blending the last point of the preceding and the first point of the succeeding trajectory");
178  error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN;
179  return false;
180  }
181 
182  // same uniform sampling time
184  EPSILON, sampling_time))
185  {
186  error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN;
187  return false;
188  }
189 
190  // end position of the first trajectory and start position of second
191  // trajectory must have zero
192  // velocities/accelerations
194  EPSILON) ||
196  EPSILON))
197  {
198  RCLCPP_ERROR(LOGGER, "Intersection point of the blending trajectories has non-zero velocities/accelerations.");
199  error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_MOTION_PLAN;
200  return false;
201  }
202 
203  return true;
204 }
205 
206 void pilz_industrial_motion_planner::TrajectoryBlenderTransitionWindow::blendTrajectoryCartesian(
207  const pilz_industrial_motion_planner::TrajectoryBlendRequest& req, const std::size_t first_interse_index,
208  const std::size_t second_interse_index, const std::size_t blend_align_index, double sampling_time,
210 {
211  // other fields of the trajectory
212  trajectory.group_name = req.group_name;
213  trajectory.link_name = req.link_name;
214 
215  // Pose on first trajectory
216  Eigen::Isometry3d blend_sample_pose1 =
217  req.first_trajectory->getWayPoint(first_interse_index).getFrameTransform(req.link_name);
218 
219  // Pose on second trajectory
220  Eigen::Isometry3d blend_sample_pose2 =
221  req.second_trajectory->getWayPoint(second_interse_index).getFrameTransform(req.link_name);
222 
223  // blend the trajectory
224  double blend_sample_num = second_interse_index + blend_align_index - first_interse_index + 1;
226  blend_sample_pose2 = req.second_trajectory->getFirstWayPoint().getFrameTransform(req.link_name);
227 
228  // Pose on blending trajectory
229  Eigen::Isometry3d blend_sample_pose;
230  for (std::size_t i = 0; i < blend_sample_num; ++i)
231  {
232  // if the first trajectory does not reach the last sample, update
233  if ((first_interse_index + i) < req.first_trajectory->getWayPointCount())
234  {
235  blend_sample_pose1 = req.first_trajectory->getWayPoint(first_interse_index + i).getFrameTransform(req.link_name);
236  }
237 
238  // if after the alignment, the second trajectory starts, update
239  if ((first_interse_index + i) > blend_align_index)
240  {
241  blend_sample_pose2 = req.second_trajectory->getWayPoint(first_interse_index + i - blend_align_index)
242  .getFrameTransform(req.link_name);
243  }
244 
245  double s = (i + 1) / blend_sample_num;
246  double alpha = 6 * std::pow(s, 5) - 15 * std::pow(s, 4) + 10 * std::pow(s, 3);
247 
248  // blend the translation
249  blend_sample_pose.translation() = blend_sample_pose1.translation() +
250  alpha * (blend_sample_pose2.translation() - blend_sample_pose1.translation());
251 
252  // blend the orientation
253  Eigen::Quaterniond start_quat(blend_sample_pose1.rotation());
254  Eigen::Quaterniond end_quat(blend_sample_pose2.rotation());
255  blend_sample_pose.linear() = start_quat.slerp(alpha, end_quat).toRotationMatrix();
256 
257  // push to the trajectory
258  waypoint.pose = tf2::toMsg(blend_sample_pose);
259  waypoint.time_from_start = rclcpp::Duration::from_seconds((i + 1.0) * sampling_time);
260  trajectory.points.push_back(waypoint);
261  }
262 }
263 
264 bool pilz_industrial_motion_planner::TrajectoryBlenderTransitionWindow::searchIntersectionPoints(
265  const pilz_industrial_motion_planner::TrajectoryBlendRequest& req, std::size_t& first_interse_index,
266  std::size_t& second_interse_index) const
267 {
268  RCLCPP_INFO(LOGGER, "Search for start and end point of blending trajectory.");
269 
270  // compute the position of the center of the blend sphere
271  // (last point of the first trajectory, first point of the second trajectory)
272  Eigen::Isometry3d circ_pose = req.first_trajectory->getLastWayPoint().getFrameTransform(req.link_name);
273 
274  // Searh for intersection points according to distance
275  if (!linearSearchIntersectionPoint(req.link_name, circ_pose.translation(), req.blend_radius, req.first_trajectory,
276  true, first_interse_index))
277  {
278  RCLCPP_ERROR_STREAM(LOGGER, "Intersection point of first trajectory not found.");
279  return false;
280  }
281  RCLCPP_INFO_STREAM(LOGGER, "Intersection point of first trajectory found, index: " << first_interse_index);
282 
283  if (!linearSearchIntersectionPoint(req.link_name, circ_pose.translation(), req.blend_radius, req.second_trajectory,
284  false, second_interse_index))
285  {
286  RCLCPP_ERROR_STREAM(LOGGER, "Intersection point of second trajectory not found.");
287  return false;
288  }
289 
290  RCLCPP_INFO_STREAM(LOGGER, "Intersection point of second trajectory found, index: " << second_interse_index);
291  return true;
292 }
293 
294 void pilz_industrial_motion_planner::TrajectoryBlenderTransitionWindow::determineTrajectoryAlignment(
295  const pilz_industrial_motion_planner::TrajectoryBlendRequest& req, std::size_t first_interse_index,
296  std::size_t second_interse_index, std::size_t& blend_align_index) const
297 {
298  size_t way_point_count_1 = req.first_trajectory->getWayPointCount() - first_interse_index;
299  size_t way_point_count_2 = second_interse_index + 1;
300 
301  if (way_point_count_1 > way_point_count_2)
302  {
303  blend_align_index = req.first_trajectory->getWayPointCount() - second_interse_index - 1;
304  }
305  else
306  {
307  blend_align_index = first_interse_index;
308  }
309 }
const JointLimitsContainer & getJointLimitContainer() const
Obtain the Joint Limits from the container.
bool blend(const planning_scene::PlanningSceneConstPtr &planning_scene, const pilz_industrial_motion_planner::TrajectoryBlendRequest &req, pilz_industrial_motion_planner::TrajectoryBlendResponse &res) override
Blend two trajectories using transition window. The trajectories have to be equally and uniformly dis...
const pilz_industrial_motion_planner::LimitsContainer limits_
bool generateJointTrajectory(const planning_scene::PlanningSceneConstPtr &scene, const JointLimitsContainer &joint_limits, const KDL::Trajectory &trajectory, const std::string &group_name, const std::string &link_name, const std::map< std::string, double > &initial_joint_position, const double &sampling_time, trajectory_msgs::msg::JointTrajectory &joint_trajectory, moveit_msgs::msg::MoveItErrorCodes &error_code, bool check_self_collision=false)
Generate joint trajectory from a KDL Cartesian trajectory.
bool linearSearchIntersectionPoint(const std::string &link_name, const Eigen::Vector3d &center_position, const double &r, const robot_trajectory::RobotTrajectoryPtr &traj, bool inverseOrder, std::size_t &index)
Performs a linear search for the intersection point of the trajectory with the blending radius.
bool isRobotStateStationary(const moveit::core::RobotState &state, const std::string &group, double EPSILON)
check if the robot state have zero velocity/acceleration
bool determineAndCheckSamplingTime(const robot_trajectory::RobotTrajectoryPtr &first_trajectory, const robot_trajectory::RobotTrajectoryPtr &second_trajectory, double EPSILON, double &sampling_time)
Determines the sampling time and checks that both trajectroies use the same sampling time.
bool isRobotStateEqual(const moveit::core::RobotState &state1, const moveit::core::RobotState &state2, const std::string &joint_group_name, double epsilon)
Check if the two robot states have the same joint position/velocity/acceleration.
This namespace includes the central class for representing planning contexts.
const rclcpp::Logger LOGGER
Definition: async_test.h:31
std::vector< CartesianTrajectoryPoint > points