-
Notifications
You must be signed in to change notification settings - Fork 608
Adds multi (joint) state validation service #3426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
riv-ljames
wants to merge
2
commits into
moveit:main
Choose a base branch
from
rivelinrobotics:feature/multi_state_validation_service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
moveit_ros/move_group/src/default_capabilities/multi_state_validation_service_capability.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include "multi_state_validation_service_capability.h" | ||
#include <moveit/moveit_cpp/moveit_cpp.h> | ||
#include <moveit/robot_state/conversions.h> | ||
#include <moveit/utils/message_checks.h> | ||
#include <moveit/collision_detection/collision_tools.h> | ||
#include <moveit/move_group/capability_names.h> | ||
|
||
namespace move_group | ||
{ | ||
MoveGroupMultiStateValidationService::MoveGroupMultiStateValidationService() : MoveGroupCapability("StateValidationService") | ||
{ | ||
} | ||
|
||
void MoveGroupMultiStateValidationService::initialize() | ||
{ | ||
validity_service_ = context_->moveit_cpp_->getNode()->create_service<moveit_msgs::srv::GetMultiStateValidity>( | ||
MULTI_STATE_VALIDITY_SERVICE_NAME, [this](const std::shared_ptr<rmw_request_id_t>& request_header, | ||
const std::shared_ptr<moveit_msgs::srv::GetMultiStateValidity::Request>& req, | ||
const std::shared_ptr<moveit_msgs::srv::GetMultiStateValidity::Response>& res) { | ||
return computeService(request_header, req, res); | ||
}); | ||
} | ||
|
||
bool MoveGroupMultiStateValidationService::computeService( | ||
const std::shared_ptr<rmw_request_id_t>& /* unused */, | ||
const std::shared_ptr<moveit_msgs::srv::GetMultiStateValidity::Request>& req, | ||
const std::shared_ptr<moveit_msgs::srv::GetMultiStateValidity::Response>& res) | ||
{ | ||
res->valid = true; | ||
planning_scene_monitor::LockedPlanningSceneRO ls(context_->planning_scene_monitor_); | ||
|
||
auto robot_state = req->robot_state; | ||
moveit::core::RobotState rs = ls->getCurrentState(); | ||
moveit::core::robotStateMsgToRobotState(robot_state, rs); | ||
|
||
for(int i=0; i<req->joint_states.size(); i++){ | ||
|
||
// Update robot state with next set of joint states | ||
rs.setVariableValues(req->joint_states[i]); | ||
|
||
// configure collision request | ||
collision_detection::CollisionRequest creq; | ||
creq.group_name = req->group_name; | ||
creq.cost = true; | ||
creq.contacts = true; | ||
creq.max_contacts = ls->getWorld()->size() + ls->getRobotModel()->getLinkModelsWithCollisionGeometry().size(); | ||
creq.max_cost_sources = creq.max_contacts; | ||
creq.max_contacts *= creq.max_contacts; | ||
collision_detection::CollisionResult cres; | ||
|
||
// check collision | ||
ls->checkCollision(creq, cres, rs); | ||
|
||
// copy contacts if any | ||
if (cres.collision) | ||
{ | ||
rclcpp::Time time_now = context_->moveit_cpp_->getNode()->get_clock()->now(); | ||
res->contacts.reserve(cres.contact_count); | ||
res->valid = false; | ||
for (collision_detection::CollisionResult::ContactMap::const_iterator it = cres.contacts.begin(); | ||
it != cres.contacts.end(); ++it) | ||
for (const collision_detection::Contact& contact : it->second) | ||
{ | ||
res->contacts.resize(res->contacts.size() + 1); | ||
collision_detection::contactToMsg(contact, res->contacts.back()); | ||
res->contacts.back().header.frame_id = ls->getPlanningFrame(); | ||
res->contacts.back().header.stamp = time_now; | ||
} | ||
} | ||
|
||
// copy cost sources | ||
res->cost_sources.reserve(cres.cost_sources.size()); | ||
for (const collision_detection::CostSource& cost_source : cres.cost_sources) | ||
{ | ||
res->cost_sources.resize(res->cost_sources.size() + 1); | ||
collision_detection::costSourceToMsg(cost_source, res->cost_sources.back()); | ||
} | ||
|
||
// evaluate constraints | ||
if (!moveit::core::isEmpty(req->constraints)) | ||
{ | ||
kinematic_constraints::KinematicConstraintSet kset(ls->getRobotModel()); | ||
kset.add(req->constraints, ls->getTransforms()); | ||
std::vector<kinematic_constraints::ConstraintEvaluationResult> kres; | ||
kinematic_constraints::ConstraintEvaluationResult total_result = kset.decide(rs, kres); | ||
if (!total_result.satisfied) | ||
res->valid = false; | ||
|
||
// copy constraint results | ||
res->constraint_result.resize(kres.size()); | ||
for (std::size_t k = 0; k < kres.size(); ++k) | ||
{ | ||
res->constraint_result[k].result = kres[k].satisfied; | ||
res->constraint_result[k].distance = kres[k].distance; | ||
} | ||
} | ||
|
||
// No need to check any more joint states after the first invalid one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily, depends on whether users want speed or a complete list of the states that were invalid. You could consider adding an "early stop" boolean in the service request? |
||
if(!res->valid){ | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} // namespace move_group | ||
|
||
#include <pluginlib/class_list_macros.hpp> | ||
|
||
PLUGINLIB_EXPORT_CLASS(move_group::MoveGroupMultiStateValidationService, move_group::MoveGroupCapability) |
22 changes: 22 additions & 0 deletions
22
moveit_ros/move_group/src/default_capabilities/multi_state_validation_service_capability.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <moveit/move_group/move_group_capability.h> | ||
#include <moveit_msgs/srv/get_multi_state_validity.hpp> | ||
|
||
namespace move_group | ||
{ | ||
class MoveGroupMultiStateValidationService : public MoveGroupCapability | ||
{ | ||
public: | ||
MoveGroupMultiStateValidationService(); | ||
|
||
void initialize() override; | ||
|
||
private: | ||
bool computeService(const std::shared_ptr<rmw_request_id_t>& request_header, | ||
const std::shared_ptr<moveit_msgs::srv::GetMultiStateValidity::Request>& req, | ||
const std::shared_ptr<moveit_msgs::srv::GetMultiStateValidity::Response>& res); | ||
|
||
rclcpp::Service<moveit_msgs::srv::GetMultiStateValidity>::SharedPtr validity_service_; | ||
}; | ||
} // namespace move_group |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
++i
as in other loops