Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import roslib; roslib.load_manifest('pr2_plugs_actions')
00036 import rospy
00037 import PyKDL
00038 import actionlib
00039 from pr2_common_action_msgs.msg import *
00040 from pr2_plugs_msgs.msg import *
00041 from pr2_controllers_msgs.msg import *
00042 from pr2_arm_move_ik.tools import *
00043 from tf_conversions.posemath import fromMsg, toMsg
00044 from actionlib_msgs.msg import *
00045 import geometry_msgs.msg
00046 from math import pi
00047 import math
00048 from pr2_plugs_actions.tf_util import TFUtil
00049
00050 def drange(start, stop, step):
00051 r = start
00052 if start < stop:
00053 while r < stop:
00054 yield r
00055 r += step
00056 if start > stop:
00057 while r > stop:
00058 yield r
00059 r -= step
00060
00061
00062 def outlet_to_plug_error(goal):
00063 time = rospy.Time.now()
00064 try:
00065 pose_base_gripper = fromMsg(TFUtil.wait_and_lookup("base_link","r_gripper_tool_frame", time).pose)
00066 except rospy.ServiceException, e:
00067 rospy.logerr('Could not transform between gripper and wrist at time %f' %time.to_sec())
00068 server.set_aborted()
00069 return
00070 outlet_to_plug = fromMsg(goal.base_to_outlet).Inverse() * pose_base_gripper * fromMsg(goal.gripper_to_plug)
00071 return outlet_to_plug
00072
00073
00074 def execute_cb(goal):
00075 rospy.loginfo("Action server received goal")
00076 preempt_timeout = rospy.Duration(10.0)
00077 cart_space_goal.ik_seed = get_action_seed('pr2_plugs_configuration/approach_outlet_seed')
00078 try:
00079 pose_gripper_wrist= fromMsg(TFUtil.wait_and_lookup("r_gripper_tool_frame", "r_wrist_roll_link").pose)
00080 except rospy.ServiceException, e:
00081 rospy.logerr('Could not transform between gripper and wrist at time %f' %time.to_sec())
00082 server.set_aborted()
00083 return
00084
00085 pose_base_outlet = fromMsg(goal.base_to_outlet)
00086 pose_plug_gripper = fromMsg(goal.gripper_to_plug).Inverse()
00087
00088 rate = rospy.Rate(100.0)
00089 current_error = outlet_to_plug_error(goal)
00090 forward_start = current_error.p[0]
00091 if goal.insert == 1:
00092 forward_stop = current_error.p[0] + 0.02
00093 forward_step = 0.0005
00094 else:
00095 forward_stop = current_error.p[0] - 0.04
00096 forward_step = 0.001
00097
00098 wiggle = 1.0
00099 wiggle_count = 1
00100 for offset in drange(forward_start, forward_stop, forward_step):
00101 pose_outlet_plug = PyKDL.Frame(PyKDL.Rotation.RPY(0, math.pi/30*wiggle, 0), PyKDL.Vector(offset, 0, 0))
00102 cart_space_goal.pose.pose = toMsg(pose_base_outlet * pose_outlet_plug * pose_plug_gripper * pose_gripper_wrist)
00103 cart_space_goal.pose.header.stamp = rospy.Time.now()
00104 cart_space_goal.pose.header.frame_id = 'base_link'
00105 cart_space_goal.move_duration = rospy.Duration(0.0)
00106
00107 result_state = cart_space_client.send_goal_and_wait(cart_space_goal, rospy.Duration(20.0), preempt_timeout)
00108
00109 if goal.insert != 1 and wiggle_count % 5 == 0 and result_state == GoalStatus.SUCCEEDED:
00110 server.set_succeeded(WigglePlugResult())
00111 return
00112
00113 wiggle = wiggle * -1
00114 wiggle_count += 1
00115
00116
00117 pose_outlet_plug = PyKDL.Frame(PyKDL.Vector(offset, 0, 0))
00118 cart_space_goal.pose.pose = toMsg(pose_base_outlet * pose_outlet_plug * pose_plug_gripper * pose_gripper_wrist)
00119 cart_space_goal.pose.header.stamp = rospy.Time.now()
00120 cart_space_goal.pose.header.frame_id = 'base_link'
00121 cart_space_goal.move_duration = rospy.Duration(0.5)
00122 cart_space_client.send_goal_and_wait(cart_space_goal, rospy.Duration(20.0), preempt_timeout)
00123 rate.sleep()
00124
00125 result = WigglePlugResult()
00126 server.set_succeeded(result)
00127
00128
00129 if __name__ == '__main__':
00130
00131 name = 'wiggle_plug'
00132 rospy.init_node(name)
00133
00134
00135 TFUtil()
00136
00137
00138 cart_space_client = actionlib.SimpleActionClient('r_arm_ik', ArmMoveIKAction)
00139 cart_space_client.wait_for_server()
00140 cart_space_goal = ArmMoveIKGoal()
00141 rospy.loginfo('Connected to action clients')
00142
00143
00144 server = actionlib.simple_action_server.SimpleActionServer(name, WigglePlugAction, execute_cb)
00145 rospy.loginfo('%s: Action server running', name)
00146
00147
00148 rospy.spin()