moveit2
The MoveIt Motion Planning Framework for ROS 2.
test_robot_state.py
Go to the documentation of this file.
1 import unittest
2 import numpy as np
3 
4 from geometry_msgs.msg import Pose
5 
6 from test_moveit.core.robot_state import RobotState
7 from test_moveit.core.robot_model import RobotModel
8 
9 
10 # TODO (peterdavidfagan): depend on moveit_resources package directly.
11 # (https://github.com/peterdavidfagan/moveit2/blob/moveit_py/moveit_core/utils/src/robot_model_test_utils.cpp)
12 
13 import os
14 
15 dir_path = os.path.dirname(os.path.realpath(__file__))
16 URDF_FILE = "{}/fixtures/panda.urdf".format(dir_path)
17 SRDF_FILE = "{}/fixtures/panda.srdf".format(dir_path)
18 
19 
21  """Helper function that returns a RobotModel instance."""
22  return RobotModel(urdf_xml_path=URDF_FILE, srdf_xml_path=SRDF_FILE)
23 
24 
25 class TestRobotState(unittest.TestCase):
27  """
28  Test that RobotState can be initialized with a RobotModel
29  """
30  robot_model = get_robot_model()
31  robot_state = RobotState(robot_model)
32 
33  self.assertIsInstance(robot_state, RobotState)
34 
36  """
37  Test that the robot_model property returns the correct RobotModel
38  """
39  robot_model = get_robot_model()
40  robot_state = RobotState(robot_model)
41 
42  self.assertEqual(robot_state.robot_model, robot_model)
43 
45  """
46  Test that the frame transform is correct
47  """
48  robot_model = get_robot_model()
49  robot_state = RobotState(robot_model)
50  robot_state.update()
51  frame_transform = robot_state.get_frame_transform("panda_link0")
52 
53  self.assertIsInstance(frame_transform, np.ndarray)
54  # TODO(peterdavidfagan): add assertion for particular values
55 
56  def test_get_pose(self):
57  """
58  Test that the pose is correct
59  """
60  robot_model = get_robot_model()
61  robot_state = RobotState(robot_model)
62  robot_state.update()
63  pose = robot_state.get_pose(link_name="panda_link8")
64 
65  self.assertIsInstance(pose, Pose)
66  # TODO(peterdavidfagan): add assertion for particular values
67 
69  """
70  Test that the jacobian is correct
71  """
72  robot_model = get_robot_model()
73  robot_state = RobotState(robot_model)
74  robot_state.update()
75  jacobian = robot_state.get_jacobian(
76  joint_model_group_name="panda_arm",
77  reference_point_position=np.array([0.0, 0.0, 0.0]),
78  )
79 
80  self.assertIsInstance(jacobian, np.ndarray)
81  # TODO(peterdavidfagan): add assertion for particular values
82 
84  """
85  Test that the jacobian is correct
86  """
87  robot_model = get_robot_model()
88  robot_state = RobotState(robot_model)
89  robot_state.update()
90  jacobian = robot_state.get_jacobian(
91  joint_model_group_name="panda_arm",
92  link_name="panda_link6",
93  reference_point_position=np.array([0.0, 0.0, 0.0]),
94  )
95 
96  self.assertIsInstance(jacobian, np.ndarray)
97  # TODO(peterdavidfagan): add assertion for particular values
98 
100  """
101  Test that the joint group positions can be set
102  """
103  robot_model = get_robot_model()
104  robot_state = RobotState(robot_model)
105  robot_state.update()
106  joint_group_positions = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
107  robot_state.set_joint_group_positions(
108  joint_model_group_name="panda_arm", position_values=joint_group_positions
109  )
110 
111  self.assertEqual(
112  joint_group_positions.tolist(),
113  robot_state.get_joint_group_positions("panda_arm").tolist(),
114  )
115 
117  """
118  Test that the joint group velocities can be set
119  """
120  robot_model = get_robot_model()
121  robot_state = RobotState(robot_model)
122  robot_state.update()
123  joint_group_velocities = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
124  robot_state.set_joint_group_velocities(
125  joint_model_group_name="panda_arm", velocity_values=joint_group_velocities
126  )
127 
128  self.assertEqual(
129  joint_group_velocities.tolist(),
130  robot_state.get_joint_group_velocities("panda_arm").tolist(),
131  )
132 
134  """
135  Test that the joint group accelerations can be set
136  """
137  robot_model = get_robot_model()
138  robot_state = RobotState(robot_model)
139  robot_state.update()
140  joint_group_accelerations = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
141  robot_state.set_joint_group_accelerations(
142  joint_model_group_name="panda_arm",
143  acceleration_values=joint_group_accelerations,
144  )
145 
146  self.assertEqual(
147  joint_group_accelerations.tolist(),
148  robot_state.get_joint_group_accelerations("panda_arm").tolist(),
149  )
150 
151  # TODO (peterdavidfagan): requires kinematics solver to be loaded
152  # def test_set_from_ik(self):
153  # """
154  # Test that the robot state can be set from an IK solution
155  # """
156  # robot_model = RobotModel(
157  # urdf_xml_path="./fixtures/panda.urdf", srdf_xml_path="./fixtures/panda.srdf"
158  # )
159  # robot_state = RobotState(robot_model)
160  # robot_state.update()
161  # pose = Pose()
162  # pose.position.x = 0.5
163  # pose.position.y = 0.5
164  # pose.position.z = 0.5
165  # pose.orientation.w = 1.0
166 
167  # robot_state.set_from_ik(
168  # joint_model_group_name="panda_arm",
169  # geometry_pose=pose,
170  # tip_name="panda_link8",
171  # )
172 
173  # self.assertEqual(robot_state.get_pose("panda_link8"), pose)
174 
175 
176 if __name__ == "__main__":
177  unittest.main()