-
Notifications
You must be signed in to change notification settings - Fork 69
/
grasp_detection_server.cpp
141 lines (110 loc) · 4.51 KB
/
grasp_detection_server.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
#include <gpd_ros/grasp_detection_server.h>
GraspDetectionServer::GraspDetectionServer(ros::NodeHandle& node)
{
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];
std::string cfg_file;
node.param("config_file", cfg_file, std::string(""));
grasp_detector_ = new gpd::GraspDetector(cfg_file);
std::string rviz_topic;
node.param("rviz_topic", rviz_topic, std::string(""));
if (!rviz_topic.empty())
{
rviz_plotter_ = new GraspPlotter(node, grasp_detector_->getHandSearchParameters().hand_geometry_);
use_rviz_ = true;
}
else
{
use_rviz_ = false;
}
// Advertise ROS topic for detected grasps.
grasps_pub_ = node.advertise<gpd_ros::GraspConfigList>("clustered_grasps", 10);
node.getParam("workspace", workspace_);
}
bool GraspDetectionServer::detectGrasps(gpd_ros::detect_grasps::Request& req, gpd_ros::detect_grasps::Response& res)
{
ROS_INFO("Received service request ...");
// 1. Initialize cloud camera.
cloud_camera_ = NULL;
const gpd_ros::CloudSources& cloud_sources = req.cloud_indexed.cloud_sources;
// Set view points.
Eigen::Matrix3Xd view_points(3, cloud_sources.view_points.size());
for (int i = 0; i < cloud_sources.view_points.size(); i++)
{
view_points.col(i) << cloud_sources.view_points[i].x, cloud_sources.view_points[i].y,
cloud_sources.view_points[i].z;
}
// Set point cloud.
if (cloud_sources.cloud.fields.size() == 6 && cloud_sources.cloud.fields[3].name == "normal_x"
&& cloud_sources.cloud.fields[4].name == "normal_y" && cloud_sources.cloud.fields[5].name == "normal_z")
{
PointCloudPointNormal::Ptr cloud(new PointCloudPointNormal);
pcl::fromROSMsg(cloud_sources.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 < cloud_sources.camera_source.size(); i++)
{
camera_source(cloud_sources.camera_source[i].data, i) = 1;
}
cloud_camera_ = new gpd::util::Cloud(cloud, camera_source, view_points);
}
else
{
PointCloudRGBA::Ptr cloud(new PointCloudRGBA);
pcl::fromROSMsg(cloud_sources.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 < cloud_sources.camera_source.size(); i++)
{
camera_source(cloud_sources.camera_source[i].data, i) = 1;
}
cloud_camera_ = new gpd::util::Cloud(cloud, camera_source, view_points);
std::cout << "view_points:\n" << view_points << "\n";
}
// Set the indices at which to sample grasp candidates.
std::vector<int> indices(req.cloud_indexed.indices.size());
for (int i=0; i < indices.size(); i++)
{
indices[i] = req.cloud_indexed.indices[i].data;
}
cloud_camera_->setSampleIndices(indices);
frame_ = req.cloud_indexed.cloud_sources.cloud.header.frame_id;
ROS_INFO_STREAM("Received cloud with " << cloud_camera_->getCloudProcessed()->size() << " points, and "
<< req.cloud_indexed.indices.size() << " samples");
// 2. Preprocess the point cloud.
grasp_detector_->preprocessPointCloud(*cloud_camera_);
// 3. Detect grasps in the point cloud.
std::vector<std::unique_ptr<gpd::candidate::Hand>> grasps = grasp_detector_->detectGrasps(*cloud_camera_);
if (grasps.size() > 0)
{
// Visualize the detected grasps in rviz.
if (use_rviz_)
{
rviz_plotter_->drawGrasps(grasps, frame_);
}
// Publish the detected grasps.
gpd_ros::GraspConfigList selected_grasps_msg = GraspMessages::createGraspListMsg(grasps, cloud_camera_header_);
res.grasp_configs = selected_grasps_msg;
ROS_INFO_STREAM("Detected " << selected_grasps_msg.grasps.size() << " highest-scoring grasps.");
return true;
}
ROS_WARN("No grasps detected!");
return false;
}
int main(int argc, char** argv)
{
// seed the random number generator
std::srand(std::time(0));
// initialize ROS
ros::init(argc, argv, "detect_grasps_server");
ros::NodeHandle node("~");
GraspDetectionServer grasp_detection_server(node);
ros::ServiceServer service = node.advertiseService("detect_grasps", &GraspDetectionServer::detectGrasps,
&grasp_detection_server);
ROS_INFO("Grasp detection service is waiting for a point cloud ...");
ros::spin();
return 0;
}