moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
state_validation_service_capability.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2012, 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
43
44namespace move_group
45{
49
51{
52 validity_service_ = context_->moveit_cpp_->getNode()->create_service<moveit_msgs::srv::GetStateValidity>(
53 STATE_VALIDITY_SERVICE_NAME, [this](const std::shared_ptr<rmw_request_id_t>& request_header,
54 const std::shared_ptr<moveit_msgs::srv::GetStateValidity::Request>& req,
55 const std::shared_ptr<moveit_msgs::srv::GetStateValidity::Response>& res) {
56 return computeService(request_header, req, res);
57 });
58}
59
62 const std::string& group_name, const moveit_msgs::msg::Constraints& constraints,
63 std::vector<moveit_msgs::msg::ContactInformation>& contacts,
64 std::vector<moveit_msgs::msg::CostSource>& cost_sources,
65 std::vector<moveit_msgs::msg::ConstraintEvalResult>& constraint_result)
66{
67 bool valid = true;
68
69 // configure collision request
71 creq.group_name = group_name;
72 creq.cost = true;
73 creq.contacts = true;
74 creq.max_contacts = ls->getWorld()->size() + ls->getRobotModel()->getLinkModelsWithCollisionGeometry().size();
76 creq.max_contacts *= creq.max_contacts;
78
79 // check collision
80 ls->checkCollision(creq, cres, rs);
81
82 // copy contacts if any
83 if (cres.collision)
84 {
85 rclcpp::Time time_now = context_->moveit_cpp_->getNode()->get_clock()->now();
86 contacts.reserve(cres.contact_count);
87 valid = false;
88 for (collision_detection::CollisionResult::ContactMap::const_iterator it = cres.contacts.begin();
89 it != cres.contacts.end(); ++it)
90 {
91 for (const collision_detection::Contact& contact : it->second)
92 {
93 contacts.resize(contacts.size() + 1);
94 collision_detection::contactToMsg(contact, contacts.back());
95 contacts.back().header.frame_id = ls->getPlanningFrame();
96 contacts.back().header.stamp = time_now;
97 }
98 }
99 }
100
101 // copy cost sources
102 cost_sources.reserve(cres.cost_sources.size());
103 for (const collision_detection::CostSource& cost_source : cres.cost_sources)
104 {
105 cost_sources.resize(cost_sources.size() + 1);
106 collision_detection::costSourceToMsg(cost_source, cost_sources.back());
107 }
108
109 // evaluate constraints
110 if (!moveit::core::isEmpty(constraints))
111 {
112 kinematic_constraints::KinematicConstraintSet kset(ls->getRobotModel());
113 kset.add(constraints, ls->getTransforms());
114 std::vector<kinematic_constraints::ConstraintEvaluationResult> kres;
115 kinematic_constraints::ConstraintEvaluationResult total_result = kset.decide(rs, kres);
116 if (!total_result.satisfied)
117 {
118 valid = false;
119 }
120
121 // copy constraint results
122 constraint_result.resize(kres.size());
123 for (std::size_t k = 0; k < kres.size(); ++k)
124 {
125 constraint_result[k].result = kres[k].satisfied;
126 constraint_result[k].distance = kres[k].distance;
127 }
128 }
129
130 return valid;
131}
132
133bool MoveGroupStateValidationService::computeService(
134 const std::shared_ptr<rmw_request_id_t>& /* unused */,
135 const std::shared_ptr<moveit_msgs::srv::GetStateValidity::Request>& req,
136 const std::shared_ptr<moveit_msgs::srv::GetStateValidity::Response>& res)
137{
138 planning_scene_monitor::LockedPlanningSceneRO ls(context_->planning_scene_monitor_);
139 moveit::core::RobotState rs = ls->getCurrentState();
140 moveit::core::robotStateMsgToRobotState(req->robot_state, rs);
141 res->valid =
142 isStateValid(ls, rs, req->group_name, req->constraints, res->contacts, res->cost_sources, res->constraint_result);
143 return true;
144}
145} // namespace move_group
146
147#include <pluginlib/class_list_macros.hpp>
148
PLUGINLIB_EXPORT_CLASS(cached_ik_kinematics_plugin::CachedIKKinematicsPlugin< kdl_kinematics_plugin::KDLKinematicsPlugin >, kinematics::KinematicsBase)
A class that contains many different constraints, and can check RobotState *versus the full set....
bool add(const moveit_msgs::msg::Constraints &c, const moveit::core::Transforms &tf)
Add all known constraints.
ConstraintEvaluationResult decide(const moveit::core::RobotState &state, bool verbose=false) const
Determines whether all constraints are satisfied by state, returning a single evaluation result.
bool isStateValid(const planning_scene_monitor::LockedPlanningSceneRO &ls, const moveit::core::RobotState &rs, const std::string &group_name, const moveit_msgs::msg::Constraints &constraints, std::vector< moveit_msgs::msg::ContactInformation > &contacts, std::vector< moveit_msgs::msg::CostSource > &cost_sources, std::vector< moveit_msgs::msg::ConstraintEvalResult > &constraint_result)
Representation of a robot's state. This includes position, velocity, acceleration and effort.
This is a convenience class for obtaining access to an instance of a locked PlanningScene.
void contactToMsg(const Contact &contact, moveit_msgs::msg::ContactInformation &msg)
void costSourceToMsg(const CostSource &cost_source, moveit_msgs::msg::CostSource &msg)
bool isEmpty(const moveit_msgs::msg::PlanningScene &msg)
Check if a message includes any information about a planning scene, or whether it is empty.
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::msg::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
Convert a robot state msg (with accompanying extra transforms) to a MoveIt robot state.
Representation of a collision checking request.
std::string group_name
The group name to check collisions for (optional; if empty, assume the complete robot)....
std::size_t max_cost_sources
When costs are computed, this value defines how many of the top cost sources should be returned.
bool contacts
If true, compute contacts. Otherwise only a binary collision yes/no is reported.
bool cost
If true, a collision cost is computed.
std::size_t max_contacts
Overall maximum number of contacts to compute.
Representation of a collision checking result.
std::set< CostSource > cost_sources
These are the individual cost sources when costs are computed.
bool collision
True if collision was found, false otherwise.
std::size_t contact_count
Number of contacts returned.
Definition of a contact point.
When collision costs are computed, this structure contains information about the partial cost incurre...
Struct for containing the results of constraint evaluation.
bool satisfied
Whether or not the constraint or constraints were satisfied.