39#include <moveit_msgs/srv/get_planning_scene.hpp>
40#include <moveit_msgs/srv/apply_planning_scene.hpp>
42#include <rclcpp/executors.hpp>
43#include <rclcpp/future_return_code.hpp>
56 rclcpp::NodeOptions options;
57 options.arguments({
"--ros-args",
"-r",
58 "__node:=" + std::string(
"planning_scene_interface_") +
59 std::to_string(
reinterpret_cast<std::size_t
>(
this)) });
60 node_ = rclcpp::Node::make_shared(
"_", ns, options);
62 planning_scene_diff_publisher_ = node_->create_publisher<moveit_msgs::msg::PlanningScene>(
"planning_scene", 1);
63 planning_scene_service_ =
64 node_->create_client<moveit_msgs::srv::GetPlanningScene>(move_group::GET_PLANNING_SCENE_SERVICE_NAME);
65 apply_planning_scene_service_ =
66 node_->create_client<moveit_msgs::srv::ApplyPlanningScene>(move_group::APPLY_PLANNING_SCENE_SERVICE_NAME);
70 waitForService(std::static_pointer_cast<rclcpp::ClientBase>(planning_scene_service_));
71 waitForService(std::static_pointer_cast<rclcpp::ClientBase>(apply_planning_scene_service_));
77 auto request = std::make_shared<moveit_msgs::srv::GetPlanningScene::Request>();
78 moveit_msgs::srv::GetPlanningScene::Response::SharedPtr response;
79 std::vector<std::string> result;
80 request->components.components = request->components.WORLD_OBJECT_NAMES;
82 auto res = planning_scene_service_->async_send_request(request);
83 if (rclcpp::spin_until_future_complete(node_, res) != rclcpp::FutureReturnCode::SUCCESS)
91 for (
const moveit_msgs::msg::CollisionObject& collision_object : response->scene.world.collision_objects)
93 if (!collision_object.type.key.empty())
94 result.push_back(collision_object.id);
99 for (
const moveit_msgs::msg::CollisionObject& collision_object : response->scene.world.collision_objects)
100 result.push_back(collision_object.id);
106 double maxz,
bool with_type, std::vector<std::string>& types)
108 auto request = std::make_shared<moveit_msgs::srv::GetPlanningScene::Request>();
109 moveit_msgs::srv::GetPlanningScene::Response::SharedPtr response;
110 std::vector<std::string> result;
111 request->components.components = request->components.WORLD_OBJECT_GEOMETRY;
113 auto res = planning_scene_service_->async_send_request(request);
114 if (rclcpp::spin_until_future_complete(node_, res) != rclcpp::FutureReturnCode::SUCCESS)
116 RCLCPP_WARN(node_->get_logger(),
"Could not call planning scene service to get object names");
119 response = res.get();
121 for (
const moveit_msgs::msg::CollisionObject& collision_object : response->scene.world.collision_objects)
123 if (with_type && collision_object.type.key.empty())
125 if (collision_object.mesh_poses.empty() && collision_object.primitive_poses.empty())
128 for (
const geometry_msgs::msg::Pose& mesh_pose : collision_object.mesh_poses)
130 if (mesh_pose.position.x < minx || mesh_pose.position.x > maxx || mesh_pose.position.y < miny ||
131 mesh_pose.position.y > maxy || mesh_pose.position.z < minz || mesh_pose.position.z > maxz)
137 for (
const geometry_msgs::msg::Pose& primitive_pose : collision_object.primitive_poses)
139 if (primitive_pose.position.x < minx || primitive_pose.position.x > maxx || primitive_pose.position.y < miny ||
140 primitive_pose.position.y > maxy || primitive_pose.position.z < minz || primitive_pose.position.z > maxz)
148 result.push_back(collision_object.id);
150 types.push_back(collision_object.type.key);
156 std::map<std::string, geometry_msgs::msg::Pose>
getObjectPoses(
const std::vector<std::string>& object_ids)
158 auto request = std::make_shared<moveit_msgs::srv::GetPlanningScene::Request>();
159 moveit_msgs::srv::GetPlanningScene::Response::SharedPtr response;
160 std::map<std::string, geometry_msgs::msg::Pose> result;
161 request->components.components = request->components.WORLD_OBJECT_GEOMETRY;
163 auto res = planning_scene_service_->async_send_request(request);
164 if (rclcpp::spin_until_future_complete(node_, res) == rclcpp::FutureReturnCode::SUCCESS)
166 response = res.get();
167 for (
const moveit_msgs::msg::CollisionObject& collision_object : response->scene.world.collision_objects)
169 if (std::find(object_ids.begin(), object_ids.end(), collision_object.id) != object_ids.end())
171 result[collision_object.id] = collision_object.pose;
178 std::map<std::string, moveit_msgs::msg::CollisionObject>
getObjects(
const std::vector<std::string>& object_ids)
180 auto request = std::make_shared<moveit_msgs::srv::GetPlanningScene::Request>();
181 moveit_msgs::srv::GetPlanningScene::Response::SharedPtr response;
182 std::map<std::string, moveit_msgs::msg::CollisionObject> result;
183 request->components.components = request->components.WORLD_OBJECT_GEOMETRY;
185 auto res = planning_scene_service_->async_send_request(request);
186 if (rclcpp::spin_until_future_complete(node_, res) != rclcpp::FutureReturnCode::SUCCESS)
188 RCLCPP_WARN(node_->get_logger(),
"Could not call planning scene service to get object geometries");
191 response = res.get();
193 for (
const moveit_msgs::msg::CollisionObject& collision_object : response->scene.world.collision_objects)
195 if (object_ids.empty() || std::find(object_ids.begin(), object_ids.end(), collision_object.id) != object_ids.end())
197 result[collision_object.id] = collision_object;
203 std::map<std::string, moveit_msgs::msg::AttachedCollisionObject>
206 auto request = std::make_shared<moveit_msgs::srv::GetPlanningScene::Request>();
207 moveit_msgs::srv::GetPlanningScene::Response::SharedPtr response;
208 std::map<std::string, moveit_msgs::msg::AttachedCollisionObject> result;
209 request->components.components = request->components.ROBOT_STATE_ATTACHED_OBJECTS;
211 auto res = planning_scene_service_->async_send_request(request);
212 if (rclcpp::spin_until_future_complete(node_, res) != rclcpp::FutureReturnCode::SUCCESS)
214 RCLCPP_WARN(node_->get_logger(),
"Could not call planning scene service to get attached object geometries");
217 response = res.get();
219 for (
const moveit_msgs::msg::AttachedCollisionObject& attached_collision_object :
220 response->scene.robot_state.attached_collision_objects)
222 if (object_ids.empty() ||
223 std::find(object_ids.begin(), object_ids.end(), attached_collision_object.object.id) != object_ids.end())
225 result[attached_collision_object.object.id] = attached_collision_object;
233 auto request = std::make_shared<moveit_msgs::srv::ApplyPlanningScene::Request>();
234 moveit_msgs::srv::ApplyPlanningScene::Response::SharedPtr response;
237 auto res = apply_planning_scene_service_->async_send_request(request);
238 if (rclcpp::spin_until_future_complete(node_, res) != rclcpp::FutureReturnCode::SUCCESS)
240 RCLCPP_WARN(node_->get_logger(),
"Failed to call ApplyPlanningScene service");
242 response = res.get();
243 return response->success;
247 const std::vector<moveit_msgs::msg::ObjectColor>& object_colors)
const
255 if (
planning_scene.object_colors[i].id.empty() && i < collision_objects.size())
272 moveit_msgs::msg::CollisionObject object;
273 for (
const std::string& object_id : object_ids)
275 object.id = object_id;
276 object.operation =
object.REMOVE;
284 void waitForService(
const std::shared_ptr<rclcpp::ClientBase>& srv)
288 std::chrono::duration<double> d(5.0);
289 srv->wait_for_service(std::chrono::duration_cast<std::chrono::nanoseconds>(d));
290 if (!srv->service_is_ready())
292 RCLCPP_WARN_STREAM(node_->get_logger(),
293 "service '" << srv->get_service_name() <<
"' not advertised yet. Continue waiting...");
294 srv->wait_for_service();
298 rclcpp::Node::SharedPtr node_;
299 rclcpp::Client<moveit_msgs::srv::GetPlanningScene>::SharedPtr planning_scene_service_;
300 rclcpp::Client<moveit_msgs::srv::ApplyPlanningScene>::SharedPtr apply_planning_scene_service_;
301 rclcpp::Publisher<moveit_msgs::msg::PlanningScene>::SharedPtr planning_scene_diff_publisher_;
302 moveit::core::RobotModelConstPtr robot_model_;
321 double maxx,
double maxy,
double maxz,
323 std::vector<std::string>& types)
328std::map<std::string, geometry_msgs::msg::Pose>
334std::map<std::string, moveit_msgs::msg::CollisionObject>
340std::map<std::string, moveit_msgs::msg::AttachedCollisionObject>
348 moveit_msgs::msg::PlanningScene ps;
349 ps.robot_state.is_diff =
true;
351 ps.world.collision_objects.reserve(1);
352 ps.world.collision_objects.push_back(collision_object);
357 const std_msgs::msg::ColorRGBA& object_color)
359 moveit_msgs::msg::PlanningScene ps;
360 ps.robot_state.is_diff =
true;
362 ps.world.collision_objects.reserve(1);
363 ps.world.collision_objects.push_back(collision_object);
364 moveit_msgs::msg::ObjectColor oc;
365 oc.id = collision_object.id;
366 oc.color = object_color;
367 ps.object_colors.push_back(oc);
372 const std::vector<moveit_msgs::msg::CollisionObject>& collision_objects,
373 const std::vector<moveit_msgs::msg::ObjectColor>& object_colors)
375 moveit_msgs::msg::PlanningScene ps;
376 ps.robot_state.is_diff =
true;
378 ps.world.collision_objects = collision_objects;
379 ps.object_colors = object_colors;
381 for (
size_t i = 0; i < ps.object_colors.size(); ++i)
383 if (ps.object_colors[i].id.empty() && i < collision_objects.size())
385 ps.object_colors[i].id = collision_objects[i].id;
397 const moveit_msgs::msg::AttachedCollisionObject& collision_object)
399 moveit_msgs::msg::PlanningScene ps;
400 ps.robot_state.is_diff =
true;
402 ps.robot_state.attached_collision_objects.reserve(1);
403 ps.robot_state.attached_collision_objects.push_back(collision_object);
408 const std::vector<moveit_msgs::msg::AttachedCollisionObject>& collision_objects)
410 moveit_msgs::msg::PlanningScene ps;
411 ps.robot_state.is_diff =
true;
413 ps.robot_state.attached_collision_objects = collision_objects;
423 const std::vector<moveit_msgs::msg::ObjectColor>& object_colors)
const
std::map< std::string, geometry_msgs::msg::Pose > getObjectPoses(const std::vector< std::string > &object_ids)
std::map< std::string, moveit_msgs::msg::AttachedCollisionObject > getAttachedObjects(const std::vector< std::string > &object_ids)
std::map< std::string, moveit_msgs::msg::CollisionObject > getObjects(const std::vector< std::string > &object_ids)
std::vector< std::string > getKnownObjectNames(bool with_type)
bool applyPlanningScene(const moveit_msgs::msg::PlanningScene &planning_scene)
void removeCollisionObjects(const std::vector< std::string > &object_ids) const
void addCollisionObjects(const std::vector< moveit_msgs::msg::CollisionObject > &collision_objects, const std::vector< moveit_msgs::msg::ObjectColor > &object_colors) const
std::vector< std::string > getKnownObjectNamesInROI(double minx, double miny, double minz, double maxx, double maxy, double maxz, bool with_type, std::vector< std::string > &types)
PlanningSceneInterfaceImpl(const std::string &ns="", bool wait=true)
bool applyAttachedCollisionObject(const moveit_msgs::msg::AttachedCollisionObject &attached_collision_object)
Apply attached collision object to the planning scene of the move_group node synchronously....
std::map< std::string, moveit_msgs::msg::AttachedCollisionObject > getAttachedObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
Get the attached objects identified by the given object ids list. If no ids are provided,...
bool applyPlanningScene(const moveit_msgs::msg::PlanningScene &ps)
Update the planning_scene of the move_group node with the given ps synchronously. Other PlanningScene...
std::vector< std::string > getKnownObjectNames(bool with_type=false)
Get the names of all known objects in the world. If with_type is set to true, only return objects tha...
bool applyAttachedCollisionObjects(const std::vector< moveit_msgs::msg::AttachedCollisionObject > &attached_collision_objects)
Apply attached collision objects to the planning scene of the move_group node synchronously....
std::map< std::string, moveit_msgs::msg::CollisionObject > getObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
Get the objects identified by the given object ids list. If no ids are provided, return all the known...
std::vector< std::string > getKnownObjectNamesInROI(double minx, double miny, double minz, double maxx, double maxy, double maxz, bool with_type, std::vector< std::string > &types)
Get the names of known objects in the world that are located within a bounding region (specified in t...
PlanningSceneInterface(const std::string &ns="", bool wait=true)
void removeCollisionObjects(const std::vector< std::string > &object_ids) const
Remove collision objects from the world via /planning_scene.
std::map< std::string, geometry_msgs::msg::Pose > getObjectPoses(const std::vector< std::string > &object_ids)
Get the poses from the objects identified by the given object ids list.
~PlanningSceneInterface()
bool applyCollisionObject(const moveit_msgs::msg::CollisionObject &collision_object)
Apply collision object to the planning scene of the move_group node synchronously....
bool applyCollisionObjects(const std::vector< moveit_msgs::msg::CollisionObject > &collision_objects, const std::vector< moveit_msgs::msg::ObjectColor > &object_colors=std::vector< moveit_msgs::msg::ObjectColor >())
Apply collision objects to the planning scene of the move_group node synchronously....
void addCollisionObjects(const std::vector< moveit_msgs::msg::CollisionObject > &collision_objects, const std::vector< moveit_msgs::msg::ObjectColor > &object_colors=std::vector< moveit_msgs::msg::ObjectColor >()) const
Add collision objects to the world via /planning_scene. Make sure object.operation is set to object....
Main namespace for MoveIt.
void setNodeLoggerName(const std::string &name)
Call once after creating a node to initialize logging namespaces.
This namespace includes the base class for MoveIt planners.
This namespace includes the central class for representing planning contexts.