38 #include <ompl/base/spaces/SE3StateSpace.h>
44 static const rclcpp::Logger LOGGER = rclcpp::get_logger(
"moveit.ompl_planning.pose_model_state_space");
59 for (
const auto& it : m)
60 poses_.emplace_back(it.first, it.second);
63 RCLCPP_ERROR(LOGGER,
"No kinematics solvers specified. Unable to construct a "
64 "PoseModelStateSpace");
66 std::sort(poses_.begin(), poses_.end());
73 const ompl::base::State* state2)
const
76 for (std::size_t i = 0; i < poses_.size(); ++i)
84 for (
const auto& pose : poses_)
85 total += pose.state_space_->getMaximumExtent();
93 new double[variable_count_];
94 state->poses =
new ompl::base::SE3StateSpace::StateType*[poses_.size()];
95 for (std::size_t i = 0; i < poses_.size(); ++i)
96 state->poses[i] = poses_[i].state_space_->allocState()->as<ompl::base::SE3StateSpace::StateType>();
102 for (std::size_t i = 0; i < poses_.size(); ++i)
103 poses_[i].state_space_->freeState(state->as<
StateType>()->
poses[i]);
109 const ompl::base::State* source)
const
114 for (std::size_t i = 0; i < poses_.size(); ++i)
118 computeStateK(destination);
123 ModelBasedStateSpace::sanityChecks(std::numeric_limits<double>::epsilon(), std::numeric_limits<float>::epsilon(),
124 ~ompl::base::StateSpace::STATESPACE_TRIANGLE_INEQUALITY);
128 const double t, ompl::base::State* state)
const
137 for (std::size_t i = 0; i < poses_.size(); ++i)
142 state->as<
StateType>()->setPoseComputed(
true);
154 if (computeStateIK(state))
161 if (d_from + d_to > std::max(0.2, dj))
167 double minZ,
double maxZ)
170 ompl::base::RealVectorBounds b(3);
177 for (
auto& pose : poses_)
178 pose.state_space_->as<ompl::base::SE3StateSpace>()->setBounds(b);
181 ompl_interface::PoseModelStateSpace::PoseComponent::PoseComponent(
183 : subgroup_(subgroup), kinematics_solver_(k.allocator_(subgroup)), bijection_(k.bijection_)
185 state_space_ = std::make_shared<ompl::base::SE3StateSpace>();
186 state_space_->setName(subgroup_->getName() +
"_Workspace");
187 fk_link_.resize(1, kinematics_solver_->getTipFrame());
188 if (!fk_link_[0].empty() && fk_link_[0][0] ==
'/')
189 fk_link_[0] = fk_link_[0].substr(1);
192 bool ompl_interface::PoseModelStateSpace::PoseComponent::computeStateFK(StateType* full_state,
unsigned int idx)
const
195 std::vector<double> values(bijection_.size());
196 for (
unsigned int i = 0; i < bijection_.size(); ++i)
197 values[i] = full_state->values[bijection_[i]];
200 std::vector<geometry_msgs::msg::Pose> poses;
201 if (!kinematics_solver_->getPositionFK(fk_link_, values, poses))
205 ompl::base::SE3StateSpace::StateType* se3_state = full_state->poses[idx];
206 se3_state->setXYZ(poses[0].position.x, poses[0].position.y, poses[0].position.z);
207 ompl::base::SO3StateSpace::StateType& so3_state = se3_state->rotation();
208 so3_state.x = poses[0].orientation.x;
209 so3_state.y = poses[0].orientation.y;
210 so3_state.z = poses[0].orientation.z;
211 so3_state.w = poses[0].orientation.w;
216 bool ompl_interface::PoseModelStateSpace::PoseComponent::computeStateIK(StateType* full_state,
unsigned int idx)
const
219 std::vector<double> seed_values(bijection_.size());
220 for (std::size_t i = 0; i < bijection_.size(); ++i)
221 seed_values[i] = full_state->values[bijection_[i]];
231 geometry_msgs::msg::Pose pose;
232 const ompl::base::SE3StateSpace::StateType* se3_state = full_state->poses[idx];
233 pose.position.x = se3_state->getX();
234 pose.position.y = se3_state->getY();
235 pose.position.z = se3_state->getZ();
236 const ompl::base::SO3StateSpace::StateType& so3_state = se3_state->rotation();
237 pose.orientation.x = so3_state.x;
238 pose.orientation.y = so3_state.y;
239 pose.orientation.z = so3_state.z;
240 pose.orientation.w = so3_state.w;
243 std::vector<double> solution(bijection_.size());
244 moveit_msgs::msg::MoveItErrorCodes err_code;
245 if (!kinematics_solver_->getPositionIK(pose, seed_values, solution, err_code))
247 if (err_code.val != moveit_msgs::msg::MoveItErrorCodes::TIMED_OUT ||
248 !kinematics_solver_->searchPositionIK(pose, seed_values, kinematics_solver_->getDefaultTimeout() * 2.0,
253 for (std::size_t i = 0; i < bijection_.size(); ++i)
254 full_state->values[bijection_[i]] = solution[i];
263 for (std::size_t i = 0; i < poses_.size(); ++i)
264 if (!poses_[i].computeStateFK(state->as<
StateType>(), i))
269 state->as<
StateType>()->setPoseComputed(
true);
277 for (std::size_t i = 0; i < poses_.size(); ++i)
278 if (!poses_[i].computeStateIK(state->as<
StateType>(), i))
283 state->as<
StateType>()->setJointsComputed(
true);
290 return computeStateFK(state);
292 return computeStateIK(state);
301 class PoseModelStateSampler :
public ompl::base::StateSampler
304 PoseModelStateSampler(
const ompl::base::StateSpace* space, ompl::base::StateSamplerPtr sampler)
305 : ompl::base::StateSampler(space), sampler_(std::move(sampler))
309 void sampleUniform(ompl::base::State* state)
override
311 sampler_->sampleUniform(state);
312 afterStateSample(state);
315 void sampleUniformNear(ompl::base::State* state,
const ompl::base::State* near,
const double distance)
override
317 sampler_->sampleUniformNear(state, near,
distance);
318 afterStateSample(state);
321 void sampleGaussian(ompl::base::State* state,
const ompl::base::State* mean,
const double stdDev)
override
323 sampler_->sampleGaussian(state, mean, stdDev);
324 afterStateSample(state);
328 void afterStateSample(ompl::base::State* sample)
const
330 sample->as<
StateType>()->setJointsComputed(
true);
331 sample->as<
StateType>()->setPoseComputed(
false);
335 ompl::base::StateSamplerPtr sampler_;
338 return ompl::base::StateSamplerPtr(
static_cast<ompl::base::StateSampler*
>(
346 state->as<
StateType>()->setJointsComputed(
true);
347 state->as<
StateType>()->setPoseComputed(
false);
348 computeStateFK(state);
const std::pair< KinematicsSolver, KinematicsSolverMap > & getGroupKinematics() const
std::map< const JointModelGroup *, KinematicsSolver > KinematicsSolverMap
Map from group instances to allocator functions & bijections.
Representation of a robot's state. This includes position, velocity, acceleration and effort.
virtual void setPlanningVolume(double minX, double maxX, double minY, double maxY, double minZ, double maxZ)
Set the planning volume for the possible SE2 and/or SE3 components of the state space.
ompl::base::StateSamplerPtr allocDefaultStateSampler() const override
void interpolate(const ompl::base::State *from, const ompl::base::State *to, const double t, ompl::base::State *state) const override
void freeState(ompl::base::State *state) const override
void copyState(ompl::base::State *destination, const ompl::base::State *source) const override
virtual void copyToOMPLState(ompl::base::State *state, const moveit::core::RobotState &rstate) const
Copy the data from a set of joint states to an OMPL state.
double distance(const ompl::base::State *state1, const ompl::base::State *state2) const override
bool jointsComputed() const
bool poseComputed() const
ompl::base::SE3StateSpace::StateType ** poses
double distance(const ompl::base::State *state1, const ompl::base::State *state2) const override
ompl::base::State * allocState() const override
PoseModelStateSpace(const ModelBasedStateSpaceSpecification &spec)
void copyState(ompl::base::State *destination, const ompl::base::State *source) const override
void setPlanningVolume(double minX, double maxX, double minY, double maxY, double minZ, double maxZ) override
Set the planning volume for the possible SE2 and/or SE3 components of the state space.
void sanityChecks() const override
double getMaximumExtent() const override
bool computeStateFK(ompl::base::State *state) const
void copyToOMPLState(ompl::base::State *state, const moveit::core::RobotState &rstate) const override
Copy the data from a set of joint states to an OMPL state.
bool computeStateK(ompl::base::State *state) const
void freeState(ompl::base::State *state) const override
static const std::string PARAMETERIZATION_TYPE
~PoseModelStateSpace() override
ompl::base::StateSamplerPtr allocDefaultStateSampler() const override
void interpolate(const ompl::base::State *from, const ompl::base::State *to, const double t, ompl::base::State *state) const override
bool computeStateIK(ompl::base::State *state) const
The MoveIt interface to OMPL.
double distance(const urdf::Pose &transform)
const moveit::core::JointModelGroup * joint_model_group_