basic_grasping_perception.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2014, Unbounded Robotics Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Unbounded Robotics, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 // Author: Michael Ferguson
31 
32 #include <ros/ros.h>
34 #include <tf/transform_listener.h>
35 
38 #include <grasping_msgs/FindGraspableObjectsAction.h>
39 
40 #include <pcl_ros/point_cloud.h>
41 #include <pcl_ros/transforms.h>
42 #include <pcl/filters/passthrough.h>
44 
45 namespace simple_grasping
46 {
47 
52 {
54 
55 public:
57  {
58  // use_debug: enable/disable output of a cloud containing object points
59  nh_.getParam("use_debug", debug_);
60 
61  // optionally enable object detection from the beginning without need to call the action
62  nh_.param<bool>("continuous_detection", continuous_detection_, false);
63 
64  // frame_id: frame to transform cloud to (should be XY horizontal)
65  world_frame_ = "base_link";
66  nh_.getParam("frame_id", world_frame_);
67 
68  // Create planner
69  planner_.reset(new ShapeGraspPlanner(nh_));
70 
71  // Create perception
73 
74  // Advertise an action for perception + planning
75  server_.reset(new server_t(nh_, "find_objects",
76  boost::bind(&BasicGraspingPerception::executeCallback, this, _1),
77  false));
78 
79  // Publish debugging views
80  if (debug_)
81  {
82  object_cloud_pub_ = nh_.advertise< pcl::PointCloud<pcl::PointXYZRGB> >("object_cloud", 1);
83  support_cloud_pub_ = nh_.advertise< pcl::PointCloud<pcl::PointXYZRGB> >("support_cloud", 1);
84  }
85 
86  // Range filter for cloud
87  range_filter_.setFilterFieldName("z");
88  range_filter_.setFilterLimits(0, 2.5);
89 
90  // Subscribe to head camera cloud
91  cloud_sub_ = nh_.subscribe< pcl::PointCloud<pcl::PointXYZRGB> >("/head_camera/depth_registered/points",
92  1,
94  this);
95 
96  // Start thread for action
97  server_->start();
98 
99  ROS_INFO("basic_grasping_perception initialized");
100  }
101 
102 private:
103  void cloudCallback(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& cloud)
104  {
105  // be lazy
107  return;
108 
109  ROS_DEBUG("Cloud received with %d points.", static_cast<int>(cloud->points.size()));
110 
111  // Filter out noisy long-range points
112  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZRGB>);
113  range_filter_.setInputCloud(cloud);
114  range_filter_.filter(*cloud_filtered);
115  ROS_DEBUG("Filtered for range, now %d points.", static_cast<int>(cloud_filtered->points.size()));
116 
117  // Transform to grounded
118  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_transformed(new pcl::PointCloud<pcl::PointXYZRGB>);
119  if (!pcl_ros::transformPointCloud(world_frame_, *cloud_filtered, *cloud_transformed, listener_))
120  {
121  ROS_ERROR("Error transforming to frame %s", world_frame_.c_str());
122  return;
123  }
124 
125  // Run segmentation
126  objects_.clear();
127  supports_.clear();
128  pcl::PointCloud<pcl::PointXYZRGB> object_cloud;
129  pcl::PointCloud<pcl::PointXYZRGB> support_cloud;
130  if (debug_)
131  {
132  object_cloud.header.frame_id = cloud_transformed->header.frame_id;
133  support_cloud.header.frame_id = cloud_transformed->header.frame_id;
134  }
135  segmentation_->segment(cloud_transformed, objects_, supports_, object_cloud, support_cloud, debug_);
136 
137  if (debug_)
138  {
139  object_cloud_pub_.publish(object_cloud);
140  support_cloud_pub_.publish(support_cloud);
141  }
142 
143  // Ok to continue processing
144  find_objects_ = false;
145  }
146 
147  void executeCallback(const grasping_msgs::FindGraspableObjectsGoalConstPtr& goal)
148  {
149  grasping_msgs::FindGraspableObjectsResult result;
150 
151  // Get objects
152  find_objects_ = true;
154  while (find_objects_ == true)
155  {
156  ros::Duration(1/50.0).sleep();
157  if (ros::Time::now() - t > ros::Duration(3.0))
158  {
159  find_objects_ = false;
160  server_->setAborted(result, "Failed to get camera data in allocated time.");
161  ROS_ERROR("Failed to get camera data in allocated time.");
162  return;
163  }
164  }
165 
166  // Set object results
167  for (size_t i = 0; i < objects_.size(); ++i)
168  {
169  grasping_msgs::GraspableObject g;
170  g.object = objects_[i];
171  if (goal->plan_grasps)
172  {
173  // Plan grasps for object
174  planner_->plan(objects_[i], g.grasps);
175  }
176  result.objects.push_back(g);
177  }
178  // Set support surfaces
179  result.support_surfaces = supports_;
180 
181  server_->setSucceeded(result, "Succeeded.");
182  }
183 
185 
186  bool debug_;
187 
189  std::string world_frame_;
190 
193  std::vector<grasping_msgs::Object> objects_;
194  std::vector<grasping_msgs::Object> supports_;
195 
199 
202 
204 
205  pcl::PassThrough<pcl::PointXYZRGB> range_filter_;
206 };
207 
208 } // namespace simple_grasping
209 
210 int main(int argc, char* argv[])
211 {
212  ros::init(argc, argv, "basic_grasping_perception");
213  ros::NodeHandle n("~");
214  simple_grasping::BasicGraspingPerception perception_n_planning(n);
215  ros::spin();
216  return 0;
217 }
ROS wrapper for shape grasp planner + object support segmentation.
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Class that segments a point cloud into objects and supporting surfaces.
pcl::PassThrough< pcl::PointXYZRGB > range_filter_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void executeCallback(const grasping_msgs::FindGraspableObjectsGoalConstPtr &goal)
void publish(const boost::shared_ptr< M > &message) const
std::vector< grasping_msgs::Object > objects_
int main(int argc, char *argv[])
#define ROS_INFO(...)
bool getParam(const std::string &key, std::string &s) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
boost::shared_ptr< ObjectSupportSegmentation > segmentation_
boost::shared_ptr< ShapeGraspPlanner > planner_
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const tf::Transform &transform)
actionlib::SimpleActionServer< grasping_msgs::FindGraspableObjectsAction > server_t
std::vector< grasping_msgs::Object > supports_
static Time now()
bool sleep() const
void cloudCallback(const pcl::PointCloud< pcl::PointXYZRGB >::ConstPtr &cloud)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)
A simple grasp planner that uses the bounding box shape to generate viable grasps.


simple_grasping
Author(s): Michael Ferguson
autogenerated on Wed Apr 26 2023 03:06:29