sample_simulate_tabletop_cloud.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from sensor_msgs.msg import PointCloud2
5 from sensor_msgs.point_cloud2 import create_cloud_xyz32
6 from std_msgs.msg import Header
7 from jsk_recognition_msgs.msg import (PolygonArray, ModelCoefficientsArray,
8  BoundingBox, BoundingBoxArray)
9 from pcl_msgs.msg import ModelCoefficients
10 from geometry_msgs.msg import PolygonStamped, Point32
11 from std_srvs.srv import Empty
12 import numpy as np
13 from math import pi, cos, sin
14 
16  plane_points = []
17  object_points = []
18  dx = 0.02
19  for x in np.arange(-1, 1, dx):
20  for y in np.arange(-1, 1, dx):
21  plane_points.append([x, y, 0.0])
22  for x in np.arange(-0.1, 0.1, dx):
23  for y in np.arange(-0.1, 0.1, dx):
24  object_points.append([x, y, 0.2])
25  return plane_points + object_points
26 
28  plane_points = []
29  object_points = []
30  dx = 0.02
31  for x in np.arange(-1, 1, dx):
32  for y in np.arange(-1, 1, dx):
33  plane_points.append([x, y, 0.0])
34  for x in np.arange(-0.3, 0.2, dx):
35  for y in np.arange(-0.1, 0.1, dx):
36  object_points.append([x, y, 0.2])
37  return plane_points + object_points
38 
40  plane_points = []
41  object_points = []
42  dx = 0.02
43  for x in np.arange(-1, 1, dx):
44  for y in np.arange(-1, 1, dx):
45  plane_points.append([x, y, 0.0])
46  for x in np.arange(-0.1, 0.1, dx):
47  for y in np.arange(-0.1, 0.1, dx):
48  object_points.append([x, y, 0.2])
49  for x in np.arange(-0.4, -0.2, dx):
50  for y in np.arange(-0.1, 0.1, dx):
51  object_points.append([x, y, 0.2])
52 
53  return plane_points + object_points
54 
56  plane_points = []
57  object_points = []
58  dx = 0.02
59  for x in np.arange(-1, 1, dx):
60  for y in np.arange(-1, 1, dx):
61  plane_points.append([x, y, 0.0])
62  for x in np.arange(-0.1, 0.1, dx):
63  for y in np.arange(-0.1, 0.1, dx):
64  object_points.append([x, y, 0.2])
65  for z in np.arange(0.0, 0.2, dx):
66  for y in np.arange(-0.1, 0.1, dx):
67  object_points.append([0.1, y, z])
68  object_points.append([-0.1, y, z])
69  for z in np.arange(0.0, 0.2, dx):
70  for x in np.arange(-0.1, 0.1, dx):
71  object_points.append([x, 0.1, z])
72  object_points.append([x, -0.1, z])
73 
74  return plane_points + object_points
75 
77  plane_points = []
78  object_points = []
79  dx = 0.02
80  for y in np.arange(-0.5, 0.5, dx):
81  for z in np.arange(0, 2, dx):
82  plane_points.append([0.0, y, z])
83  for y in np.arange(0.2, 0.4, dx):
84  for z in np.arange(0.8, 0.9, dx):
85  object_points.append([-0.1, y, z])
86  return plane_points + object_points
87 
88 def generatePoints(model_index):
89  if model_index == 0:
90  return generatePoints0()
91  elif model_index == 1:
92  return generatePoints1()
93  elif model_index == 2:
94  return generatePoints2()
95  elif model_index == 3:
96  return generatePoints3()
97  elif model_index == 4:
98  return generatePointsDoor()
99 
100 def generatePolygons(header, model_index):
101  polygon = PolygonArray()
102  polygon.header = header
103  polygon.polygons = [PolygonStamped()]
104  polygon.polygons[0].header = header;
105  coef = ModelCoefficientsArray()
106  coef.coefficients = [ModelCoefficients()]
107  coef.header = header
108  if model_index in [0, 1, 2, 3]:
109  # Rectangle
110  polygon.polygons[0].polygon.points = [Point32(x=1.0, y=1.0),
111  Point32(x=-1.0, y=1.0),
112  Point32(x=-1.0, y=-1.0),
113  Point32(x=1.0, y=-1.0)]
114  coef.coefficients[0].header = header
115  coef.coefficients[0].values = [0, 0, 1, 0]
116  elif model_index == 4:
117  polygon.polygons[0].polygon.points = [Point32(x=0.0, y=-0.5, z=0.0),
118  Point32(x=0.0, y=-0.5, z=2.0),
119  Point32(x=0.0, y=0.5, z=2.0),
120  Point32(x=0.0, y=0.5, z=0.0)]
121  # polygon.polygons[0].polygon.points.reverse()
122  coef.coefficients[0].header = header
123  coef.coefficients[0].values = [-1, 0, 0, 0]
124  return (polygon, coef)
125 
126 
127 def candidateBoxes(header, model_index):
128  box_array = BoundingBoxArray()
129  box_array.header.stamp = header.stamp
130  box_array.header.frame_id = "odom"
131  dx = 0.1
132  for y in np.arange(0.0, 0.6, dx):
133  for z in np.arange(0.7, 1.0, dx):
134  for x in np.arange(0.0, -0.5, -dx):
135  box = BoundingBox()
136  box.header = box_array.header
137  box.pose.orientation.w = 1.0
138  box.pose.position.x = x
139  box.pose.position.y = y
140  box.pose.position.z = z
141  box.dimensions.x = 0.1
142  box.dimensions.y = 0.1
143  box.dimensions.z = 0.1
144  box_array.boxes.append(box)
145  return box_array
146 
147 if __name__ == "__main__":
148  rospy.init_node("sample_simulate_tabletop_cloud")
149  pub = rospy.Publisher("~output", PointCloud2, queue_size=1)
150  pub_polygon = rospy.Publisher(
151  "~output/polygon", PolygonArray, queue_size=1)
152  pub_coef = rospy.Publisher(
153  "~output/coef", ModelCoefficientsArray, queue_size=1)
154  pub_boxes = rospy.Publisher(
155  "~output/candidate_boxes", BoundingBoxArray, queue_size=1)
156  r = rospy.Rate(10)
157  counter = 0
158  model_index = 4
159  reset = False
160  while not rospy.is_shutdown():
161  points = generatePoints(model_index)
162  header = Header()
163  header.frame_id = "odom"
164  header.stamp = rospy.Time.now()
165  msg = create_cloud_xyz32(header, points)
166  pub.publish(msg)
167  (polygon, coef) = generatePolygons(header, model_index)
168  pub_polygon.publish(polygon)
169  pub_coef.publish(coef)
170  counter = counter + 1
171  pub_boxes.publish(candidateBoxes(header, model_index))
172  if reset and counter > 1.0 / r.sleep_dur.to_sec() * 10:
173  reset = rospy.ServiceProxy("/plane_supported_cuboid_estimator/reset", Empty)
174  counter = 0
175  model_index = model_index + 1
176  if model_index >= 5:
177  model_index = 0
178  reset()
179  r.sleep()


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:47