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


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