test_plugin.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import unittest
4 import rospy
5 import rostest
6 from std_msgs.msg import Header
7 from gazebo_msgs.srv import GetJointProperties, GetJointPropertiesRequest, GetJointPropertiesResponse
8 from sensor_msgs.msg import JointState
9 
10 
11 class TestSetJointPositionsPlugin(unittest.TestCase):
12  @classmethod
13  def setUpClass(cls):
14  cls.__get_joint_state_srv = rospy.ServiceProxy(
15  name='/gazebo/get_joint_properties',
16  service_class=GetJointProperties
17  )
18  cls.__get_joint_state_srv.wait_for_service(timeout=30)
19 
20  cls.__joint_state_pub = rospy.Publisher("/ur_driver/joint_states", JointState, queue_size=100, latch=True)
21 
22  cls.__test_joint_name = 'set_link_joint'
23 
24  def test_set(self):
25  rospy.sleep(1)
26  self.set_and_test_position(1.0)
27  self.set_and_test_position(3.0)
28  self.set_and_test_position(-1.0)
29  self.set_and_test_position(-3.0)
30 
31  def set_and_test_position(self, test_position):
32  # Set test position
33  self.set_position(test_position)
34 
35  rospy.sleep(1) # Small sleep to wait for the set to take effect
36 
37  # Get joint position
38  get_joint_response = self.__get_joint_state_srv.call(
39  GetJointPropertiesRequest(joint_name=self.__test_joint_name))
40 
41  rospy.loginfo('Joint pose retrieved as value: ''{}'' expected ''{}'''.format(get_joint_response.position[0],
42  test_position))
43  assert isinstance(get_joint_response, GetJointPropertiesResponse)
44  self.assertTrue(get_joint_response.success)
45  self.assertAlmostEqual(get_joint_response.position[0], test_position)
46 
47  def set_position(self, position):
48  test_state = JointState()
49  test_state.header = Header()
50  test_state.header.stamp = rospy.Time.now()
51  test_state.header.frame_id = 'world'
52  test_state.name = [self.__test_joint_name]
53  test_state.position = [position]
54  test_state.velocity = []
55  test_state.effort = []
56  self.__joint_state_pub.publish(test_state)
57 
58 
59 if __name__ == '__main__':
60  rospy.init_node('test_plugin')
61 
62  rostest.rosrun('set_joint_positions', 'test_set_joint_positions', TestSetJointPositionsPlugin)
test_plugin.TestSetJointPositionsPlugin.setUpClass
def setUpClass(cls)
Definition: test_plugin.py:13
test_plugin.TestSetJointPositionsPlugin
Definition: test_plugin.py:11
test_plugin.TestSetJointPositionsPlugin.set_and_test_position
def set_and_test_position(self, test_position)
Definition: test_plugin.py:31
test_plugin.TestSetJointPositionsPlugin.__test_joint_name
__test_joint_name
Definition: test_plugin.py:22
test_plugin.TestSetJointPositionsPlugin.test_set
def test_set(self)
Definition: test_plugin.py:24
test_plugin.TestSetJointPositionsPlugin.__joint_state_pub
__joint_state_pub
Definition: test_plugin.py:20
test_plugin.TestSetJointPositionsPlugin.__get_joint_state_srv
__get_joint_state_srv
Definition: test_plugin.py:14
test_plugin.TestSetJointPositionsPlugin.set_position
def set_position(self, position)
Definition: test_plugin.py:47


gazebo_set_joint_positions_plugin
Author(s): Boeing
autogenerated on Wed Jan 17 2024 03:34:06