test_khi_robot_control.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding:utf-8
3 
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2019, Kawasaki Heavy Industries, LTD.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of the Willow Garage nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
35 
36 PKG = 'khi_robot_test'
37 import roslib; roslib.load_manifest(PKG)
38 
39 import copy
40 import math
41 import sys
42 import unittest
43 import rospy
44 import moveit_commander
45 import geometry_msgs.msg
46 from khi_robot_msgs.srv import *
47 from rospkg import RosPack, ResourceNotFound
48 
49 if rospy.has_param('/test_group_name'):
50  gn = '/' + rospy.get_param('/test_group_name')
51 else:
52  gn = ''
53 service = gn+'/khi_robot_command_service'
54 planner = 'RRTConnectkConfigDefault'
55 
56 class KhiRobot:
57  arm_name = ''
58  arm_num = 1
59  max_jt = 6
60  group = gn+'/manipulator'
61  min_pos_list = []
62  max_pos_list = []
63  max_vel_list = []
64  max_acc_list = []
65  base_pos_list = []
66 
67  def __init__(self):
68  self.min_pos_list = []
69  self.max_pos_list = []
70  self.max_vel_list = []
71  self.max_acc_list = []
72  self.arm_name = rospy.get_param(gn+'/khi_robot_param/robot')
73  limits = rospy.get_param(gn+'/'+self.arm_name+'/joint_limits')
74  if self.arm_name == 'WD002N':
75  self.arm_num = 2
76  self.max_jt = 8
77  self.group = 'botharms'
78  self.base_pos_list = [ -90*math.pi/180, 0, 0, 0, 90*math.pi/180, 0, 0, 0 ]
79  for jt in range(self.max_jt):
80  if jt < 4:
81  self.min_pos_list.append(limits['lower_joint'+str(jt+1)]['min_position'])
82  self.max_pos_list.append(limits['lower_joint'+str(jt+1)]['max_position'])
83  self.max_acc_list.append(limits['lower_joint'+str(jt+1)]['max_acceleration'])
84  self.max_vel_list.append(limits['lower_joint'+str(jt+1)]['max_velocity'])
85  else:
86  self.min_pos_list.append(limits['upper_joint'+str(jt-3)]['min_position'])
87  self.max_pos_list.append(limits['upper_joint'+str(jt-3)]['max_position'])
88  self.max_acc_list.append(limits['upper_joint'+str(jt-3)]['max_acceleration'])
89  self.max_vel_list.append(limits['upper_joint'+str(jt-3)]['max_velocity'])
90  else:
91  self.arm_num = 1
92  self.max_jt = 6
93  self.group = 'manipulator'
94  self.base_pos_list = [ 90*math.pi/180, 0, 90*math.pi/180, 0, 90*math.pi/180, 0 ]
95  for jt in range(self.max_jt):
96  self.min_pos_list.append(limits['joint'+str(jt+1)]['min_position'])
97  self.max_pos_list.append(limits['joint'+str(jt+1)]['max_position'])
98  self.max_vel_list.append(limits['joint'+str(jt+1)]['max_velocity'])
99  self.max_acc_list.append(limits['joint'+str(jt+1)]['max_acceleration'])
100 
101  def get_pos_list(self, ano, jt, type):
102  jt_list = copy.deepcopy(self.base_pos_list)
103  if self.arm_name.startswith('RS'):
104  if type == 'min':
105  if jt+1 == 2:
106  jt_list[2] = -130*math.pi/180
107  jt_list[jt] = self.min_pos_list[jt]
108  else:
109  if jt+1 == 2:
110  jt_list[2] = 130*math.pi/180
111  jt_list[jt] = self.max_pos_list[jt]
112  elif self.arm_name.startswith('WD'):
113  if type == 'min':
114  if jt+1 == 5:
115  jt_list[0] = -170*math.pi/180
116  jt_list[jt] = self.min_pos_list[jt]
117  else:
118  if jt+1 == 1:
119  jt_list[4] = 220*math.pi/180
120  elif jt+1 == 5:
121  jt_list[0] = 170*math.pi/180
122  jt_list[jt] = self.max_pos_list[jt]
123 
124  return jt_list
125 
126 def cmdhandler_client(type_arg , cmd_arg):
127  rospy.wait_for_service(service)
128  try:
129  khi_robot_command_service = rospy.ServiceProxy(service,KhiRobotCmd)
130  resp1 = khi_robot_command_service(type_arg, cmd_arg)
131  return resp1
132  except rospy.ServiceException as e:
133  rospy.loginfo('Service call failed: %s', e)
134 
136  ret = cmdhandler_client('driver' , 'get_status')
137  return ret
138 
139 def plan_and_execute(mgc,jt,num,timeout):
140  for cnt in range(timeout):
141  # checking the version of the 'moveit_core' package
142  (major,minor,bugfix) = get_package_version('moveit_commander')
143  if major > 1 or ( major == 1 and minor >= 1 ):
144  (flag, trajectory, plannint_time, error_code) = mgc.plan()
145  if flag is False:
146  rospy.loginfo('JT%d-%d: cannot be planned %d times', jt+1, num+1, cnt+1)
147  else:
148  ret = mgc.execute(trajectory)
149  if ret is False:
150  rospy.loginfo('JT%d-%d: cannot be executed %d times', jt+1, num+1, cnt+1)
151  else:
152  return True
153  else:
154  plan = mgc.plan()
155  if plan is False:
156  rospy.loginfo('JT%d-%d: cannot be planned %d times', jt+1, num+1, cnt+1)
157  else:
158  ret = mgc.execute(plan)
159  if ret is False:
160  rospy.loginfo('JT%d-%d: cannot be executed %d times', jt+1, num+1, cnt+1)
161  else:
162  return True
163 
164  if cnt == timeout-1:
165  rospy.loginfo('timeout')
166  return False
167  return False
168 
169 def get_package_version(packagename):
170  rp = RosPack()
171  try:
172  manifest = rp.get_manifest(packagename)
173  return tuple(map(int,manifest.version.split('.')))
174  except ResourceNotFound:
175  return tuple()
176 
177 class TestKhiRobotControl(unittest.TestCase):
179  ##############################set parameter############################
180  accuracy_jt = 0.01 # joint accuracy
181  #accuracy_pos = 0.01 # position accuracy
182  #accuracy_ori = 0.01 # orientation accuracy
183 
184  max_vel = 1.0 # max velocity scale
185  max_acc = 1.0 # max acceleration scale
186  min_vel = 0.5 # min velocity scale
187  min_acc = 0.5 # min acceleration scale
188 
189  cyc_num = 3 # reperat num
190  timeout = 10 # timeout num
191  retcode = 0
192  ########################################################################
193 
194  ######################################### set range of move #####################################################
195  khi_robot = KhiRobot()
196 
197  # RobotCommander
198  rc = moveit_commander.RobotCommander()
199 
200  # MoveGroupCommander
201  mgc = moveit_commander.MoveGroupCommander(khi_robot.group)
202 
203  # mgc setting
204  mgc.set_planner_id(planner)
205  mgc.set_goal_joint_tolerance(accuracy_jt)
206  #mgc.set_goal_position_tolerance(accuracy_pos)
207  #mgc.set_goal_orientation_tolerance(accuracy_ori)
208 
209  ret = get_driver_state()
210  if ret.cmd_ret == 'ERROR':
211  cmdhandler_client('driver', 'restart')
212  rospy.sleep(3)
213 
214  ret = get_driver_state()
215  self.assertEqual('ACTIVE', ret.cmd_ret)
216 
217  # move each joint
218  for jt in range(khi_robot.max_jt):
219  if khi_robot.arm_name == 'WD002N':
220  if jt < 4:
221  ano = 0
222  else:
223  ano = 1
224  else:
225  ano = 0
226 
227  # min position/velocity/acceleration
228  mgc.set_max_velocity_scaling_factor(min_vel)
229  mgc.set_max_acceleration_scaling_factor(min_acc)
230  jt_list = khi_robot.get_pos_list(ano, jt, 'min')
231  rospy.loginfo('JT%d : base', jt+1)
232  mgc.set_joint_value_target(jt_list)
233  self.assertTrue(plan_and_execute(mgc,jt,-1,timeout))
234 
235  # max velocity/acceleration
236  mgc.set_max_velocity_scaling_factor(max_vel)
237  mgc.set_max_acceleration_scaling_factor(max_acc)
238 
239  # do repeat moving
240  for num in range(cyc_num):
241  # max position
242  jt_list = khi_robot.get_pos_list(ano, jt, 'max')
243  rospy.loginfo('JT%d-%d: max ', jt+1, num+1)
244  mgc.set_joint_value_target(jt_list)
245  retcode = plan_and_execute(mgc,jt,num,timeout)
246  self.assertTrue(retcode)
247  if retcode == False:
248  rospy.loginfo('JT%d-%d: max faild.', jt+1, num+1)
249  break
250  now_jt_list = mgc.get_current_joint_values()
251  self.assertAlmostEqual(jt_list[jt], now_jt_list[jt], delta = accuracy_jt*2)
252 
253  # min position
254  jt_list = khi_robot.get_pos_list(ano, jt, 'min')
255  rospy.loginfo('JT%d-%d: min', jt+1, num+1)
256  mgc.set_joint_value_target(jt_list)
257  retcode = plan_and_execute(mgc,jt,1,timeout)
258  self.assertTrue(retcode)
259  if retcode == False:
260  rospy.loginfo('JT%d-%d: min faild.', jt+1, num+1)
261  break
262  now_jt_list = mgc.get_current_joint_values()
263  self.assertAlmostEqual(jt_list[jt], now_jt_list[jt], delta = accuracy_jt*2)
264 
265  # state check
266  ret = get_driver_state()
267  self.assertEqual('ACTIVE', ret.cmd_ret)
268  if ret.cmd_ret != 'ACTIVE':
269  rospy.loginfo('JT%d-%d: not active', jt+1, num+1)
270  break
271 
272  # finish repeat moveing
273  ret = get_driver_state()
274  self.assertEqual('ACTIVE', ret.cmd_ret)
275  if ret.cmd_ret != 'ACTIVE':
276  break
277 
278  def test_state(self):
279  cmdhandler_client('driver', 'restart')
280  rospy.sleep(3)
281  ret = get_driver_state()
282  self.assertEqual('ACTIVE', ret.cmd_ret)
283 
284  cmdhandler_client('driver', 'hold')
285  rospy.sleep(3)
286  ret = get_driver_state()
287  self.assertEqual('HOLDED', ret.cmd_ret)
288 
289  cmdhandler_client('driver', 'restart')
290  rospy.sleep(3)
291  ret = get_driver_state()
292  self.assertEqual('ACTIVE', ret.cmd_ret)
293 
294 if __name__ == '__main__':
295  import rostest
296  cmdhandler_client('driver', 'restart')
297  rospy.init_node("test_khi_robot_control_node")
298  rostest.rosrun(PKG, 'test_khi_robot_control', TestKhiRobotControl)
def get_package_version(packagename)
def get_pos_list(self, ano, jt, type)
def plan_and_execute(mgc, jt, num, timeout)
def cmdhandler_client(type_arg, cmd_arg)


khi_robot_test
Author(s): nakamichi_d
autogenerated on Fri Mar 26 2021 02:34:39