nextage_rtm_playpattern.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 math
00040 ###
00041 
00042 def circularPositions(center=[0.3, 0.2, 0.1], radius=0.1 ,steps=12):
00043     '''
00044     Create a array of circular position coordinates
00045     from a center position and a radius divided by steps.
00046     
00047     @type  center : [float, float, float]
00048     @param center : Position (x,y,z) [m] coordinates of a circle center
00049     @type  radius : float
00050     @param radius : Radius length [m]
00051     @type  steps  : int
00052     @param steps  : Number of steps for dividing a circle
00053     
00054     @rtype  : [ [float, float, float], [float, float, float], ...]
00055     @return : Array of circular position coordinates (x,y,z) [m]
00056               Length of steps+1 (including an end position same as the start position)
00057     '''
00058     positions_xyz = []
00059     step_rad = 2 * math.pi / steps
00060     for i in range(steps+1):
00061         ang_rad = step_rad * i
00062         px = center[0] - radius * math.cos(ang_rad)
00063         py = center[1] + radius * math.sin(ang_rad)
00064         pz = center[2]
00065         positions_xyz.append([px,py,pz])
00066     
00067     return positions_xyz
00068 
00069 
00070 def rectangularPositions(dp_a=[0.25, 0.0, 0.1], dp_b=[0.45, 0.2, 0.1]):
00071     '''
00072     Create a array of rectangular position coordinates
00073     from diagonal points of A and B.
00074     
00075     @type  dp_a : [float, float, float]
00076     @param dp_a : Position (x,y,z) [m] coordinates of a diagonal point A
00077     @type  dp_a : [float, float, float]
00078     @param dp_a : Position (x,y,z) [m] coordinates of a diagonal point B
00079     
00080     @rtype  : [ [float, float, float], [float, float, float], ...]
00081     @return : Array of rectangular position coordinates (x,y,z) [m]
00082               Length of 5 (including an end position same as the start position)
00083     '''
00084     positions_xyz = [
00085                      [ dp_a[0], dp_a[1], dp_a[2] ],
00086                      [ dp_a[0], dp_b[1], dp_a[2] ],
00087                      [ dp_b[0], dp_b[1], dp_b[2] ],
00088                      [ dp_b[0], dp_a[1], dp_b[2] ],
00089                      [ dp_a[0], dp_a[1], dp_a[2] ]
00090                     ]
00091     
00092     return positions_xyz
00093     
00094 
00095 def samePostureRPY(rpy, pat_length):
00096     '''
00097     Create a array of the same posture Roll, Pitch, Yaw angles
00098     from one set of Roll Pitch Yaw angles.
00099     
00100     @type  rpy        : [float, float, float]
00101     @param rpy        : Posture angle Roll, Pitch, Yaw (r,p,y) [rad] to copy
00102     @type  pat_length : int
00103     @param pat_length : Array length of pat_length for the same posture angles
00104     
00105     @rtype  : [ [float, float, float], [float, float, float], ...]
00106     @return : Array of the same posture angles (r,p,y) [rad]
00107               Length of pat_length
00108     '''
00109     posture_rpy = []
00110     for i in range(pat_length):
00111         posture_rpy.append(rpy)
00112     
00113     return posture_rpy
00114 
00115 
00116 def equalTimeList(whole_tm, pat_length):
00117     '''
00118     Create a array of the same time length
00119     from whole motion time.
00120     
00121     @type  whole_tm   : float
00122     @param whole_tm   : Whole time [s]
00123     @type  pat_length : int
00124     @param pat_length : Number to divide the whole time
00125     
00126     @rtype  : [ float, float, float, ... ]
00127     @return : Array of the equally divided whole time [s]
00128               Length of pat_length
00129     '''
00130     tm_list = []
00131     tm = whole_tm / pat_length
00132     for i in range(pat_length):
00133         tm_list.append(tm)
00134     
00135     return tm_list
00136 
00137 
00138 def setTargetPoseSequence(limb, pos_list, rpy_list, tm_list):
00139     '''
00140     Create a array of limb joint angles for the each waypoints
00141     from data of the end effector postions and postures
00142     using robot moving using RTM interface.
00143     
00144     @type  limb     : str
00145     @param limb     : limb to create a pattern, right arm 'rarm' or left arm 'larm'
00146     @type  pos_list : [[float,float,float],[float,float,float],...]
00147     @param pos_list : Array of end effector positions (x,y,z) [m]
00148     @type  rpy_list : [[float,float,float],[float,float,float],...]
00149     @param rpy_list : Array of end effector postures (r,p,y) [m]
00150     @type  tm_list  : [float,float,...]
00151     @param tm_list  : Array of motion time lengths of the each pose [s]
00152     
00153     @rtype  : [[float,float,...],[float,float,...],...]
00154     @return : Array of limb joint angles [rad]
00155     '''
00156     pattern_arm = []
00157     for pos, rpy, tm in zip(pos_list, rpy_list, tm_list):
00158         robot.setTargetPose(limb, pos, rpy, tm)
00159         robot.waitInterpolationOfGroup(limb)
00160         if limb == 'rarm':
00161             joint_angles_deg = robot.getJointAngles()[3:9]
00162         else:
00163             joint_angles_deg = robot.getJointAngles()[9:]
00164         joint_angles_rad = [math.radians(angle_in_degree) for angle_in_degree in joint_angles_deg]
00165         pattern_arm.append(joint_angles_rad)
00166 
00167     return pattern_arm
00168 
00169 ###
00170 
00171 from nextage_ros_bridge import nextage_client
00172 
00173 if __name__ == '__main__':
00174     '''
00175     Main sequence for moving at waypoints
00176     using RTM interface.
00177     '''
00178     robot = nextage_client.NextageClient()
00179     # #robot.init(robotname=robotname, url=modelfile)
00180     robot.init(robotname="HiroNX(Robot)0")#, url=modelfile)
00181 
00182     # go initial
00183     #robot.goInitial()
00184 
00185     # playPatternOfGroup() Example
00186     positions_arm = []  # All elements are lists. E.g. pos1 = [x1, y1, z1]
00187     rpys_arm = []       # All elements are lists. E.g. rpy1 = [r1, p1, y1]
00188     time_list = []      # Time list [t1, t2, t3,...]
00189     limb_name = 'larm'  # Limb 'rarm', 'larm'
00190 
00191     # Rectangular Positions, RPYs, Time List
00192     rect_xyzs = rectangularPositions(dp_a=[0.25, 0.0, 0.1], dp_b=[0.45, 0.2, 0.1])
00193     rect_rpys = samePostureRPY([-3.073437, -1.569023, 3.073247], len(rect_xyzs))
00194     rect_time = equalTimeList(10.0, len(rect_xyzs))
00195 
00196     # Circular Positions, RPYs, Time List
00197     circ_xyzs = circularPositions(center=[0.35, 0.1, 0.1], radius=0.1 ,steps=12)
00198     circ_rpys = samePostureRPY([-3.073437, -1.569023, 3.073247], len(circ_xyzs))
00199     circ_time = equalTimeList(10.0, len(circ_xyzs))
00200 
00201     # Adjust Transition Time
00202     rect_time[0] += 1.0
00203     circ_time[0] += 1.0
00204 
00205     # Combine Patterns
00206     positions_arm = rect_xyzs + circ_xyzs
00207     rpys_arm      = rect_rpys + circ_rpys
00208     time_list     = rect_time + circ_time
00209 
00210     print('Limb: %s' % limb_name)
00211     print('Positions (%d): %s' % (len(positions_arm), positions_arm))
00212     print('RPY Pattern (%d): %s' % (len(rpys_arm), rpys_arm))
00213     print('Time List (%d): %s' % (len(time_list), time_list))
00214 
00215     # Generate Joint Angle Pattern with Moving
00216     print('Generating Joint Angle Pattern')
00217     play_pattern_arm = setTargetPoseSequence(limb_name, positions_arm, rpys_arm, time_list)
00218     play_pattern_time = time_list
00219 
00220     print('Generated')
00221     print('Limb: %s' % limb_name)
00222     print('Joint Angle Pattern [rad]: %s' % play_pattern_arm)
00223     print('Motion Time Pattern [sec]: %s' % play_pattern_time)
00224 
00225     robot.goInitial()
00226 
00227     # Play Pattern - playPatternOfGroup()
00228     print('Start: Play Pattern')
00229     robot.playPatternOfGroup(limb_name, play_pattern_arm, play_pattern_time)
00230     robot.waitInterpolationOfGroup(limb_name)
00231     print('End: Play Pattern')
00232 
00233     robot.goInitial()
00234 


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