localization.cpp
Go to the documentation of this file.
00001 #include "handle_detector/affordances.h"
00002 #include "handle_detector/CylinderArrayMsg.h"
00003 #include "handle_detector/CylinderMsg.h"
00004 #include "handle_detector/HandleListMsg.h"
00005 #include <ctype.h>
00006 #include "handle_detector/cylindrical_shell.h"
00007 #include "Eigen/Dense"
00008 #include "Eigen/Core"
00009 #include <iostream>
00010 #include "handle_detector/messages.h"
00011 #include <pcl_conversions/pcl_conversions.h>
00012 #include <pcl/point_cloud.h>
00013 #include <pcl/point_types.h>
00014 #include <pcl/io/pcd_io.h>
00015 #include <pcl/visualization/cloud_viewer.h>
00016 #include <ros/ros.h>
00017 #include <sensor_msgs/PointCloud2.h>
00018 #include <sstream>
00019 #include <stdlib.h> 
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <tf/transform_datatypes.h>
00023 #include <tf/transform_listener.h>
00024 #include <vector>
00025 #include "handle_detector/visualizer.h"
00026 #define EIGEN_DONT_PARALLELIZE
00027 
00028 typedef pcl::PointCloud<pcl::PointXYZRGBA> PointCloud;
00029 
00030 const std::string RANGE_SENSOR_TOPIC = "/camera/depth_registered/points";
00031 
00032 // input and output ROS topic data
00033 std::string g_sensor_frame;
00034 PointCloud::Ptr g_cloud(new PointCloud);
00035 Affordances g_affordances;
00036 std::vector<CylindricalShell> g_cylindrical_shells;
00037 std::vector<std::vector<CylindricalShell> > g_handles;
00038 tf::StampedTransform g_transform;
00039 
00040 // synchronization
00041 double g_prev_time;
00042 double g_update_interval;
00043 bool g_has_read = false;
00044 
00045 
00046 void chatterCallback(const sensor_msgs::PointCloud2ConstPtr& input)
00047 {
00048   if (omp_get_wtime() - g_prev_time < g_update_interval)
00049     return;
00050 
00051   // convert ROS sensor message to PCL point cloud
00052   pcl::fromROSMsg(*input, *g_cloud);
00053   g_has_read = true;
00054   g_sensor_frame = input->header.frame_id;
00055 
00056   // search grasp affordances
00057   double start_time = omp_get_wtime();
00058   g_cylindrical_shells = g_affordances.searchAffordances(g_cloud, &g_transform);
00059   if (g_cylindrical_shells.size() == 0)
00060   {
00061     printf("No handles found!\n");
00062     g_prev_time = omp_get_wtime();
00063     return;
00064   }
00065 
00066   // search handles
00067   g_handles = g_affordances.searchHandles(g_cloud, g_cylindrical_shells);
00068 
00069   // measure runtime
00070   printf("Affordance and handle search done in %.3f sec.\n", omp_get_wtime() - start_time);
00071 
00072   // store current time
00073   g_prev_time = omp_get_wtime();
00074 }
00075 
00076 
00077 int main(int argc, char** argv)
00078 {
00079   // constants
00080   const int PCD_FILE = 0;
00081   const int SENSOR = 1;
00082 
00083   // initialize random seed
00084   srand (time(NULL));
00085 
00086   // initialize ROS
00087   ros::init(argc, argv, "localization");
00088   ros::NodeHandle node("~");
00089 
00090   // set point cloud source from launch file
00091   std::string cloud_topic;
00092   node.param("cloud_topic", cloud_topic, RANGE_SENSOR_TOPIC);
00093 
00094   // set point cloud update interval from launch file
00095   node.param("update_interval", g_update_interval, 10.0);
00096 
00097   // read parameters
00098   g_affordances.initParams(node);
00099 
00100   ros::Subscriber sub;
00101 
00102   g_transform.setIdentity();
00103 
00104   pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_vis(new pcl::PointCloud<pcl::PointXYZRGB>);
00105 
00106   // point cloud read from file
00107   if (!g_affordances.getPCDFile().empty())
00108   {
00109     g_sensor_frame = "/map";
00110     std::string file = g_affordances.getPCDFile();
00111 
00112     // load point cloud from PCD file
00113     if (pcl::io::loadPCDFile<pcl::PointXYZRGBA>(file, *g_cloud) == -1)
00114     {
00115       std::cerr << "Couldn't read pcd file" << std::endl;
00116       return (-1);
00117     }
00118     printf("Loaded *.pcd-file: %s\n", file.c_str());
00119 
00120     pcl::copyPointCloud(*g_cloud, *cloud_vis);
00121 
00122     //~ // search grasp affordances using indices
00123     //~ g_cylindrical_shells = g_affordances.searchAffordances(g_cloud);
00124 
00125     // search grasp affordances using samples
00126     double start_time = omp_get_wtime();
00127     std::vector<int> indices = g_affordances.createRandomIndices(g_cloud, g_affordances.getNumSamples());
00128     g_cylindrical_shells = g_affordances.searchAffordances(g_cloud, indices);
00129 
00130     // search handles
00131     g_handles = g_affordances.searchHandles(g_cloud, g_cylindrical_shells);
00132 
00133     // set boolean variable so that visualization topics get updated
00134     g_has_read = true;
00135 
00136     // measure runtime
00137     printf("Affordance and handle search done in %.3f sec.\n", omp_get_wtime() - start_time);
00138   }
00139   // point cloud read from sensor
00140   else if (!cloud_topic.empty())
00141   {
00142     // create subscriber for camera topic
00143     printf("Reading point cloud data from sensor topic: %s\n", cloud_topic.c_str());
00144     sub = node.subscribe(cloud_topic, 10, chatterCallback);
00145   }
00146 
00147   // visualization of point cloud, grasp affordances, and handles
00148   Visualizer visualizer(g_update_interval);
00149   sensor_msgs::PointCloud2 pc2msg;
00150   ros::Publisher marker_array_pub = node.advertise<visualization_msgs::MarkerArray>("visualization_all_affordances",
00151                                                                                     1);
00152   ros::Publisher marker_array_pub_handles = node.advertise<visualization_msgs::MarkerArray>("visualization_all_handles",
00153                                                                                             1);
00154   ros::Publisher marker_array_pub_handle_numbers = node.advertise<visualization_msgs::MarkerArray>(
00155       "visualization_handle_numbers", 1);
00156   std::vector<visualization_msgs::MarkerArray> marker_arrays;
00157   visualization_msgs::MarkerArray marker_array_msg;
00158   visualization_msgs::MarkerArray marker_array_msg_handles;
00159   visualization_msgs::MarkerArray marker_array_msg_handle_numbers;
00160 
00161   // publication of grasp affordances and handles as ROS topics
00162   Messages messages;
00163   ros::Publisher cylinder_pub = node.advertise<handle_detector::CylinderArrayMsg>("cylinder_list", 1);
00164   ros::Publisher handles_pub = node.advertise<handle_detector::HandleListMsg>("handle_list", 1);
00165   ros::Publisher pcl_pub = node.advertise<sensor_msgs::PointCloud2>("point_cloud", 1);
00166   std::vector<ros::Publisher> handle_pubs;
00167   handle_detector::CylinderArrayMsg cylinder_list_msg;
00168   handle_detector::HandleListMsg handle_list_msg;
00169 
00170   // how often things are published
00171   ros::Rate rate(10);
00172 
00173   double prev_time = omp_get_wtime();
00174 
00175   while (ros::ok())
00176   {
00177     if (g_has_read)
00178     {
00179       // create visual point cloud
00180 //      cloud_vis = g_affordances.workspaceFilter(g_cloud, &g_transform);
00181       ROS_INFO("update cloud");
00182 
00183       // create cylinder messages for visualization and ROS topic
00184       marker_array_msg = visualizer.createCylinders(g_cylindrical_shells, g_sensor_frame);
00185       cylinder_list_msg = messages.createCylinderArray(g_cylindrical_shells, g_sensor_frame);
00186       ROS_INFO("update visualization");
00187 
00188       // create handle messages for visualization and ROS topic
00189       handle_list_msg = messages.createHandleList(g_handles, g_sensor_frame);
00190       visualizer.createHandles(g_handles, g_sensor_frame, marker_arrays, marker_array_msg_handles);
00191       handle_pubs.resize(g_handles.size());
00192       for (std::size_t i = 0; i < handle_pubs.size(); i++)
00193         handle_pubs[i] = node.advertise<visualization_msgs::MarkerArray>(
00194             "visualization_handle_" + boost::lexical_cast < std::string > (i), 10);
00195 
00196       marker_array_msg_handle_numbers = visualizer.createHandleNumbers(g_handles, g_sensor_frame);
00197 
00198       ROS_INFO("update messages");
00199 
00200       g_has_read = false;
00201     }
00202 
00203     // publish point cloud
00204     pcl::toROSMsg(*cloud_vis, pc2msg);
00205     pc2msg.header.stamp = ros::Time::now();
00206     pc2msg.header.frame_id = g_sensor_frame;
00207     pcl_pub.publish(pc2msg);
00208 
00209     // publish cylinders for visualization
00210     marker_array_pub.publish(marker_array_msg);
00211 
00212     // publish handles for visualization
00213     for (std::size_t i = 0; i < handle_pubs.size(); i++)
00214       handle_pubs[i].publish(marker_arrays[i]);
00215 
00216     // publish handles for visualization
00217     marker_array_pub_handles.publish(marker_array_msg_handles);
00218 
00219     // publish handle numbers for visualization
00220     marker_array_pub_handle_numbers.publish(marker_array_msg_handle_numbers);
00221 
00222     // publish cylinders as ROS topic
00223     cylinder_pub.publish(cylinder_list_msg);
00224 
00225     // publish handles as ROS topic
00226     handles_pub.publish(handle_list_msg);
00227 
00228     //~ ROS_INFO("published %i grasp affordances for grasping", (int) cylinder_list_msg.cylinders.size());
00229     //~ ROS_INFO("published %i handles for grasping", (int) handle_list_msg.handles.size());
00230     //~ for(int i=0; i < handle_list_msg.handles.size(); i++)
00231     //~ std::cout<<" - handle "<<i<<": "<<handle_list_msg.handles[i].cylinders.size()<<std::endl;
00232     //~ ROS_INFO("published %i cylinders for visualization", (int) marker_array_msg.markers.size());
00233     //~ ROS_INFO("published %i handles for visualization", (int) handle_pubs.size());
00234     //~ for(int i=0; i < marker_arrays.size(); i++)
00235     //~ std::cout<<" - visual handle "<<i<<": "<<marker_arrays[i].markers.size()<<std::endl;
00236 
00237     ros::spinOnce();
00238     rate.sleep();
00239   }
00240 
00241   return 0;
00242 }


handle_detector
Author(s):
autogenerated on Thu Jun 6 2019 17:36:23