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
00035
00036
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
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
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
00064 PointCloud::Ptr cloud(new PointCloud);
00065 pcl::fromROSMsg(*input, *cloud);
00066 g_has_read = true;
00067
00068
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
00081
00082
00083
00084
00085
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
00090 g_handles = g_affordances.searchHandles(g_cloud, g_cylindrical_shells);
00091
00092
00093 g_prev_time = omp_get_wtime();
00094 }
00095
00096 int main(int argc, char** argv)
00097 {
00098
00099 const int PCD_FILE = 0;
00100 const int SENSOR = 1;
00101
00102
00103 srand (time(NULL));
00104
00105
00106 ros ::init(argc, argv, "localization");
00107 ros::NodeHandle node("~");
00108
00109
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
00115 node.param("update_interval", g_update_interval, 10.0);
00116
00117
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
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
00141
00142
00143
00144
00145
00146
00147
00148
00149 double start_time = omp_get_wtime();
00150 g_cylindrical_shells = g_sampling.searchAffordances(g_cloud, cloudrgb, g_affordances.getTargetRadius());
00151
00152
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
00158 g_has_read = true;
00159
00160
00161 printf("Affordance and handle search done in %.3f sec.\n", omp_get_wtime() - start_time);
00162 }
00163
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
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
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
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
00203 cloud_vis_rgb = g_affordances.workspaceFilter(cloudrgb);
00204
00205 ROS_INFO("update cloud");
00206
00207
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
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
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
00231 marker_array_pub.publish(marker_array_msg);
00232
00233
00234 for (std::size_t i = 0; i < handle_pubs.size(); i++)
00235 handle_pubs[i].publish(marker_arrays[i]);
00236
00237
00238 marker_array_pub_handles.publish(marker_array_msg_handles);
00239
00240
00241 cylinder_pub.publish(cylinder_list_msg);
00242
00243
00244 handles_pub.publish(handle_list_msg);
00245
00246 ros::spinOnce();
00247 rate.sleep();
00248 }
00249
00250 return 0;
00251 }