colorized_point_cloud.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import rostest
4 
5 import unittest
6 
7 import actionlib
8 
9 from actionlib_msgs.msg import GoalStatus
10 from ensenso_camera_msgs.msg import TexturedPointCloudAction, TexturedPointCloudGoal
11 from ensenso_camera_msgs.msg import RequestDataMonoAction, RequestDataMonoGoal
12 from ensenso_camera_msgs.msg import RequestDataAction, RequestDataGoal
13 
14 import ctypes
15 import struct
16 
17 import sensor_msgs.point_cloud2 as pc2
18 
19 WAIT_TIMEOUT = 20
20 
21 STEREO_NAMESPACE = "stereo"
22 MONO_NAMESPACE = "mono"
23 MONO_SERIAL = "color_mono"
24 
25 
26 class TestColorizedPointCloud(unittest.TestCase):
27  def setUp(self):
28  self.request_stereo = actionlib.SimpleActionClient(STEREO_NAMESPACE + "/request_data", RequestDataAction)
29  self.request_mono = actionlib.SimpleActionClient(MONO_NAMESPACE + "/request_data", RequestDataMonoAction)
30  self.colorize_point_cloud = actionlib.SimpleActionClient(STEREO_NAMESPACE + "/texture_point_cloud",
31  TexturedPointCloudAction)
32  for client in [self.request_mono, self.request_stereo,
34  if not client.wait_for_server(rospy.Duration(WAIT_TIMEOUT)):
35  self.fail(msg="Timeout reached for servers.")
36 
38  self.send_data_goals()
39  self.aquire_data()
41  self.check_color()
42 
43  def send_data_goals(self):
44  goal_mono = RequestDataMonoGoal()
45  goal_mono.request_rectified_images = True
46  goal = RequestDataGoal()
47  goal.request_point_cloud = True
48  self.request_mono.send_goal(goal_mono)
49  self.request_stereo.send_goal(goal)
50 
51  for client in [self.request_mono, self.request_stereo]:
52  if not client.wait_for_result(rospy.Duration(WAIT_TIMEOUT)):
53  self.fail(msg="Timeout reached for results.")
54  self.assertTrue(client.get_state() == GoalStatus.SUCCEEDED, msg="Request action has not been successful.")
55 
56  def aquire_data(self):
57  goal_texture = TexturedPointCloudGoal()
58  goal_texture.use_opengl = False
59  goal_texture.include_results_in_response = True
60  goal_texture.mono_serial = MONO_SERIAL
61  goal_texture.far_plane = 4000.0
62  goal_texture.near_plane = 100.0
63  self.colorize_point_cloud.send_goal(goal_texture)
64  if not self.colorize_point_cloud.wait_for_result(rospy.Duration(WAIT_TIMEOUT)):
65  self.fail(msg="Timeout reached for results.")
66 
68  result = self.colorize_point_cloud.get_result()
69  self.assertEqual(result.error.message, "", msg="Got an error with the result")
70  self.cloud_points = list(pc2.read_points(result.point_cloud, skip_nans=True))
71  self.assertTrue(len(self.cloud_points) != 0,
72  msg=" The recieved point cloud is empty. Recieved {} points".format(len(self.cloud_points)))
73 
74  def check_color(self):
75  for p in self.cloud_points:
76  # p has 4 points. The last one holds the color information.
77  # (http://answers.ros.org/question/208834/read-colours-from-a-pointcloud2-python/)
78 
79  test = p[3]
80  # cast float32 to int so that bitwise operations are possible
81  s = struct.pack('>f', test)
82  i = struct.unpack('>l', s)[0]
83  # you can get back the float value by the inverse operations
84  pack = ctypes.c_uint32(i).value
85  r = (pack & 0x00FF0000) >> 16
86  g = (pack & 0x0000FF00) >> 8
87  b = (pack & 0x000000FF)
88 
89  self.assertTrue(r != 0., msg="r-value cannot be zero")
90  self.assertTrue(g != 0., msg="g-value cannot be zero")
91  self.assertTrue(b != 0., msg="b-value cannot be zero")
92 
93 
94 if __name__ == "__main__":
95  try:
96  rospy.init_node("test_colorized_point_cloud")
97  rostest.rosrun("ensenso_camera_test", "test_colorized_point_cloud", TestColorizedPointCloud)
98  except rospy.ROSInterruptException:
99  pass


ensenso_camera_test
Author(s): Ensenso
autogenerated on Thu May 6 2021 02:50:10