7 from sensor_msgs.msg
import LaserScan
8 from laser_geometry
import LaserProjection
11 from itertools
import product
15 PROJECTION_TEST_RANGE_MIN = 0.23
16 PROJECTION_TEST_RANGE_MAX = 40.00
22 range_val, intensity_val,
23 angle_min, angle_max, angle_increment, scan_time):
24 count = np.uint(np.ceil((angle_max - angle_min) / angle_increment))
26 raise BuildScanException
29 scan.header.stamp = rospy.rostime.Time.from_sec(10.10)
30 scan.header.frame_id =
"laser_frame" 31 scan.angle_min = angle_min
32 scan.angle_max = angle_max
33 scan.angle_increment = angle_increment
34 scan.scan_time = scan_time.to_sec()
35 scan.range_min = PROJECTION_TEST_RANGE_MIN
36 scan.range_max = PROJECTION_TEST_RANGE_MAX
37 scan.ranges = [range_val
for _
in range(count)]
38 scan.intensities = [intensity_val
for _
in range(count)]
39 scan.time_increment = scan_time.to_sec()/count
49 ranges = [-1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 100.0]
50 intensities = np.arange(1.0, 6.0).tolist()
52 min_angles = -np.pi / np.array([1.0, 1.5, 2.0, 4.0, 8.0])
53 max_angles = -min_angles
55 angle_increments = np.pi / np.array([180., 360., 720.])
57 scan_times = [rospy.Duration(1./i)
for i
in [40, 20]]
59 for range_val, intensity_val, \
60 angle_min, angle_max, angle_increment, scan_time
in \
61 product(ranges, intensities,
62 min_angles, max_angles, angle_increments, scan_times):
65 range_val, intensity_val,
66 angle_min, angle_max, angle_increment, scan_time)
67 except BuildScanException:
68 if (angle_max - angle_min)/angle_increment > 0:
71 cloud_out = projector.projectLaser(scan, -1.0,
72 LaserProjection.ChannelOption.INDEX)
73 self.assertEquals(len(cloud_out.fields), 4,
74 "PointCloud2 with channel INDEX: fields size != 4")
76 cloud_out = projector.projectLaser(scan, -1.0,
77 LaserProjection.ChannelOption.INTENSITY)
78 self.assertEquals(len(cloud_out.fields), 4,
79 "PointCloud2 with channel INDEX: fields size != 4")
81 cloud_out = projector.projectLaser(scan, -1.0)
82 self.assertEquals(len(cloud_out.fields), 5,
83 "PointCloud2 with channel INDEX: fields size != 5")
84 cloud_out = projector.projectLaser(scan, -1.0,
85 LaserProjection.ChannelOption.INTENSITY |
86 LaserProjection.ChannelOption.INDEX)
87 self.assertEquals(len(cloud_out.fields), 5,
88 "PointCloud2 with channel INDEX: fields size != 5")
90 cloud_out = projector.projectLaser(scan, -1.0,
91 LaserProjection.ChannelOption.INTENSITY |
92 LaserProjection.ChannelOption.INDEX |
93 LaserProjection.ChannelOption.DISTANCE)
94 self.assertEquals(len(cloud_out.fields), 6,
95 "PointCloud2 with channel INDEX: fields size != 6")
97 cloud_out = projector.projectLaser(scan, -1.0,
98 LaserProjection.ChannelOption.INTENSITY |
99 LaserProjection.ChannelOption.INDEX |
100 LaserProjection.ChannelOption.DISTANCE |
101 LaserProjection.ChannelOption.TIMESTAMP)
102 self.assertEquals(len(cloud_out.fields), 7,
103 "PointCloud2 with channel INDEX: fields size != 7")
106 for i
in range(len(scan.ranges)):
108 if (PROJECTION_TEST_RANGE_MIN <= ri
and 109 ri <= PROJECTION_TEST_RANGE_MAX):
112 self.assertEqual(valid_points, cloud_out.width,
113 "Valid points != PointCloud2 width")
115 idx_x = idx_y = idx_z = 0
116 idx_intensity = idx_index = 0
117 idx_distance = idx_stamps = 0
120 for f
in cloud_out.fields:
127 elif f.name ==
"intensity":
129 elif f.name ==
"index":
131 elif f.name ==
"distances":
133 elif f.name ==
"stamps":
138 for point
in pc2.read_points(cloud_out):
140 ai = scan.angle_min + i * scan.angle_increment
142 self.assertAlmostEqual(point[idx_x], ri * np.cos(ai),
143 tolerance,
"x not equal")
144 self.assertAlmostEqual(point[idx_y], ri * np.sin(ai),
145 tolerance,
"y not equal")
146 self.assertAlmostEqual(point[idx_z], 0,
147 tolerance,
"z not equal")
148 self.assertAlmostEqual(point[idx_intensity],
150 tolerance,
"Intensity not equal")
151 self.assertAlmostEqual(point[idx_index], i,
152 tolerance,
"Index not equal")
153 self.assertAlmostEqual(point[idx_distance], ri,
154 tolerance,
"Distance not equal")
155 self.assertAlmostEqual(point[idx_stamps],
156 i * scan.time_increment,
157 tolerance,
"Timestamp not equal")
161 if __name__ ==
'__main__':
163 rosunit.unitrun(PKG,
'projection_test', ProjectionTest)
A Class to Project Laser Scan.
def build_constant_scan(range_val, intensity_val, angle_min, angle_max, angle_increment, scan_time)
def test_project_laser(self)