示例#1
0
    def __init__(self):

        # Initialize the move_group API
        moveit_commander.roscpp_initialize(sys.argv)

        rospy.init_node('moveit_demo')

        ptu_action = FollowTrajectoryClient(
            "ptu_controller", ["ptu_pan_joint", "ptu_tilt_joint"])
        ptu_action.move_to([
            0.0,
            -0.75,
        ])

        # Use the planning scene object to add or remove objects
        scene = PlanningSceneInterface(REFERENCE_FRAME)

        # Create a scene publisher to push changes to the scene
        self.scene_pub = rospy.Publisher('planning_scene', PlanningScene)

        # Create a publisher for displaying gripper poses
        self.gripper_pose_pub = rospy.Publisher('gripper_pose', PoseStamped)

        # Create a dictionary to hold object colors
        self.colors = dict()

        # Initialize the move group for the right arm
        right_arm = MoveGroupCommander(GROUP_NAME_ARM)

        # Initialize the move group for the right gripper
        right_gripper = MoveGroupCommander(GROUP_NAME_GRIPPER)

        # Get the name of the end-effector link
        end_effector_link = right_arm.get_end_effector_link()
        print("End Effector Link", end_effector_link)

        # Allow some leeway in position (meters) and orientation (radians)
        right_arm.set_goal_position_tolerance(0.02)
        right_arm.set_goal_orientation_tolerance(0.05)

        # Allow replanning to increase the odds of a solution
        right_arm.allow_replanning(True)

        # Set the right arm reference frame
        right_arm.set_pose_reference_frame(REFERENCE_FRAME)

        # Allow 15 seconds per planning attempt
        right_arm.set_planning_time(15)

        # Set a limit on the number of pick attempts before bailing
        max_pick_attempts = 10

        # Set a limit on the number of place attempts
        max_place_attempts = 3

        # Give the scene a chance to catch up
        rospy.sleep(2)

        # Connect to the UBR-1 find_objects action server
        find_topic = "basic_grasping_perception/find_objects"
        sub_topic = "handles_position"
        rospy.Subscriber(sub_topic, target_pose, getHandlesCb)
        rospy.loginfo(
            "Connecting to basic_grasping_perception/find_objects...")
        find_objects = actionlib.SimpleActionClient(
            find_topic, FindGraspableObjectsAction)
        find_objects.wait_for_server()
        rospy.loginfo("...connected")

        # Give the scene a chance to catch up
        rospy.sleep(1)

        # Start the arm in the "resting" pose stored in the SRDF file
        # right_arm.set_named_target('right_down')
        # right_arm.go()

        # Open the gripper to the neutral position
        # right_gripper.set_joint_value_target(GRIPPER_NEUTRAL)
        # right_gripper.go()

        # rospy.sleep(1)

        # Begin the main perception and pick-and-place loop
        while not rospy.is_shutdown():
            # Initialize the grasping goal
            goal = FindGraspableObjectsGoal()

            # We don't use the UBR-1 grasp planner as it does not work with our gripper
            goal.plan_grasps = False

            # Send the goal request to the find_objects action server which will trigger
            # the perception pipeline
            find_objects.send_goal(goal)

            # Wait for a result
            find_objects.wait_for_result(rospy.Duration(10.0))

            # The result will contain support surface(s) and objects(s) if any are detected
            find_result = find_objects.get_result()

            # Display the number of objects found
            rospy.loginfo("Found %d objects" % len(find_result.objects))

            rospy.loginfo("Found %d Support Surfaces" %
                          len(find_result.support_surfaces))

            # print("perception_results_primitives",find_result.objects.primitives[0])
            # print("perception_results_primitives_poses",find_result.objects.primitives_poses)

            # Remove all previous objects from the planning scene
            # for name in scene.getKnownCollisionObjects():
            # scene.removeCollisionObject(name, False)
            # for name in scene.getKnownAttachedObjects():
            # scene.removeAttachedObject(name, False)
            # scene.waitForSync()

            # Clear the virtual object colors
            scene._colors = dict()

            # Use the nearest object on the table as the target
            target_pose = PoseStamped()
            target_pose.header.frame_id = REFERENCE_FRAME
            target_size = None
            # target_position = None
            the_object = None
            the_object_dist = 1.0
            count = -1
            # target_size
            # Cycle through all detected objects and keep the nearest one
            for obj in find_result.objects:
                count += 1
                scene.addSolidPrimitive(
                    "object%d" % count,
                    obj.object.primitives[0],
                    obj.object.primitive_poses[0],
                )  #wait = False

                # Choose the object nearest to the robot
                dx = obj.object.primitive_poses[0].position.x - args.x
                dy = obj.object.primitive_poses[0].position.y
                d = math.sqrt((dx * dx) + (dy * dy))
                if d < the_object_dist:
                    the_object_dist = d
                    the_object = count

                    # Get the size of the target
                    target_size = obj.object.primitives[0].dimensions
                    print("target_size %d ", count, target_size)
                    # target_position = obj.object.primitive_poses[0].position
                    # print("target_size %d ", %count, target_size )

                    # Set the target pose
                    target_pose.pose = obj.object.primitive_poses[0]
                    print("target_pose_before ", target_pose)

                    # We want the gripper to be horizontal
                    target_pose.pose.orientation.x = 0.519617092464
                    target_pose.pose.orientation.y = -0.510439243162
                    target_pose.pose.orientation.z = 0.479912175114
                    target_pose.pose.orientation.w = -0.489013456294

                    print("target_pose_after ", target_pose)

                # Make sure we found at least one object before setting the target ID
                if the_object != None:
                    target_id = "object%d" % the_object

            # Insert the support surface into the planning scene
            for obj in find_result.support_surfaces:
                # Extend surface to floor
                # height = obj.primitive_poses[0].position.z
                # obj.primitives[0].dimensions = [obj.primitives[0].dimensions[0],
                # 2.0, # make table wider
                # obj.primitives[0].dimensions[2] ]
                # obj.primitive_poses[0].position.z += -height/1.5

                # Add to scene
                scene.addSolidPrimitive(
                    obj.name,
                    obj.primitives[0],
                    obj.primitive_poses[0],
                )  #wait = False

                # Get the table dimensions
                table_size = obj.primitives[0].dimensions
                print("table_size", table_size)

            # If no objects detected, try again
            if the_object == None or target_size is None:
                rospy.logerr("Nothing to grasp! try again...")
                continue

            # Wait for the scene to sync
            scene.waitForSync()

            # Set colors of the table and the object we are grabbing
            scene.setColor(target_id, 223.0 / 256.0, 90.0 / 256.0,
                           12.0 / 256.0)  # orange
            scene.setColor(
                find_result.objects[the_object].object.support_surface, 0.3,
                0.3, 0.3, 0.7)  # grey
            scene.sendColors()

            # Skip pick-and-place if we are just detecting objects
            if args.objects:
                if args.once:
                    exit(0)
                else:
                    continue

            # Get the support surface ID
            #support_surface = find_result.objects[the_object].object.support_surface

            # Set the support surface name to the table object
            #right_arm.set_support_surface_name(support_surface)

            # Specify a pose to place the target after being picked up
            # place_pose = PoseStamped()
            # place_pose.header.frame_id = REFERENCE_FRAME
            # place_pose.pose.position.x = target_pose.pose.position.x
            # place_pose.pose.position.y = 0.03
            # place_pose.pose.position.z = table_size[2] + target_size[2] / 2.0 + 0.015
            # place_pose.pose.orientation.w = 1.0

            # Initialize the grasp pose to the target pose
            grasp_pose = target_pose

            # Shift the grasp pose half the size of the target to center it in the gripper
            try:
                grasp_pose.pose.position.x += target_size[0]
                grasp_pose.pose.position.y -= 0.01
                grasp_pose.pose.position.z += target_size[2] / 2.0
            except:
                rospy.loginfo("Invalid object size so skipping")
                continue

            # Generate a list of grasps
            grasps = self.make_grasps(grasp_pose, [target_id])

            # Publish the grasp poses so they can be viewed in RViz
            for grasp in grasps:
                self.gripper_pose_pub.publish(grasp.grasp_pose)
                rospy.sleep(0.2)

            # Track success/failure and number of attempts for pick operation
            result = None
            n_attempts = 0

            # Set the start state to the current state
            right_arm.set_start_state_to_current_state()

            # Repeat until we succeed or run out of attempts
            while result != MoveItErrorCodes.SUCCESS and n_attempts < max_pick_attempts:
                result = right_arm.pick(target_id, grasps)
                n_attempts += 1
                rospy.loginfo("Pick attempt: " + str(n_attempts))
                rospy.sleep(1.0)

            # If the pick was successful, attempt the place operation
            # if result == MoveItErrorCodes.SUCCESS:
            # result = None
            # n_attempts = 0

            # # Generate valid place poses
            # places = self.make_places(place_pose)

            # # Set the start state to the current state
            # #right_arm.set_start_state_to_current_state()

            # # Repeat until we succeed or run out of attempts
            # while result != MoveItErrorCodes.SUCCESS and n_attempts < max_place_attempts:
            # for place in places:
            # result = right_arm.place(target_id, place)
            # if result == MoveItErrorCodes.SUCCESS:
            # break
            # n_attempts += 1
            # rospy.loginfo("Place attempt: " +  str(n_attempts))
            # rospy.sleep(0.2)

            # if result != MoveItErrorCodes.SUCCESS:
            # rospy.loginfo("Place operation failed after " + str(n_attempts) + " attempts.")
            else:
                rospy.loginfo("Pick operation failed after " +
                              str(n_attempts) + " attempts.")

            rospy.sleep(2)

            # Open the gripper to the neutral position
            # right_gripper.set_joint_value_target(GRIPPER_NEUTRAL)
            # right_gripper.go()

            # rospy.sleep(2)

            # Return the arm to the "resting" pose stored in the SRDF file
            # right_arm.set_named_target('resting')
            # right_arm.go()

            # rospy.sleep(2)

            # Give the servos a rest
            # arbotix_relax_all_servos()

            # rospy.sleep(2)

            if args.once:
                # Shut down MoveIt cleanly
                moveit_commander.roscpp_shutdown()

                # Exit the script
                moveit_commander.os._exit(0)
    def __init__(self):
        # Initialize the move_group API
        moveit_commander.roscpp_initialize(sys.argv)
        
        rospy.init_node('moveit_demo')
                
        # Use the planning scene object to add or remove objects
        scene = PlanningSceneInterface("base_link")
        
        # Create a scene publisher to push changes to the scene
        self.scene_pub = rospy.Publisher('planning_scene', PlanningScene)
        
        # Create a publisher for displaying gripper poses
        self.gripper_pose_pub = rospy.Publisher('gripper_pose', PoseStamped)
        
        # Create a dictionary to hold object colors
        self.colors = dict()
                        
        # Initialize the move group for the right arm
        right_arm = MoveGroupCommander(GROUP_NAME_ARM)
        
        # Initialize the move group for the right gripper
        right_gripper = MoveGroupCommander(GROUP_NAME_GRIPPER)
        
        # Get the name of the end-effector link
        end_effector_link = right_arm.get_end_effector_link()
 
        # Allow some leeway in position (meters) and orientation (radians)
        right_arm.set_goal_position_tolerance(0.05)
        right_arm.set_goal_orientation_tolerance(0.1)

        # Allow replanning to increase the odds of a solution
        right_arm.allow_replanning(True)
        
        # Set the right arm reference frame
        right_arm.set_pose_reference_frame(REFERENCE_FRAME)
        
        # Allow 5 seconds per planning attempt
        right_arm.set_planning_time(15)
        
        # Set a limit on the number of pick attempts before bailing
        max_pick_attempts = 5
        
        # Set a limit on the number of place attempts
        max_place_attempts = 3
                
        # Give the scene a chance to catch up
        rospy.sleep(2)
        
        # Connect to the UBR-1 find_objects action server
        rospy.loginfo("Connecting to basic_grasping_perception/find_objects...")
        find_objects = actionlib.SimpleActionClient("basic_grasping_perception/find_objects", FindGraspableObjectsAction)
        find_objects.wait_for_server()
        rospy.loginfo("...connected")
        
        # Give the scene a chance to catch up    
        rospy.sleep(1)
        
        # Start the arm in the "resting" pose stored in the SRDF file
        right_arm.set_named_target('resting')
        right_arm.go()
        
        # Open the gripper to the neutral position
        right_gripper.set_joint_value_target(GRIPPER_NEUTRAL)
        right_gripper.go()
       
        rospy.sleep(1)
        
        # Begin the main perception and pick-and-place loop
        while not rospy.is_shutdown():
            # Initialize the grasping goal
            goal = FindGraspableObjectsGoal()
            
            # We don't use the UBR-1 grasp planner as it does not work with our gripper
            goal.plan_grasps = False
            
            # Send the goal request to the find_objects action server which will trigger
            # the perception pipeline
            find_objects.send_goal(goal)
            
            # Wait for a result
            find_objects.wait_for_result(rospy.Duration(5.0))
            
            # The result will contain support surface(s) and objects(s) if any are detected
            find_result = find_objects.get_result()
    
            # Display the number of objects found
            rospy.loginfo("Found %d objects" % len(find_result.objects))
    
            # Remove all previous objects from the planning scene
            for name in scene.getKnownCollisionObjects():
                scene.removeCollisionObject(name, False)
            for name in scene.getKnownAttachedObjects():
                scene.removeAttachedObject(name, False)
            scene.waitForSync()
            
            # Clear the virtual object colors
            scene._colors = dict()
    
            # Use the nearest object on the table as the target
            target_pose = PoseStamped()
            target_pose.header.frame_id = REFERENCE_FRAME
            target_size = None
            the_object = None
            the_object_dist = 1.0
            count = -1
            
            # Cycle through all detected objects and keep the nearest one
            for obj in find_result.objects:
                count += 1
                scene.addSolidPrimitive("object%d"%count,
                                        obj.object.primitives[0],
                                        obj.object.primitive_poses[0],
                                        wait = False)

                # Choose the object nearest to the robot
                dx = obj.object.primitive_poses[0].position.x - args.x
                dy = obj.object.primitive_poses[0].position.y
                d = math.sqrt((dx * dx) + (dy * dy))
                if d < the_object_dist:
                    the_object_dist = d
                    the_object = count
                    
                    # Get the size of the target
                    target_size = obj.object.primitives[0].dimensions
                    
                    # Set the target pose
                    target_pose.pose = obj.object.primitive_poses[0]
                    
                    # We want the gripper to be horizontal
                    target_pose.pose.orientation.x = 0.0
                    target_pose.pose.orientation.y = 0.0
                    target_pose.pose.orientation.z = 0.0
                    target_pose.pose.orientation.w = 1.0
                
                # Make sure we found at least one object before setting the target ID
                if the_object != None:
                    target_id = "object%d"%the_object
                    
            # Insert the support surface into the planning scene
            for obj in find_result.support_surfaces:
                # Extend surface to floor
                height = obj.primitive_poses[0].position.z
                obj.primitives[0].dimensions = [obj.primitives[0].dimensions[0],
                                                2.0, # make table wider
                                                obj.primitives[0].dimensions[2] + height]
                obj.primitive_poses[0].position.z += -height/2.0
    
                # Add to scene
                scene.addSolidPrimitive(obj.name,
                                        obj.primitives[0],
                                        obj.primitive_poses[0],
                                        wait = False)
                
                # Get the table dimensions
                table_size = obj.primitives[0].dimensions
                
            # If no objects detected, try again
            if the_object == None or target_size is None:
                rospy.logerr("Nothing to grasp! try again...")
                continue
    
            # Wait for the scene to sync
            scene.waitForSync()
    
            # Set colors of the table and the object we are grabbing
            scene.setColor(target_id, 223.0/256.0, 90.0/256.0, 12.0/256.0)  # orange
            scene.setColor(find_result.objects[the_object].object.support_surface, 0.3, 0.3, 0.3, 0.7)  # grey
            scene.sendColors()
    
            # Skip pick-and-place if we are just detecting objects
            if args.objects:
                if args.once:
                    exit(0)
                else:
                    continue
    
            # Get the support surface ID
            support_surface = find_result.objects[the_object].object.support_surface
                        
            # Set the support surface name to the table object
            right_arm.set_support_surface_name(support_surface)
            
            # Specify a pose to place the target after being picked up
            place_pose = PoseStamped()
            place_pose.header.frame_id = REFERENCE_FRAME
            place_pose.pose.position.x = target_pose.pose.position.x
            place_pose.pose.position.y = 0.03
            place_pose.pose.position.z = table_size[2] + target_size[2] / 2.0 + 0.015
            place_pose.pose.orientation.w = 1.0
                            
            # Initialize the grasp pose to the target pose
            grasp_pose = target_pose
             
            # Shift the grasp pose half the size of the target to center it in the gripper
            try:
                grasp_pose.pose.position.x += target_size[0] / 2.0
                grasp_pose.pose.position.y -= 0.01
                grasp_pose.pose.position.z += target_size[2] / 2.0
            except:
                rospy.loginfo("Invalid object size so skipping")
                continue

            # Generate a list of grasps
            grasps = self.make_grasps(grasp_pose, [target_id])
     
            # Publish the grasp poses so they can be viewed in RViz
            for grasp in grasps:
                self.gripper_pose_pub.publish(grasp.grasp_pose)
                rospy.sleep(0.2)
         
            # Track success/failure and number of attempts for pick operation
            result = None
            n_attempts = 0
            
            # Set the start state to the current state
            right_arm.set_start_state_to_current_state()
             
            # Repeat until we succeed or run out of attempts
            while result != MoveItErrorCodes.SUCCESS and n_attempts < max_pick_attempts:
                result = right_arm.pick(target_id, grasps)
                n_attempts += 1
                rospy.loginfo("Pick attempt: " +  str(n_attempts))
                rospy.sleep(1.0)
             
            # If the pick was successful, attempt the place operation   
            if result == MoveItErrorCodes.SUCCESS:
                result = None
                n_attempts = 0
                  
                # Generate valid place poses
                places = self.make_places(place_pose)
                
                # Set the start state to the current state
                #right_arm.set_start_state_to_current_state()
                  
                # Repeat until we succeed or run out of attempts
                while result != MoveItErrorCodes.SUCCESS and n_attempts < max_place_attempts:
                    for place in places:
                        result = right_arm.place(target_id, place)
                        if result == MoveItErrorCodes.SUCCESS:
                            break
                    n_attempts += 1
                    rospy.loginfo("Place attempt: " +  str(n_attempts))
                    rospy.sleep(0.2)
                      
                if result != MoveItErrorCodes.SUCCESS:
                    rospy.loginfo("Place operation failed after " + str(n_attempts) + " attempts.")
            else:
                rospy.loginfo("Pick operation failed after " + str(n_attempts) + " attempts.")

            rospy.sleep(2)
            
            # Open the gripper to the neutral position
            right_gripper.set_joint_value_target(GRIPPER_NEUTRAL)
            right_gripper.go()
            
            rospy.sleep(2)
            
            # Return the arm to the "resting" pose stored in the SRDF file
            right_arm.set_named_target('resting')
            right_arm.go()
             
            rospy.sleep(2)
            
            # Give the servos a rest
            arbotix_relax_all_servos()
            
            rospy.sleep(2)
        
            if args.once:
                # Shut down MoveIt cleanly
                moveit_commander.roscpp_shutdown()
                
                # Exit the script
                moveit_commander.os._exit(0)