45#include <kdl/path_roundedcomposite.hpp>
46#include <kdl/trajectory_segment.hpp>
47#include <kdl/utilities/error.h>
49#if __has_include(<tf2/convert.hpp>)
50#include <tf2/convert.hpp>
52#include <tf2/convert.h>
54#include <rclcpp/logger.hpp>
55#include <rclcpp/logging.hpp>
56#include <tf2_eigen/tf2_eigen.hpp>
57#include <tf2_eigen_kdl/tf2_eigen_kdl.hpp>
58#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
78void TrajectoryGeneratorPolyline::extractMotionPlanInfo(
const planning_scene::PlanningSceneConstPtr& scene,
82 RCLCPP_DEBUG(
getLogger(),
"Extract necessary information from motion plan request.");
89 info.
link_name = req.goal_constraints.front().position_constraints.front().link_name;
90 if (req.goal_constraints.front().position_constraints.front().header.frame_id.empty() ||
91 req.goal_constraints.front().orientation_constraints.front().header.frame_id.empty())
93 RCLCPP_WARN(
getLogger(),
"Frame id is not set in position/orientation constraints of "
94 "goal. Use model frame as default");
99 frame_id = req.goal_constraints.front().position_constraints.front().header.frame_id;
103 for (
const auto& pc : req.path_constraints.position_constraints)
105 Eigen::Isometry3d waypoint;
106 waypoint =
getConstraintPose(pc.constraint_region.primitive_poses.front().position,
107 pc.constraint_region.primitive_poses.front().orientation, pc.target_point_offset);
108 waypoint = scene->getFrameTransform(frame_id) * waypoint;
116 std::map<std::string, double> ik_solution;
120 std::ostringstream os;
121 os <<
"Failed to compute inverse kinematics for link: " << info.
link_name <<
" of goal pose";
122 throw LinInverseForGoalIncalculable(os.str());
130void TrajectoryGeneratorPolyline::plan(
const planning_scene::PlanningSceneConstPtr& scene,
132 const MotionPlanInfo& plan_info,
double sampling_time,
133 trajectory_msgs::msg::JointTrajectory& joint_trajectory)
138 std::unique_ptr<KDL::Path> path;
141 path = setPathPolyline(plan_info.start_pose, plan_info.waypoints, req.smoothness_level);
143 catch (
const KDL::Error_MotionPlanning& e)
145 RCLCPP_ERROR(
getLogger(),
"Motion planning error: %s", e.Description());
146 int code = e.GetType();
147 std::ostringstream os;
148 if (code == 3102 || code == 3103)
150 os <<
"zero distance between two points";
152 else if (code == 3104)
154 os <<
"waypoints specified in path constraints have three consicutive colinear points";
156 else if (code == 3105 || code == 3106)
158 os <<
"rounding circle of a point is bigger than the distance with one of the neighbor points";
160 else if (code == 3001 || code == 3002)
162 os <<
"the rounding radius is lower than KDL::epsilon. use bigger smoothness or resample your waypoints";
164 throw ConsicutiveColinearWaypoints(os.str());
167 std::unique_ptr<KDL::VelocityProfile> vp(
174 KDL::Trajectory_Segment cart_trajectory(path.get(), vp.get(),
false);
176 moveit_msgs::msg::MoveItErrorCodes error_code;
180 plan_info.link_name, plan_info.start_joint_position, sampling_time, joint_trajectory,
183 std::ostringstream os;
184 os <<
"Failed to generate valid joint trajectory from the Cartesian path";
185 throw LinTrajectoryConversionFailure(os.str(), error_code.val);
189std::unique_ptr<KDL::Path> TrajectoryGeneratorPolyline::setPathPolyline(
const Eigen::Affine3d& start_pose,
190 const std::vector<Eigen::Isometry3d>& waypoints,
191 double smoothness_level)
const
193 RCLCPP_DEBUG(
getLogger(),
"Set Cartesian path for POLYLINE command.");
195 KDL::Frame kdl_start_pose;
196 tf2::transformEigenToKDL(start_pose, kdl_start_pose);
198 std::vector<KDL::Frame> kdl_waypoints;
199 for (
const auto& waypoint : waypoints)
201 KDL::Frame kdl_waypoint;
202 tf2::transformEigenToKDL(waypoint, kdl_waypoint);
203 kdl_waypoints.push_back(kdl_waypoint);
206 RCLCPP_INFO_STREAM(
getLogger(),
"Transformed waypoints number: " << kdl_waypoints.size());
209 KDL::RotationalInterpolation* rot_interpo =
new KDL::RotationalInterpolation_SingleAxis();
217 if (req.path_constraints.position_constraints.size() < 2)
219 std::ostringstream os;
220 os <<
"waypoints specified in path constraints is less than 2 for POLYLINE motion.";
221 throw NoWaypointsSpecified(os.str());
Representation of a robot's state. This includes position, velocity, acceleration and effort.
This class combines CartesianLimit and JointLimits into on single class.
static std::unique_ptr< KDL::Path > polylineFromWaypoints(const KDL::Frame &start_pose, const std::vector< KDL::Frame > &waypoints, KDL::RotationalInterpolation *rot_interpo, double smoothness, double eqradius)
set the path polyline from waypoints
TrajectoryGeneratorPolyline(const moveit::core::RobotModelConstPtr &robot_model, const pilz_industrial_motion_planner::LimitsContainer &planner_limits, const std::string &group_name)
Constructor of Polyline Trajectory Generator.
This class is used to extract needed information from motion plan request.
std::vector< Eigen::Isometry3d > waypoints
Eigen::Isometry3d start_pose
Eigen::Isometry3d goal_pose
std::map< std::string, double > start_joint_position
const moveit::core::RobotModelConstPtr robot_model_
double max_cartesian_speed_
TrajectoryGenerator(const moveit::core::RobotModelConstPtr &robot_model, const pilz_industrial_motion_planner::LimitsContainer &planner_limits)
const pilz_industrial_motion_planner::LimitsContainer planner_limits_
std::unique_ptr< KDL::VelocityProfile > cartesianTrapVelocityProfile(double max_velocity_scaling_factor, double max_acceleration_scaling_factor, const std::unique_ptr< KDL::Path > &path) const
build cartesian velocity profile for the path
void setMaxCartesianSpeed(const moveit_msgs::msg::MotionPlanRequest &req)
Set the max cartesian speed from motion request.
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
bool computeLinkFK(moveit::core::RobotState &robot_state, const std::string &link_name, const std::map< std::string, double > &joint_state, Eigen::Isometry3d &pose)
compute the pose of a link at a given robot state
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, 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 computePoseIK(const planning_scene::PlanningSceneConstPtr &scene, const std::string &group_name, const std::string &link_name, const Eigen::Isometry3d &pose, const std::string &frame_id, const std::map< std::string, double > &seed, std::map< std::string, double > &solution, bool check_self_collision=true, const double timeout=0.0)
compute the inverse kinematics of a given pose, also check robot self collision
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
rclcpp::Logger getLogger()
Eigen::Isometry3d getConstraintPose(const geometry_msgs::msg::Point &position, const geometry_msgs::msg::Quaternion &orientation, const geometry_msgs::msg::Vector3 &offset)
Adapt goal pose, defined by position+orientation, to consider offset.