nextage_rtm_playpattern2.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # Software License Agreement (BSD License)
00005 #
00006 # Copyright (c) 2017, Tokyo Opensource Robotics Kyokai Association
00007 # All rights reserved.
00008 #
00009 # Redistribution and use in source and binary forms, with or without
00010 # modification, are permitted provided that the following conditions
00011 # are met:
00012 #
00013 #  * Redistributions of source code must retain the above copyright
00014 #    notice, this list of conditions and the following disclaimer.
00015 #  * Redistributions in binary form must reproduce the above
00016 #    copyright notice, this list of conditions and the following
00017 #    disclaimer in the documentation and/or other materials provided
00018 #    with the distribution.
00019 #  * Neither the name of Tokyo Opensource Robotics Kyokai Association. nor the
00020 #    names of its contributors may be used to endorse or promote products
00021 #    derived from this software without specific prior written permission.
00022 #
00023 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034 # POSSIBILITY OF SUCH DAMAGE.
00035 #
00036 # Author: Yosuke Yamamoto
00037 
00038 from nextage_ros_bridge import nextage_client
00039 import rospy
00040 import geometry_msgs.msg
00041 import copy, tf, math
00042 
00043 from moveit_msgs.srv import GetPositionIK
00044 from moveit_msgs.msg import PositionIKRequest
00045 
00046 import nextage_rtm_playpattern as nxtpp
00047 
00048 
00049 def setTargetPoseSequenceRTM(limb, pos_list, rpy_list, tm_list):
00050     '''
00051     Create a array of limb joint angles for the each waypoints
00052     from data of the end effector postions and postures
00053     using robot moving using RTM interface.
00054     
00055     @type  limb     : str
00056     @param limb     : limb to create a pattern, right arm 'rarm' or left arm 'larm'
00057     @type  pos_list : [[float,float,float],[float,float,float],...]
00058     @param pos_list : Array of end effector positions (x,y,z) [m]
00059     @type  rpy_list : [[float,float,float],[float,float,float],...]
00060     @param rpy_list : Array of end effector postures (r,p,y) [m]
00061     @type  tm_list  : [float,float,...]
00062     @param tm_list  : Array of motion time lengths of the each pose [s]
00063     
00064     @rtype  : [[float,float,...],[float,float,...],...]
00065     @return : Array of limb joint angles [rad]
00066     '''
00067     pattern_arm = []
00068     for pos, rpy, tm in zip(pos_list, rpy_list, tm_list):
00069         robot.setTargetPose(limb, pos, rpy, tm)
00070         robot.waitInterpolationOfGroup(limb)
00071         if limb == 'rarm':
00072             joint_angles_deg = robot.getJointAngles()[3:9]
00073         else:
00074             joint_angles_deg = robot.getJointAngles()[9:]
00075         joint_angles_rad = [math.radians(angle_in_degree) for angle_in_degree in joint_angles_deg]
00076         pattern_arm.append(joint_angles_rad)
00077 
00078     return pattern_arm
00079 
00080 def setTargetPoseSequenceMoveIt(limb, pos_list, rpy_list, tm_list):
00081     '''
00082     Create a array of limb joint angles for the each waypoints
00083     from data of the end effector postions and postures
00084     using MoveIt! IK for computing joint angles.
00085     
00086     @type  limb     : str
00087     @param limb     : limb to create a pattern, right arm 'rarm' or left arm 'larm'
00088     @type  pos_list : [[float,float,float],[float,float,float],...]
00089     @param pos_list : Array of end effector positions (x,y,z) [m]
00090     @type  rpy_list : [[float,float,float],[float,float,float],...]
00091     @param rpy_list : Array of end effector postures (r,p,y) [m]
00092     @type  tm_list  : [float,float,...]
00093     @param tm_list  : Array of motion time lengths of the each pose [s]
00094     
00095     @rtype  : [[float,float,...],[float,float,...],...]
00096     @return : Array of limb joint angles [rad]
00097     '''
00098     pattern_arm = []
00099     wpose = geometry_msgs.msg.Pose()
00100 
00101     rospy.wait_for_service('compute_ik')
00102     compute_ik = rospy.ServiceProxy('compute_ik', GetPositionIK)
00103 
00104     if limb == 'rarm':
00105         limb_name = "right_arm"  # Limb 'right_arm', 'left_arm'
00106     else:
00107         limb_name = "left_arm"
00108 
00109     joint_name = filter(lambda n:n[0] == limb, robot.Groups)[0][1]
00110 
00111     for pos, rpy, tm in zip(pos_list, rpy_list, tm_list):
00112         quaternion = tf.transformations.quaternion_from_euler(rpy[0], rpy[1], rpy[2])
00113         wpose.position.x = pos[0]
00114         wpose.position.y = pos[1]
00115         wpose.position.z = pos[2]
00116         wpose.orientation.x = quaternion[0]
00117         wpose.orientation.y = quaternion[1]
00118         wpose.orientation.z = quaternion[2]
00119         wpose.orientation.w = quaternion[3]
00120         req = PositionIKRequest()
00121         req.group_name = limb_name
00122         #req.ik_link_name = ''
00123         req.pose_stamped.pose = wpose
00124         ret = compute_ik(req)
00125         if ret.error_code.val != 1:
00126             error(ret)
00127 
00128         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))))
00129     return pattern_arm
00130 
00131 if __name__ == '__main__':
00132     '''
00133     Main sequence for moving at waypoints with RTM interface
00134     using MoveIt! IK for computing joint angles.
00135     '''
00136     robot = nextage_client.NextageClient()
00137     # #robot.init(robotname=robotname, url=modelfile)
00138     robot.init(robotname="HiroNX(Robot)0")#, url=modelfile)
00139 
00140     # go initial
00141     #robot.goInitial()
00142     
00143     positions_arm = []  # All elements are lists. E.g. pos1 = [x1, y1, z1]
00144     rpys_arm = []       # All elements are lists. E.g. rpy1 = [r1, p1, y1]
00145     time_list = []      # Time list [t1, t2, t3,...]
00146     limb_name = 'larm'  # Limb 'rarm', 'larm'
00147     
00148     # Rectangular Positions, RPYs, Time List
00149     rect_xyzs = nxtpp.rectangularPositions(dp_a=[0.25, 0.0, 0.1], dp_b=[0.45, 0.2, 0.1])
00150     rect_rpys = nxtpp.samePostureRPY([-3.073437, -1.569023, 3.073247], len(rect_xyzs))
00151     rect_time = nxtpp.equalTimeList(10.0, len(rect_xyzs))
00152     
00153     # Circular Positions, RPYs, Time List
00154     circ_xyzs = nxtpp.circularPositions(center=[0.35, 0.1, 0.1], radius=0.1 ,steps=12)
00155     circ_rpys = nxtpp.samePostureRPY([-3.073437, -1.569023, 3.073247], len(circ_xyzs))
00156     circ_time = nxtpp.equalTimeList(10.0, len(circ_xyzs))
00157 
00158     # Adjust Transition Time
00159     rect_time[0] += 1.0
00160     circ_time[0] += 1.0
00161     
00162     # Combine Patterns
00163     positions_arm = rect_xyzs + circ_xyzs
00164     rpys_arm      = rect_rpys + circ_rpys
00165     time_list     = rect_time + circ_time
00166 
00167     print('Limb: %s' % limb_name)
00168     print('Positions (%d): %s' % (len(positions_arm), positions_arm))
00169     print('RPY Pattern (%d): %s' % (len(rpys_arm), rpys_arm))
00170     print('Time List (%d): %s' % (len(time_list), time_list))    
00171     
00172     # Generate Joint Angle Pattern with MoveIt IK
00173     print('Generating Joint Angle Pattern')
00174     play_pattern_arm = setTargetPoseSequenceMoveIt(limb_name, positions_arm, rpys_arm, time_list)
00175     play_pattern_time = time_list
00176 
00177     print('Generated')
00178     print('Limb: %s' % limb_name)
00179     print('Joint Angle Pattern [rad]: %s' % play_pattern_arm)
00180     print('Motion Time Pattern [sec]: %s' % play_pattern_time)
00181 
00182     robot.goInitial()
00183 
00184     # Play Pattern - playPatternOfGroup()
00185     print('Start: Play Pattern')
00186     robot.playPatternOfGroup(limb_name, play_pattern_arm, play_pattern_time)
00187     robot.waitInterpolationOfGroup(limb_name)
00188     print('End: Play Pattern')
00189 
00190     robot.goInitial()


nextage_ros_bridge
Author(s): Isaac Isao Saito , Akio Ochiai
autogenerated on Wed May 15 2019 04:45:36