test_controller.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys
3 import unittest
4 import copy
5 import rospy
6 import actionlib
7 from actionlib_msgs.msg import GoalStatus
8 import time
9 from control_msgs.msg import FollowJointTrajectoryAction, FollowJointTrajectoryGoal
10 from cartesian_control_msgs.msg import (
11  FollowCartesianTrajectoryAction,
12  FollowCartesianTrajectoryGoal,
13  FollowCartesianTrajectoryResult,
14  CartesianTrajectoryPoint)
15 from trajectory_msgs.msg import JointTrajectoryPoint
16 from controller_manager_msgs.srv import SwitchControllerRequest, SwitchController
17 
18 PKG = 'cartesian_trajectory_controller'
19 NAME = 'test_controller'
20 
21 
22 class IntegrationTest(unittest.TestCase):
23  def __init__(self, *args):
24  super(IntegrationTest, self).__init__(*args)
25 
26  rospy.init_node('test_controller')
27 
28  timeout = rospy.Duration(3)
29 
31  "/joint_trajectory_controller/follow_joint_trajectory", FollowJointTrajectoryAction)
32  try:
33  self.set_joints.wait_for_server(timeout)
34  except rospy.exceptions.ROSException as err:
35  self.fail("Could not reach controller action. Msg: {}".format(err))
36 
38  'cartesian_trajectory_controller/follow_cartesian_trajectory', FollowCartesianTrajectoryAction)
39  try:
40  self.cart_client.wait_for_server(timeout)
41  except rospy.exceptions.ROSException as err:
42  self.fail("Could not reach controller action. Msg: {}".format(err))
43 
45  'cartesian_trajectory_publisher/follow_cartesian_trajectory', FollowCartesianTrajectoryAction)
46  if not self.cart_client.wait_for_server(timeout):
47  self.fail("Could not reach trajectory publisher action.")
48 
49  self.switch = rospy.ServiceProxy('/controller_manager/switch_controller', SwitchController)
50  try:
51  self.switch.wait_for_service(timeout.to_sec())
52  except rospy.exceptions.ROSException as err:
53  self.fail(
54  "Could not reach controller switch service. Msg: {}".format(err))
55 
57  """ Test the basic functionality on a straight line """
58  self.move_to_start()
60  self.move_square(self.cart_client)
61  self.assertEqual(self.cart_client.get_result().error_code,
62  FollowCartesianTrajectoryResult.SUCCESSFUL)
63 
64  def test_preemption(self):
65  """ Test whether the preemption mechanism works correctly """
66  self.move_to_start()
68  self.move_square(self.cart_client, wait_for_result=False)
69  time.sleep(3) # stop somewhere during execution
70  self.cart_client.cancel_goal()
71  time.sleep(1)
72  self.assertEqual(self.cart_client.get_state(),
73  GoalStatus.PREEMPTED)
74 
75  def test_invalid_goals(self):
76  """ Test whether invalid goals are rejected correctly """
77 
78  self.move_to_start()
80 
81  # Time from start is not strictly increasing.
82  # This also covers points without time_from_start (they are all implicitly 0).
83  p1 = CartesianTrajectoryPoint()
84  p1.time_from_start = rospy.Duration(1.0)
85  p2 = CartesianTrajectoryPoint()
86  p2.time_from_start = rospy.Duration(0.9999)
87  goal = FollowCartesianTrajectoryGoal()
88  goal.trajectory.points.append(p1)
89  goal.trajectory.points.append(p2)
90  self.cart_client.send_goal(goal)
91  self.cart_client.wait_for_result()
92  self.assertEqual(self.cart_client.get_result().error_code,
93  FollowCartesianTrajectoryResult.INVALID_GOAL)
94 
96  """ Test whether trajectory publishing works """
97 
98  self.move_to_start()
99 
100  # Activate trajectory publishing
101  srv = SwitchControllerRequest()
102  srv.stop_controllers = []
103  srv.start_controllers = ['cartesian_trajectory_publisher']
104  srv.strictness = SwitchControllerRequest.BEST_EFFORT
105  self.switch(srv)
106 
107  self.move_square(self.pub_trajectory)
108 
109  # Deactivate trajectory publishing to not spam the other tests.
110  srv = SwitchControllerRequest()
111  srv.stop_controllers = ['cartesian_trajectory_publisher']
112  srv.start_controllers = []
113  srv.strictness = SwitchControllerRequest.BEST_EFFORT
114  self.switch(srv)
115 
116  self.assertEqual(self.pub_trajectory.get_result().error_code,
117  FollowCartesianTrajectoryResult.SUCCESSFUL)
118 
119  def move_to_start(self):
120  srv = SwitchControllerRequest()
121  srv.stop_controllers = ['cartesian_trajectory_controller']
122  srv.start_controllers = ['joint_trajectory_controller']
123  srv.strictness = SwitchControllerRequest.BEST_EFFORT
124  self.switch(srv)
125 
126  q_start = [0, -2.0, 2.26, -0.2513274122872, 1.57, 0.0] # From base to tip
127  start_joint_state = FollowJointTrajectoryGoal()
128  start_joint_state.trajectory.joint_names = [
129  'joint1',
130  'joint2',
131  'joint3',
132  'joint4',
133  'joint5',
134  'joint6']
135 
136  start_joint_state.trajectory.points = [
137  JointTrajectoryPoint(positions=q_start, time_from_start=rospy.Duration(3.0))]
138  self.set_joints.send_goal(
139  start_joint_state)
140  self.set_joints.wait_for_result()
141 
143  srv = SwitchControllerRequest()
144  srv.stop_controllers = ['joint_trajectory_controller']
145  srv.start_controllers = ['cartesian_trajectory_controller']
146  srv.strictness = SwitchControllerRequest.BEST_EFFORT
147  self.switch(srv)
148 
149  def move_square(self, action_client, wait_for_result=True):
150  """ Follow a 20 cm square in the y-z plane within 10 sec """
151 
152  # Cartesian end-effector pose that corresponds to the start joint
153  # configuration
154  start = CartesianTrajectoryPoint()
155  start.pose.position.x = 0.354
156  start.pose.position.y = 0.180
157  start.pose.position.z = 0.390
158  start.pose.orientation.x = 0.502
159  start.pose.orientation.y = 0.502
160  start.pose.orientation.z = 0.498
161  start.pose.orientation.w = 0.498
162 
163  duration = 10
164 
165  p1 = copy.deepcopy(start)
166  p1.pose.position.y += 0.2
167  p1.time_from_start = rospy.Duration(1.0 / 4 * duration)
168 
169  p2 = copy.deepcopy(p1)
170  p2.pose.position.z += 0.2
171  p2.time_from_start = rospy.Duration(2.0 / 4 * duration)
172 
173  p3 = copy.deepcopy(p2)
174  p3.pose.position.y -= 0.2
175  p3.time_from_start = rospy.Duration(3.0 / 4 * duration)
176 
177  p4 = copy.deepcopy(p3)
178  p4.pose.position.z -= 0.2
179  p4.time_from_start = rospy.Duration(4.0 / 4 * duration)
180 
181  goal = FollowCartesianTrajectoryGoal()
182  goal.trajectory.points.append(p1)
183  goal.trajectory.points.append(p2)
184  goal.trajectory.points.append(p3)
185  goal.trajectory.points.append(p4)
186 
187  action_client.send_goal(goal)
188  if wait_for_result:
189  action_client.wait_for_result()
190 
191 
192 if __name__ == '__main__':
193  import rostest
194  rostest.run(PKG, NAME, IntegrationTest, sys.argv)
def move_square(self, action_client, wait_for_result=True)


cartesian_trajectory_controller
Author(s):
autogenerated on Thu Feb 23 2023 03:10:48