tool_goal_pose.cpp
Go to the documentation of this file.
1 
26 #include <math.h>
28 #include <XmlRpcException.h>
30 #include <ros/console.h>
31 
33 
34 static const int CARTESIAN_DOF_SIZE = 6;
35 static const double DEFAULT_POS_TOLERANCE = 0.001;
36 static const double DEFAULT_ROT_TOLERANCE = 0.01;
37 static const double POS_MAX_ERROR_RATIO = 10.0;
38 static const double ROT_MAX_ERROR_RATIO = 10.0;
39 
40 namespace stomp_moveit
41 {
42 namespace cost_functions
43 {
44 
46  name_("ToolGoalPose")
47 {
48  // TODO Auto-generated constructor stub
49 
50 }
51 
53 {
54  // TODO Auto-generated destructor stub
55 }
56 
57 bool ToolGoalPose::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
58  const std::string& group_name,XmlRpc::XmlRpcValue& config)
59 {
60  group_name_ = group_name;
61  robot_model_ = robot_model_ptr;
62 
63  return configure(config);
64 }
65 
67 {
68  using namespace XmlRpc;
69 
70  try
71  {
72  XmlRpcValue params = config;
73 
74  position_cost_weight_ = static_cast<double>(params["position_cost_weight"]);
75  orientation_cost_weight_ = static_cast<double>(params["orientation_cost_weight"]);
76 
77  // total weight
79 
80  }
81  catch(XmlRpc::XmlRpcException& e)
82  {
83  ROS_ERROR("%s failed to load parameters, %s",getName().c_str(),e.getMessage().c_str());
84  return false;
85  }
86 
87  return true;
88 }
89 
90 bool ToolGoalPose::setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr& planning_scene,
91  const moveit_msgs::MotionPlanRequest &req,
92  const stomp_core::StompConfiguration &config,
93  moveit_msgs::MoveItErrorCodes& error_code)
94 {
95  using namespace Eigen;
96  using namespace moveit::core;
97 
98  const JointModelGroup* joint_group = robot_model_->getJointModelGroup(group_name_);
99  int num_joints = joint_group->getActiveJointModels().size();
100  tool_link_ = joint_group->getLinkModelNames().back();
101  state_.reset(new RobotState(robot_model_));
102  robotStateMsgToRobotState(req.start_state,*state_);
103 
104  const std::vector<moveit_msgs::Constraints>& goals = req.goal_constraints;
105  if(goals.empty())
106  {
107  ROS_ERROR("A goal constraint was not provided");
108  error_code.val = error_code.INVALID_GOAL_CONSTRAINTS;
109  return false;
110  }
111 
112  // storing tool goal pose
113  bool found_goal = false;
114  for(const auto& g: goals)
115  {
116 
118  {
119  // tool cartesian goal data
120  state_->updateLinkTransforms();
121  Eigen::Affine3d start_tool_pose = state_->getGlobalLinkTransform(tool_link_);
122  boost::optional<moveit_msgs::Constraints> cartesian_constraints = utils::kinematics::curateCartesianConstraints(g,start_tool_pose);
123  if(cartesian_constraints.is_initialized())
124  {
125  found_goal = utils::kinematics::decodeCartesianConstraint(robot_model_,cartesian_constraints.get(),tool_goal_pose_,
126  tool_goal_tolerance_,robot_model_->getRootLinkName());
127  ROS_DEBUG_STREAM("ToolGoalTolerance cost function will use tolerance: "<<tool_goal_tolerance_.transpose());
128  }
129  break;
130  }
131 
132 
133  if(!found_goal)
134  {
135  ROS_DEBUG("%s a cartesian goal pose in MotionPlanRequest was not provided,calculating it from FK",getName().c_str());
136 
137  // check joint constraints
138  if(g.joint_constraints.empty())
139  {
140  ROS_ERROR_STREAM("No joint values for the goal were found");
141  error_code.val = error_code.INVALID_GOAL_CONSTRAINTS;
142  return false;
143  }
144 
145  // compute FK to obtain tool pose
146  const std::vector<moveit_msgs::JointConstraint>& joint_constraints = g.joint_constraints;
147 
148  // copying goal values into state
149  for(auto& jc: joint_constraints)
150  {
151  state_->setVariablePosition(jc.joint_name,jc.position);
152  }
153 
154  // storing tool goal pose and tolerance
155  state_->update(true);
156  tool_goal_pose_ = state_->getGlobalLinkTransform(tool_link_);
158  double ptol = DEFAULT_POS_TOLERANCE;
159  double rtol = DEFAULT_ROT_TOLERANCE;
160  tool_goal_tolerance_ << ptol, ptol, ptol, rtol, rtol, rtol;
161  found_goal = true;
162  break;
163  }
164  }
165 
166  // setting cartesian error range
168  max_twist_error_.resize(min_twist_error_.size());
171 
172  return true;
173 }
174 
175 bool ToolGoalPose::computeCosts(const Eigen::MatrixXd& parameters,
176  std::size_t start_timestep,
177  std::size_t num_timesteps,
178  int iteration_number,
179  int rollout_number,
180  Eigen::VectorXd& costs,
181  bool& validity)
182 {
183 
184  using namespace Eigen;
185  using namespace utils::kinematics;
186  validity = true;
187 
188  auto compute_scaled_error = [](const VectorXd& val,VectorXd& min,VectorXd& max) -> VectorXd
189  {
190  VectorXd capped_val;
191  capped_val = (val.array() > max.array()).select(max,val);
192  capped_val = (val.array() < min.array()).select(min,val);
193  auto range = max - min;
194  VectorXd scaled = (capped_val - min).array()/(range.array());
195  return scaled;
196  };
197 
198 
199  last_joint_pose_ = parameters.rightCols(1);
200  state_->setJointGroupPositions(group_name_,last_joint_pose_);
201  state_->updateLinkTransforms();
202  last_tool_pose_ = state_->getGlobalLinkTransform(tool_link_);
203 
204  // computing twist error
205  Eigen::Affine3d tf = tool_goal_pose_.inverse() * last_tool_pose_;
206  Eigen::Vector3d angles_err = tf.rotation().eulerAngles(2,1,0);
207  angles_err.reverseInPlace();
208  Eigen::Vector3d pos_err = tool_goal_pose_.translation() - last_tool_pose_.translation();
209 
210  tool_twist_error_.resize(6);
211  tool_twist_error_.head(3) = pos_err.head(3);
212  tool_twist_error_.tail(3) = angles_err.tail(3);
213 
214 
215  // computing relative error values
216  VectorXd scaled_twist_error = compute_scaled_error(tool_twist_error_,min_twist_error_,max_twist_error_);
217  double pos_error = scaled_twist_error.head(3).cwiseAbs().maxCoeff();
218  double orientation_error = scaled_twist_error.tail(3).cwiseAbs().maxCoeff();
219 
220  // computing cost of last point
221  costs.resize(parameters.cols());
222  costs.setConstant(0.0);
223  costs(costs.size()-1) = pos_error*position_cost_weight_ + orientation_error * orientation_cost_weight_;
224 
225  // check if valid when twist errors are below the allowed tolerance.
226  validity = (tool_twist_error_.cwiseAbs().array() <= tool_goal_tolerance_.array()).all();
227 
228  return true;
229 }
230 void ToolGoalPose::done(bool success,int total_iterations,double final_cost,const Eigen::MatrixXd& parameters)
231 {
232  ROS_DEBUG_STREAM(getName()<<" last tool error: "<<tool_twist_error_.transpose());
233  ROS_DEBUG_STREAM(getName()<<" used tool tolerance: "<<tool_goal_tolerance_.transpose());
234  ROS_DEBUG_STREAM(getName()<<" last joint position: "<<last_joint_pose_.transpose());
235 }
236 
237 } /* namespace cost_functions */
238 } /* namespace stomp_moveit */
moveit::core::RobotModelConstPtr robot_model_
const std::string & getMessage() const
bool decodeCartesianConstraint(moveit::core::RobotModelConstPtr model, const moveit_msgs::Constraints &constraints, Eigen::Affine3d &tool_pose, std::vector< double > &tolerance, std::string target_frame="")
Evaluates the cost of the goal pose by determining how far the Cartesian tool pose is from the desire...
virtual bool initialize(moveit::core::RobotModelConstPtr robot_model_ptr, const std::string &group_name, XmlRpc::XmlRpcValue &config) override
Initializes and configures the Cost Function.
const std::vector< std::string > & getLinkModelNames() const
virtual bool configure(const XmlRpc::XmlRpcValue &config) override
Sets internal members of the plugin from the configuration data.
static const double DEFAULT_POS_TOLERANCE
static const double ROT_MAX_ERROR_RATIO
static const double POS_MAX_ERROR_RATIO
virtual bool setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr &planning_scene, const moveit_msgs::MotionPlanRequest &req, const stomp_core::StompConfiguration &config, moveit_msgs::MoveItErrorCodes &error_code) override
Stores the planning details which will be used during the costs calculations.
boost::optional< moveit_msgs::Constraints > curateCartesianConstraints(const moveit_msgs::Constraints &c, const Eigen::Affine3d &ref_pose, double default_pos_tol=0.0005, double default_rot_tol=M_PI)
virtual void done(bool success, int total_iterations, double final_cost, const Eigen::MatrixXd &parameters) override
Called by the Stomp Task at the end of the optimization process.
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
double orientation_cost_weight_
factor multiplied to the scaled orientation error
virtual bool computeCosts(const Eigen::MatrixXd &parameters, std::size_t start_timestep, std::size_t num_timesteps, int iteration_number, int rollout_number, Eigen::VectorXd &costs, bool &validity) override
computes the goal state costs as a function of the distance from the desired task manifold...
double min(double a, double b)
#define ROS_DEBUG_STREAM(args)
PLUGINLIB_EXPORT_CLASS(stomp_moveit::cost_functions::ToolGoalPose, stomp_moveit::cost_functions::StompCostFunction)
bool isCartesianConstraints(const moveit_msgs::Constraints &c)
virtual std::string getName() const override
This defines a cost function for tool goal pose.
const std::vector< const JointModel * > & getActiveJointModels() const
Eigen::Affine3d tool_goal_pose_
The desired goal pose for the active plan request.
#define ROS_ERROR_STREAM(args)
double max(double a, double b)
#define ROS_ERROR(...)
static const double DEFAULT_ROT_TOLERANCE
double position_cost_weight_
factor multiplied to the scaled position error
static const int CARTESIAN_DOF_SIZE
#define ROS_DEBUG(...)


stomp_plugins
Author(s): Jorge Nicho
autogenerated on Fri May 8 2020 03:35:53