def pcl_callback(ros_point_cloud_msg): # TODO: Convert ROS msg of type PointCloud2 to PCL data PointXYZRGB format pcl_data = pclh.ros_to_pcl(ros_point_cloud_msg) # TODO: Statistical filter stat_inlier, stat_outlier = flt.statistical_outlier_filter(pcl_data) # TODO: Voxel Grid Downsampling vox_filt = flt.voxel_downsampling(stat_inlier) # TODO: PassThrough Filter pass_filt = flt.pass_through_filter(vox_filt) # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers cloud_table, cloud_objects = flt.ransac(pass_filt) # TODO: Euclidean Clustering cluster_indices = flt.euclidean_clustering(cloud_objects) # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately cluster_cloud = flt.visualize_clusters(cloud_objects, cluster_indices) # TODO: Convert PCL data to ROS messages ros_table_cloud = pclh.pcl_to_ros(cloud_table) ros_objects_cloud = pclh.pcl_to_ros(cloud_objects) ros_cluster_cloud = pclh.pcl_to_ros(cluster_cloud) # TODO: Publish ROS messages pcl_table_pub.publish(ros_table_cloud) pcl_objects_pub.publish(ros_objects_cloud) pcl_cluster_pub.publish(ros_cluster_cloud)
def callback(input_ros_msg): cloud = pcl_helper.ros_to_pcl(input_ros_msg) #print("INPUT: ", cloud) cloud = do_passthrough(cloud, "x", 0.0, 20.0) cloud = do_passthrough(cloud, "y", -5.0, 5.0) _, _, cloud = do_ransac_plane_normal_segmentation(cloud, 0.14) #cloud = do_euclidian_clustering(cloud) cloud_new = pcl_helper.pcl_to_ros(cloud) pub.publish(cloud_new)
def pc2CB(self, msg): self.cb_start_time = rospy.Time.now().to_sec() print "--------------------------------------------" # Convert ROS PointCloud2 msg to PCL data self.cloud = pcl_helper.ros_to_pcl(msg) print "CB_start_time", self.cb_start_time print "CB_end_time", rospy.Time.now().to_sec()
def callback(input_ros_msg): cloud = pcl_helper.ros_to_pcl(input_ros_msg) # print("INPUT: ", cloud) LEAF_SIZE = 0.1 cloud = do_voxel_grid_downsampling(cloud, LEAF_SIZE) # print("OUTPUT: ", cloud) # print("") cloud_new = pcl_helper.pcl_to_ros(cloud) pub.publish(cloud_new)
def callback(input_ros_msg): cloud = pcl_helper.ros_to_pcl(input_ros_msg) print("INPUT: ", cloud, type(cloud)) cloud = pcl_helper.XYZRGB_to_XYZ(cloud) mean_k = 10 thresh = 0.001 cloud = do_statistical_outlier_filtering(cloud, mean_k, thresh) color = pcl_helper.random_color_gen() cloud = pcl_helper.XYZ_to_XYZRGB(cloud, color) cloud_new = pcl_helper.pcl_to_ros(cloud) print("OUTPUT: ", cloud, type(cloud)) pub.publish(cloud_new)
def calc_object_size(item): points_array = pclh.ros_to_pcl(item.cloud).to_array() centroid = np.mean(points_array, axis=0)[:3] max_dim = np.max(points_array, axis=0)[:3] min_dim = np.min(points_array, axis=0)[:3] object_size = max_dim - min_dim length, width, height = make_ros_pose(object_size[0], object_size[1], object_size[3]) print( "The dimensions of the detected item are: length=%f, width=%f, height=%f" % (length.width, height)) return length, width, height, centroid
def CB_msgPCL(msgPCL): """ ROS "/sensor_stick/point_cloud" subscription Callback handler Handle the PointCloud ROS msg received by the "/sensor_stick/point_cloud" This function is almost entirely unpacking/packing ROS messages and publishing. The the unpacked input pcl is processed by Process_rawPCL(pclpcRawIn) which returns the values that need to be packed and published :param msgPCL: :return: """ global g_callBackCount g_callBackCount += 1 if (g_callBackCount % g_callBackSkip != 0): return; print "\rCBCount= {:05d}".format(g_callBackCount), sys.stdout.flush() DebugDumpMsgPCL(msgPCL) # Extract pcl Raw from Ros msgPCL pclpcRawIn = pcl_helper.ros_to_pcl(msgPCL) #------- PROCESS RAW PCL------------------------- labelRecs, pclpcObjects, pclpcTable, pclpcClusters = Process_rawPCL(pclpcRawIn) detected_objects_labels = [] # For ros loginfo only detected_objects = [] # For publish - for PROJ3! for (labelText, labelPos, labelIndex) in labelRecs: detected_objects_labels.append(labelText) g_object_markers_pub.publish(marker_tools.make_label(labelText, labelPos, labelIndex )) # Add detected object to the list of detected objects. do = DetectedObject() do.label = labelText do.cloud = pclpcClusters detected_objects.append(do) rospy.loginfo('Detected {} objects: {}'.format(len(detected_objects_labels), detected_objects_labels)) # Package Processed pcls into Ros msgPCL msgPCLObjects = pcl_helper.pcl_to_ros(pclpcObjects) msgPCLTable = pcl_helper.pcl_to_ros(pclpcTable) msgPCLClusters = pcl_helper.pcl_to_ros(pclpcClusters) # Publish everything # This is the output you'll need to complete the upcoming project! g_detected_objects_pub.publish(detected_objects) # THIS IS THE CRUCIAL STEP FOR PROJ3 g_pcl_objects_pub.publish(msgPCLObjects) g_pcl_table_pub.publish(msgPCLTable) g_pcl_cluster_pub.publish(msgPCLClusters)
def Process_msgPCL(msgPCL): # Extract pcl Raw from Ros msgPCL pclpcRawIn = pcl_helper.ros_to_pcl(msgPCL) #------- PROCESS RAW PC------------------------- pclpcObjects, pclpcTable, pclpcClusters = ProcessPCL(pclpcRawIn) # Package Processed pcls into Ros msgPCL msgPCLObjects = pcl_helper.pcl_to_ros(pclpcObjects) msgPCLTable = pcl_helper.pcl_to_ros(pclpcTable) msgPCLClusters = pcl_helper.pcl_to_ros(pclpcClusters) return msgPCLObjects, msgPCLTable, msgPCLClusters
def pc2CB(self, msg): self.start_time = rospy.Time.now().to_sec() print "----------------------------------" outer_pts = inner_pts = remaining_pts = [] # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Passthrough Filter fil_cloud = filtering_helper.do_passthrough_filter( cloud, 'x', self.filter_xmin, self.filter_xmax) fil_cloud = filtering_helper.do_passthrough_filter( fil_cloud, 'y', self.filter_ymin, self.filter_ymax) # Statistical Outlier Filter fil = fil_cloud.make_statistical_outlier_filter() fil.set_mean_k(10) fil.set_std_dev_mul_thresh(0.02) filtered_cloud = fil.filter() # self.load_params_from_yaml(self.filepath) # Setting Outer-Region and Inner-Region self.outer_region, self.inner_region = self.getDetectionRegion( self.fsm_state, self.lifter_status) # Visualize Detection Region self.visualize_region(self.outer_region, self.inner_region) self.middle_time_1 = rospy.Time.now().to_sec() # Grouping raw cloud into 2 groups of points outer_pts, inner_pts = self.getPointsGroupedFromCloud( filtered_cloud, self.outer_region, self.inner_region) self.middle_time_2 = rospy.Time.now().to_sec() remaining_pts = list(set(outer_pts) - set(inner_pts)) print "outer_pts", len(outer_pts) print "inner_pts", len(inner_pts) print "remaining_pts", len(remaining_pts) if (len(remaining_pts) != 0): print "Obstacle detected and stopping AGV" self.stopping() print "start_time", self.start_time print "middle_time_1", self.middle_time_1 print "middle_time_2", self.middle_time_2 print "end_time", rospy.Time.now().to_sec()
def get_clusters(self, cloud): #print("point cloud size = ",len(cloud.data), " bytes") marker_array = MarkerArray() pcl_cloud = ros_to_pcl(cloud) #convert pointcloud_xyzrgb to pointcloud_xyz # since pointcloud_xyzrgb does not support make_EuclideanClusterExtraction xyz_pc = pcl.PointCloud() xyz_pc.from_list( [ x[:3] for x in pcl_cloud.to_list() ] ) tree = xyz_pc.make_kdtree() t1 = time.clock() ec = xyz_pc.make_EuclideanClusterExtraction() ec.set_ClusterTolerance (0.005) ec.set_MinClusterSize (200) ec.set_MaxClusterSize (5000) ec.set_SearchMethod (tree) cluster_indices = ec.Extract() t2 = time.clock() #print("ECE took ", t2-t1) #for all j clusters print("# clusters ", len(cluster_indices)) for j, indices in enumerate(cluster_indices): print("Cluster", j ,": # points ", len(indices)) sum_x = 0 sum_y = 0 sum_z = 0 #for all points in the cluster j for i, indice in enumerate(indices): sum_x = sum_x + xyz_pc[indice][0] sum_y = sum_y + xyz_pc[indice][1] sum_z = sum_z + xyz_pc[indice][2] x = sum_x/len(indices) y = sum_y/len(indices) z = sum_z/len(indices) label = "obj_" + str(j) marker_array.markers.append(get_marker(j, label, cloud.header.frame_id, x, y, z)) obj = pcl_cloud.extract(indices, negative=False) pc_msg = pcl_to_ros(obj, cloud.header) self.pub.publish(pc_msg) self.track(marker_array)
def callback(input_ros_msg): cloud = pcl_helper.ros_to_pcl(input_ros_msg) filter_axis = 'x' axis_min = 0.0 axis_max = 20.0 cloud = do_passthrough(cloud, filter_axis, axis_min, axis_max) filter_axis = 'y' axis_min = -5.0 axis_max = 5.0 cloud = do_passthrough(cloud, filter_axis, axis_min, axis_max) cloud_new = pcl_helper.pcl_to_ros(cloud) pub.publish(cloud_new)
def ground_filter(cloud): pcl_cloud = ros_to_pcl(cloud) t1 = time.clock() ''' seg = pcl_cloud.make_segmenter_normals(ksearch=5) seg.set_optimize_coefficients(True) seg.set_model_type(pcl.SACMODEL_NORMAL_PLANE) seg.set_normal_distance_weight(0.1) seg.set_method_type(pcl.SAC_RANSAC) seg.set_max_iterations(10) seg.set_distance_threshold(0.03) indices, model = seg.segment() cnt = 0 i = 0 ind = [] for p in pc2.read_points(cloud, skip_nans=True): d = distance_point_to_plane(p, model) #0.005 is a fudge factor to correct for any error in the model #to avoid having any parts of the ground to creep in if d > 0.005: cnt = cnt + 1 ind.append(i) i = i + 1 ''' seg = pcl_cloud.make_segmenter() seg.set_optimize_coefficients(True) seg.set_model_type(pcl.SACMODEL_PLANE) seg.set_method_type(pcl.SAC_RANSAC) seg.set_distance_threshold(0.0075) indices, model = seg.segment() t2 = time.clock() #print("RANSAC took ", t2-t1) #print("model:", model) print("org size", cloud.width * cloud.height, "ransac size", cloud.width * cloud.height - len(indices)) #return pcl_cloud.extract(ind, negative=False) return pcl_cloud.extract(indices, negative=True)
def callback(input_ros_msg): cloud = pcl_helper.ros_to_pcl(input_ros_msg) #cloud = pcl_helper.XYZRGB_to_XYZ(cloud) #cloud_denoised = do_statistical_outlier_filtering(cloud) #cloud_downsampled = do_voxel_grid_downsampling(cloud) cloud_roi_x = do_passthrough(cloud, 'x', 0.0, 20.0) cloud_roi_y = do_passthrough(cloud_roi_x, 'y', -5.0, 5.0) _, _, cloud_ransaced = do_ransac_plane_normal_segmentation( cloud_roi_y, 0.007) cloud_clustered = do_euclidian_clustering(cloud_ransaced) cloud_new = pcl_helper.pcl_to_ros(cloud_clustered) pub.publish(cloud_new)
def pc2CB(self, msg): # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Filter the Cloud according limitation on x-axis and y-axis fil_cloud = filtering_helper.do_passthrough_filter( cloud, 'x', -0.5, 0.0) fil_cloud = filtering_helper.do_passthrough_filter( fil_cloud, 'y', -0.5, 0.5) print fil_cloud.size # Visualize the filtered cloud outmsg = pcl_helper.pcl_to_ros(fil_cloud, "base_link") outmsg.header = msg.header self.pc2_pub.publish(outmsg) # Save the cloud if self.init: pcl.save(fil_cloud, "/home/samuel/charger_unit.pcd", None, False) self.init = False print "record finish"
def pc2CB(self, msg): self.start_time = rospy.Time.now().to_sec() print "--------------------------------------------" # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # self.load_params_from_yaml(self.filepath) print "fsm_state", self.fsm_state # Setting Detection-Region for stopping detection_region = self.getDetectionRegion(self.fsm_state, self.lifter_status, self.speed) # Finding Min & Max of detection region x_max, x_min, y_max, y_min = self.getMinMaxXYFromRegion( detection_region) # Passthrough Filter fil_cloud = filtering_helper.do_passthrough_filter( cloud, 'x', x_min, x_max) fil_cloud = filtering_helper.do_passthrough_filter( fil_cloud, 'y', y_min, y_max) # Voxel Grid Filter filtered_cloud = filtering_helper.do_voxel_grid_filter(fil_cloud, 0.01) self.middle_time_1 = rospy.Time.now().to_sec() # Use Euclidean clustering to filter out noise clusters = self.getClusters(filtered_cloud, 0.05, 10, 9000) #0.05, 10 # Setting Front-region for snapshot front_region = self.camera_region # Visualize Detection Region self.visualize_region(detection_region) self.middle_time_2 = rospy.Time.now().to_sec() # Grouping clusters into 2 groups of points detected_pts = self.getPointsInRegionFromClusters( clusters, filtered_cloud, detection_region) # front_pts = self.getPointsInRegionFromPoints(detected_pts, front_region) self.middle_time_3 = rospy.Time.now().to_sec() print "detected_pts", len(detected_pts) # If Obstacle detected, do following: if (len(detected_pts) != 0): self.stopping() print "--------------------------------------" print "| Obstacle detected and stopping AGV |" print "--------------------------------------" # Rosservice call the health_monitoring of obstacle blocking if (rospy.Time.now().to_sec() - self.obs_timer >= self.obs_timeout): print "#######################################################" print "########## calling health_monitoring ##########" print "#######################################################" self.obstacle_health_call() self.obs_timer = rospy.Time.now().to_sec() # Use for snapshot once per obstacle at front if (self.fsm_state != "SUSPEND"): self.error = 1 # Rosservice call to snapshot the front obstacle once # print "front_pts", len(front_pts) # if(len(front_pts)!=0 and self.fsm_state=="SUSPEND" and self.error==1): # if(len(detected_pts)!=0 and self.fsm_state=="SUSPEND" and self.error==1): if (len(detected_pts) != 0 and self.fsm_state == "SUSPEND" and self.fsm_state != "ESTOP" and self.error == 1): self.error = 0 print "######################################" print "########## snapshot ##########" print "######################################" self.snapshot_call() self.prev_detection_region = detection_region print "start_time", self.start_time print "middle_time_1", self.middle_time_1 print "middle_time_2", self.middle_time_2 print "middle_time_3", self.middle_time_3 print "end_time", rospy.Time.now().to_sec()
def callback(input_ros_msg): cloud = pcl_helper.ros_to_pcl(input_ros_msg) #color = pcl_helper.random_color_gen() #white_cloud = pcl_helper.XYZ_to_XYZRGB(cloud, color) cluster_cloud, cluster_indices = do_euclidian_clustering(cloud) pub.publish(cluster_cloud)
def pcl_callback(ros_pcl_msg): # Exercise-1 TODOs: # TODO: Load point cloud data # TODO: Voxel Grid Downsampling # TODO: PassThrough Filter # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers # TODO: Save the inliers in .pcd file # TODO: Run the inliers.pcd file on terminal # Exercise-2 TODOs: # TODO: Convert ROS msg to PCL data # TODO: Voxel Grid Downsampling # TODO: PassThrough Filter # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers # TODO: Euclidean Clustering # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately # TODO: Convert PCL data to ROS messages # TODO: Publish ROS messages # Exercise-3 TODOs: # TODO: Classify the clusters! (loop through each detected cluster one at a time) # TODO: Grab the points for the cluster # TODO: Compute the associated feature vector # TODO: Make the prediction # TODO: Publish a label into RViz # TODO: Add the detected object to the list of detected objects. # TODO: Publish the list of detected objects # TODO: Convert ROS msg of type PointCloud2 to PCL data PointXYZRGB format pcl_data = pclh.ros_to_pcl(ros_pcl_msg) # TODO: Statistical filter stat_inlier, stat_outlier = flt.statistical_outlier_filter(pcl_data) pcl_data = stat_inlier # TODO: Voxel Grid Downsampling vox_filt = flt.voxel_downsampling(pcl_data) # TODO: PassThrough Filter pass_filt = flt.pass_through_filter(vox_filt) # TODO: RANSAC Plane Segmentation # TODO: Extract inliers (objects) and outliers (table) cloud_table, cloud_objects = flt.ransac(pass_filt) # TODO: Euclidean Clustering cluster_indices = flt.euclidean_clustering(cloud_objects) # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately cluster_cloud = flt.visualize_clusters(cloud_objects, cluster_indices) # TODO: Convert PCL data to ROS messages ros_table_cloud = pclh.pcl_to_ros(cloud_table) ros_objects_cloud = pclh.pcl_to_ros(cloud_objects) ros_cluster_cloud = pclh.pcl_to_ros(cluster_cloud) # TODO: Publish ROS messages pcl_table_pub.publish(ros_table_cloud) pcl_objects_pub.publish(ros_objects_cloud) pcl_cluster_pub.publish(ros_cluster_cloud) # TODO: Classify the clusters! detected_objects_labels = [] detected_objects = [] labeled_features = [] for index, pts_list in enumerate(cluster_indices): # Grab the points for the cluster from the extracted outliers (cloud_objects) pcl_cluster = cloud_objects.extract(pts_list) # TODO: convert the cluster from pcl to ROS using helper function ros_cluster = pclh.pcl_to_ros(pcl_cluster) # Extract histogram features # TODO: complete this step just as is covered in capture_features.py # Extract histogram features hists_color = compute_color_histograms(ros_cluster, using_hsv=True) normals = get_normals(ros_cluster) hists_normals = compute_normal_histograms(normals) feature = np.concatenate((hists_color, hists_normals)) #labeled_features.append([feature, model_name]) # Make the prediction, retrieve the label for the result # and add it to detected_objects_labels list prediction = clf.predict(scaler.transform(feature.reshape(1,-1))) label = encoder.inverse_transform(prediction)[0] detected_objects_labels.append(label) # Publish a label into RViz white_cloud = pclh.XYZRGB_to_XYZ(cloud_objects) label_pos = list(white_cloud[pts_list[0]]) label_pos[2] += .4 object_markers_pub.publish(make_label(label,label_pos, index)) # Add the detected object to the list of detected objects. do = DetectedObject() do.label = label do.cloud = ros_cluster detected_objects.append(do) rospy.loginfo('Detected {} objects: {}'.format(len(detected_objects_labels), detected_objects_labels)) # Publish the list of detected objects # This is the output you'll need to complete the upcoming project! detected_objects_pub.publish(detected_objects)
def pc2CB(self, msg): if self.init: # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Filter the Cloud fil_cloud = filtering_helper.do_passthrough_filter( cloud, 'x', self.x_min, self.x_max) filtered_cloud = filtering_helper.do_passthrough_filter( fil_cloud, 'y', self.y_min, self.y_max) # Turn cloud into colorless cloud colorless_cloud = pcl_helper.XYZRGB_to_XYZ(filtered_cloud) # Get groups of cluster of points # clusters = self.getClusters(colorless_cloud, 0.05, 20, 300) # Table on Top AGV - 60~300 clusters = self.getClusters(colorless_cloud, 0.05, 20, 350) print "len(clusters)", len(clusters) for i in range(0, len(clusters)): print "len(clusters[", i, "]", len(clusters[i]) # Get mean points from clusters mean_pts = [] for cluster in clusters: mean_pts.append(self.getMeanPoint(cluster, filtered_cloud)) print "len(mean_pts)", len(mean_pts) for i in range(0, len(mean_pts)): print "mean_pts[", i, "]", mean_pts[i] # Remove other points, leave the table points only table_pts = [] table_pts = self.getTablePoints(mean_pts) print "len(table_pts)", len(table_pts) print table_pts for i in range(len(table_pts)): self.vizPoint(i, table_pts[i], ColorRGBA(1, 1, 1, 1), "base_link") # Get transform of "base_link" relative to "map" trans = self.getTransform("map", "base_link") # Finding 2 middle points from 4 table legs and use them as path_pts path_pts = [] if (len(table_pts) == 4): for p1 in table_pts: for p2 in table_pts: if (p1 == p2): break # Register 2 middle_pts of 4 table legs if (0.73 < self.getDistance(p1, p2) < 0.83): path_pts.append(self.getMiddlePoint(p1, p2)) break print "len(path_pts)", len(path_pts) print path_pts # Turn path_pts into map frame for i in range(len(path_pts)): path_pts[i] = tf2_geometry_msgs.do_transform_pose( self.toPoseStamped(path_pts[i], Quaternion(0, 0, 0, 1)), trans).pose.position path_pts[i] = Point(path_pts[i].x, path_pts[i].y, 0) # Record the starting point and heading once if self.record_once: self.start_pt = trans.transform.translation self.record_once = False # Create a list with distance information between initial_pt and path_pts distance_list = [ self.getDistance(p, self.start_pt) for p in path_pts ] # Rearrange the path_pts to be in descending order path_pts = self.getAscend(path_pts, distance_list) # Duplicate the path_pts to prevent from being edited table_pathpts = path_pts[:] print "table_pathpts: ", len(table_pathpts) if (len(table_pathpts) == 2): # Get gradient of the Line of Best Fit m1 = self.getGradientLineOfBestFit(table_pathpts) # Insert another point on the line with d[m] away from the 2nd table_pathpts path_pts.append( self.getPointOnLine(m1, 1.0, table_pathpts[1], self.start_pt)) # Visualize the path_pts self.vizPoint(0, path_pts[0], ColorRGBA(1, 0, 0, 1), "map") # Red self.vizPoint(1, path_pts[1], ColorRGBA(1, 1, 0, 1), "map") # Yellow self.vizPoint(2, path_pts[2], ColorRGBA(1, 0, 1, 1), "map") # Magenta print len(path_pts) # Find the heading of the table (last 2 path_pts) heading_q = self.getOrientation(table_pathpts[0], table_pathpts[1]) # Publish the path self.route_pub.publish(self.getPath2Table(path_pts, heading_q)) print "-----------------"
def pc2CB(self, msg): if self.init: print "start_time", rospy.Time.now().to_sec() # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Filter the Cloud fil_cloud = filtering_helper.do_passthrough_filter(cloud, 'x', self.x_min, self.x_max) filtered_cloud = filtering_helper.do_passthrough_filter(fil_cloud, 'y', self.y_min, self.y_max) # Turn cloud into colorless cloud # colorless_cloud = pcl_helper.XYZRGB_to_XYZ(filtered_cloud) colorless_cloud = pcl_helper.XYZI_to_XYZ(filtered_cloud) # Get groups of cluster of points # clusters = self.getClusters(colorless_cloud, 0.05, 20, 250) # Table on Top AGV - 60~230 clusters = self.getClusters(colorless_cloud, 0.05, 20, 350) # print "len(clusters)", len(clusters) # for i in range(0,len(clusters)): # print "len(clusters[", i, "]", len(clusters[i]) # Get mean points from clusters mean_pts = [] for cluster in clusters: mean_pts.append(self.getMeanPoint(cluster, filtered_cloud)) # print "len(mean_pts)", len(mean_pts) # for i in range(0,len(mean_pts)): # print "mean_pts[", i, "]", mean_pts[i] # Remove other points, leave the table points only table_pts = [] table_pts = self.getTablePoints(mean_pts) # print "len(table_pts)", len(table_pts) # print table_pts for i in range(len(table_pts)): self.vizPoint(i, table_pts[i], ColorRGBA(1,1,1,1), "base_link") # Get transform of "base_link" relative to "map" trans = self.getTransform("map", "base_link") # Finding 2 middle points from 4 table legs and use them as path_pts path_pts = [] if(len(table_pts)==4): for p1 in table_pts: for p2 in table_pts: if(p1==p2): break # Register 2 middle_pts of 4 table legs if(0.73 < self.getDistance(p1, p2) < 0.83): path_pts.append(self.getMiddlePoint(p1, p2)) break # print "len(path_pts)", len(path_pts) # print path_pts # Path_pts that generated from between 2 legs of table must be fulfill below requirement, else return if(len(path_pts)==2): if not (0.33 < self.getDistance(path_pts[0], path_pts[1]) < 0.48): return # Turn path_pts into map frame for i in range(len(path_pts)): path_pts[i] = tf2_geometry_msgs.do_transform_pose(self.toPoseStamped(path_pts[i], Quaternion(0,0,0,1)), trans).pose.position # Record the starting point once if self.record_once: self.start_pt = trans.transform.translation self.record_once = False # Create a list with distance information between initial_pt and path_pts distance_list = [self.getDistance(p, self.start_pt) for p in path_pts] # Rearrange the path_pts to be in ascending order path_pts = self.getAscend(path_pts, distance_list) # Duplicate the path_pts to prevent from being edited table_pathpts = path_pts[:] if(len(table_pathpts)==2): # Get gradient of the Line of Best Fit m1 = self.getGradientLineOfBestFit(table_pathpts) # Insert another point on the line with d[m] away from the 1st table_pathpts path_pts.insert(0, self.getPointOnLine(m1, 1.0, table_pathpts[0], self.start_pt)) # Insert a point ahead d[m] of table to the begin of path_pts # ahead_pt = self.getPointAheadTable(table_pathpts, 1.0) # path_pts.insert(0, ahead_pt) # Insert a point ahead d[m] of table to the middle of table_pathpts # midway_pt = self.getPointAheadTable(table_pathpts, -0.4) # path_pts.insert(2, midway_pt) # Insert another point on the line with d[m] away from the 1st table_pathpts # path_pts.insert(2, self.getPointOnLine(m1, 0.05, table_pathpts[1], self.start_pt)) # Remove the last point of path_pts # path_pts = path_pts[:len(path_pts)-1] # Add the AGV start_pt to the begin of path_pts # path_pts.insert(0, self.start_pt) # Visualize the path_pts # self.vizPoint(0, path_pts[0], ColorRGBA(1,0,0,1), "map") # Red # self.vizPoint(1, path_pts[1], ColorRGBA(1,1,0,1), "map") # Yellow # self.vizPoint(2, path_pts[2], ColorRGBA(0,0,1,1), "map") # Blue # Find the heading of the table (last 2 path_pts) heading_q = self.getOrientation(table_pathpts[0], table_pathpts[1]) # Publish the path self.route_pub.publish(self.getPath2Table(path_pts, heading_q)) # print "-----------------" print "end_time", rospy.Time.now().to_sec()
def pc2CB(self, msg): if self.init: self.start_time = rospy.Time.now().to_sec() print "-----------------" # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Get transform of "base_link" relative to "map" trans = self.getTransform("map", "base_link") # Filter the Cloud fil_cloud = filtering_helper.do_passthrough_filter(cloud, 'x', -2.5, 2.5) fil_cloud = filtering_helper.do_passthrough_filter(fil_cloud, 'y', -1.5, 1.5) # Turn cloud into colorless cloud # colorless_cloud = pcl_helper.XYZRGB_to_XYZ(fil_cloud) # Statistical outlier filter # fil = colorless_cloud.make_statistical_outlier_filter() fil = fil_cloud.make_statistical_outlier_filter() fil.set_mean_k(30) fil.set_std_dev_mul_thresh(0.1) #1.0 / 0.1 filtered_cloud = fil.filter() # pc2_msg = pcl_helper.pcl_to_ros(filtered_cloud, "base_link") # self.pc2_pub.publish(pc2_msg) # Get groups of cluster of points clusters = self.getClusters(filtered_cloud, 0.05, 20, 1000) print "len(clusters)", len(clusters) for i in range(len(clusters)): print "clusters["+str(i)+"]", len(clusters[i]) # Not enough clusters to be identified as charger unit if(len(clusters)<3): return self.middle_time_1 = rospy.Time.now().to_sec() # Extract charger_wall & charger_pillars clusters # - use for compute gradient of Line of Best Fit # - use for compute middle_pt wall_cluster, pillar_clusters = self.getChargerClusters(clusters, filtered_cloud) self.middle_time_2 = rospy.Time.now().to_sec() # Cannot find the charger_wall and charger_pillars if(wall_cluster==None or pillar_clusters==[]): print "wall cluster or pillar clusters not found" return print "wall_cluster", len(wall_cluster) self.vizPoint(0, self.getMeanPointFromCluster(wall_cluster, filtered_cloud), ColorRGBA(0,1,0,1), "base_link") for i in range(len(pillar_clusters)): print "pillar_clusters["+str(i)+"]", len(pillar_clusters[i]) self.vizPoint(i+1, self.getMeanPointFromCluster(pillar_clusters[i], filtered_cloud), ColorRGBA(0,1,0,1), "base_link") # Get Points of wall_cluster wall_pts = self.getPointsFromCluster(wall_cluster, filtered_cloud) # Turn wall_pts into map frame for i in range(len(wall_cluster)): wall_pts[i] = tf2_geometry_msgs.do_transform_pose(self.toPoseStamped(wall_pts[i], Quaternion(0,0,0,1)), trans).pose.position # Get gradient of the Line of Best Fit m1 = self.getGradientLineOfBestFit(wall_pts) print "gradient", m1 pillar_pts = [] # Get mean points from pillar_clusters for cluster in pillar_clusters: pillar_pts.append(self.getMeanPointFromCluster(cluster, filtered_cloud)) # Finding 1 middle_pt from 2 charger_pillars and use it as path_pts print pillar_pts if(len(pillar_pts)==2): middle_pt = None for p1 in pillar_pts: for p2 in pillar_pts: if(p1==p2): break # Only register middle_pt of 2 charger_pillars points if(0.5 < self.getDistance(p1, p2) < 0.65): middle_pt = self.getMiddlePoint(p1, p2) break if(middle_pt==None): "middle_pt not found" return # Turn middle_pt into map frame middle_pt = tf2_geometry_msgs.do_transform_pose(self.toPoseStamped(middle_pt, Quaternion(0,0,0,1)), trans).pose.position # Register the middle_pt as one of the path_pts path_pts = [] path_pts.append(middle_pt) # Record the starting point and heading once if self.record_once: self.start_pt = trans.transform.translation self.record_once = False # Insert another point on the normal line with d[m] away from middle_pt path_pts.insert(0, self.getPointOnNormalLine(m1, 1.0, middle_pt, self.start_pt)) # Duplicate the path points to prevent from being edited charger_pathpts = path_pts[:] # Insert another point on the normal line with d[m] away from middle_pt path_pts.insert(0, self.getPointOnNormalLine(m1, 0.3, middle_pt, self.start_pt)) # Create a list with distance information between init_pt and path_pts distance_list = [self.getDistance(p, self.start_pt) for p in path_pts] # Rearrange the path_pts to be in ascending order path_pts = self.getAscend(path_pts, distance_list) # Add the AGV start_pt to the begin of path_pts # path_pts.insert(0, self.start_pt) # Remove last point of path_pts (middle pt of 2 charger feature pts) path_pts = path_pts[:len(path_pts)-1] print "len(path_pts)", len(path_pts) # # Visualize the path_pts self.vizPoint(0, path_pts[0], ColorRGBA(1,0,0,1), "map") # Red self.vizPoint(1, path_pts[1], ColorRGBA(1,1,0,1), "map") # Yellow # self.vizPoint(2, path_pts[2], ColorRGBA(0,0,1,1), "map") # Blue # Find the heading of the charger (last 2 path_pts) heading_q = self.getOrientation(charger_pathpts[0], charger_pathpts[1]) # Publish the path self.route_pub.publish(self.getPath2Charger(path_pts, heading_q)) print "start_time", self.start_time print "middle_time_1", self.middle_time_1 print "middle_time_2", self.middle_time_2 print "end_time", rospy.Time.now().to_sec()
def pc2CB(self, msg): if self.init: # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Get transform of "base_link" relative to "map" trans = self.getTransform("map", "base_link") # Filter the Cloud fil_cloud = filtering_helper.do_passthrough_filter( cloud, 'x', -3.0, 3.0) fil_cloud = filtering_helper.do_passthrough_filter( fil_cloud, 'y', -3.0, 3.0) # Turn cloud into colorless cloud colorless_cloud = pcl_helper.XYZRGB_to_XYZ(fil_cloud) # source_cloud = colorless_cloud # Get groups of cluster of points clusters = self.getClusters(colorless_cloud, 0.05, 100, 450) # print "len(clusters)", len(clusters) source_cloud = self.getCloudFromCluster(clusters[0], colorless_cloud) middle_pt = self.getMeanPoint(clusters[0], colorless_cloud) self.vizPoint(0, middle_pt, ColorRGBA(1, 1, 1, 1), "base_link") icp = source_cloud.make_IterativeClosestPoint() converged, transform, estimate, fitness = icp.icp( source_cloud, self.target_cloud, 100) # gicp = colorless_cloud.make_GeneralizedIterativeClosestPoint() # converged, transform, estimate, fitness = gicp.gicp(colorless_cloud, self.target_cloud, 100) # icp_nl = colorless_cloud.make_IterativeClosestPointNonLinear() # converged, transform, estimate, fitness = icp_nl.icp_nl(self.target_cloud, colorless_cloud, 100) # if not converged: print "converged", converged print "transform", transform print "estimate", estimate print "fitness", fitness # new_transform = transform.inverse() # print new_transform translation = Vector3(transform[0, 3], transform[1, 3], transform[2, 3]) q = tf.transformations.quaternion_from_matrix(transform) rotation = Quaternion(q[0], q[1], q[2], q[3]) roll, pitch, yaw = tf.transformations.euler_from_matrix(transform) print np.rad2deg(yaw) # print "translation\n", translation # print "rotation\n", rotation # transformation = self.toTransformStamped(translation, rotation, "map", "base_link") print "------------------------------------------------" # Visualize the cloud outmsg = pcl_helper.pcl_to_ros(estimate, "map") outmsg.header = msg.header self.pc2_pub.publish(outmsg)
def pcl_callback(ros_pcl_msg): # Exercise-1 TODOs: # TODO: Load point cloud data # TODO: Statistical Outlier Filtering # TODO: Voxel Grid Downsampling # TODO: PassThrough Filter # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers # TODO: Save the inliers in .pcd file # TODO: Run the inliers.pcd file on terminal # Exercise-2 TODOs: # TODO: Convert ROS msg to PCL data # TODO: Statistical Outlier Filtering # TODO: Voxel Grid Downsampling # TODO: PassThrough Filter # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers # TODO: Euclidean Clustering # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately # TODO: Convert PCL data to ROS messages # TODO: Publish ROS messages # Exercise-3 TODOs: # TODO: Classify the clusters! (loop through each detected cluster one at a time) # TODO: Grab the points for the cluster # TODO: Compute the associated feature vector # TODO: Make the prediction # TODO: Publish a label into RViz # TODO: Add the detected object to the list of detected objects. # TODO: Publish the list of detected objects # TODO: Convert ROS msg of type PointCloud2 to PCL data PointXYZRGB format pcl_data = pclh.ros_to_pcl(ros_pcl_msg) # TODO: Statistical filter stat_inlier, stat_outlier = flt.statistical_outlier_filter(pcl_data) # TODO: Voxel Grid Downsampling vox_filt = flt.voxel_downsampling(stat_inlier) # TODO: PassThrough Filter pass_filt_z = flt.pass_through_filter(vox_filt, 0.6, 1.2, 'z') pass_filt_zy = flt.pass_through_filter(pass_filt_z, -0.5, 0.5, 'y') pass_filt_zyx = flt.pass_through_filter(pass_filt_zy, 0.4, 1.2, 'x') # TODO: RANSAC Plane Segmentation # TODO: Extract inliers (objects) and outliers (table) cloud_table, cloud_objects = flt.ransac(pass_filt_zyx) # TODO: Euclidean Clustering cluster_indices = flt.euclidean_clustering(cloud_objects) # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately cluster_cloud = flt.visualize_clusters(cloud_objects, cluster_indices) # TODO: Convert PCL data to ROS messages ros_table_cloud = pclh.pcl_to_ros(cloud_table) ros_objects_cloud = pclh.pcl_to_ros(cloud_objects) ros_cluster_cloud = pclh.pcl_to_ros(cluster_cloud) # TODO: Publish ROS messages pcl_table_pub.publish(ros_table_cloud) pcl_objects_pub.publish(ros_objects_cloud) pcl_cluster_pub.publish(ros_cluster_cloud) # TODO: Classify the clusters and create list of detected objects detected_objects, detected_objects_labels = detect_objects( cloud_objects, cluster_indices) # Suggested location for where to invoke your pr2_mover() function within pcl_callback() # Could add some logic to determine whether or not your object detections are robust # before calling pr2_mover() detected_objects_pub.publish(detected_objects) try: pr2_mover(detected_objects) except rospy.ROSInterruptException: pass
def pc2CB(self, msg): if self.init: # Convert ROS PointCloud2 msg to PCL data cloud = pcl_helper.ros_to_pcl(msg) # Filter the Cloud fil_cloud = filtering_helper.do_passthrough_filter(cloud, 'x', -2.5, 2.5) filtered_cloud = filtering_helper.do_passthrough_filter(fil_cloud, 'y', -2.5, 2.5) # Turn cloud into colorless cloud colorless_cloud = pcl_helper.XYZRGB_to_XYZ(filtered_cloud) # Get groups of cluster of points clusters = self.getClusters(colorless_cloud, 0.05, 10, 1000) print "len(clusters)", len(clusters) # Extract charger wall cluster - use for compute Line of Best Fit if(len(clusters) != 0): size_clusters = [] for cluster in clusters: size_clusters.append(len(cluster)) # Find the biggest cluster max_id = size_clusters.index(max(size_clusters)) wall_cluster = clusters.pop(max_id) print "len(wall_cluster)", len(wall_cluster) feature_clusters = clusters print "len(feature_clusters)", len(feature_clusters) # Get transform of "base_link" relative to "map" trans = self.getTransform("map", "base_link") # Record the starting point and heading once if self.record_once: self.start_pt = trans.transform.translation self.record_once = False # Get Points of wall cluster wall_pts = self.getPointsFromCluster(wall_cluster, filtered_cloud) # Turn wall_pts into map frame for i in range(len(wall_cluster)): wall_pts[i] = tf2_geometry_msgs.do_transform_pose(self.toPoseStamped(wall_pts[i], Quaternion(0,0,0,1)), trans).pose.position # Get gradient of the Line of Best Fit m1 = self.getGradientLineOfBestFit(wall_pts) print m1 # Get mean points from feature clusters mean_pts = [] for cluster in feature_clusters: mean_pts.append(self.getMeanPointFromCluster(cluster, filtered_cloud)) print "len(mean_pts)", len(mean_pts) for i in range(len(mean_pts)): self.vizPoint(i, mean_pts[i], ColorRGBA(1,1,1,1), "base_link") # Finding 1 middle point from 2 charger feature columns and use it as path_pts if(len(mean_pts)>=2): for p1 in mean_pts: for p2 in mean_pts: if(p1==p2): break # Only register middle_pt of 2 charger feature points if(0.5 < self.getDistance(p1, p2) < 0.65): middle_pt = self.getMiddlePoint(p1, p2) # Turn middle_pt into map frame middle_pt = tf2_geometry_msgs.do_transform_pose(self.toPoseStamped(middle_pt, Quaternion(0,0,0,1)), trans).pose.position # Register the middle_pt as one of the path_pts path_pts = [] path_pts.append(middle_pt) # Insert another point on the normal line with d[m] away from middle_pt path_pts.insert(0, self.getPointOnNormalLine(m1, 1.0, middle_pt, self.start_pt)) # Duplicate the path points to prevent from being edited feature_pathpts = path_pts[:] # Insert another point on the normal line with d[m] away from middle_pt path_pts.insert(1, self.getPointOnNormalLine(m1, 0.3, middle_pt, self.start_pt)) # Create a list with distance information between init_pt and path_pts distance_list = [self.getDistance(p, self.start_pt) for p in path_pts] # Rearrange the path_pts to be in ascending order path_pts = self.getAscend(path_pts, distance_list) print "len(path_pts)", len(path_pts) # Add the AGV start_pt to the begin of path_pts # path_pts.insert(0, self.start_pt) # Remove last point of path_pts (middle pt of 2 charger feature pts) path_pts = path_pts[:len(path_pts)-1] # Visualize the path_pts self.vizPoint(0, path_pts[0], ColorRGBA(1,0,0,1), "map") # Red self.vizPoint(1, path_pts[1], ColorRGBA(1,1,0,1), "map") # Yellow # self.vizPoint(2, path_pts[2], ColorRGBA(0,0,1,1), "map") # Blue # Find the heading of the charger (last 2 path_pts) heading_q = self.getOrientation(feature_pathpts[-2], feature_pathpts[-1]) # Publish the path self.route_pub.publish(self.getPath2Charger(path_pts, heading_q)) print "-----------------"
def pcl_callback(pcl_msg): # Convert ROS msg to PCL data cloud = ros_to_pcl(pcl_msg) # create a voxelgrid filter object for our input point cloud vox = cloud.make_voxel_grid_filter() # Choose a voxel (also known as leaf) size LEAF_SIZE = 0.01 # set the voxel (or leaf)size vox.set_leaf_size(LEAF_SIZE, LEAF_SIZE, LEAF_SIZE) # Call the filter function to obtain the resultant downsampled point cloud cloud_filtered = vox.filter() #filename = 'voxel_downsampled.pcd' #pcl.save(cloud_filtered, filename) # PassThrough filter # create pass through filter object passthrough = cloud_filtered.make_passthrough_filter() # assign axis and ranve to the passthrough filter object filter_axis = 'z' passthrough.set_filter_field_name(filter_axis) axis_min = 0.6 axis_max = 1.1 passthrough.set_filter_limits(axis_min, axis_max) # generate the resultant point cloud cloud_filtered = passthrough.filter() # RANSAC plane segmentation # create the segmentation object seg = cloud_filtered.make_segmenter() # set the model to fit seg.set_model_type(pcl.SACMODEL_PLANE) seg.set_method_type(pcl.SAC_RANSAC) # Max distance for a point to be considered fitting the model max_distance = 0.01 seg.set_distance_threshold(max_distance) # call the segment function to obtain set of inlier indices and model coefficients inliers, coefficients = seg.segment() # Extract table cloud_table = cloud_filtered.extract(inliers, negative=False) #filename = 'cloud_table.pcd' #pcl.save(cloud_table, filename) # Extract objects #cloud_objects = cloud_filtered.extract(inliers, negative=True) # Extract outliers # create the filter objectd objects = cloud_filtered.extract(inliers, negative=True) outlier_filter = objects.make_statistical_outlier_filter() # set the number of points to analyze for any given point outlier_filter.set_mean_k(50) # set threshold scale factor x = 1.0 # any point with a mena distance large than the global (mean distance + x * std_dev) # will be considered outlier outlier_filter.set_std_dev_mul_thresh(x) # call the filter function cloud_objects = outlier_filter.filter() filename = 'cloud_objects.pcd' pcl.save(cloud_objects, filename) # TODO: Euclidean Clustering white_cloud = XYZRGB_to_XYZ(cloud_objects) tree = white_cloud.make_kdtree() # create cluster extraction ec = white_cloud.make_EuclideanClusterExtraction() # set the tolerances for ec.set_ClusterTolerance(0.05) ec.set_MinClusterSize(100) ec.set_MaxClusterSize(2000) ec.set_SearchMethod(tree) cluster_indices = ec.Extract() # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately # asign colors to the individual point cloud indices for visualization cluster_color = get_color_list(len(cluster_indices)) color_cluster_point_list = [] for j, indices in enumerate(cluster_indices): for i, indice in enumerate(indices): color_cluster_point_list.append([ white_cloud[indice][0], white_cloud[indice][1], white_cloud[indice][2], rgb_to_float(cluster_color[j]) ]) # create new cloud containing all clusters, each with unique color cluster_cloud = pcl.PointCloud_PointXYZRGB() cluster_cloud.from_list(color_cluster_point_list) # TODO: Convert PCL data to ROS messages ros_cloud_objects = pcl_to_ros(cloud_objects) ros_cloud_table = pcl_to_ros(cloud_table) ros_cluster_cloud = pcl_to_ros(cluster_cloud) # TODO: Publish ROS messages pcl_objects_pub.publish(ros_cloud_objects) pcl_table_pub.publish(ros_cloud_table) pcl_cluster_pub.publish(ros_cluster_cloud)
def pcl_callback(ros_pcl_msg): # Exercise-1 TODOs: # TODO: Load point cloud data # TODO: Voxel Grid Downsampling # TODO: PassThrough Filter # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers # TODO: Save the inliers in .pcd file # TODO: Run the inliers.pcd file on terminal # Exercise-2 TODOs: # TODO: Convert ROS msg to PCL data # TODO: Voxel Grid Downsampling # TODO: PassThrough Filter # TODO: RANSAC Plane Segmentation # TODO: Extract inliers and outliers # TODO: Euclidean Clustering # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately # TODO: Convert PCL data to ROS messages # TODO: Publish ROS messages # Exercise-3 TODOs: # TODO: Classify the clusters! (loop through each detected cluster one at a time) # TODO: Grab the points for the cluster # TODO: Compute the associated feature vector # TODO: Make the prediction # TODO: Publish a label into RViz # TODO: Add the detected object to the list of detected objects. # TODO: Publish the list of detected objects # TODO: Convert ROS msg of type PointCloud2 to PCL data PointXYZRGB format pcl_data = pclh.ros_to_pcl(ros_pcl_msg) # TODO: Statistical filter stat_inlier, stat_outlier = flt.statistical_outlier_filter(pcl_data) # TODO: Voxel Grid Downsampling vox_filt = flt.voxel_downsampling(stat_inlier) # TODO: PassThrough Filter pass_filt = flt.pass_through_filter(vox_filt) # TODO: RANSAC Plane Segmentation # TODO: Extract inliers (objects) and outliers (table) cloud_table, cloud_objects = flt.ransac(pass_filt) # TODO: Euclidean Clustering cluster_indices = flt.euclidean_clustering(cloud_objects) # TODO: Create Cluster-Mask Point Cloud to visualize each cluster separately cluster_cloud = flt.visualize_clusters(cloud_objects, cluster_indices) # TODO: Classify the clusters and create list of detected objects detected_objects, detected_objects_labels = detect_objects(cloud_objects, cluster_indices) # TODO: Convert PCL data to ROS messages ros_table_cloud = pclh.pcl_to_ros(cloud_table) ros_objects_cloud = pclh.pcl_to_ros(cloud_objects) ros_cluster_cloud = pclh.pcl_to_ros(cluster_cloud) # TODO: Publish ROS messages pcl_table_pub.publish(ros_table_cloud) pcl_objects_pub.publish(ros_objects_cloud) pcl_cluster_pub.publish(ros_cluster_cloud) detected_objects_pub.publish(detected_objects)
def pr2_mover(detected_objects): object_list = detected_objects # TODO: Initialize variables test_scene_num = msgStd.Int32() arm_name = msgStd.String() object_name = msgStd.String() pick_pose = msgGeo.Pose() place_pose = msgGeo.Pose() test_scene_num.data = g_WORLD_NUM detected = 0 dict_list = [] # TODO: Get/Read parameters # get parameters object_list_param = rospy.get_param('/object_list') dropbox_param = rospy.get_param('/dropbox') dictGroupColorToBoxParam = {} for boxParam in dropbox_param: groupColorStr = boxParam['group'] dictGroupColorToBoxParam[groupColorStr] = boxParam # TODO: Loop through the pick list for objIndex in range(0, len(object_list_param)): # TODO: Parse parameters into individual variables object_group = object_list_param[objIndex]['group'] # TODO: Get the PointCloud for a given object and obtain it's centroid for object in object_list: if object_list_param[objIndex]['name'] == object.label: # labels.append(object.label) points_arr = pcl_helper.ros_to_pcl(object.cloud).to_array() centroids = np.mean(points_arr, axis=0)[:3] pick_pose.position.x = np.asscalar(centroids[0]) pick_pose.position.y = np.asscalar(centroids[1]) pick_pose.position.z = np.asscalar(centroids[2]) object_name.data = object_list_param[objIndex]['name'] detected += 1 break # TODO: Create 'place_pose' for the object targetBox = dictGroupColorToBoxParam[object_group] dropbox_pose = targetBox['position'] place_pose.position.x = dropbox_pose[0] place_pose.position.y = dropbox_pose[1] place_pose.position.z = dropbox_pose[2] # TODO: Assign the arm to be used for pick_place arm_name.data = targetBox['name'] # TODO: Create a list of dictionaries (made with make_yaml_dict()) for later output to yaml format # Populate various ROS messages yaml_dict = make_yaml_dict(test_scene_num, arm_name, object_name, pick_pose, place_pose) dict_list.append(yaml_dict) # TODO: Rotate PR2 in place to capture side tables for the collision map if g_doCommandRobot: try: rospy.wait_for_service('pick_place_routine') #pick_place_routine = rospy.ServiceProxy('pick_place_routine', PickPlace) # TODO: Insert your message variables to be sent as a service request #resp = pick_place_routine(g_WORLD_NUM, OBJECT_NAME, WHICH_ARM, PICK_POSE, PLACE_POSE) #print ("Response: ",resp.success) except rospy.ServiceException, e: print "Service call failed: %s" % e
def get_clusters(self, cloud): print("point cloud size = ",len(cloud.data), " bytes") marker_array = MarkerArray() pcl_cloud = ros_to_pcl(cloud) #convert pointcloud_xyzrgb to pointcloud_xyz # since pointcloud_xyzrgb does not support make_EuclideanClusterExtraction xyz_pc = pcl.PointCloud() xyz_pc.from_list( [ x[:3] for x in pcl_cloud.to_list() ] ) tree = xyz_pc.make_kdtree() ec = xyz_pc.make_EuclideanClusterExtraction() ec.set_ClusterTolerance (0.01) ec.set_MinClusterSize (100) ec.set_MaxClusterSize (25000) ec.set_SearchMethod (tree) cluster_indices = ec.Extract() indx = [] #for all j clusters print("# clusters ", len(cluster_indices)) for j, indices in enumerate(cluster_indices): tmp_indx = [] print("# points ", len(indices)) min_x = 999.9 max_x = -999.9 min_y = 999.9 max_y = -999.9 min_z = 999.9 max_z = -999.9 #for all points in the cluster j for i, indice in enumerate(indices): tmp_indx.append(indice) if xyz_pc[indice][0] < min_x: min_x = xyz_pc[indice][0] elif xyz_pc[indice][0] > max_x: max_x = xyz_pc[indice][0] if xyz_pc[indice][1] < min_y: min_y = xyz_pc[indice][1] elif xyz_pc[indice][1] > max_y: max_y = xyz_pc[indice][1] if xyz_pc[indice][2] < min_z: min_z = xyz_pc[indice][2] elif xyz_pc[indice][2] > max_z: max_z = xyz_pc[indice][2] x = (min_x + max_x)/2 y = (min_y + max_y)/2 z = (min_z + max_z)/2 p = get_stamped_point(x, y, z, cloud.header.frame_id) label = "obj_" + str(j) marker_array.markers.append(get_marker(j, label, cloud.header.frame_id, x, y, z)) obj = pcl_cloud.extract(indices, negative=False) pc_msg = pcl_to_ros(obj, stamp=cloud.header.stamp, frame_id=cloud.header.frame_id, seq=cloud.header.seq) converter = PointCloudToImages('/home/phiggin1/deep_rgbd_v01.2/data/') converter.convert(obj) fuse_color_depth() features = get_features() self.pub.publish(pc_msg) print("object at ", p, "\nwith features: ",features) self.obj_markers_pub.publish(marker_array)
def create_pick_pose(item): points_array = pclh.ros_to_pcl(item.cloud).to_array() centroid = np.mean(points_array, axis=0)[:3] return make_ros_pose(centroid[0], centroid[1], centroid[2])