16 #include <moveit/collision_detection/collision_plugin.h>
17 #include <moveit/robot_state/conversions.h>
18 #include <pluginlib/class_loader.h>
21 #include <boost/tti/has_member_function.hpp>
22 BOOST_TTI_HAS_MEMBER_FUNCTION(getPose)
32 using PluginLoader = pluginlib::ClassLoader<collision_detection::CollisionPlugin>;
41 loader_.reset(
new PluginLoader(
"moveit_core",
"collision_detection::CollisionPlugin"));
43 catch (pluginlib::PluginlibException &e)
45 RBX_ERROR(
"Unable to construct collision plugin loader. Error: %s", e.what());
56 collision_detection::CollisionPluginPtr plugin;
60 plugin.reset(loader_->createUnmanagedInstance(name));
61 plugins_[name] = plugin;
63 catch (pluginlib::PluginlibException &ex)
65 RBX_ERROR(
"Exception while loading %s: %s", name, ex.what());
79 auto it = plugins_.find(name);
80 if (it == plugins_.end())
82 collision_detection::CollisionPluginPtr plugin = load(name);
84 return plugin->initialize(
scene, exclusive);
87 return it->second->initialize(
scene, exclusive);
106 template <
typename T>
107 T *attachObjectHelper(
114 has_member_function_getPose<
const Eigen::Isometry3d &(T::*)()
const>::value>::type *dummy = 0)
118 Eigen::Isometry3d::Identity(),
122 trajectory_msgs::JointTrajectory());
126 template <
typename T>
127 T *attachObjectHelper(
134 not has_member_function_getPose<
const Eigen::Isometry3d &(T::*)()
const>::value>::type *dummy = 0)
141 trajectory_msgs::JointTrajectory());
167 auto scene = std::make_shared<Scene>(
scene_->getRobotModel());
186 moveit_msgs::PlanningScene msg;
187 scene_->getPlanningSceneMsg(msg);
194 return scene_->getCurrentStateNonConst();
199 return scene_->getCurrentState();
205 return scene_->getAllowedCollisionMatrixNonConst();
210 return scene_->getAllowedCollisionMatrix();
218 scene_->setPlanningSceneMsg(msg);
220 scene_->setPlanningSceneDiffMsg(msg);
225 for (
auto &co : msg.world.collision_objects)
226 if (co.header.frame_id.empty() or not
scene_->knowsFrameTransform(co.header.frame_id))
227 co.header.frame_id =
scene_->getRobotModel()->getRootLinkName();
235 const auto &world =
scene_->getWorldNonConst();
236 if (world->hasObject(name))
238 if (!world->moveShapeInObject(name, geometry->getShape(), pose))
239 world->removeObject(name);
244 world->addToObject(name, geometry->getShape(), pose);
249 const auto &world =
scene_->getWorld();
250 return world->getObjectIds();
255 const auto &world =
scene_->getWorld();
257 const auto &obj = world->getObject(name);
259 return std::make_shared<Geometry>(*obj->shapes_[0]);
261 RBX_WARN(
"Object %s does not exist in scene!", name);
267 scene_->getWorldNonConst()->removeObject(name);
272 const auto &world =
scene_->getWorldNonConst();
273 const auto &obj = world->getObject(name);
276 #if ROBOWFLEX_MOVEIT_VERSION >= ROBOWFLEX_MOVEIT_VERSION_COMPUTE(1, 1, 6)
277 return obj->pose_ * obj->shape_poses_[0];
279 return obj->shape_poses_[0];
283 return RobotPose::Identity();
311 bool success =
false;
312 #if ROBOWFLEX_AT_LEAST_KINETIC
313 const auto &world =
scene_->getWorldNonConst();
314 success = world->moveObject(name, transform);
317 RBX_ERROR(
"Failed to move object %s", name);
327 const auto global_tf = pose * transform * pose.inverse();
335 if (not
scene_->knowsFrameTransform(
id))
336 RBX_WARN(
"Frame %s in not present in the scene!",
id);
338 return scene_->getFrameTransform(
id);
347 RBX_WARN(
"Was not able to load collision detector plugin '%s'", detector_name);
350 RBX_INFO(
"Using collision detector: %s",
scene_->getActiveCollisionDetectorName());
356 const auto &
robot = state.getRobotModel();
357 const auto &ee =
robot->getEndEffectors();
362 const auto &links = ee[0]->getLinkModelNames();
372 const auto &world =
scene_->getWorld();
373 if (!world->hasObject(name))
375 RBX_ERROR(
"World does not have object `%s`", name);
379 const auto &obj = world->getObject(name);
382 RBX_ERROR(
"Could not get object `%s`", name);
386 const auto &tf = state.getGlobalLinkTransform(ee_link);
389 for (
const auto &pose : obj->shape_poses_)
393 auto *body = attachObjectHelper<moveit::core::AttachedBody>(state.getLinkModel(ee_link),
398 state.attachBody(body);
433 const auto &world =
scene_->getWorld();
434 return world->hasObject(name);
446 const auto &world =
scene_->getWorldNonConst();
447 const auto &body = state.getAttachedBody(name);
451 RBX_ERROR(
"Robot does not have attached object `%s`", name);
455 world->addToObject(name, body->getShapes(), body->getGlobalCollisionBodyTransforms());
457 if (not state.clearAttachedBody(name))
459 RBX_ERROR(
"Could not detach object `%s`", name);
470 scene_->checkCollision(request, result, state);
477 return scene_->distanceToCollision(state);
488 #if ROBOWFLEX_MOVEIT_VERSION >= ROBOWFLEX_MOVEIT_VERSION_COMPUTE(1, 1, 0)
489 scene_->getCollisionEnv()->distanceRobot(req, res, state);
491 scene_->getCollisionWorld()->distanceRobot(req, res, *
scene_->getCollisionRobot(), state);
502 for (
unsigned int i = 0; i < links.size(); ++i)
503 for (
unsigned int j = i + 1; j < links.size(); ++j)
504 acm.
setEntry(links[i], links[j],
true);
507 for (
unsigned int i = 0; i < objs.size(); ++i)
508 for (
unsigned int j = i + 1; j < objs.size(); ++j)
509 acm.
setEntry(objs[i], objs[j],
true);
512 for (
const auto &link : links)
513 for (
const auto &obj : objs)
521 RBX_ERROR(
"World does not have object `%s`",
object);
542 RBX_ERROR(
"World does not have object `%s`", one);
548 RBX_ERROR(
"World does not have object `%s`", two);
565 return [
this, verbose](robot_state::RobotState *state,
567 const double *values)
569 state->setJointGroupPositions(jmg, values);
570 state->updateCollisionBodyTransforms();
576 return not result.collision;
582 moveit_msgs::PlanningScene msg;
583 scene_->getPlanningSceneMsg(msg);
591 moveit_msgs::PlanningScene msg;
598 if (msg.robot_state.joint_state.position.empty())
605 if (msg.allowed_collision_matrix.entry_names.empty())
613 if (models_dir.
empty())
616 moveit_msgs::PlanningScene msg;
620 scene_->usePlanningSceneMsg(msg);
void setEntry(const std::string &name1, const std::string &name2, bool allowed)
Exception that contains a message and an error code.
A const shared pointer wrapper for robowflex::Geometry.
A shared pointer wrapper for robowflex::Geometry.
void incrementVersion()
Increment the version number of this object.
A const shared pointer wrapper for robowflex::Robot.
A shared pointer wrapper for robowflex::Scene.
The actual plugin loader for collision plugins. Heavily inspired by code in moveit_ros/planning.
pluginlib::ClassLoader< collision_detection::CollisionPlugin > PluginLoader
The pluginlib loader for collision detection plugins.
collision_detection::CollisionPluginPtr load(const std::string &name)
Attempts to load the collision detector plugin by the given name. Saves the plugin in an internal map...
std::map< std::string, collision_detection::CollisionPluginPtr > plugins_
Loaded plugins.
CollisionPluginLoader()
Constructor. Attempts to create the pluginlib loader for collision plugins.
std::shared_ptr< PluginLoader > loader_
bool activate(const std::string &name, const planning_scene::PlanningScenePtr &scene, bool exclusive)
Loads a collision detector into a planning scene instance.
Wrapper class around the planning scene and collision geometry.
double distanceToCollision(const robot_state::RobotState &state) const
Get the distance to collision for a robot state.
bool moveObjectGlobal(const std::string &name, const RobotPose &transform)
Move all shapes in an object according to the given transform specified in world frame.
void clearACM(collision_detection::AllowedCollisionMatrix &acm) const
Disables collision between all entries in the ACM (all robot links and objects in the scene)
planning_scene::PlanningScenePtr scene_
Underlying planning scene.
CollisionPluginLoaderPtr loader_
Plugin loader that sets collision detectors for the scene.
ScenePtr deepCopy() const
Deep Copy.
void useMessage(const moveit_msgs::PlanningScene &msg, bool diff=false)
Set the planning scene to be the same as a message.
double distanceACM(const robot_state::RobotState &state, const collision_detection::AllowedCollisionMatrix &acm) const
Get the distance to collision using an ACM.
moveit::core::GroupStateValidityCallbackFn getGSVCF(bool verbose) const
Get the group state validity callback function that uses this scene.
RobotPose getObjectPose(const std::string &name) const
Get the current pose of a collision object. If the object has multiple geometries,...
void fixCollisionObjectFrame(moveit_msgs::PlanningScene &msg)
Corrects frame mismatches on loaded scenes by using the current root frame instead.
double distanceToObject(const robot_state::RobotState &state, const std::string &object) const
Get the distance to collision to a specific object.
bool fromYAMLFile(const std::string &file)
Load a planning scene from a YAML file.
bool detachObject(const std::string &name)
Helper function that detaches the object from the internal scene state.
void removeCollisionObject(const std::string &name)
Removes an object from the planning scene.
bool attachObjectToState(robot_state::RobotState &state, const std::string &name) const
Attach the named collision object name to the default end-effector of the given robot state....
moveit_msgs::PlanningScene getMessage() const
Get the message that describes the current planning scene.
collision_detection::AllowedCollisionMatrix & getACM()
Get the current allowed collision matrix of the planning scene.
std::vector< std::string > getCollisionObjects() const
Returns a list of all the names of collision objects in the scene.
bool toYAMLFile(const std::string &file) const
Serialize the current planning scene to a YAML file.
const planning_scene::PlanningScenePtr & getSceneConst() const
Get a const reference to the planning scene.
RobotPose getFramePose(const std::string &id) const
Get the pose of a particular frame in the scene. Example, use this to get the pose from /world to /ba...
bool hasObject(const std::string &name) const
Returns true if the object name is in the scene.
planning_scene::PlanningScenePtr & getScene()
Get a reference to the planning scene.
robot_state::RobotState & getCurrentState()
Get a reference to the current robot state in the planning scene.
GeometryPtr getObjectGeometry(const std::string &name) const
Returns a representation of a collision object in the scene as a Geometry. If the object has multiple...
void operator=(const Scene &scene)
Assignment Copy Constructor.
Scene(const RobotConstPtr &robot)
Constructor.
double distanceBetweenObjects(const std::string &one, const std::string &two) const
Get the distance to collision between two collision objects in the scene.
const collision_detection::AllowedCollisionMatrix & getACMConst() const
Get the current allowed collision matrix of the planning scene.
RobotPose getObjectGraspPose(const std::string &name, const RobotPose &offset) const
Get a pose in the global frame relative to a collision object for grasping.
bool attachObject(const std::string &name)
Helper function that attaches object to internal state and removes from scene.
const robot_state::RobotState & getCurrentStateConst() const
Get a constant reference to the current robot state in the planning scene.
bool setCollisionDetector(const std::string &detector_name) const
Attempts to set the collision detector plugin used by the scene to name. In MoveIt by default,...
collision_detection::CollisionResult checkCollision(const robot_state::RobotState &state, const collision_detection::CollisionRequest &request={}) const
Check if a robot state is in collision.
bool fromOpenRAVEXMLFile(const std::string &file, std::string models_dir="")
void updateCollisionObject(const std::string &name, const GeometryConstPtr &geometry, const RobotPose &pose)
Adds or updates collision object in the planning scene. If the geometry reference is the same,...
bool moveAllObjectsGlobal(const RobotPose &transform)
Move all objects according to a transform specified in world frame.
bool moveObjectLocal(const std::string &name, const RobotPose &transform)
Move all shapes in an object according to the given transform specified in object frame....
#define RBX_WARN(fmt,...)
Output a warning logging message.
#define RBX_ERROR(fmt,...)
Output a error logging message.
#define RBX_INFO(fmt,...)
Output a info logging message.
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::RobotState &robot_state, bool copy_attached_bodies=true)
boost::function< bool(RobotState *robot_state, const JointModelGroup *joint_group, const double *joint_group_variable_values)> GroupStateValidityCallbackFn
Functions for loading and animating robots in Blender.
bool YAMLToFile(const YAML::Node &node, const std::string &file)
Write the contents of a YAML node out to a potentially new file.
std::string resolveParent(const std::string &path)
Resolves package:// URLs to get the directory this path is in.
bool fromYAMLFile(moveit_msgs::PlanningScene &msg, const std::string &file)
Loads a planning scene from a YAML file.
YAML::Node toNode(const geometry_msgs::Pose &msg)
Converts a pose message to YAML.
std::string format(const std::string &fmt, Args &&... args)
Recursion base case, return string form of formatted arguments.
bool fromXMLFile(moveit_msgs::PlanningScene &planning_scene, const std::string &file, const std::string &model_dir)
Loads a planning_scene from an OpenRAVE Environment XML.
Main namespace. Contains all library classes and functions.
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Functions for loading and animating scenes in Blender.
const AllowedCollisionMatrix * acm
DistanceResultsData minimum_distance