localization.cpp
Go to the documentation of this file.
2 #include "handle_detector/CylinderArrayMsg.h"
3 #include "handle_detector/CylinderMsg.h"
4 #include "handle_detector/HandleListMsg.h"
5 #include <ctype.h>
7 #include "Eigen/Dense"
8 #include "Eigen/Core"
9 #include <iostream>
12 #include <pcl/point_cloud.h>
13 #include <pcl/point_types.h>
14 #include <pcl/io/pcd_io.h>
15 #include <pcl/visualization/cloud_viewer.h>
16 #include <ros/ros.h>
17 #include <sensor_msgs/PointCloud2.h>
18 #include <sstream>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <tf/transform_datatypes.h>
23 #include <tf/transform_listener.h>
24 #include <vector>
26 #define EIGEN_DONT_PARALLELIZE
27 
28 typedef pcl::PointCloud<pcl::PointXYZRGBA> PointCloud;
29 
30 const std::string RANGE_SENSOR_TOPIC = "/camera/depth_registered/points";
31 
32 // input and output ROS topic data
33 std::string g_sensor_frame;
34 PointCloud::Ptr g_cloud(new PointCloud);
36 std::vector<CylindricalShell> g_cylindrical_shells;
37 std::vector<std::vector<CylindricalShell> > g_handles;
39 
40 // synchronization
41 double g_prev_time;
43 bool g_has_read = false;
44 
45 
46 void chatterCallback(const sensor_msgs::PointCloud2ConstPtr& input)
47 {
48  if (omp_get_wtime() - g_prev_time < g_update_interval)
49  return;
50 
51  // convert ROS sensor message to PCL point cloud
52  pcl::fromROSMsg(*input, *g_cloud);
53  g_has_read = true;
54  g_sensor_frame = input->header.frame_id;
55 
56  // search grasp affordances
57  double start_time = omp_get_wtime();
58  g_cylindrical_shells = g_affordances.searchAffordances(g_cloud, &g_transform);
59  if (g_cylindrical_shells.size() == 0)
60  {
61  printf("No handles found!\n");
62  g_prev_time = omp_get_wtime();
63  return;
64  }
65 
66  // search handles
68 
69  // measure runtime
70  printf("Affordance and handle search done in %.3f sec.\n", omp_get_wtime() - start_time);
71 
72  // store current time
73  g_prev_time = omp_get_wtime();
74 }
75 
76 
77 int main(int argc, char** argv)
78 {
79  // constants
80  const int PCD_FILE = 0;
81  const int SENSOR = 1;
82 
83  // initialize random seed
84  srand (time(NULL));
85 
86  // initialize ROS
87  ros::init(argc, argv, "localization");
88  ros::NodeHandle node("~");
89 
90  // set point cloud source from launch file
91  std::string cloud_topic;
92  node.param("cloud_topic", cloud_topic, RANGE_SENSOR_TOPIC);
93 
94  // set point cloud update interval from launch file
95  node.param("update_interval", g_update_interval, 10.0);
96 
97  // read parameters
98  g_affordances.initParams(node);
99 
100  ros::Subscriber sub;
101 
102  g_transform.setIdentity();
103 
104  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_vis(new pcl::PointCloud<pcl::PointXYZRGB>);
105 
106  // point cloud read from file
107  if (!g_affordances.getPCDFile().empty())
108  {
109  g_sensor_frame = "/map";
110  std::string file = g_affordances.getPCDFile();
111 
112  // load point cloud from PCD file
113  if (pcl::io::loadPCDFile<pcl::PointXYZRGBA>(file, *g_cloud) == -1)
114  {
115  std::cerr << "Couldn't read pcd file" << std::endl;
116  return (-1);
117  }
118  printf("Loaded *.pcd-file: %s\n", file.c_str());
119 
120  pcl::copyPointCloud(*g_cloud, *cloud_vis);
121 
122  //~ // search grasp affordances using indices
123  //~ g_cylindrical_shells = g_affordances.searchAffordances(g_cloud);
124 
125  // search grasp affordances using samples
126  double start_time = omp_get_wtime();
127  std::vector<int> indices = g_affordances.createRandomIndices(g_cloud, g_affordances.getNumSamples());
128  g_cylindrical_shells = g_affordances.searchAffordances(g_cloud, indices);
129 
130  // search handles
132 
133  // set boolean variable so that visualization topics get updated
134  g_has_read = true;
135 
136  // measure runtime
137  printf("Affordance and handle search done in %.3f sec.\n", omp_get_wtime() - start_time);
138  }
139  // point cloud read from sensor
140  else if (!cloud_topic.empty())
141  {
142  // create subscriber for camera topic
143  printf("Reading point cloud data from sensor topic: %s\n", cloud_topic.c_str());
144  sub = node.subscribe(cloud_topic, 10, chatterCallback);
145  }
146 
147  // visualization of point cloud, grasp affordances, and handles
148  Visualizer visualizer(g_update_interval);
149  sensor_msgs::PointCloud2 pc2msg;
150  ros::Publisher marker_array_pub = node.advertise<visualization_msgs::MarkerArray>("visualization_all_affordances",
151  1);
152  ros::Publisher marker_array_pub_handles = node.advertise<visualization_msgs::MarkerArray>("visualization_all_handles",
153  1);
154  ros::Publisher marker_array_pub_handle_numbers = node.advertise<visualization_msgs::MarkerArray>(
155  "visualization_handle_numbers", 1);
156  std::vector<visualization_msgs::MarkerArray> marker_arrays;
157  visualization_msgs::MarkerArray marker_array_msg;
158  visualization_msgs::MarkerArray marker_array_msg_handles;
159  visualization_msgs::MarkerArray marker_array_msg_handle_numbers;
160 
161  // publication of grasp affordances and handles as ROS topics
162  Messages messages;
163  ros::Publisher cylinder_pub = node.advertise<handle_detector::CylinderArrayMsg>("cylinder_list", 1);
164  ros::Publisher handles_pub = node.advertise<handle_detector::HandleListMsg>("handle_list", 1);
165  ros::Publisher pcl_pub = node.advertise<sensor_msgs::PointCloud2>("point_cloud", 1);
166  std::vector<ros::Publisher> handle_pubs;
167  handle_detector::CylinderArrayMsg cylinder_list_msg;
168  handle_detector::HandleListMsg handle_list_msg;
169 
170  // how often things are published
171  ros::Rate rate(10);
172 
173  double prev_time = omp_get_wtime();
174 
175  while (ros::ok())
176  {
177  if (g_has_read)
178  {
179  // create visual point cloud
180 // cloud_vis = g_affordances.workspaceFilter(g_cloud, &g_transform);
181  ROS_INFO("update cloud");
182 
183  // create cylinder messages for visualization and ROS topic
184  marker_array_msg = visualizer.createCylinders(g_cylindrical_shells, g_sensor_frame);
185  cylinder_list_msg = messages.createCylinderArray(g_cylindrical_shells, g_sensor_frame);
186  ROS_INFO("update visualization");
187 
188  // create handle messages for visualization and ROS topic
189  handle_list_msg = messages.createHandleList(g_handles, g_sensor_frame);
190  visualizer.createHandles(g_handles, g_sensor_frame, marker_arrays, marker_array_msg_handles);
191  handle_pubs.resize(g_handles.size());
192  for (std::size_t i = 0; i < handle_pubs.size(); i++)
193  handle_pubs[i] = node.advertise<visualization_msgs::MarkerArray>(
194  "visualization_handle_" + boost::lexical_cast < std::string > (i), 10);
195 
196  marker_array_msg_handle_numbers = visualizer.createHandleNumbers(g_handles, g_sensor_frame);
197 
198  ROS_INFO("update messages");
199 
200  g_has_read = false;
201  }
202 
203  // publish point cloud
204  pcl::toROSMsg(*cloud_vis, pc2msg);
205  pc2msg.header.stamp = ros::Time::now();
206  pc2msg.header.frame_id = g_sensor_frame;
207  pcl_pub.publish(pc2msg);
208 
209  // publish cylinders for visualization
210  marker_array_pub.publish(marker_array_msg);
211 
212  // publish handles for visualization
213  for (std::size_t i = 0; i < handle_pubs.size(); i++)
214  handle_pubs[i].publish(marker_arrays[i]);
215 
216  // publish handles for visualization
217  marker_array_pub_handles.publish(marker_array_msg_handles);
218 
219  // publish handle numbers for visualization
220  marker_array_pub_handle_numbers.publish(marker_array_msg_handle_numbers);
221 
222  // publish cylinders as ROS topic
223  cylinder_pub.publish(cylinder_list_msg);
224 
225  // publish handles as ROS topic
226  handles_pub.publish(handle_list_msg);
227 
228  //~ ROS_INFO("published %i grasp affordances for grasping", (int) cylinder_list_msg.cylinders.size());
229  //~ ROS_INFO("published %i handles for grasping", (int) handle_list_msg.handles.size());
230  //~ for(int i=0; i < handle_list_msg.handles.size(); i++)
231  //~ std::cout<<" - handle "<<i<<": "<<handle_list_msg.handles[i].cylinders.size()<<std::endl;
232  //~ ROS_INFO("published %i cylinders for visualization", (int) marker_array_msg.markers.size());
233  //~ ROS_INFO("published %i handles for visualization", (int) handle_pubs.size());
234  //~ for(int i=0; i < marker_arrays.size(); i++)
235  //~ std::cout<<" - visual handle "<<i<<": "<<marker_arrays[i].markers.size()<<std::endl;
236 
237  ros::spinOnce();
238  rate.sleep();
239  }
240 
241  return 0;
242 }
std::vector< std::vector< CylindricalShell > > g_handles
int getNumSamples()
Return the number of samples, i.e., the number of neighborhoods to be searched for.
Definition: affordances.h:146
tf::StampedTransform g_transform
std::vector< CylindricalShell > g_cylindrical_shells
void initParams(ros::NodeHandle node)
Read the parameters from a ROS launch file.
Definition: affordances.cpp:30
void publish(const boost::shared_ptr< M > &message) const
int main(int argc, char **argv)
std::string g_sensor_frame
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void chatterCallback(const sensor_msgs::PointCloud2ConstPtr &input)
MarkerArray createCylinders(const std::vector< CylindricalShell > &list, const std::string &frame)
Create a MarkerArray message from a list of cylindrical shells.
Definition: visualizer.cpp:9
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Affordances g_affordances
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
handle_detector::CylinderArrayMsg createCylinderArray(const std::vector< CylindricalShell > &list, std::string frame)
Create a CylinderArray message from a list of cylindrical shells.
Definition: messages.cpp:3
visualization_msgs::MarkerArray MarkerArray
Definition: visualizer.h:42
std::vector< CylindricalShell > searchAffordances(const PointCloud::Ptr &cloud, tf::StampedTransform *transform=NULL)
Search grasp affordances (cylindrical shells) in a given point cloud.
void setIdentity()
const std::string RANGE_SENSOR_TOPIC
MarkerArray createHandleNumbers(const std::vector< std::vector< CylindricalShell > > &handles, const std::string &frame)
Create a MarkerArray message from a list of cylindrical shells.
Definition: visualizer.cpp:68
bool g_has_read
std::vector< int > createRandomIndices(const PointCloud::Ptr &cloud, int size)
handle_detector::HandleListMsg createHandleList(const std::vector< std::vector< CylindricalShell > > &handles, std::string frame)
Create a HandleList message from a list of cylindrical shells.
Definition: messages.cpp:55
std::string getPCDFile()
Return the *.pcd file given by the corresponding parameter in the ROS launch file.
Definition: affordances.h:142
#define ROS_INFO(...)
pcl::PointCloud< pcl::PointXYZRGBA > PointCloud
Definition: affordances.h:53
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
std::vector< std::vector< CylindricalShell > > searchHandles(const PointCloud::Ptr &cloud, std::vector< CylindricalShell > shells)
Search handles in a set of cylindrical shells. If occlusion filtering is turned on (using the corresp...
Affordances localizes grasp affordances and handles in a point cloud. It also provides helper methods...
Definition: affordances.h:73
void createHandles(const std::vector< std::vector< CylindricalShell > > &handles, const std::string &frame, std::vector< MarkerArray > &marker_arrays, MarkerArray &all_handle_markers)
Create a list of MarkerArray messages and a MarkerArray from a list of handles. The former represents...
Definition: visualizer.cpp:134
double g_update_interval
bool sleep()
PointCloud::Ptr g_cloud(new PointCloud)
Messages creates custom ROS messages to publish the results of the localization. The messages that ca...
Definition: messages.h:50
void toROSMsg(const sensor_msgs::PointCloud2 &cloud, sensor_msgs::Image &image)
static Time now()
pcl::PointCloud< pcl::PointXYZRGBA > PointCloud
double g_prev_time
ROSCPP_DECL void spinOnce()
Visualizer creates ROS messages to visualize the results of the localization in RViz. The possible objects that can be visualized are: neighborhoods, cylindrical shells, and handles.
Definition: visualizer.h:49


handle_detector
Author(s):
autogenerated on Mon Jun 10 2019 13:29:00