pick_up_cloth.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 import roslib; roslib.load_manifest('estirabot_apps')
00005 import rospy
00006 import smach
00007 import smach_ros
00008 
00009 from pprint import pprint
00010 
00011 from smach_ros import SimpleActionState
00012 from smach_ros import ServiceState
00013 from smach import CBState
00014 
00015 from geometry_msgs.msg import PoseStamped
00016 
00017 from iri_common_smach.st_get_pcl import GetPCL
00018 from iri_common_smach.utils_msg  import build_pose
00019 from iri_common_smach.utils_msg  import build_transform_msg
00020 #from iri_common_smach.transform_manager import TransformManager
00021 #from iri_common_smach.st_transform_pose   import TransformPoseStamped
00022 from iri_common_smach.homogeneous_product import homogeneous_product_pose_transform
00023 from iri_wam_smach.sm_wam_move_from_pose  import SM_WAM_MoveFromPose
00024 from estirabot_apps_base.sm_move             import SM_ESTIRABOT_GoHome
00025 
00026 from estirabot_msgs.srv import TransformPose
00027 
00028 from iri_common_drivers_msgs.msg import tool_closeAction, tool_openAction
00029 
00030 from iri_common_smach.utils_msg import build_pose_stamped_msg
00031 
00032 from iri_bow_object_detector.msg import *
00033 from iri_wam_common_msgs.msg import *
00034 
00035 from actionlib import *
00036 from actionlib.msg import *
00037 
00038 import ipdb
00039 
00040 @smach.cb_interface(input_keys=['grasp_point'],
00041                                         output_keys=['grasp_pose_st', 'target_frame'],
00042                                         outcomes=['done'])
00043 def build_grasp_pose_from_point_cb(ud):
00044         p                 = PoseStamped()
00045         p.pose            = build_pose(ud.grasp_point.x, ud.grasp_point.y, ud.grasp_point.z, 0, 1, 0, 0)
00046         p.header.frame_id = '/camera_rgb_optical_frame'
00047         p.header.stamp = rospy.Time.now()
00048         pub = rospy.Publisher('/debug/grasp_point', PoseStamped, None, False, True)
00049         rospy.sleep(1) # wait for subscribers
00050         pub.publish(p)
00051         ud.grasp_pose_st = p
00052         
00053         # transform pose
00054         #tf_manager = TransformManager()
00055         #p_transformed = tf_manager.transform_pose('wam_link0', p)
00056         #ud.grasp_pose_st = p_transformed
00057         
00058         ud.target_frame = '/estirabot_link_base'
00059         
00060         return 'done'
00061 
00062 def grasping_point_goal_cb(userdata, goal):
00063         goal = GetGraspingPointGoal()
00064         goal.pointcloud = userdata.pcl_RGB
00065         return goal
00066 
00067 
00068 def build_pick_up_goal_from_grasping_pose(pose_st):
00069         goal = SimpleBhandPickUpGoal()
00070         
00071         #goal.fingers_position_for_pre_grasp=["GM 5000"]
00072         #goal.fingers_position_for_grasp=["GTC"]
00073         goal.fingers_position_for_pre_grasp=["open"]
00074         goal.fingers_position_for_grasp=["close"]
00075         
00076         # Grasp pose is pose_st
00077         goal.grasp_pose = pose_st
00078         
00079         # Pre-grasp pose -0.15m above grasp
00080         goal.pre_grasp_modifier.position.x = 0
00081         goal.pre_grasp_modifier.position.y = 0
00082         goal.pre_grasp_modifier.position.z = -0.15
00083         goal.pre_grasp_modifier.orientation.x = 0
00084         goal.pre_grasp_modifier.orientation.y = 0
00085         goal.pre_grasp_modifier.orientation.z = 0
00086         goal.pre_grasp_modifier.orientation.w = 1
00087         
00088         # Lift modifier -0.20 on Z
00089         goal.lift.direction.header.frame_id = "/estirabot_link_base"
00090         goal.lift.direction.header.stamp = pose_st.header.stamp
00091         goal.lift.direction.vector.x = 0
00092         goal.lift.direction.vector.y = 0.441176471
00093         goal.lift.direction.vector.z = 0.735294118
00094         goal.lift.desired_distance = 0.60
00095         
00096         return goal
00097 
00098 
00099 def move_pose_away(pose, z_offset):
00100         transform = build_transform_msg(0, 0, z_offset, 0, 0, 0, 1)
00101         return homogeneous_product_pose_transform(pose, transform)
00102 
00103 
00104 def grasp_cloth_goal_cb(userdata, goal):
00105         #ipdb.set_trace()
00106         pose_st = userdata.sm_transformed_pose_st
00107         
00108         # Set orientation pointing to the table
00109         pose_st.pose.orientation.x = 0
00110         pose_st.pose.orientation.y = 1
00111         pose_st.pose.orientation.z = 0
00112         pose_st.pose.orientation.w = 0
00113         pose_st.pose = move_pose_away(pose_st.pose, 0.02)
00114         
00115         pub = rospy.Publisher('/debug/grasp_point2', PoseStamped, None, False, True)
00116         rospy.sleep(1) # wait for subscribers
00117         pub.publish(pose_st)
00118         
00119         goal = build_pick_up_goal_from_grasping_pose(pose_st)
00120         return goal
00121 
00122 def lwpr_goal_cb(userdata, goal):
00123         #ipdb.set_trace()
00124         goal = LWPRTrajectoryReturningForceEstimationGoal()
00125         goal.model_filename = "/home/robot/code/iri-libbarrett/build/apps/iros13_2-LWPR"
00126         goal.points_filename = "/home/robot/code/iri-libbarrett/build/apps/tmp/iros13_2.txt"
00127         return goal
00128 
00129 class TransformPoseStamped(smach.State):
00130         def __init__(self):
00131                 smach.State.__init__(self, outcomes=['succeeded','preempted','aborted'],
00132                                      input_keys=['sm_grasp_pose','sm_target_frame'],
00133                                      output_keys=['target_pose'])
00134                 self.service_topic = '/estirabot/skills/bow_detector/iri_transform_pose/transform_pose'
00135         
00136         def execute(self, userdata):
00137                 rospy.logdebug('Executing state TransformPoseStamped')
00138                 rospy.wait_for_service(self.service_topic)
00139                 try:
00140                         get_target_pose = rospy.ServiceProxy(self.service_topic, TransformPose)
00141                         resp = get_target_pose(userdata.sm_grasp_pose, userdata.sm_target_frame)
00142                         
00143                         userdata.target_pose = resp.target_pose_st
00144                         return 'succeeded'
00145                 
00146                 except rospy.ServiceException, e:
00147                         print "Service call failed: %s"%e
00148                         return 'aborted'
00149 
00150 
00151 def main():
00152         rospy.init_node('sm_pick_up_cloths')
00153         
00154         sm_move_factory = SM_WAM_MoveFromPose('/estirabot/wam_wrapper/joints_move',
00155                                               '/estirabot/wam_ik/pose_move')
00156         sm_move         = sm_move_factory.build_sm()
00157         factory    = SM_ESTIRABOT_GoHome()
00158         sm_goHome  = factory.build_sm()
00159         
00160         # Create a SMACH state machine
00161         sm = smach.StateMachine(outcomes=['finishOk','failedVision','failedMoveArm'])
00162 
00163         # Open the container
00164         with sm:
00165                 # Add states to the container
00166                 smach.StateMachine.add('GetPCL', GetPCL('/camera/depth_registered/points'),
00167                 #smach.StateMachine.add('GetPCL', GetPCL('/estirabot/ceiling_kinect/camera/rgb/points'),
00168                                 transitions={'success':'GetGraspingPointAction', 
00169                                                 'fail':'failedVision'},
00170                                 remapping={'pcl_RGB':'pcl_RGB'})
00171                 
00172                 smach.StateMachine.add('GetGraspingPointAction', 
00173                                 SimpleActionState('/estirabot/skills/bow_detector/bow_object_detector/get_grasping_point',
00174                                                   GetGraspingPointAction,
00175                                                   #goal_slots=['pcl_RGB'],
00176                                                   goal_cb=grasping_point_goal_cb,
00177                                                   input_keys=['pcl_RGB'],
00178                                                   result_slots=['grasping_point']),
00179                                                   #server_wait_timeout=rospy.Duration(10.0)),
00180                                 transitions={'succeeded':'ConvertGraspPoint',
00181                                                 'preempted':'failedVision',
00182                                                 'aborted':'failedVision'},
00183                                 remapping={'grasping_point':'sm_grasp_point'})
00184                 
00185                 smach.StateMachine.add('ConvertGraspPoint', 
00186                                                           CBState(build_grasp_pose_from_point_cb),
00187                                 transitions={'done':'TransformPoseStamped'},
00188                                 remapping={'grasp_point' : 'sm_grasp_point',
00189                            'grasp_pose_st' : 'sm_grasp_pose',
00190                            'target_frame' : 'sm_target_frame'})
00191 
00192                 
00193                 #smach.StateMachine.add('TransformPoseStamped', TransformPoseStamped(),
00194                                 #transitions={'success':'SimpleBhandPickUpAction', 
00195                                                 #'fail':'failedVision'},
00196                                 #remapping={'pose_st':'sm_grasp_pose',
00197                                                 #'target_frame' : 'sm_target_frame',
00198                                                 #'transformed_pose_st' : 'sm_transformed_pose_st'})
00199                 
00200                 
00201                 smach.StateMachine.add('TransformPoseStamped', 
00202                                         TransformPoseStamped(),
00203                                 transitions={'succeeded':'SimpleBhandPickUpAction', 
00204                                                 'preempted':'failedVision',
00205                                                 'aborted':'failedVision'},
00206                                 remapping={'target_pose':'sm_transformed_pose_st'})
00207                 
00208                 smach.StateMachine.add('SimpleBhandPickUpAction', 
00209                                 SimpleActionState('/estirabot/skills/grasp/pickup',
00210                                                   SimpleBhandPickUpAction,
00211                                                   #goal_slots=['pcl_RGB'],
00212                                                   goal_cb=grasp_cloth_goal_cb,
00213                                                   input_keys=['sm_transformed_pose_st']),
00214                                                   #server_wait_timeout=rospy.Duration(10.0)),
00215                                 #transitions={'succeeded':'LWPR_action',
00216                                 transitions={'succeeded':'OpenGripper',
00217                                                 'preempted':'failedMoveArm',
00218                                                 'aborted':'failedMoveArm'})
00219                 
00220                 # move to home
00221                 smach.StateMachine.add('GoHome', 
00222                                         sm_goHome,
00223                                 transitions={'success':'LWPR_action',
00224                                         'aborted':'failedMoveArm'})
00225                 
00226                 #smach.StateMachine.add('MoveHome', sm_move,
00227                      #transitions = {'success' : 'OpenGripper',
00228                                     #'no_kinematic_solution' : 'failedMoveArm',
00229                                     #'aborted': 'failedMoveArm'},
00230                      #remapping   = {'pose_st': 'sm_grasp_pose'})
00231                 
00232                 # lwpr
00233                 smach.StateMachine.add('LWPR_action', 
00234                                 SimpleActionState('/estirabot/wam_wrapper/lwpr_trajectory',
00235                                                   LWPRTrajectoryReturningForceEstimationAction,
00236                                                   #goal_slots=['pcl_RGB'],
00237                                                   goal_cb=lwpr_goal_cb),
00238                                                   #input_keys=['sm_transformed_pose_st']),
00239                                                   #server_wait_timeout=rospy.Duration(10.0)),
00240                                 transitions={'succeeded':'finishOk',
00241                                                 'preempted':'failedMoveArm',
00242                                                 'aborted':'failedMoveArm'})
00243                 
00244                 # release
00245                 smach.StateMachine.add('OpenGripper',
00246                        smach_ros.SimpleActionState('/estirabot/gripper/tool_open_action',
00247                                                   tool_openAction),
00248                        {'succeeded': 'CloseGripper',
00249                         'preempted': 'failedMoveArm',
00250                         'aborted'  : 'failedMoveArm'})
00251                         
00252                 #move home
00253                 smach.StateMachine.add('GoHome2', 
00254                                         sm_goHome,
00255                                         transitions={'success':'finishOk',
00256                                         'aborted':'failedMoveArm'})
00257                 
00258                 
00259                 smach.StateMachine.add('CloseGripper',
00260                        smach_ros.SimpleActionState('/estirabot/gripper/tool_close_action',
00261                                                   tool_closeAction),
00262                        {'succeeded': 'finishOk',
00263                         'preempted': 'failedMoveArm',
00264                         'aborted'  : 'failedMoveArm'})
00265                 
00266                 # initial
00267                 
00268 
00269         # Execute SMACH plan
00270         outcome = sm.execute()
00271 
00272 
00273 
00274 if __name__ == '__main__':
00275         main()


estirabot_apps
Author(s): Jose Luis Rivero
autogenerated on Fri Dec 6 2013 23:20:59