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


handle_detector
Author(s):
autogenerated on Fri Aug 28 2015 10:59:15