test_polygon_array_unwrapper.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
4 
5 
6 import rospy
7 from threading import Event
8 from unittest import TestCase
9 
10 import dynamic_reconfigure.client
11 from jsk_recognition_msgs.msg import PolygonArray, ModelCoefficientsArray
12 from geometry_msgs.msg import PolygonStamped
13 from pcl_msgs.msg import ModelCoefficients
14 
15 TEST_NODE = "/polygon_array_unwrapper"
16 
17 class TestPolygonArrayUnwrapper(TestCase):
18  def setUp(self):
19  self.client = dynamic_reconfigure.client.Client(TEST_NODE, timeout=30)
20  self.pub_polygon = rospy.Publisher(TEST_NODE + "/input_polygons",
21  PolygonArray, queue_size=1)
22  self.pub_coefficients = rospy.Publisher(TEST_NODE + "/input_coefficients",
23  ModelCoefficientsArray, queue_size=1)
24  self.sub_polygon = None
25  self.sub_coefficients = None
26  self.msg_wait_timeout = 10
27  self.polygon_msg = None
29  self.coeff_msg = None
30  self.coeff_msg_callback_event = Event()
31  self.timer = rospy.Timer(rospy.Duration(0.1), self.publishPolygons)
32  def tearDown(self):
33  self.unsubscribe()
34  self.timer.shutdown()
35  self.polygon_msg_callback_event.clear()
36  self.coeff_msg_callback_event.clear()
37  def subscribe(self):
38  self.sub_polygon = rospy.Subscriber(TEST_NODE + "/output_polygon",
39  PolygonStamped,
40  self.polygonMsgCallback)
41  self.sub_coefficients = rospy.Subscriber(TEST_NODE + "/output_coefficients",
42  ModelCoefficients,
43  self.coeffMsgCallback)
44  def unsubscribe(self):
45  self.sub_polygon.unregister()
46  self.sub_coefficients.unregister()
47  def polygonMsgCallback(self, msg):
48  self.polygon_msg = msg
49  self.polygon_msg_callback_event.set()
50  def coeffMsgCallback(self, msg):
51  self.coeff_msg = msg
52  self.coeff_msg_callback_event.set()
53  def publishPolygons(self, event=None):
54  pmsg = PolygonArray()
55  pmsg.header.stamp = rospy.Time.now()
56  cmsg = ModelCoefficientsArray()
57  cmsg.header.stamp = pmsg.header.stamp
58  for i in range(10):
59  pmsg.polygons.append(PolygonStamped())
60  pmsg.polygons[i].header.stamp = pmsg.header.stamp
61  pmsg.polygons[i].header.frame_id = str(i)
62  cmsg.coefficients.append(ModelCoefficients())
63  cmsg.coefficients[i].header.stamp = cmsg.header.stamp
64  cmsg.coefficients[i].header.frame_id = str(i)
65  pmsg.likelihood = [1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0]
66  self.pub_polygon.publish(pmsg)
67  self.pub_coefficients.publish(cmsg)
68  def waitMsgs(self):
69  self.polygon_msg_callback_event.wait(self.msg_wait_timeout)
70  self.coeff_msg_callback_event.wait(self.msg_wait_timeout)
71  def testUnwrap(self):
72  self.client.update_configuration({
73  "plane_index": 0,
74  "use_likelihood": False
75  })
76  self.subscribe()
77  self.waitMsgs()
78 
79  self.assertTrue(self.polygon_msg is not None)
80  self.assertTrue(self.coeff_msg is not None)
81  self.assertEqual(self.polygon_msg.header.frame_id, str(0))
82  self.assertEqual(self.coeff_msg.header.frame_id, str(0))
83  def testUnwrapIndex(self):
84  self.client.update_configuration({
85  "plane_index": 7,
86  "use_likelihood": False
87  })
88  self.subscribe()
89  self.waitMsgs()
90 
91  self.assertTrue(self.polygon_msg is not None)
92  self.assertTrue(self.coeff_msg is not None)
93  self.assertEqual(self.polygon_msg.header.frame_id, str(7))
94  self.assertEqual(self.coeff_msg.header.frame_id, str(7))
96  self.client.update_configuration({
97  "plane_index": 2,
98  "use_likelihood": True
99  })
100  self.subscribe()
101  self.waitMsgs()
102 
103  self.assertTrue(self.polygon_msg is not None)
104  self.assertTrue(self.coeff_msg is not None)
105  self.assertEqual(self.polygon_msg.header.frame_id, str(4))
106  self.assertEqual(self.coeff_msg.header.frame_id, str(4))
107 
108 if __name__ == '__main__':
109  import rostest
110  rospy.init_node("polygon_array_unwrapper_test")
111  rostest.rosrun("jsk_pcl_ros_utils", "polygon_array_unwrapper_test", TestPolygonArrayUnwrapper)


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:15