test_controller.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 3d mouse tester module
4 '''
5 from __future__ import division
6 import unittest
7 import actionlib
8 import rospy
9 from moveit_msgs.msg import MoveGroupAction
10 from sensor_msgs.msg import Joy
11 from geometry_msgs.msg import PoseStamped
12 from geometry_msgs.msg import Pose
13 from sr_robot_commander.sr_arm_commander import SrArmCommander
14 
15 
16 PKG = 'mouse_test'
17 
18 
19 class MouseTester(unittest.TestCase):
20  '''
21  Main classmethod
22  '''
23  def setUp(self):
24  # init
25  rospy.init_node("mouse_tester", anonymous=True)
26  client = actionlib.SimpleActionClient('move_group', MoveGroupAction)
27  client.wait_for_server()
28  rospy.sleep(15.)
29  self.data = Joy()
30  self.target_pose = PoseStamped()
31  start_pose = Pose()
32  self.new_pose = PoseStamped()
33  # expected values
34  self.expvals = [[[0.3, 0.0, 1.1, 0, 0.7, 0.05, 0.05],
35  [0.51, 0.25, 1.1, 0.7, 0.71, 0.05, 0.048],
36  [0.51, 0.26, 1.1, 0.7, 0.71, 0.05, 0.048]],
37  [[0.27, 0.25, 1.1, 0.7, 0.7, 0.05, 0.05],
38  [0.27, 0.35, 1.1, 0.7, 0.7, 0.05, 0.05],
39  [0.27, 0.35, 1.1, 0.7, 0.7, 0.05, 0.05]],
40  [[0.27, 0.25, 1.1, 0.7, 0.7, 0.05, 0.05],
41  [0.27, 0.25, 0.9, 0.7, 0.7, 0.05, 0.05],
42  [0.27, 0.25, 0.85, 0.7, 0.7, 0.05, 0.05]],
43  [[0.27, 0.25, 1.1, 0.7, 0.7, 0.05, 0.05],
44  [0.27, 0.25, 1.1, 0.69, 0.7, 0.05, 0.1],
45  [0.27, 0.25, 1, 0.7, 0.7, 0.05, 0.1]],
46  [[0.27, 0.25, 1.1, 0.7, 0.7, 0.05, 0.05],
47  [0.27, 0.25, 1.1, 0.69, 0.69, 0.1, 0.1],
48  [0.27, 0.25, 1.1, 0.69, 0.69, 0.1, 0.1]],
49  [[0.27, 0.25, 1.1, 0.7, 0.7, 0.05, 0.05],
50  [0.27, 0.25, 1.1, 0.7, 0.6, 0.05, 0.08],
51  [0.27, 0.25, 1.1, 0.7, 0.6, 0.05, 0.05]]]
52  # initialize arm's position
53  self._arm_commander = SrArmCommander(name="right_arm", set_ground=True)
54  start_pose = self._arm_commander.get_current_pose()
55  self.new_pose.header.stamp = rospy.get_rostime()
56  self.new_pose.header.frame_id = "world"
57  self.new_pose.pose = start_pose
58 
59  self.new_pose.pose.position.x = 0.3
60  self.new_pose.pose.position.y = 0.25
61  self.new_pose.pose.position.z = 1.1
62  self.new_pose.pose.orientation.x = 0.7
63  self.new_pose.pose.orientation.y = 0.7
64  self.new_pose.pose.orientation.z = 0
65  self.new_pose.pose.orientation.w = 0
66  self.sub = rospy.Subscriber("command", PoseStamped, self.comparecall)
67  self.pub_arm = rospy.Publisher("command", PoseStamped, queue_size=1)
68  self.pub = rospy.Publisher("/spacenav/joy", Joy, queue_size=1)
69  # initialise joy publisher
70  self.data.buttons = [0, 0]
71  self.data.axes = [0, 0, 0, 0, 0, 0]
72  self.reset_joy = Joy()
73  self.reset_joy = self.data
74  self.pub.publish(self.data)
75  # move arm to initial position
76  self._arm_commander.move_to_pose_value_target_unsafe(self.new_pose,
77  avoid_collisions=True,
78  wait=False)
79  self.pub_arm.publish(self.new_pose)
80  # wait for two seconds to finish initialization
81  rospy.sleep(2)
82  self.tester()
83 
84  def tester(self):
85  "publishing data"
86 
87  rate = rospy.Rate(1)
88  for i in range(0, 6):
89  self.data.axes = [0, 0, 0, 0, 0, 0]
90  self.data.axes[i] = -0.08
91  for j in range(0, 3):
92  self.pub.publish(self.data)
93  self.data.axes[i] = self.data.axes[i] + 0.08
94  rate.sleep()
95  self.assertAlmostEqual(self.target_pose.pose.position.x,
96  abs(self.expvals[i][j][0]), msg="poseX failed", delta=2)
97  self.assertAlmostEqual(self.target_pose.pose.position.y,
98  abs(self.expvals[i][j][1]), msg="poseY failed", delta=2)
99  self.assertAlmostEqual(self.target_pose.pose.position.z,
100  abs(self.expvals[i][j][2]), msg="poseZ failed", delta=2)
101  self.assertAlmostEqual(self.target_pose.pose.orientation.x,
102  abs(self.expvals[i][j][3]), msg="rotX failed", delta=2)
103  self.assertAlmostEqual(self.target_pose.pose.orientation.y,
104  abs(self.expvals[i][j][4]), msg="rotY failed", delta=2)
105  self.assertAlmostEqual(self.target_pose.pose.orientation.z,
106  abs(self.expvals[i][j][5]), msg="rotZ failed", delta=2)
107  self.assertAlmostEqual(self.target_pose.pose.orientation.w,
108  abs(self.expvals[i][j][6]), msg="rotW failed", delta=2)
109  # reset joystic
110  self.reset_joy.axes = [0, 0, 0, 0, 0, 0]
111  self.pub.publish(self.reset_joy)
112 
113  def comparecall(self, data):
114  '''
115  checks the output of the controller
116  '''
117  self.target_pose = data
118 
119 if __name__ == '__main__':
120  import rostest
121  rostest.rosrun(PKG, "mouse_test", MouseTester)


sr_3dmouse
Author(s):
autogenerated on Tue Oct 13 2020 03:50:42