45 static const rclcpp::Logger LOGGER = rclcpp::get_logger(
"moveit.ompl_planning.constrained_goal_sampler");
49 kinematic_constraints::KinematicConstraintSetPtr ks,
50 constraint_samplers::ConstraintSamplerPtr cs)
51 : ob::GoalLazySamples(
52 pc->getOMPLSimpleSetup()->getSpaceInformation(),
53 [this](const GoalLazySamples* gls, ompl::base::State* state) {
54 return sampleUsingConstraintSampler(gls, state);
57 , planning_context_(pc)
58 , kinematic_constraint_set_(std::move(ks))
59 , constraint_sampler_(std::move(cs))
60 , work_state_(pc->getCompleteInitialRobotState())
61 , invalid_sampled_constraints_(0)
62 , warned_invalid_samples_(
false)
65 if (!constraint_sampler_)
66 default_sampler_ = si_->allocStateSampler();
67 RCLCPP_DEBUG(LOGGER,
"Constructed a ConstrainedGoalSampler instance at address %p",
this);
71 bool ompl_interface::ConstrainedGoalSampler::checkStateValidity(ob::State* new_goal,
75 planning_context_->getOMPLStateSpace()->copyToOMPLState(new_goal, state);
76 return static_cast<const StateValidityChecker*
>(si_->getStateValidityChecker().get())->isValid(new_goal, verbose);
79 bool ompl_interface::ConstrainedGoalSampler::stateValidityCallback(ob::State* new_goal,
82 const double* jpos,
bool verbose)
const
86 solution_state.setJointGroupPositions(jmg, jpos);
87 solution_state.update();
88 return checkStateValidity(new_goal, solution_state, verbose);
91 bool ompl_interface::ConstrainedGoalSampler::sampleUsingConstraintSampler(
const ob::GoalLazySamples* gls,
94 unsigned int max_attempts = planning_context_->getMaximumGoalSamplingAttempts();
95 unsigned int attempts_so_far = gls->samplingAttemptsCount();
98 if (attempts_so_far >= max_attempts)
102 if (gls->getStateCount() >= planning_context_->getMaximumGoalSamples())
106 if (planning_context_->getOMPLSimpleSetup()->getProblemDefinition()->hasSolution())
109 unsigned int max_attempts_div2 = max_attempts / 2;
110 for (
unsigned int a = gls->samplingAttemptsCount(); a < max_attempts && gls->isSampling(); ++
a)
112 bool verbose =
false;
113 if (gls->getStateCount() == 0 &&
a >= max_attempts_div2)
114 if (verbose_display_ < 1)
120 if (constraint_sampler_)
126 const double* joint_group_variable_values) {
127 return stateValidityCallback(new_goal, robot_state, joint_group, joint_group_variable_values, verbose);
129 constraint_sampler_->setGroupStateValidityCallback(gsvcf);
131 if (constraint_sampler_->sample(work_state_, planning_context_->getMaximumStateSamplingAttempts()))
133 work_state_.update();
134 if (kinematic_constraint_set_->decide(work_state_, verbose).satisfied)
136 if (checkStateValidity(new_goal, work_state_, verbose))
141 invalid_sampled_constraints_++;
142 if (!warned_invalid_samples_ && invalid_sampled_constraints_ >= (attempts_so_far * 8) / 10)
144 warned_invalid_samples_ =
true;
145 RCLCPP_WARN(LOGGER,
"More than 80%% of the sampled goal states "
146 "fail to satisfy the constraints imposed on the goal sampler. "
147 "Is the constrained sampler working correctly?");
154 default_sampler_->sampleUniform(new_goal);
155 if (
static_cast<const StateValidityChecker*
>(si_->getStateValidityChecker().get())->isValid(new_goal, verbose))
157 planning_context_->getOMPLStateSpace()->copyToRobotState(work_state_, new_goal);
158 if (kinematic_constraint_set_->decide(work_state_, verbose).satisfied)
Representation of a robot's state. This includes position, velocity, acceleration and effort.
ConstrainedGoalSampler(const ModelBasedPlanningContext *pc, kinematic_constraints::KinematicConstraintSetPtr ks, constraint_samplers::ConstraintSamplerPtr cs=constraint_samplers::ConstraintSamplerPtr())
std::function< bool(RobotState *robot_state, const JointModelGroup *joint_group, const double *joint_group_variable_values)> GroupStateValidityCallbackFn
Signature for functions that can verify that if the group joint_group in robot_state is set to joint_...
The MoveIt interface to OMPL.