moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
constrained_goal_sampler.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Ioan Sucan */
36
41
42#include <utility>
43
44namespace ompl_interface
45{
46namespace
47{
48rclcpp::Logger getLogger()
49{
50 return moveit::getLogger("moveit.planners.ompl.constrained_goal_sampler");
51}
52} // namespace
53
55 kinematic_constraints::KinematicConstraintSetPtr ks,
56 constraint_samplers::ConstraintSamplerPtr cs)
57 : ob::GoalLazySamples(
58 pc->getOMPLSimpleSetup()->getSpaceInformation(),
59 [this](const GoalLazySamples* gls, ompl::base::State* state) {
60 return sampleUsingConstraintSampler(gls, state);
61 },
62 false)
63 , planning_context_(pc)
64 , kinematic_constraint_set_(std::move(ks))
65 , constraint_sampler_(std::move(cs))
66 , work_state_(pc->getCompleteInitialRobotState())
67 , invalid_sampled_constraints_(0)
68 , warned_invalid_samples_(false)
69 , verbose_display_(0)
70{
71 if (!constraint_sampler_)
72 default_sampler_ = si_->allocStateSampler();
73 RCLCPP_DEBUG(getLogger(), "Constructed a ConstrainedGoalSampler instance at address %p", this);
74 startSampling();
75}
76
77bool ConstrainedGoalSampler::checkStateValidity(ob::State* new_goal, const moveit::core::RobotState& state,
78 bool verbose) const
79{
80 planning_context_->getOMPLStateSpace()->copyToOMPLState(new_goal, state);
81 return static_cast<const StateValidityChecker*>(si_->getStateValidityChecker().get())->isValid(new_goal, verbose);
82}
83
84bool ConstrainedGoalSampler::stateValidityCallback(ob::State* new_goal, const moveit::core::RobotState* state,
85 const moveit::core::JointModelGroup* jmg, const double* jpos,
86 bool verbose) const
87{
88 // we copy the state to not change the seed state
89 moveit::core::RobotState solution_state(*state);
90 solution_state.setJointGroupPositions(jmg, jpos);
91 solution_state.update();
92 return checkStateValidity(new_goal, solution_state, verbose) &&
93 kinematic_constraint_set_->decide(solution_state, verbose).satisfied;
94}
95
96bool ConstrainedGoalSampler::sampleUsingConstraintSampler(const ob::GoalLazySamples* gls, ob::State* new_goal)
97{
98 unsigned int max_attempts = planning_context_->getMaximumGoalSamplingAttempts();
99 unsigned int attempts_so_far = gls->samplingAttemptsCount();
100
101 // terminate after too many attempts
102 if (attempts_so_far >= max_attempts)
103 return false;
104
105 // terminate after a maximum number of samples
106 if (gls->getStateCount() >= planning_context_->getMaximumGoalSamples())
107 return false;
108
109 // terminate the sampling thread when a solution has been found
110 if (planning_context_->getOMPLSimpleSetup()->getProblemDefinition()->hasSolution())
111 return false;
112
113 unsigned int max_attempts_div2 = max_attempts / 2;
114 for (unsigned int a = gls->samplingAttemptsCount(); a < max_attempts && gls->isSampling(); ++a)
115 {
116 bool verbose = false;
117 if (gls->getStateCount() == 0 && a >= max_attempts_div2)
118 {
119 if (verbose_display_ < 1)
120 {
121 verbose = true;
122 verbose_display_++;
123 }
124 }
125
126 if (constraint_sampler_)
127 {
128 // makes the constraint sampler also perform a validity callback
129 moveit::core::GroupStateValidityCallbackFn gsvcf = [this, new_goal,
130 verbose](moveit::core::RobotState* robot_state,
131 const moveit::core::JointModelGroup* joint_group,
132 const double* joint_group_variable_values) {
133 return stateValidityCallback(new_goal, robot_state, joint_group, joint_group_variable_values, verbose);
134 };
135 constraint_sampler_->setGroupStateValidityCallback(gsvcf);
136
137 if (constraint_sampler_->sample(work_state_, planning_context_->getMaximumStateSamplingAttempts()))
138 {
139 work_state_.update();
140 if (kinematic_constraint_set_->decide(work_state_, verbose).satisfied)
141 {
142 if (checkStateValidity(new_goal, work_state_, verbose))
143 return true;
144 }
145 else
146 {
147 invalid_sampled_constraints_++;
148 if (!warned_invalid_samples_ && invalid_sampled_constraints_ >= (attempts_so_far * 8) / 10)
149 {
150 warned_invalid_samples_ = true;
151 RCLCPP_WARN(getLogger(), "More than 80%% of the sampled goal states "
152 "fail to satisfy the constraints imposed on the goal sampler. "
153 "Is the constrained sampler working correctly?");
154 }
155 }
156 }
157 }
158 else
159 {
160 default_sampler_->sampleUniform(new_goal);
161 if (static_cast<const StateValidityChecker*>(si_->getStateValidityChecker().get())->isValid(new_goal, verbose))
162 {
163 planning_context_->getOMPLStateSpace()->copyToRobotState(work_state_, new_goal);
164 if (kinematic_constraint_set_->decide(work_state_, verbose).satisfied)
165 return true;
166 }
167 }
168 }
169 return false;
170}
171
172} // namespace ompl_interface
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())
const ModelBasedStateSpacePtr & getOMPLStateSpace() const
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_...
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition logger.cpp:106
The MoveIt interface to OMPL.