39 #include <visualization_msgs/MarkerArray.h> 44 #include <eigen3/Eigen/LU> 45 #include <eigen3/Eigen/Core> 51 return ((
double)
random() / (
double)RAND_MAX);
57 : full_trajectory_(trajectory)
58 , kmodel_(planning_scene->getRobotModel())
59 , planning_group_(planning_group)
60 , parameters_(parameters)
62 , planning_scene_(planning_scene)
64 , start_state_(start_state)
67 std::vector<std::string> cd_names;
68 planning_scene->getCollisionDetectorNames(cd_names);
70 ROS_INFO_STREAM(
"The following collision detectors are active in the planning scene.");
71 for (std::size_t i = 0; i < cd_names.size(); i++)
76 ROS_INFO_STREAM(
"Active collision detector is: " + planning_scene->getActiveCollisionDetectorName());
79 planning_scene->getCollisionWorld(planning_scene->getActiveCollisionDetectorName()).
get());
82 ROS_WARN_STREAM(
"Could not initialize hybrid collision world from planning scene");
87 planning_scene->getCollisionRobot(planning_scene->getActiveCollisionDetectorName()).
get());
90 ROS_WARN_STREAM(
"Could not initialize hybrid collision robot from planning scene");
114 for (
size_t i = 0; i <
gsr_->gradients_.size(); i++)
122 double max_cost_scale = 0.0;
127 for (
size_t i = 0; i < joint_models.size(); i++)
130 double joint_cost = 1.0;
131 std::string joint_name = model->
getName();
133 std::vector<double> derivative_costs(3);
138 double cost_scale =
joint_costs_[i].getMaxQuadCostInvValue();
139 if (max_cost_scale < cost_scale)
140 max_cost_scale = cost_scale;
154 jacobian_ = Eigen::MatrixXd::Zero(3, num_joints_);
196 std::map<std::string, std::string> fixed_link_resolution_map;
202 fixed_link_resolution_map[
joint_names_[i]] = joint_names_[i];
207 if (!jm->getParentLinkModel())
210 fixed_link_resolution_map[jm->getName()] = jm->getParentLinkModel()->getParentJointModel()->getName();
216 if (fixed_link_resolution_map.find(
218 fixed_link_resolution_map.end())
221 bool found_root =
false;
225 if (parent_model == NULL)
248 for (
int i = start; i <= end; ++i)
251 for (
size_t g = 0; g <
gsr_->gradients_.size(); g++)
257 if (fixed_link_resolution_map.find(info.
joint_name) != fixed_link_resolution_map.end())
280 bool found_root =
false;
282 if (model ==
kmodel_->getRootJoint())
287 if (parent_model == NULL)
303 if (parent_model ==
kmodel_->getRootJoint())
318 bool optimization_result = 0;
323 std::vector<double> costs(costWindow, 0.0);
325 bool should_break_out =
false;
335 double cost = cCost + sCost;
403 ROS_INFO(
"Chomp Got mesh to mesh safety at iter %d. Breaking out early.",
iteration_);
406 should_break_out =
true;
437 if (cCost < parameters_->collision_threshold_)
442 should_break_out =
true;
452 ROS_WARN(
"Breaking out early due to time limit constraints.");
505 if (should_break_out)
526 optimization_result =
true;
527 ROS_INFO(
"Chomp path is collision free");
531 optimization_result =
false;
532 ROS_ERROR(
"Chomp path is not collision free!");
542 return optimization_result;
547 moveit_msgs::RobotTrajectory traj;
552 trajectory_msgs::JointTrajectoryPoint point;
557 traj.joint_trajectory.points.push_back(point);
559 moveit_msgs::RobotState start_state_msg;
612 Eigen::Vector3d potential_gradient;
613 Eigen::Vector3d normalized_velocity;
614 Eigen::Matrix3d orthogonal_projector;
615 Eigen::Vector3d curvature_vector;
616 Eigen::Vector3d cartesian_gradient;
632 endPoint = startPoint;
639 for (
int i = startPoint; i <= endPoint; i++)
645 if (potential < 0.0001)
651 vel_mag_sq = vel_mag * vel_mag;
656 orthogonal_projector = Eigen::Matrix3d::Identity() - (normalized_velocity * normalized_velocity.transpose());
658 cartesian_gradient = vel_mag * (orthogonal_projector * potential_gradient - potential * curvature_vector);
705 for (
size_t i = 0; i < joint_models.size(); i++)
712 if (max_scale < scale)
714 if (min_scale < scale)
732 std::cout <<
"Cost = " << cost << std::endl;
742 double smoothness_cost = 0.0;
752 double collision_cost = 0.0;
754 double worst_collision_cost = 0.0;
760 double state_collision_cost = 0.0;
765 collision_cost += state_collision_cost;
766 if (state_collision_cost > worst_collision_cost)
768 worst_collision_cost = state_collision_cost;
789 Eigen::Affine3d joint_transform =
794 Eigen::Vector3d axis;
796 if (revolute_joint != NULL)
798 axis = revolute_joint->
getAxis();
800 else if (prismatic_joint != NULL)
802 axis = prismatic_joint->
getAxis();
806 axis = Eigen::Vector3d::Identity();
809 axis = joint_transform * axis;
816 template <
typename Derived>
818 Eigen::MatrixBase<Derived>& jacobian)
const 824 Eigen::Vector3d column =
joint_axes_[trajectory_point][j].cross(
825 Eigen::Vector3d(collision_point_pos(0), collision_point_pos(1), collision_point_pos(2)) -
828 jacobian.col(j)[0] = column.x();
829 jacobian.col(j)[1] = column.y();
830 jacobian.col(j)[2] = column.z();
834 jacobian.col(j)[0] = 0.0;
835 jacobian.col(j)[1] = 0.0;
836 jacobian.col(j)[2] = 0.0;
844 for (
size_t joint_i = 0; joint_i < joint_models.size(); joint_i++)
860 double joint_max = -DBL_MAX;
861 double joint_min = DBL_MAX;
863 for (moveit::core::JointModel::Bounds::const_iterator it = bounds.begin(); it != bounds.end(); it++)
865 if (it->min_position_ < joint_min)
870 if (it->max_position_ > joint_max)
872 joint_max = it->max_position_;
878 bool violation =
false;
881 double max_abs_violation = 1e-6;
882 double max_violation = 0.0;
883 int max_violation_index = 0;
888 double absolute_amount = 0.0;
892 absolute_amount = fabs(amount);
897 absolute_amount = fabs(amount);
899 if (absolute_amount > max_abs_violation)
901 max_abs_violation = absolute_amount;
902 max_violation = amount;
903 max_violation_index = i;
912 max_violation /
joint_costs_[joint_i].getQuadraticCostInverse()(free_var_index, free_var_index);
914 multiplier *
joint_costs_[joint_i].getQuadraticCostInverse().col(free_var_index);
925 double inv_time_sq = inv_time * inv_time;
941 for (
int i = start; i <= end; ++i)
958 for (
size_t g = 0; g <
gsr_->gradients_.size(); g++)
1024 std::vector<double> joint_states;
1025 for (
int j = 0; j < group_trajectory.
getNumJoints(); j++)
1027 joint_states.push_back(point(0, j));
1043 std::vector<double> vals;
1045 double* ptr = &vals[0];
1046 Eigen::Map<Eigen::VectorXd> random_matrix(ptr, vals.size());
bool use_pseudo_inverse_
the noise added to the diagnal of the total quadratic cost matrix in the objective function ...
double smoothness_cost_weight_
int num_collision_points_
const ChompParameters * parameters_
const std::string & getName() const
const CollisionRobotDistanceFieldConstPtr getCollisionRobotDistanceField() const
void addIncrementsToTrajectory()
const std::string & getName() const
Eigen::MatrixXd::RowXpr getTrajectoryPoint(int traj_point)
const std::vector< const LinkModel * > & getUpdatedLinkModels() const
double stochasticity_factor_
void calculateSmoothnessIncrements()
void getJacobian(int trajectoryPoint, Eigen::Vector3d &collision_point_pos, std::string &jointName, Eigen::MatrixBase< Derived > &jacobian) const
std::map< std::string, std::map< std::string, bool > > joint_parent_map_
unsigned int num_collision_free_iterations_
void getCollisionGradients(const CollisionRequest &req, CollisionResult &res, const CollisionRobot &robot, const robot_state::RobotState &state, const AllowedCollisionMatrix *acm, GroupStateRepresentationPtr &gsr) const
double planning_time_limit_
Eigen::MatrixXd jacobian_pseudo_inverse_
double getSmoothnessCost()
std::vector< EigenSTL::vector_Vector3d > collision_point_potential_gradient_
ChompTrajectory group_trajectory_
std::vector< std::string > joint_names_
Generates samples from a multivariate gaussian distribution.
std::vector< MultivariateGaussian > multivariate_gaussian_
const collision_detection::CollisionWorldHybrid * hy_world_
Eigen::MatrixXd smoothness_increments_
std::vector< Eigen::Vector3d, Eigen::aligned_allocator< Eigen::Vector3d > > vector_Vector3d
void calculateCollisionIncrements()
unsigned int collision_free_iteration_
collision_detection::GroupStateRepresentationPtr gsr_
const moveit::core::JointModelGroup * joint_model_group_
std::vector< EigenSTL::vector_Vector3d > collision_point_acc_eigen_
const Eigen::Affine3d & getGlobalLinkTransform(const std::string &link_name)
std::vector< std::vector< double > > collision_point_vel_mag_
ChompOptimizer(ChompTrajectory *trajectory, const planning_scene::PlanningSceneConstPtr &planning_scene, const std::string &planning_group, const ChompParameters *parameters, const moveit::core::RobotState &start_state)
const moveit::core::RobotModelConstPtr & kmodel_
const JointModelGroup * getJointModelGroup(const std::string &group) const
std::vector< EigenSTL::vector_Vector3d > joint_axes_
planning_scene::PlanningSceneConstPtr planning_scene_
double pseudo_inverse_ridge_factor_
enable pseudo inverse calculations or not.
void getJointVelocities(int traj_point, Eigen::MatrixBase< Derived > &velocities)
Gets the joint velocities at the given trajectory point.
double getPotential(double field_distance, double radius, double clearence)
void setRobotStateFromPoint(ChompTrajectory &group_trajectory, int i)
ChompTrajectory * full_trajectory_
std::vector< std::vector< std::string > > collision_point_joint_names_
static const int DIFF_RULE_LENGTH
Eigen::MatrixXd jacobian_jacobian_tranpose_
bool isCurrentTrajectoryMeshToMeshCollisionFree() const
double getTrajectoryCost()
Eigen::MatrixXd best_group_trajectory_
double min_clearence_
set the update limit for the robot joints
int getStartIndex() const
Gets the start index.
const collision_detection::CollisionRobotHybrid * hy_robot_
EigenSTL::vector_Vector3d gradients
double smoothness_cost_acceleration_
variables associated with the cost in velocity
double joint_update_limit_
set the ridge factor if pseudo inverse is enabled
EigenSTL::vector_Vector3d sphere_locations
void copyJointGroupPositions(const std::string &joint_group_name, std::vector< double > &gstate) const
std::vector< EigenSTL::vector_Vector3d > collision_point_vel_eigen_
bool isContinuous() const
bool isParent(const std::string &childLink, const std::string &parentLink) const
std::vector< EigenSTL::vector_Vector3d > collision_point_pos_eigen_
const JointModel * getParentJointModel() const
double best_group_trajectory_cost_
std::vector< int > state_is_in_collision_
int last_improvement_iteration_
const std::vector< const JointModel * > & getFixedJointModels() const
Eigen::Block< Eigen::MatrixXd, Eigen::Dynamic, Eigen::Dynamic > getFreeJointTrajectoryBlock(int joint)
Gets the block of free (optimizable) trajectory for a single joint.
double getCollisionCost()
void update(bool force=false)
int getNumPoints() const
Gets the number of points in the trajectory.
double smoothness_cost_jerk_
variables associated with the cost in acceleration
Eigen::MatrixXd group_trajectory_backup_
std::vector< double > sphere_radii
Eigen::MatrixXd::ColXpr getJointTrajectory(int joint)
int getEndIndex() const
Gets the end index.
void calculateTotalIncrements()
std::vector< double > distances
double obstacle_cost_weight_
Eigen::MatrixXd & getTrajectory()
Gets the entire trajectory matrix.
Represents the smoothness cost for CHOMP, for a single joint.
Eigen::MatrixXd collision_increments_
moveit::core::RobotState state_
std::vector< std::vector< double > > collision_point_potential_
double min(double a, double b)
void updateFromGroupTrajectory(const ChompTrajectory &group_trajectory)
Updates the full trajectory (*this) from the group trajectory.
Eigen::MatrixXd jacobian_
#define ROS_WARN_STREAM(args)
static const double DIFF_RULES[3][DIFF_RULE_LENGTH]
double worst_collision_cost_state_
bool filter_mode_
the collision threshold cost that needs to be mainted to avoid collisions
int max_iterations_
maximum time the optimizer can take to find a solution before terminating
void updateFullTrajectory()
Represents a discretized joint-space trajectory for CHOMP.
moveit::core::RobotState start_state_
double smoothness_cost_velocity_
std::vector< std::vector< int > > point_is_in_collision_
void performForwardKinematics()
int getNumFreePoints() const
Gets the number of points (that are free to be optimized) in the trajectory.
bool use_stochastic_descent_
variables associated with the cost in jerk
const JointModel * getJointModel(const std::string &joint) const
std::vector< EigenSTL::vector_Vector3d > joint_positions_
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::RobotState &robot_state, bool copy_attached_bodies=true)
#define ROS_INFO_STREAM(args)
const LinkModel * getChildLinkModel() const
void setJointGroupPositions(const std::string &joint_group_name, const double *gstate)
void setToRandomPositions()
double getDiscretization() const
Gets the discretization time interval of the trajectory.
virtual ~ChompOptimizer()
const LinkModel * getParentLinkModel() const
const Eigen::Affine3d & getJointTransform(const std::string &joint_name)
const std::vector< const JointModel * > & getActiveJointModels() const
std::vector< ChompCost > joint_costs_
void calculatePseudoInverse()
#define ROS_ERROR_STREAM(args)
void computeJointProperties(int trajectoryPoint)
void registerParents(const moveit::core::JointModel *model)
int getNumJoints() const
Gets the number of joints in each trajectory point.
int max_iterations_after_collision_free_
Eigen::VectorXd smoothness_derivative_
Eigen::Block< Eigen::MatrixXd, Eigen::Dynamic, Eigen::Dynamic > getFreeTrajectoryBlock()
Gets the block of the trajectory which can be optimized.
const VariableBounds & getVariableBounds(const std::string &variable) const
double max(double a, double b)
std::vector< VariableBounds > Bounds
IMETHOD void random(Vector &a)
JointType getType() const
Eigen::MatrixXd final_increments_
Eigen::VectorXd joint_state_velocities_
const Eigen::Vector3d & getAxis() const
const Eigen::Vector3d & getAxis() const
Eigen::VectorXd random_state_
std::string planning_group_