forked from atenpas/gpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrasp_detection_node.cpp
449 lines (360 loc) · 14 KB
/
grasp_detection_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "../../../gpd/include/nodes/grasp_detection_node.h"
/** constants for input point cloud types */
const int GraspDetectionNode::POINT_CLOUD_2 = 0; ///< sensor_msgs/PointCloud2
const int GraspDetectionNode::CLOUD_INDEXED = 1; ///< cloud with indices
const int GraspDetectionNode::CLOUD_SAMPLES = 2; ///< cloud with (x,y,z) samples
GraspDetectionNode::GraspDetectionNode(ros::NodeHandle& node) : has_cloud_(false), has_normals_(false),
size_left_cloud_(0), has_samples_(true), frame_("")
{
cloud_camera_ = NULL;
// set camera viewpoint to default origin
std::vector<double> camera_position;
node.getParam("camera_position", camera_position);
view_point_ << camera_position[0], camera_position[1], camera_position[2];
// choose sampling method for grasp detection
node.param("use_importance_sampling", use_importance_sampling_, false);
if (use_importance_sampling_)
{
importance_sampling_ = new SequentialImportanceSampling(node);
}
grasp_detector_ = new GraspDetector(node);
// Read input cloud and sample ROS topics parameters.
int cloud_type;
node.param("cloud_type", cloud_type, POINT_CLOUD_2);
std::string cloud_topic;
node.param("cloud_topic", cloud_topic, std::string("/camera/depth_registered/points"));
std::string samples_topic;
node.param("samples_topic", samples_topic, std::string(""));
std::string rviz_topic;
node.param("rviz_topic", rviz_topic, std::string(""));
if (!rviz_topic.empty())
{
grasps_rviz_pub_ = node.advertise<visualization_msgs::MarkerArray>(rviz_topic, 1);
use_rviz_ = true;
}
else
{
use_rviz_ = false;
}
// subscribe to input point cloud ROS topic
if (cloud_type == POINT_CLOUD_2)
cloud_sub_ = node.subscribe(cloud_topic, 1, &GraspDetectionNode::cloud_callback, this);
else if (cloud_type == CLOUD_INDEXED)
cloud_sub_ = node.subscribe(cloud_topic, 1, &GraspDetectionNode::cloud_indexed_callback, this);
else if (cloud_type == CLOUD_SAMPLES)
{
cloud_sub_ = node.subscribe(cloud_topic, 1, &GraspDetectionNode::cloud_samples_callback, this);
// grasp_detector_->setUseIncomingSamples(true);
has_samples_ = false;
}
// subscribe to input samples ROS topic
if (!samples_topic.empty())
{
samples_sub_ = node.subscribe(samples_topic, 1, &GraspDetectionNode::samples_callback, this);
has_samples_ = false;
}
// uses ROS topics to publish grasp candidates, antipodal grasps, and grasps after clustering
grasps_pub_ = node.advertise<gpd::GraspConfigList>("clustered_grasps", 10);
node.getParam("workspace", workspace_);
}
void GraspDetectionNode::run()
{
ros::Rate rate(100);
ROS_INFO("Waiting for point cloud to arrive ...");
while (ros::ok())
{
if (has_cloud_)
{
// detect grasps in point cloud
std::vector<Grasp> grasps = detectGraspPosesInTopic();
// visualize grasps in rviz
if (use_rviz_)
{
grasps_rviz_pub_.publish(convertToVisualGraspMsg(grasps, 0.1, 0.06, 0.01, 0.02, frame_));
}
// reset the system
has_cloud_ = false;
has_samples_ = false;
has_normals_ = false;
ROS_INFO("Waiting for point cloud to arrive ...");
}
ros::spinOnce();
rate.sleep();
}
}
std::vector<Grasp> GraspDetectionNode::detectGraspPosesInTopic()
{
// detect grasp poses
std::vector<Grasp> grasps;
if (use_importance_sampling_)
{
cloud_camera_->filterWorkspace(workspace_);
cloud_camera_->voxelizeCloud(0.003);
cloud_camera_->calculateNormals(4);
grasps = importance_sampling_->detectGrasps(*cloud_camera_);
}
else
{
// preprocess the point cloud
grasp_detector_->preprocessPointCloud(*cloud_camera_);
// detect grasps in the point cloud
grasps = grasp_detector_->detectGrasps(*cloud_camera_);
}
// Publish the selected grasps.
gpd::GraspConfigList selected_grasps_msg = createGraspListMsg(grasps);
grasps_pub_.publish(selected_grasps_msg);
ROS_INFO_STREAM("Published " << selected_grasps_msg.grasps.size() << " highest-scoring grasps.");
return grasps;
}
std::vector<int> GraspDetectionNode::getSamplesInBall(const PointCloudRGBA::Ptr& cloud,
const pcl::PointXYZRGBA& centroid, float radius)
{
std::vector<int> indices;
std::vector<float> dists;
pcl::KdTreeFLANN<pcl::PointXYZRGBA> kdtree;
kdtree.setInputCloud(cloud);
kdtree.radiusSearch(centroid, radius, indices, dists);
return indices;
}
void GraspDetectionNode::cloud_callback(const sensor_msgs::PointCloud2& msg)
{
if (!has_cloud_)
{
delete cloud_camera_;
cloud_camera_ = NULL;
Eigen::Matrix3Xd view_points(3,1);
view_points.col(0) = view_point_;
if (msg.fields.size() == 6 && msg.fields[3].name == "normal_x" && msg.fields[4].name == "normal_y"
&& msg.fields[5].name == "normal_z")
{
PointCloudPointNormal::Ptr cloud(new PointCloudPointNormal);
pcl::fromROSMsg(msg, *cloud);
cloud_camera_ = new CloudCamera(cloud, 0, view_points);
cloud_camera_header_ = msg.header;
ROS_INFO_STREAM("Received cloud with " << cloud_camera_->getCloudProcessed()->size() << " points and normals.");
}
else
{
PointCloudRGBA::Ptr cloud(new PointCloudRGBA);
pcl::fromROSMsg(msg, *cloud);
cloud_camera_ = new CloudCamera(cloud, 0, view_points);
cloud_camera_header_ = msg.header;
ROS_INFO_STREAM("Received cloud with " << cloud_camera_->getCloudProcessed()->size() << " points.");
}
has_cloud_ = true;
frame_ = msg.header.frame_id;
}
}
void GraspDetectionNode::cloud_indexed_callback(const gpd::CloudIndexed& msg)
{
if (!has_cloud_)
{
initCloudCamera(msg.cloud_sources);
// Set the indices at which to sample grasp candidates.
std::vector<int> indices(msg.indices.size());
for (int i=0; i < indices.size(); i++)
{
indices[i] = msg.indices[i].data;
}
cloud_camera_->setSampleIndices(indices);
has_cloud_ = true;
frame_ = msg.cloud_sources.cloud.header.frame_id;
ROS_INFO_STREAM("Received cloud with " << cloud_camera_->getCloudProcessed()->size() << " points, and "
<< msg.indices.size() << " samples");
}
}
void GraspDetectionNode::cloud_samples_callback(const gpd::CloudSamples& msg)
{
if (!has_cloud_)
{
initCloudCamera(msg.cloud_sources);
// Set the samples at which to sample grasp candidates.
Eigen::Matrix3Xd samples(3, msg.samples.size());
for (int i=0; i < msg.samples.size(); i++)
{
samples.col(i) << msg.samples[i].x, msg.samples[i].y, msg.samples[i].z;
}
cloud_camera_->setSamples(samples);
has_cloud_ = true;
has_samples_ = true;
frame_ = msg.cloud_sources.cloud.header.frame_id;
ROS_INFO_STREAM("Received cloud with " << cloud_camera_->getCloudProcessed()->size() << " points, and "
<< cloud_camera_->getSamples().cols() << " samples");
}
}
void GraspDetectionNode::samples_callback(const gpd::SamplesMsg& msg)
{
if (!has_samples_)
{
Eigen::Matrix3Xd samples(3, msg.samples.size());
for (int i=0; i < msg.samples.size(); i++)
{
samples.col(i) << msg.samples[i].x, msg.samples[i].y, msg.samples[i].z;
}
cloud_camera_->setSamples(samples);
has_samples_ = true;
ROS_INFO_STREAM("Received grasp samples message with " << msg.samples.size() << " samples");
}
}
void GraspDetectionNode::initCloudCamera(const gpd::CloudSources& msg)
{
// clean up
delete cloud_camera_;
cloud_camera_ = NULL;
// Set view points.
Eigen::Matrix3Xd view_points(3, msg.view_points.size());
for (int i = 0; i < msg.view_points.size(); i++)
{
view_points.col(i) << msg.view_points[i].x, msg.view_points[i].y, msg.view_points[i].z;
}
// Set point cloud.
if (msg.cloud.fields.size() == 6 && msg.cloud.fields[3].name == "normal_x"
&& msg.cloud.fields[4].name == "normal_y" && msg.cloud.fields[5].name == "normal_z")
{
PointCloudPointNormal::Ptr cloud(new PointCloudPointNormal);
pcl::fromROSMsg(msg.cloud, *cloud);
// TODO: multiple cameras can see the same point
Eigen::MatrixXi camera_source = Eigen::MatrixXi::Zero(view_points.cols(), cloud->size());
for (int i = 0; i < msg.camera_source.size(); i++)
{
camera_source(msg.camera_source[i].data, i) = 1;
}
cloud_camera_ = new CloudCamera(cloud, camera_source, view_points);
}
else
{
PointCloudRGBA::Ptr cloud(new PointCloudRGBA);
pcl::fromROSMsg(msg.cloud, *cloud);
// TODO: multiple cameras can see the same point
Eigen::MatrixXi camera_source = Eigen::MatrixXi::Zero(view_points.cols(), cloud->size());
for (int i = 0; i < msg.camera_source.size(); i++)
{
camera_source(msg.camera_source[i].data, i) = 1;
}
cloud_camera_ = new CloudCamera(cloud, camera_source, view_points);
std::cout << "view_points:\n" << view_points << "\n";
}
}
gpd::GraspConfigList GraspDetectionNode::createGraspListMsg(const std::vector<Grasp>& hands)
{
gpd::GraspConfigList msg;
for (int i = 0; i < hands.size(); i++)
msg.grasps.push_back(convertToGraspMsg(hands[i]));
msg.header = cloud_camera_header_;
return msg;
}
gpd::GraspConfig GraspDetectionNode::convertToGraspMsg(const Grasp& hand)
{
gpd::GraspConfig msg;
tf::pointEigenToMsg(hand.getGraspBottom(), msg.bottom);
tf::pointEigenToMsg(hand.getGraspTop(), msg.top);
tf::pointEigenToMsg(hand.getGraspSurface(), msg.surface);
tf::vectorEigenToMsg(hand.getApproach(), msg.approach);
tf::vectorEigenToMsg(hand.getBinormal(), msg.binormal);
tf::vectorEigenToMsg(hand.getAxis(), msg.axis);
msg.width.data = hand.getGraspWidth();
msg.score.data = hand.getScore();
tf::pointEigenToMsg(hand.getSample(), msg.sample);
return msg;
}
visualization_msgs::MarkerArray GraspDetectionNode::convertToVisualGraspMsg(const std::vector<Grasp>& hands,
double outer_diameter, double hand_depth, double finger_width, double hand_height, const std::string& frame_id)
{
double width = outer_diameter;
double hw = 0.5 * width;
visualization_msgs::MarkerArray marker_array;
visualization_msgs::Marker left_finger, right_finger, base, approach;
Eigen::Vector3d left_bottom, right_bottom, left_top, right_top, left_center, right_center, approach_center;
for (int i = 0; i < hands.size(); i++)
{
left_bottom = hands[i].getGraspBottom() + hw * hands[i].getBinormal();
right_bottom = hands[i].getGraspBottom() - hw * hands[i].getBinormal();
left_top = left_bottom + hand_depth * hands[i].getApproach();
right_top = right_bottom + hand_depth * hands[i].getApproach();
left_center = left_bottom + 0.5 * (left_top - left_bottom) - 0.5 * finger_width * hands[i].getFrame().col(1);
right_center = right_bottom + 0.5 * (right_top - right_bottom) + 0.5 * finger_width * hands[i].getFrame().col(1);
approach_center = left_bottom + 0.5 * (right_bottom - left_bottom) - 0.04 * hands[i].getFrame().col(0);
base = createHandBaseMarker(left_bottom, right_bottom, hands[i].getFrame(), 0.02, hand_height, i, frame_id);
left_finger = createFingerMarker(left_center, hands[i].getFrame(), hand_depth, finger_width, hand_height, i*3, frame_id);
right_finger = createFingerMarker(right_center, hands[i].getFrame(), hand_depth, finger_width, hand_height, i*3+1, frame_id);
approach = createFingerMarker(approach_center, hands[i].getFrame(), 0.08, finger_width, hand_height, i*3+2, frame_id);
marker_array.markers.push_back(left_finger);
marker_array.markers.push_back(right_finger);
marker_array.markers.push_back(approach);
marker_array.markers.push_back(base);
}
return marker_array;
}
visualization_msgs::Marker GraspDetectionNode::createFingerMarker(const Eigen::Vector3d& center,
const Eigen::Matrix3d& frame, double length, double width, double height, int id, const std::string& frame_id)
{
visualization_msgs::Marker marker;
marker.header.frame_id = frame_id;
marker.header.stamp = ros::Time();
marker.ns = "finger";
marker.id = id;
marker.type = visualization_msgs::Marker::CUBE;
marker.action = visualization_msgs::Marker::ADD;
marker.pose.position.x = center(0);
marker.pose.position.y = center(1);
marker.pose.position.z = center(2);
marker.lifetime = ros::Duration(10);
// use orientation of hand frame
Eigen::Quaterniond quat(frame);
marker.pose.orientation.x = quat.x();
marker.pose.orientation.y = quat.y();
marker.pose.orientation.z = quat.z();
marker.pose.orientation.w = quat.w();
// these scales are relative to the hand frame (unit: meters)
marker.scale.x = length; // forward direction
marker.scale.y = width; // hand closing direction
marker.scale.z = height; // hand vertical direction
marker.color.a = 0.5;
marker.color.r = 0.0;
marker.color.g = 0.0;
marker.color.b = 0.5;
return marker;
}
visualization_msgs::Marker GraspDetectionNode::createHandBaseMarker(const Eigen::Vector3d& start,
const Eigen::Vector3d& end, const Eigen::Matrix3d& frame, double length, double height, int id,
const std::string& frame_id)
{
Eigen::Vector3d center = start + 0.5 * (end - start);
visualization_msgs::Marker marker;
marker.header.frame_id = frame_id;
marker.header.stamp = ros::Time();
marker.ns = "hand_base";
marker.id = id;
marker.type = visualization_msgs::Marker::CUBE;
marker.action = visualization_msgs::Marker::ADD;
marker.pose.position.x = center(0);
marker.pose.position.y = center(1);
marker.pose.position.z = center(2);
marker.lifetime = ros::Duration(10);
// use orientation of hand frame
Eigen::Quaterniond quat(frame);
marker.pose.orientation.x = quat.x();
marker.pose.orientation.y = quat.y();
marker.pose.orientation.z = quat.z();
marker.pose.orientation.w = quat.w();
// these scales are relative to the hand frame (unit: meters)
marker.scale.x = length; // forward direction
marker.scale.y = (end - start).norm(); // hand closing direction
marker.scale.z = height; // hand vertical direction
marker.color.a = 0.5;
marker.color.r = 0.0;
marker.color.g = 0.0;
marker.color.b = 1.0;
return marker;
}
int main(int argc, char** argv)
{
// seed the random number generator
std::srand(std::time(0));
// initialize ROS
ros::init(argc, argv, "detect_grasps");
ros::NodeHandle node("~");
GraspDetectionNode grasp_detection(node);
grasp_detection.run();
return 0;
}