nextage_rtm_playpattern.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 math
40 ###
41 
42 def circularPositions(center=[0.3, 0.2, 0.1], radius=0.1 ,steps=12):
43  '''
44  Create a array of circular position coordinates
45  from a center position and a radius divided by steps.
46 
47  @type center : [float, float, float]
48  @param center : Position (x,y,z) [m] coordinates of a circle center
49  @type radius : float
50  @param radius : Radius length [m]
51  @type steps : int
52  @param steps : Number of steps for dividing a circle
53 
54  @rtype : [ [float, float, float], [float, float, float], ...]
55  @return : Array of circular position coordinates (x,y,z) [m]
56  Length of steps+1 (including an end position same as the start position)
57  '''
58  positions_xyz = []
59  step_rad = 2 * math.pi / steps
60  for i in range(steps+1):
61  ang_rad = step_rad * i
62  px = center[0] - radius * math.cos(ang_rad)
63  py = center[1] + radius * math.sin(ang_rad)
64  pz = center[2]
65  positions_xyz.append([px,py,pz])
66 
67  return positions_xyz
68 
69 
70 def rectangularPositions(dp_a=[0.25, 0.0, 0.1], dp_b=[0.45, 0.2, 0.1]):
71  '''
72  Create a array of rectangular position coordinates
73  from diagonal points of A and B.
74 
75  @type dp_a : [float, float, float]
76  @param dp_a : Position (x,y,z) [m] coordinates of a diagonal point A
77  @type dp_a : [float, float, float]
78  @param dp_a : Position (x,y,z) [m] coordinates of a diagonal point B
79 
80  @rtype : [ [float, float, float], [float, float, float], ...]
81  @return : Array of rectangular position coordinates (x,y,z) [m]
82  Length of 5 (including an end position same as the start position)
83  '''
84  positions_xyz = [
85  [ dp_a[0], dp_a[1], dp_a[2] ],
86  [ dp_a[0], dp_b[1], dp_a[2] ],
87  [ dp_b[0], dp_b[1], dp_b[2] ],
88  [ dp_b[0], dp_a[1], dp_b[2] ],
89  [ dp_a[0], dp_a[1], dp_a[2] ]
90  ]
91 
92  return positions_xyz
93 
94 
95 def samePostureRPY(rpy, pat_length):
96  '''
97  Create a array of the same posture Roll, Pitch, Yaw angles
98  from one set of Roll Pitch Yaw angles.
99 
100  @type rpy : [float, float, float]
101  @param rpy : Posture angle Roll, Pitch, Yaw (r,p,y) [rad] to copy
102  @type pat_length : int
103  @param pat_length : Array length of pat_length for the same posture angles
104 
105  @rtype : [ [float, float, float], [float, float, float], ...]
106  @return : Array of the same posture angles (r,p,y) [rad]
107  Length of pat_length
108  '''
109  posture_rpy = []
110  for i in range(pat_length):
111  posture_rpy.append(rpy)
112 
113  return posture_rpy
114 
115 
116 def equalTimeList(whole_tm, pat_length):
117  '''
118  Create a array of the same time length
119  from whole motion time.
120 
121  @type whole_tm : float
122  @param whole_tm : Whole time [s]
123  @type pat_length : int
124  @param pat_length : Number to divide the whole time
125 
126  @rtype : [ float, float, float, ... ]
127  @return : Array of the equally divided whole time [s]
128  Length of pat_length
129  '''
130  tm_list = []
131  tm = whole_tm / pat_length
132  for i in range(pat_length):
133  tm_list.append(tm)
134 
135  return tm_list
136 
137 
138 def setTargetPoseSequence(limb, pos_list, rpy_list, tm_list):
139  '''
140  Create a array of limb joint angles for the each waypoints
141  from data of the end effector postions and postures
142  using robot moving using RTM interface.
143 
144  @type limb : str
145  @param limb : limb to create a pattern, right arm 'rarm' or left arm 'larm'
146  @type pos_list : [[float,float,float],[float,float,float],...]
147  @param pos_list : Array of end effector positions (x,y,z) [m]
148  @type rpy_list : [[float,float,float],[float,float,float],...]
149  @param rpy_list : Array of end effector postures (r,p,y) [m]
150  @type tm_list : [float,float,...]
151  @param tm_list : Array of motion time lengths of the each pose [s]
152 
153  @rtype : [[float,float,...],[float,float,...],...]
154  @return : Array of limb joint angles [rad]
155  '''
156  pattern_arm = []
157  for pos, rpy, tm in zip(pos_list, rpy_list, tm_list):
158  robot.setTargetPose(limb, pos, rpy, tm)
159  robot.waitInterpolationOfGroup(limb)
160  if limb == 'rarm':
161  joint_angles_deg = robot.getJointAngles()[3:9]
162  else:
163  joint_angles_deg = robot.getJointAngles()[9:]
164  joint_angles_rad = [math.radians(angle_in_degree) for angle_in_degree in joint_angles_deg]
165  pattern_arm.append(joint_angles_rad)
166 
167  return pattern_arm
168 
169 ###
170 
171 from nextage_ros_bridge import nextage_client
172 
173 if __name__ == '__main__':
174  '''
175  Main sequence for moving at waypoints
176  using RTM interface.
177  '''
178  robot = nextage_client.NextageClient()
179  # #robot.init(robotname=robotname, url=modelfile)
180  robot.init(robotname="HiroNX(Robot)0")#, url=modelfile)
181 
182  # go initial
183  #robot.goInitial()
184 
185  # playPatternOfGroup() Example
186  positions_arm = [] # All elements are lists. E.g. pos1 = [x1, y1, z1]
187  rpys_arm = [] # All elements are lists. E.g. rpy1 = [r1, p1, y1]
188  time_list = [] # Time list [t1, t2, t3,...]
189  limb_name = 'larm' # Limb 'rarm', 'larm'
190 
191  # Rectangular Positions, RPYs, Time List
192  rect_xyzs = rectangularPositions(dp_a=[0.25, 0.0, 0.1], dp_b=[0.45, 0.2, 0.1])
193  rect_rpys = samePostureRPY([-3.073437, -1.569023, 3.073247], len(rect_xyzs))
194  rect_time = equalTimeList(10.0, len(rect_xyzs))
195 
196  # Circular Positions, RPYs, Time List
197  circ_xyzs = circularPositions(center=[0.35, 0.1, 0.1], radius=0.1 ,steps=12)
198  circ_rpys = samePostureRPY([-3.073437, -1.569023, 3.073247], len(circ_xyzs))
199  circ_time = equalTimeList(10.0, len(circ_xyzs))
200 
201  # Adjust Transition Time
202  rect_time[0] += 1.0
203  circ_time[0] += 1.0
204 
205  # Combine Patterns
206  positions_arm = rect_xyzs + circ_xyzs
207  rpys_arm = rect_rpys + circ_rpys
208  time_list = rect_time + circ_time
209 
210  print('Limb: %s' % limb_name)
211  print('Positions (%d): %s' % (len(positions_arm), positions_arm))
212  print('RPY Pattern (%d): %s' % (len(rpys_arm), rpys_arm))
213  print('Time List (%d): %s' % (len(time_list), time_list))
214 
215  # Generate Joint Angle Pattern with Moving
216  print('Generating Joint Angle Pattern')
217  play_pattern_arm = setTargetPoseSequence(limb_name, positions_arm, rpys_arm, time_list)
218  play_pattern_time = time_list
219 
220  print('Generated')
221  print('Limb: %s' % limb_name)
222  print('Joint Angle Pattern [rad]: %s' % play_pattern_arm)
223  print('Motion Time Pattern [sec]: %s' % play_pattern_time)
224 
225  robot.goInitial()
226 
227  # Play Pattern - playPatternOfGroup()
228  print('Start: Play Pattern')
229  robot.playPatternOfGroup(limb_name, play_pattern_arm, play_pattern_time)
230  robot.waitInterpolationOfGroup(limb_name)
231  print('End: Play Pattern')
232 
233  robot.goInitial()
234 
def equalTimeList(whole_tm, pat_length)
def rectangularPositions(dp_a=[0.25, dp_b=[0.45)
def setTargetPoseSequence(limb, pos_list, rpy_list, tm_list)
def samePostureRPY(rpy, pat_length)
def circularPositions(center=[0.3, radius=0.1, steps=12)
bool init()


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