33 static const
double LONGEST_VALID_JOINT_MOVE = 0.01;
37 namespace cost_functions
40 ObstacleDistanceGradient::ObstacleDistanceGradient() :
41 name_(
"ObstacleDistanceGradient"),
47 ObstacleDistanceGradient::~ObstacleDistanceGradient()
52 bool ObstacleDistanceGradient::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
55 robot_model_ptr_ = robot_model_ptr;
56 group_name_ = group_name;
57 collision_request_.distance =
true;
58 collision_request_.group_name = group_name;
59 collision_request_.cost =
false;
60 collision_request_.max_contacts = 1;
61 collision_request_.max_contacts_per_pair = 1;
62 collision_request_.contacts =
false;
63 collision_request_.verbose =
false;
73 auto members = {
"cost_weight" ,
"max_distance"};
74 for(
auto& m : members)
78 ROS_ERROR(
"%s failed to find the '%s' parameter",getName().c_str(),m);
84 max_distance_ =
static_cast<double>(c[
"max_distance"]);
85 cost_weight_ =
static_cast<double>(c[
"cost_weight"]);
86 longest_valid_joint_move_ = c.
hasMember(
"longest_valid_joint_move") ?
static_cast<double>(c[
"longest_valid_joint_move"]):LONGEST_VALID_JOINT_MOVE;
88 if(!c.
hasMember(
"longest_valid_joint_move"))
90 ROS_WARN(
"%s using default value for 'longest_valid_joint_move' of %f",getName().c_str(),longest_valid_joint_move_);
95 ROS_ERROR(
"%s failed to parse configuration parameters",name_.c_str());
102 bool ObstacleDistanceGradient::setMotionPlanRequest(
const planning_scene::PlanningSceneConstPtr&
planning_scene,
103 const moveit_msgs::MotionPlanRequest &req,
105 moveit_msgs::MoveItErrorCodes& error_code)
109 planning_scene_ = planning_scene;
111 error_code.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
114 robot_state_.reset(
new RobotState(robot_model_ptr_));
118 ROS_ERROR(
"%s Failed to get current robot state from request",
getName().c_str());
123 for(
auto& rs : intermediate_coll_states_)
131 bool ObstacleDistanceGradient::computeCosts(
const Eigen::MatrixXd& parameters, std::size_t start_timestep,
132 std::size_t num_timesteps,
int iteration_number,
int rollout_number,
133 Eigen::VectorXd& costs,
bool& validity)
145 costs = Eigen::VectorXd::Zero(num_timesteps);
148 if(parameters.cols()<start_timestep + num_timesteps)
156 bool skip_next_check =
false;
158 for (
auto t=start_timestep; t<start_timestep + num_timesteps; ++t)
163 collision_result_.clear();
164 robot_state_->setJointGroupPositions(joint_group,parameters.col(t));
165 robot_state_->update();
166 collision_result_.distance = max_distance_;
168 planning_scene_->checkSelfCollision(collision_request_,collision_result_,*robot_state_,planning_scene_->getAllowedCollisionMatrix());
169 dist = collision_result_.collision ? -1.0 :collision_result_.distance ;
171 if(dist >= max_distance_)
182 costs(t) = (max_distance_ - dist)/max_distance_;
186 skip_next_check =
false;
189 if(t < start_timestep + num_timesteps - 1)
191 if(!checkIntermediateCollisions(parameters.col(t),parameters.col(t+1),longest_valid_joint_move_))
196 skip_next_check =
true;
200 skip_next_check =
false;
209 bool ObstacleDistanceGradient::checkIntermediateCollisions(
const Eigen::VectorXd& start,
210 const Eigen::VectorXd& end,
double longest_valid_joint_move)
212 Eigen::VectorXd
diff = end - start;
213 int num_intermediate = std::ceil(((diff.cwiseAbs())/longest_valid_joint_move).maxCoeff()) - 1;
214 if(num_intermediate < 1.0)
221 auto& start_state = intermediate_coll_states_[0];
222 auto& mid_state = intermediate_coll_states_[1];
223 auto& end_state = intermediate_coll_states_[2];
225 if(!start_state || !mid_state || !end_state)
232 auto req = collision_request_;
233 req.distance =
false;
236 start_state->setJointGroupPositions(joint_group,start);
237 end_state->setJointGroupPositions(joint_group,end);
240 double dt = 1.0/
static_cast<double>(num_intermediate);
241 double interval = 0.0;
242 for(std::size_t i = 1; i < num_intermediate;i++)
245 start_state->interpolate(*end_state,interval,*mid_state) ;
246 if(planning_scene_->isStateColliding(*mid_state))
255 void ObstacleDistanceGradient::done(
bool success,
int total_iterations,
double final_cost,
const Eigen::MatrixXd& parameters)
257 robot_state_.reset();
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
std::string getName(void *handle)
PLUGINLIB_EXPORT_CLASS(test_moveit_controller_manager::TestMoveItControllerManager, moveit_controller_manager::MoveItControllerManager)
double dist(const T &p1, const T &p2)
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
bool hasMember(const std::string &name) const
This defines a Robot Model for the Stomp Planner.
Assigns a cost value to each robot state by evaluating the minimum distance between the robot and the...
#define ROS_ERROR_STREAM(args)
virtual bool configure(const XmlRpc::XmlRpcValue &config) override
see base class for documentation