nextage_rtm_playpattern2.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) 2017, Tokyo Opensource Robotics Kyokai Association
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 Tokyo Opensource Robotics Kyokai Association. nor the
20 # names of its contributors may be used to endorse or promote products
21 # derived 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 # Author: Yosuke Yamamoto
37 
38 from nextage_ros_bridge import nextage_client
39 import rospy
40 import geometry_msgs.msg
41 import copy, tf, math
42 
43 from moveit_msgs.srv import GetPositionIK
44 from moveit_msgs.msg import PositionIKRequest
45 
46 import nextage_rtm_playpattern as nxtpp
47 
48 
49 def setTargetPoseSequenceRTM(limb, pos_list, rpy_list, tm_list):
50  '''
51  Create a array of limb joint angles for the each waypoints
52  from data of the end effector postions and postures
53  using robot moving using RTM interface.
54 
55  @type limb : str
56  @param limb : limb to create a pattern, right arm 'rarm' or left arm 'larm'
57  @type pos_list : [[float,float,float],[float,float,float],...]
58  @param pos_list : Array of end effector positions (x,y,z) [m]
59  @type rpy_list : [[float,float,float],[float,float,float],...]
60  @param rpy_list : Array of end effector postures (r,p,y) [m]
61  @type tm_list : [float,float,...]
62  @param tm_list : Array of motion time lengths of the each pose [s]
63 
64  @rtype : [[float,float,...],[float,float,...],...]
65  @return : Array of limb joint angles [rad]
66  '''
67  pattern_arm = []
68  for pos, rpy, tm in zip(pos_list, rpy_list, tm_list):
69  robot.setTargetPose(limb, pos, rpy, tm)
70  robot.waitInterpolationOfGroup(limb)
71  if limb == 'rarm':
72  joint_angles_deg = robot.getJointAngles()[3:9]
73  else:
74  joint_angles_deg = robot.getJointAngles()[9:]
75  joint_angles_rad = [math.radians(angle_in_degree) for angle_in_degree in joint_angles_deg]
76  pattern_arm.append(joint_angles_rad)
77 
78  return pattern_arm
79 
80 def setTargetPoseSequenceMoveIt(limb, pos_list, rpy_list, tm_list):
81  '''
82  Create a array of limb joint angles for the each waypoints
83  from data of the end effector postions and postures
84  using MoveIt! IK for computing joint angles.
85 
86  @type limb : str
87  @param limb : limb to create a pattern, right arm 'rarm' or left arm 'larm'
88  @type pos_list : [[float,float,float],[float,float,float],...]
89  @param pos_list : Array of end effector positions (x,y,z) [m]
90  @type rpy_list : [[float,float,float],[float,float,float],...]
91  @param rpy_list : Array of end effector postures (r,p,y) [m]
92  @type tm_list : [float,float,...]
93  @param tm_list : Array of motion time lengths of the each pose [s]
94 
95  @rtype : [[float,float,...],[float,float,...],...]
96  @return : Array of limb joint angles [rad]
97  '''
98  pattern_arm = []
99  wpose = geometry_msgs.msg.Pose()
100 
101  rospy.wait_for_service('compute_ik')
102  compute_ik = rospy.ServiceProxy('compute_ik', GetPositionIK)
103 
104  if limb == 'rarm':
105  limb_name = "right_arm" # Limb 'right_arm', 'left_arm'
106  else:
107  limb_name = "left_arm"
108 
109  joint_name = filter(lambda n:n[0] == limb, robot.Groups)[0][1]
110 
111  for pos, rpy, tm in zip(pos_list, rpy_list, tm_list):
112  quaternion = tf.transformations.quaternion_from_euler(rpy[0], rpy[1], rpy[2])
113  wpose.position.x = pos[0]
114  wpose.position.y = pos[1]
115  wpose.position.z = pos[2]
116  wpose.orientation.x = quaternion[0]
117  wpose.orientation.y = quaternion[1]
118  wpose.orientation.z = quaternion[2]
119  wpose.orientation.w = quaternion[3]
120  req = PositionIKRequest()
121  req.group_name = limb_name
122  #req.ik_link_name = ''
123  req.pose_stamped.pose = wpose
124  ret = compute_ik(req)
125  if ret.error_code.val != 1:
126  error(ret)
127 
128  pattern_arm.append(map(lambda n: n[1],filter(lambda n:n[0] in joint_name, zip(ret.solution.joint_state.name, ret.solution.joint_state.position))))
129  return pattern_arm
130 
131 if __name__ == '__main__':
132  '''
133  Main sequence for moving at waypoints with RTM interface
134  using MoveIt! IK for computing joint angles.
135  '''
136  robot = nextage_client.NextageClient()
137  # #robot.init(robotname=robotname, url=modelfile)
138  robot.init(robotname="HiroNX(Robot)0")#, url=modelfile)
139 
140  # go initial
141  #robot.goInitial()
142 
143  positions_arm = [] # All elements are lists. E.g. pos1 = [x1, y1, z1]
144  rpys_arm = [] # All elements are lists. E.g. rpy1 = [r1, p1, y1]
145  time_list = [] # Time list [t1, t2, t3,...]
146  limb_name = 'larm' # Limb 'rarm', 'larm'
147 
148  # Rectangular Positions, RPYs, Time List
149  rect_xyzs = nxtpp.rectangularPositions(dp_a=[0.25, 0.0, 0.1], dp_b=[0.45, 0.2, 0.1])
150  rect_rpys = nxtpp.samePostureRPY([-3.073437, -1.569023, 3.073247], len(rect_xyzs))
151  rect_time = nxtpp.equalTimeList(10.0, len(rect_xyzs))
152 
153  # Circular Positions, RPYs, Time List
154  circ_xyzs = nxtpp.circularPositions(center=[0.35, 0.1, 0.1], radius=0.1 ,steps=12)
155  circ_rpys = nxtpp.samePostureRPY([-3.073437, -1.569023, 3.073247], len(circ_xyzs))
156  circ_time = nxtpp.equalTimeList(10.0, len(circ_xyzs))
157 
158  # Adjust Transition Time
159  rect_time[0] += 1.0
160  circ_time[0] += 1.0
161 
162  # Combine Patterns
163  positions_arm = rect_xyzs + circ_xyzs
164  rpys_arm = rect_rpys + circ_rpys
165  time_list = rect_time + circ_time
166 
167  print('Limb: %s' % limb_name)
168  print('Positions (%d): %s' % (len(positions_arm), positions_arm))
169  print('RPY Pattern (%d): %s' % (len(rpys_arm), rpys_arm))
170  print('Time List (%d): %s' % (len(time_list), time_list))
171 
172  # Generate Joint Angle Pattern with MoveIt IK
173  print('Generating Joint Angle Pattern')
174  play_pattern_arm = setTargetPoseSequenceMoveIt(limb_name, positions_arm, rpys_arm, time_list)
175  play_pattern_time = time_list
176 
177  print('Generated')
178  print('Limb: %s' % limb_name)
179  print('Joint Angle Pattern [rad]: %s' % play_pattern_arm)
180  print('Motion Time Pattern [sec]: %s' % play_pattern_time)
181 
182  robot.goInitial()
183 
184  # Play Pattern - playPatternOfGroup()
185  print('Start: Play Pattern')
186  robot.playPatternOfGroup(limb_name, play_pattern_arm, play_pattern_time)
187  robot.waitInterpolationOfGroup(limb_name)
188  print('End: Play Pattern')
189 
190  robot.goInitial()
def setTargetPoseSequenceRTM(limb, pos_list, rpy_list, tm_list)
def setTargetPoseSequenceMoveIt(limb, pos_list, rpy_list, tm_list)
bool init()


nextage_ros_bridge
Author(s): Isaac Isao Saito , Akio Ochiai
autogenerated on Wed Jun 17 2020 04:14:47