tuck_arms_main.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2009, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of the Willow Garage nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 # Author: Wim Meeussen
35 # Modified by Jonathan Bohren to be an action and for untucking
36 
37 
38 import signal
39 
40 import rospy
41 
42 import os
43 import sys
44 import time
45 import math
46 
47 from trajectory_msgs.msg import *
48 from actionlib_msgs.msg import *
49 from pr2_controllers_msgs.msg import *
50 from pr2_common_action_msgs.msg import *
51 import getopt
52 import actionlib
53 
54 
55 # Joint names
56 joint_names = ["shoulder_pan",
57  "shoulder_lift",
58  "upper_arm_roll",
59  "elbow_flex",
60  "forearm_roll",
61  "wrist_flex",
62  "wrist_roll" ]
63 
64 
65 l_arm_tucked = [0.06024, 1.248526, 1.789070, -1.683386, -1.7343417, -0.0962141, -0.0864407]
66 r_arm_tucked = [-0.023593, 1.1072800, -1.5566882, -2.124408, -1.4175, -1.8417, 0.21436]
67 l_arm_untucked = [ 0.4, 1.0, 0.0, -2.05, 0.0, -0.1, 0.0]
68 r_arm_untucked = [-0.4, 1.0, 0.0, -2.05, 0.0, -0.1, 0.0]
69 r_arm_approach = [0.039, 1.1072, 0.0, -2.067, -1.231, -1.998, 0.369]
70 r_arm_up_traj = [[-0.4, 0.0, 0.0, -2.05, 0.0, -0.1, 0.0]]
71 
72 # Tuck trajectory
73 l_arm_tuck_traj = [l_arm_tucked]
74 r_arm_tuck_traj = [r_arm_approach,
75  r_arm_tucked]
76 
77 # Untuck trajctory
78 l_arm_untuck_traj = [l_arm_untucked]
79 r_arm_untuck_traj = [r_arm_approach,
80  r_arm_untucked]
81 
82 # clear trajectory
83 l_arm_clear_traj = [l_arm_untucked]
84 r_arm_clear_traj = [r_arm_untucked]
85 
87  def __init__(self, node_name):
88  self.r_received = False
89  self.l_received = False
90 
91  self.node_name = node_name
92 
93  # arm state: -1 unknown, 0 tucked, 1 untucked
94  self.l_arm_state = -1
95  self.r_arm_state = -1
96  self.success = True
97 
98  # Get controller name and start joint trajectory action clients
99  self.move_duration = rospy.get_param('~move_duration', 2.5)
100  r_action_name = rospy.get_param('~r_joint_trajectory_action', 'r_arm_controller/joint_trajectory_action')
101  l_action_name = rospy.get_param('~l_joint_trajectory_action', 'l_arm_controller/joint_trajectory_action')
102  self.left_joint_client = client = actionlib.SimpleActionClient(l_action_name, JointTrajectoryAction)
103  self.right_joint_client = client = actionlib.SimpleActionClient(r_action_name, JointTrajectoryAction)
104 
105  # Connect to controller state
106  rospy.Subscriber('l_arm_controller/state', JointTrajectoryControllerState ,self.stateCb)
107  rospy.Subscriber('r_arm_controller/state', JointTrajectoryControllerState ,self.stateCb)
108 
109  # Wait for joint clients to connect with timeout
110  if not self.left_joint_client.wait_for_server(rospy.Duration(30)):
111  rospy.logerr("pr2_tuck_arms: left_joint_client action server did not come up within timelimit")
112  if not self.right_joint_client.wait_for_server(rospy.Duration(30)):
113  rospy.logerr("pr2_tuck_arms: right_joint_client action server did not come up within timelimit")
114 
115  # Construct action server
117 
118 
119  def executeCB(self, goal):
120  # Make sure we received arm state
121  while not self.r_received or not self.l_received:
122  rospy.sleep(0.1)
123  if rospy.is_shutdown():
124  return
125 
126  # Create a new result
127  result = TuckArmsResult()
128  result.tuck_left = True
129  result.tuck_right = True
130 
131  # Tucking left and right arm
132  if goal.tuck_right and goal.tuck_left:
133  rospy.loginfo('Tucking both arms...')
134  self.tuckL()
135  self.tuckR()
136 
137  # Tucking left arm, untucking right arm
138  elif goal.tuck_left and not goal.tuck_right:
139  rospy.loginfo('Tucking left arm and untucking right arm...')
140  self.untuckR()
141  self.tuckL()
142 
143  # Tucking right arm, untucking left arm
144  if goal.tuck_right and not goal.tuck_left:
145  rospy.loginfo('Tucking right arm and untucking left arm...')
146  self.untuckL()
147  self.tuckR()
148 
149  # UnTucking both arms
150  if not goal.tuck_right and not goal.tuck_left:
151  rospy.loginfo("Untucking both arms...")
152  self.untuckL()
153  self.untuckR()
154 
155  # Succeed or fail
156  if self.success:
157  result.tuck_right = goal.tuck_right
158  result.tuck_left = goal.tuck_left
159  self.action_server.set_succeeded(result)
160  else:
161  rospy.logerr("Tuck or untuck arms FAILED: Right value: %d. Left value: %d" % (result.tuck_left, result.tuck_right))
162  result.tuck_right = (self.r_arm_state == 0)
163  result.tuck_left = (self.l_arm_state == 0)
164  self.action_server.set_aborted(result)
165 
166 
167  # clears r arm and l arm
168  def tuckL(self):
169  if self.l_arm_state != 0:
170  self.go('r', r_arm_up_traj)
171  if self.l_arm_state != 1:
172  self.go('l', l_arm_clear_traj)
173  self.go('l', l_arm_tuck_traj)
174  self.go('r', r_arm_clear_traj)
175 
176  # clears r arm
177  def untuckL(self):
178  if self.l_arm_state != 1:
179  self.go('r', r_arm_up_traj)
180  if self.l_arm_state == 0:
181  self.go('l', l_arm_untuck_traj)
182  elif self.l_arm_state == -1:
183  self.go('l', l_arm_clear_traj)
184 
185  # assumes l tucked or cleared
186  def tuckR(self):
187  if self.r_arm_state != 0:
188  self.go('r', r_arm_tuck_traj)
189 
190  # assumes l tucked or cleared
191  def untuckR(self):
192  if self.r_arm_state == 0:
193  self.go('r', r_arm_untuck_traj)
194  elif self.r_arm_state == -1:
195  self.go('r', r_arm_clear_traj)
196 
197  def go(self, side, positions, wait = True):
198  goal = JointTrajectoryGoal()
199  goal.trajectory.joint_names = [side+"_"+name+"_joint" for name in joint_names]
200  goal.trajectory.points = []
201  for p, count in zip(positions, range(0,len(positions)+1)):
202  goal.trajectory.points.append(JointTrajectoryPoint( positions = p,
203  velocities = [],
204  accelerations = [],
205  time_from_start = rospy.Duration((count+1) * self.move_duration)))
206  goal.trajectory.header.stamp = rospy.get_rostime() + rospy.Duration(0.01)
207  if wait:
208  if not {'l': self.left_joint_client, 'r': self.right_joint_client}[side].send_goal_and_wait(goal, rospy.Duration(30.0), rospy.Duration(5.0)):
209  self.success = False
210  else:
211  {'l': self.left_joint_client, 'r': self.right_joint_client}[side].send_goal(goal)
212 
213 
214  # Returns angle between -pi and + pi
215  def angleWrap(self, angle):
216  while angle > math.pi:
217  angle -= math.pi*2.0
218  while angle < -math.pi:
219  angle += math.pi*2.0
220  return angle
221 
222 
223  def stateCb(self, msg):
224  l_sum_tucked = 0
225  l_sum_untucked = 0
226  r_sum_tucked = 0
227  r_sum_untucked = 0
228  for name_state, name_desi, value_state, value_l_tucked, value_l_untucked, value_r_tucked, value_r_untucked in zip(msg.joint_names, joint_names, msg.actual.positions , l_arm_tucked, l_arm_untucked, r_arm_tucked, r_arm_untucked):
229  value_state = self.angleWrap(value_state)
230 
231  if 'l_'+name_desi+'_joint' == name_state:
232  self.l_received = True
233  l_sum_tucked = l_sum_tucked + math.fabs(value_state - value_l_tucked)
234  l_sum_untucked = l_sum_untucked + math.fabs(value_state - value_l_untucked)
235  if 'r_'+name_desi+'_joint' == name_state:
236  self.r_received = True
237  r_sum_tucked = r_sum_tucked + math.fabs(value_state - value_r_tucked)
238  r_sum_untucked = r_sum_untucked + math.fabs(value_state - value_r_untucked)
239 
240  if l_sum_tucked > 0 and l_sum_tucked < 0.1:
241  self.l_arm_state = 0
242  elif l_sum_untucked > 0 and l_sum_untucked < 0.3:
243  self.l_arm_state = 1
244  elif l_sum_tucked >= 0.1 and l_sum_untucked >= 0.3:
245  self.l_arm_state = -1
246 
247  if r_sum_tucked > 0 and r_sum_tucked < 0.1:
248  self.r_arm_state = 0
249  elif r_sum_untucked > 0 and r_sum_untucked < 0.3:
250  self.r_arm_state = 1
251  elif r_sum_tucked >= 0.1 and r_sum_untucked >= 0.3:
252  self.r_arm_state = -1
253 
254 
255 def main():
256  action_name = 'tuck_arms'
257  rospy.init_node(action_name)
258  rospy.sleep(0.001) # wait for time
259  tuck_arms_action_server = TuckArmsActionServer(action_name)
260 
261  rospy.spin()
262 
263 
264 if __name__ == '__main__':
265  main()


pr2_tuck_arms_action
Author(s): Wim Meeussen
autogenerated on Fri Jun 7 2019 22:06:46