コード例 #1
0
    def execute(self, userdata):
        rospy.loginfo('Executing state GRASP')

    	#Open SDH at the pre-grasp position -----------------------------------------------
    	#sss.move("sdh", "cylopen")
        
        
        pre_p = userdata.grasp_configuration[userdata.grasp_configuration_id].pre_grasp.position
        g_p = userdata.grasp_configuration[userdata.grasp_configuration_id].grasp.position
        
        category = grasping_functions.get_grasp_category(pre_p , g_p);
        
        if category == "TOP" or category == "DOWN":
            userdata.grasp_categorisation = 'top'
            arm_handle=sss.move("sdh", "spheropen")
        else:
            userdata.grasp_categorisation = 'side'
            arm_handle=sss.move("sdh", "cylopen")
        arm_handle.wait()
        rospy.sleep(2)
        
        #Get the current arm joint states.
        while self.sub.get_num_connections() == 0:
    		time.sleep(0.3)
    		continue


    	#Move to grasp position with SDH open ---------------------------------------------
    	#pregrasp
        pre_grasp_stamped = PoseStamped();
        pre_grasp_stamped.header.frame_id = "/base_link";
        pre_grasp_stamped.pose = userdata.grasp_configuration[userdata.grasp_configuration_id].pre_grasp;
    
    	#grasp
        grasp_stamped = PoseStamped();
        grasp_stamped.header.frame_id = "/base_link";
        grasp_stamped.pose = userdata.grasp_configuration[userdata.grasp_configuration_id].grasp;
    
    	#postgrasp
        post_grasp_stamped = copy.deepcopy(grasp_stamped);
        post_grasp_stamped.pose.position.z += 0.1;

    	#offset
        """
        offset_x = 0#(userdata.grasp_configuration.grasp.position.x - userdata.grasp_configuration.pre_grasp.position.x)/3
        offset_y = 0#(userdata.grasp_configuration.grasp.position.y - userdata.grasp_configuration.pre_grasp.position.y)/3
        offset_z = 0#(userdata.grasp_configuration.grasp.position.z - userdata.grasp_configuration.pre_grasp.position.z)/3
    
    	pre_grasp_stamped.pose.position.x += offset_x
    	pre_grasp_stamped.pose.position.y += offset_y
    	pre_grasp_stamped.pose.position.z -= offset_z
    	grasp_stamped.pose.position.x += offset_x
    	grasp_stamped.pose.position.y += offset_y
    	grasp_stamped.pose.position.z -= offset_z
    	"""

	#global current_joint_configuration

        sol = False
        for w in range(0,10):
            (pre_grasp_conf, error_code) = grasping_functions.callIKSolver(self.current_joint_configuration, pre_grasp_stamped)		
            if(error_code.val == error_code.SUCCESS):
                for k in range(0,10):
                    (grasp_conf, error_code) = grasping_functions.callIKSolver(pre_grasp_conf, grasp_stamped)
                    if(error_code.val == error_code.SUCCESS):	
                        (post_grasp_conf, error_code) = grasping_functions.callIKSolver(pre_grasp_conf, post_grasp_stamped)	
                        if(error_code.val == error_code.SUCCESS):
                            sol = True
                            break
                if sol:
                    break

        if not sol:
            return 'not_completed';
        else:
            #sss.move("torso","home")
            arm_handle = sss.move("arm", [pre_grasp_conf, grasp_conf], False)
            arm_handle.wait();

            #Close SDH based on the grasp configuration to grasp.
            arm_handle = sss.move("sdh", [list(userdata.grasp_configuration[userdata.grasp_configuration_id].sdh_joint_values)], False)
            arm_handle.wait();
            rospy.sleep(2);

            #TODO: Confirm the grasp based on force feedback
            successful_grasp = False#grasping_functions.sdh_tactil_sensor_result();

            if not successful_grasp:
                #TODO: Regrasp (close MORE the fingers)
                regrasp = list(userdata.grasp_configuration[userdata.grasp_configuration_id].sdh_joint_values)
                print "Current config, trying regrasp", regrasp
                #regrasp[2] -= 0.1
                #regrasp[4] -= 0.1
                #regrasp[6] -= 0.1
                regrasp[1] += 0.07
                regrasp[3] += 0.07
                regrasp[5] += 0.07
                print "to", regrasp
                arm_handle = sss.move("sdh", [regrasp], False)
                arm_handle.wait();
                successful_grasp = True#grasping_functions.sdh_tactil_sensor_result();
                if not successful_grasp:
                    return 'not_completed'
            sss.move("arm",[post_grasp_conf,"look_at_table","hold"])
            return 'succeeded'
コード例 #2
0
    def get_grasps_from_position(self, req):
        x = time.time()
        rospy.loginfo("/get_grasps_from_position service has been called...")

        obj_id = req.object_id
        obj_pose = req.object_pose

        req = GetGraspConfigurationsRequest()
        req.object_id = obj_id
        grasp_configuration = (self.client(req)).grasp_configuration

        # current_joint_configuration
        while self.arm_state.get_num_connections() == 0:
            time.sleep(0.3)
            continue

        rotacion = grasping_functions.rotation_matrix(obj_pose)

        resp = GetGraspsFromPositionResponse()
        resp.feasible_grasp_available = False
        resp.grasp_configuration = []

        for i in range(0, len(grasp_configuration)):
            pre_trans = rotacion * grasping_functions.matrix_from_pose(grasp_configuration[i].pre_grasp.pose)
            grasp_trans = rotacion * grasping_functions.matrix_from_pose(grasp_configuration[i].grasp.pose)

            t = translation_from_matrix(pre_trans)
            q = quaternion_from_matrix(pre_trans)
            tg = translation_from_matrix(grasp_trans)
            qg = quaternion_from_matrix(grasp_trans)

            pre = PoseStamped()
            pre.header.stamp = rospy.Time.now()
            pre.header.frame_id = "/base_link"
            pre.pose.position.x = t[0]
            pre.pose.position.y = t[1]
            pre.pose.position.z = t[2]
            pre.pose.orientation.x = q[0]
            pre.pose.orientation.y = q[1]
            pre.pose.orientation.z = q[2]
            pre.pose.orientation.w = q[3]

            g = PoseStamped()
            g.header.stamp = rospy.Time.now()
            g.header.frame_id = "/base_link"
            g.pose.position.x = tg[0]
            g.pose.position.y = tg[1]
            g.pose.position.z = tg[2]
            g.pose.orientation.x = qg[0]
            g.pose.orientation.y = qg[1]
            g.pose.orientation.z = qg[2]
            g.pose.orientation.w = qg[3]

            sol = False
            for w in range(0, self.ik_loop_reply):
                (pre_grasp_conf, error_code) = grasping_functions.callIKSolver(current_joint_configuration, pre)
                if error_code.val == error_code.SUCCESS:
                    for k in range(0, self.ik_loop_reply):
                        (grasp_conf, error_code) = grasping_functions.callIKSolver(pre_grasp_conf, g)
                        if error_code.val == error_code.SUCCESS:

                            new_valid_grasp = GraspSubConfiguration()
                            new_valid_grasp.sdh_joint_values = grasp_configuration[i].sdh_joint_values
                            new_valid_grasp.grasp = g.pose
                            new_valid_grasp.pre_grasp = pre.pose
                            new_valid_grasp.category = grasping_functions.get_grasp_category(
                                pre.pose.position, g.pose.position
                            )
                            sol = grasping_functions.valid_grasp(new_valid_grasp)

                            if sol:
                                resp.feasible_grasp_available = True
                                resp.grasp_configuration.append(new_valid_grasp)
                                break
                    if sol:
                        break
                        # for
                        # for

                        # order grasps
        if len(resp.grasp_configuration) > 0:
            (resp.grasp_configuration).sort()

        rospy.loginfo(str(len(resp.grasp_configuration)) + " valid grasps for this pose.")
        rospy.loginfo("/get_grasps_from_position call has finished.")
        print "Time employed: " + str(time.time() - x)
        print "---------------------------------------"
        return resp
コード例 #3
0
    def get_grasps_from_position(self, req):
        x = time.time()
        rospy.loginfo("/get_grasps_from_position service has been called...")

        obj_id = req.object_id
        obj_pose = req.object_pose

        req = GetGraspConfigurationsRequest()
        req.object_id = obj_id
        grasp_configuration = (self.client(req)).grasp_configuration

        #current_joint_configuration
        while self.arm_state.get_num_connections() == 0:
            time.sleep(0.3)
            continue

        rotacion = grasping_functions.rotation_matrix(obj_pose)

        resp = GetGraspsFromPositionResponse()
        resp.feasible_grasp_available = False
        resp.grasp_configuration = []

        for i in range(0, len(grasp_configuration)):
            pre_trans = rotacion * grasping_functions.matrix_from_pose(
                grasp_configuration[i].pre_grasp.pose)
            grasp_trans = rotacion * grasping_functions.matrix_from_pose(
                grasp_configuration[i].grasp.pose)

            t = translation_from_matrix(pre_trans)
            q = quaternion_from_matrix(pre_trans)
            tg = translation_from_matrix(grasp_trans)
            qg = quaternion_from_matrix(grasp_trans)

            pre = PoseStamped()
            pre.header.stamp = rospy.Time.now()
            pre.header.frame_id = "/base_link"
            pre.pose.position.x = t[0]
            pre.pose.position.y = t[1]
            pre.pose.position.z = t[2]
            pre.pose.orientation.x = q[0]
            pre.pose.orientation.y = q[1]
            pre.pose.orientation.z = q[2]
            pre.pose.orientation.w = q[3]

            g = PoseStamped()
            g.header.stamp = rospy.Time.now()
            g.header.frame_id = "/base_link"
            g.pose.position.x = tg[0]
            g.pose.position.y = tg[1]
            g.pose.position.z = tg[2]
            g.pose.orientation.x = qg[0]
            g.pose.orientation.y = qg[1]
            g.pose.orientation.z = qg[2]
            g.pose.orientation.w = qg[3]

            sol = False
            for w in range(0, self.ik_loop_reply):
                (pre_grasp_conf, error_code) = grasping_functions.callIKSolver(
                    current_joint_configuration, pre)
                if (error_code.val == error_code.SUCCESS):
                    for k in range(0, self.ik_loop_reply):
                        (grasp_conf,
                         error_code) = grasping_functions.callIKSolver(
                             pre_grasp_conf, g)
                        if (error_code.val == error_code.SUCCESS):

                            new_valid_grasp = GraspSubConfiguration()
                            new_valid_grasp.sdh_joint_values = grasp_configuration[
                                i].sdh_joint_values
                            new_valid_grasp.grasp = g.pose
                            new_valid_grasp.pre_grasp = pre.pose
                            new_valid_grasp.category = grasping_functions.get_grasp_category(
                                pre.pose.position, g.pose.position)
                            sol = grasping_functions.valid_grasp(
                                new_valid_grasp)

                            if sol:
                                resp.feasible_grasp_available = True
                                resp.grasp_configuration.append(
                                    new_valid_grasp)
                                break
                    if sol:
                        break
            #for
        #for

        #order grasps
        if len(resp.grasp_configuration) > 0:
            (resp.grasp_configuration).sort()

        rospy.loginfo(
            str(len(resp.grasp_configuration)) +
            " valid grasps for this pose.")
        rospy.loginfo("/get_grasps_from_position call has finished.")
        print "Time employed: " + str(time.time() - x)
        print "---------------------------------------"
        return resp
コード例 #4
0
	def get_grasps_from_position(self, req):
		x = time.time();
		rospy.loginfo("/get_grasps_from_position service has been called...");

		obj_id = req.object_id;
		obj_pose = req.object_pose;

		req = GetGraspConfigurationsRequest();
		req.object_id = obj_id;
		grasp_configuration = (self.client(req)).grasp_configuration;

		#current_joint_configuration
		rospy.loginfo("Waiting /arm_controller/state...");
		sub = rospy.Subscriber("/arm_controller/state", JointTrajectoryControllerState, self.get_joint_state);
		while sub.get_num_connections() == 0:
			time.sleep(0.3);
			continue;
		rospy.loginfo("/arm_controller/state has finished.");

		rotacion = grasping_functions.rotation_matrix(obj_pose);

		resp = GetGraspsFromPositionResponse();
		resp.feasible_grasp_available = False;
		resp.grasp_configuration = [];

		for i in range(0,len(grasp_configuration)):
			pre_trans = rotacion * grasping_functions.matrix_from_pose(grasp_configuration[i].pre_grasp.pose);
			grasp_trans = rotacion *  grasping_functions.matrix_from_pose(grasp_configuration[i].grasp.pose);

			t = translation_from_matrix(pre_trans);
			q = quaternion_from_matrix(pre_trans);
			tg = translation_from_matrix(grasp_trans);
			qg = quaternion_from_matrix(grasp_trans);

			pre = PoseStamped();
			pre.header.stamp = rospy.Time.now();
			pre.header.frame_id = "/base_link";
			pre.pose.position.x = t[0];
			pre.pose.position.y = t[1];
			pre.pose.position.z = t[2];
			pre.pose.orientation.x = q[0];
			pre.pose.orientation.y = q[1];
			pre.pose.orientation.z = q[2];
			pre.pose.orientation.w = q[3];

			g = PoseStamped();
			g.header.stamp = rospy.Time.now();
			g.header.frame_id = "/base_link";
			g.pose.position.x = tg[0];
			g.pose.position.y = tg[1];
			g.pose.position.z = tg[2];
			g.pose.orientation.x = qg[0];
			g.pose.orientation.y = qg[1];
			g.pose.orientation.z = qg[2];
			g.pose.orientation.w = qg[3];

			offset_x = 0#(g.pose.position.x - pre.pose.position.x)/3
			offset_y = 0#(g.pose.position.y - pre.pose.position.y)/3
			offset_z = 0#(g.pose.position.z - pre.pose.position.z)/3

			pre.pose.position.x += offset_x
			pre.pose.position.y += offset_y
			pre.pose.position.z -= offset_z
			g.pose.position.x += offset_x
			g.pose.position.y += offset_y
			g.pose.position.z -= offset_z
			

		
			sol = False;
			for w in range(0,self.ik_loop_reply):
				(pre_grasp_conf, error_code) = grasping_functions.callIKSolver(current_joint_configuration, pre);		
				if(error_code.val == error_code.SUCCESS):
					for k in range(0,self.ik_loop_reply):
						(grasp_conf, error_code) = grasping_functions.callIKSolver(pre_grasp_conf, g);
						if(error_code.val == error_code.SUCCESS):

							new_valid_grasp = GraspSubConfiguration();
							new_valid_grasp.sdh_joint_values = grasp_configuration[i].sdh_joint_values;
							new_valid_grasp.grasp = g.pose;
							new_valid_grasp.pre_grasp = pre.pose;
							new_valid_grasp.category = grasping_functions.get_grasp_category(pre.pose.position, g.pose.position);
							sol = grasping_functions.valid_grasp(new_valid_grasp);

							if sol:
								resp.feasible_grasp_available = True;
								resp.grasp_configuration.append(new_valid_grasp);
								break;
					if sol:
						break;
			#for
		#for

		#order grasps
		if len(resp.grasp_configuration) > 0:
			(resp.grasp_configuration).sort();

		rospy.loginfo(str(len(resp.grasp_configuration))+" valid grasps for this pose.");	
		rospy.loginfo("/get_grasps_from_position call has finished.");
		print "Time employed: " + str(time.time() - x);
		print "---------------------------------------";
		return resp;
コード例 #5
0
    def get_pregrasps(self, req):
        x = time.time()
        rospy.loginfo("/get_pregrasps service has been called...")

        obj_id = req.object_id
        obj_pose = req.object_pose
        num_configurations = req.num_configurations

        req = GetGraspConfigurationsRequest()
        req.object_id = obj_id
        grasp_configuration = (self.client(req)).grasp_configuration

        rotacion = grasping_functions.rotation_matrix(obj_pose)

        resp = GetPreGraspResponse()
        resp.side = []
        resp.mside = []
        resp.top = []
        resp.front = []

        for i in range(0, len(grasp_configuration)):
            pre_trans = rotacion * grasping_functions.matrix_from_pose(
                grasp_configuration[i].pre_grasp.pose)
            grasp_trans = rotacion * grasping_functions.matrix_from_pose(
                grasp_configuration[i].grasp.pose)

            t = translation_from_matrix(pre_trans)
            q = quaternion_from_matrix(pre_trans)
            tg = translation_from_matrix(grasp_trans)
            qg = quaternion_from_matrix(grasp_trans)

            pre = Pose()
            pre.position.x = t[0]
            pre.position.y = t[1]
            pre.position.z = t[2]
            pre.orientation.x = q[0]
            pre.orientation.y = q[1]
            pre.orientation.z = q[2]
            pre.orientation.w = q[3]

            g = Pose()
            g.position.x = tg[0]
            g.position.y = tg[1]
            g.position.z = tg[2]
            g.orientation.x = qg[0]
            g.orientation.y = qg[1]
            g.orientation.z = qg[2]
            g.orientation.w = qg[3]

            aux = GraspConfiguration()
            aux.object_id = obj_id
            aux.hand_type = "SDH"
            aux.sdh_joint_values = grasp_configuration[i].sdh_joint_values
            aux.target_link = "/sdh_palm_link"
            aux.pre_grasp.pose = pre
            aux.grasp.pose = g
            aux.category = grasping_functions.get_grasp_category(
                pre.position, g.position)

            if aux.category == "TOP":
                if len(resp.top) < num_configurations:
                    resp.top.append(aux)
            elif aux.category == "FRONT":
                if len(resp.front) < num_configurations:
                    resp.front.append(aux)
            elif aux.category == "SIDE":
                if len(resp.side) < num_configurations:
                    resp.side.append(aux)
            elif aux.category == "-SIDE":
                if len(resp.mside) < num_configurations:
                    resp.mside.append(aux)
            else:
                continue

            if len(resp.top) == num_configurations and len(
                    resp.side) == num_configurations and len(
                        resp.mside) == num_configurations and len(
                            resp.front) == num_configurations:
                break

        rospy.loginfo("/get_pregrasps call has finished.")
        print "Time employed: " + str(time.time() - x)
        print "---------------------------------------"
        return resp
コード例 #6
0
    def execute_cb(self, server_goal):
        x = time.time()
        rospy.loginfo("/get_grasps_from_position_server has been called...")

        obj_id = server_goal.object_id
        obj = server_goal.object_pose

        goal = GraspCGoal()
        goal.object_id = obj_id
        self.client.send_goal(goal)
        self.client.wait_for_result(rospy.Duration.from_sec(5.0))
        grasp_configuration = (self.client.get_result()).grasp_configuration

        # current_joint_configuration
        rospy.loginfo("Waiting /arm_controller/state...")
        sub = rospy.Subscriber("/arm_controller/state", JointTrajectoryControllerState, self.get_joint_state)
        while sub.get_num_connections() == 0:
            time.sleep(0.3)
            continue
        rospy.loginfo("/arm_controller/state has finished.")

        rotacion = grasping_functions.rotation_matrix(obj)

        server_result = GraspFActionResult().result
        server_result.feasible_grasp_available = False
        server_result.grasp_configuration = []

        for i in range(0, len(grasp_configuration)):
            pre_trans = rotacion * grasping_functions.matrix_from_pose(grasp_configuration[i].pre_grasp.pose)
            grasp_trans = rotacion * grasping_functions.matrix_from_pose(grasp_configuration[i].grasp.pose)

            t = translation_from_matrix(pre_trans)
            q = quaternion_from_matrix(pre_trans)
            tg = translation_from_matrix(grasp_trans)
            qg = quaternion_from_matrix(grasp_trans)

            pre = PoseStamped()
            pre.header.stamp = rospy.Time.now()
            pre.header.frame_id = "/base_link"
            pre.pose.position.x = t[0]
            pre.pose.position.y = t[1]
            pre.pose.position.z = t[2]
            pre.pose.orientation.x = q[0]
            pre.pose.orientation.y = q[1]
            pre.pose.orientation.z = q[2]
            pre.pose.orientation.w = q[3]

            g = PoseStamped()
            g.header.stamp = rospy.Time.now()
            g.header.frame_id = "/base_link"
            g.pose.position.x = tg[0]
            g.pose.position.y = tg[1]
            g.pose.position.z = tg[2]
            g.pose.orientation.x = qg[0]
            g.pose.orientation.y = qg[1]
            g.pose.orientation.z = qg[2]
            g.pose.orientation.w = qg[3]

            offset_x = 0  # (g.pose.position.x - pre.pose.position.x)/3
            offset_y = 0  # (g.pose.position.y - pre.pose.position.y)/3
            offset_z = 0  # (g.pose.position.z - pre.pose.position.z)/3

            pre.pose.position.x += offset_x
            pre.pose.position.y += offset_y
            pre.pose.position.z -= offset_z
            g.pose.position.x += offset_x
            g.pose.position.y += offset_y
            g.pose.position.z -= offset_z

            sol = False
            for w in range(0, self.ik_loop_reply):
                (pre_grasp_conf, error_code) = grasping_functions.callIKSolver(current_joint_configuration, pre)
                if error_code.val == error_code.SUCCESS:
                    for k in range(0, self.ik_loop_reply):
                        (grasp_conf, error_code) = grasping_functions.callIKSolver(pre_grasp_conf, g)
                        if error_code.val == error_code.SUCCESS:
                            print i
                            new_valid_grasp = GraspSubConfiguration()
                            new_valid_grasp.sdh_joint_values = grasp_configuration[i].sdh_joint_values
                            new_valid_grasp.grasp = g.pose
                            new_valid_grasp.pre_grasp = pre.pose
                            new_valid_grasp.category = grasping_functions.get_grasp_category(
                                pre.pose.position, g.pose.position
                            )

                            server_result.feasible_grasp_available = True
                            server_result.grasp_configuration.append(new_valid_grasp)

                            sol = True
                            break
                    if sol:
                        break
                        # for
                        # while

                        # order grasps
        if len(server_result.grasp_configuration) > 0:
            (server_result.grasp_configuration).sort()

        rospy.loginfo(str(len(server_result.grasp_configuration)) + " valid grasps for this pose.")
        rospy.loginfo("/get_grasps_from_position call has finished.")
        print "Time employed: " + str(time.time() - x)
        print "---------------------------------------"
        self.get_grasps_from_position_server.set_succeeded(server_result)
コード例 #7
0
ファイル: get_pregrasps.py プロジェクト: Mazet/srs_public
	def get_pregrasps(self, req):
		x = time.time();
		rospy.loginfo("/get_pregrasps service has been called...");

		obj_id = req.object_id;
		obj_pose = req.object_pose;
		num_configurations = req.num_configurations;

		req = GetGraspConfigurationsRequest();
		req.object_id = obj_id;
		grasp_configuration = (self.client(req)).grasp_configuration;


		rotacion = grasping_functions.rotation_matrix(obj_pose);

		resp = GetPreGraspResponse();
		resp.side = []
		resp.mside = []
		resp.top = []
		resp.front = []

		for i in range(0,len(grasp_configuration)):
			pre_trans = rotacion * grasping_functions.matrix_from_pose(grasp_configuration[i].pre_grasp.pose);
			grasp_trans = rotacion *  grasping_functions.matrix_from_pose(grasp_configuration[i].grasp.pose);

			t = translation_from_matrix(pre_trans);
			q = quaternion_from_matrix(pre_trans);
			tg = translation_from_matrix(grasp_trans);
			qg = quaternion_from_matrix(grasp_trans);

			pre = Pose();
			pre.position.x = t[0];
			pre.position.y = t[1];
			pre.position.z = t[2];
			pre.orientation.x = q[0];
			pre.orientation.y = q[1];
			pre.orientation.z = q[2];
			pre.orientation.w = q[3];

			g = Pose();
			g.position.x = tg[0];
			g.position.y = tg[1];
			g.position.z = tg[2];
			g.orientation.x = qg[0];
			g.orientation.y = qg[1];
			g.orientation.z = qg[2];
			g.orientation.w = qg[3];



			aux = GraspConfiguration();
			aux.object_id = obj_id;
			aux.hand_type = "SDH";
			aux.sdh_joint_values = grasp_configuration[i].sdh_joint_values;
			aux.target_link = "/sdh_palm_link";
			aux.pre_grasp.pose = pre;
			aux.grasp.pose = g;
			aux.category = grasping_functions.get_grasp_category(pre.position, g.position);

			if aux.category == "TOP":
				if len(resp.top) < num_configurations:
					resp.top.append(aux);
			elif aux.category == "FRONT":
				if len(resp.front) < num_configurations:
					resp.front.append(aux);
			elif aux.category == "SIDE":
				if len(resp.side) < num_configurations:
					resp.side.append(aux);
			elif aux.category == "-SIDE":
				if len(resp.mside) < num_configurations:
					resp.mside.append(aux);
			else:
				continue

			if len(resp.top)==num_configurations and len(resp.side)==num_configurations and len(resp.mside)==num_configurations and len(resp.front)==num_configurations:
				break;

		rospy.loginfo("/get_pregrasps call has finished.");
		print "Time employed: " + str(time.time() - x);
		print "---------------------------------------";
		return resp;
コード例 #8
0
    def execute_cb(self, server_goal):
        x = time.time()
        rospy.loginfo("/get_grasps_from_position_server has been called...")

        obj_id = server_goal.object_id
        obj = server_goal.object_pose

        goal = GraspCGoal()
        goal.object_id = obj_id
        self.client.send_goal(goal)
        self.client.wait_for_result(rospy.Duration.from_sec(5.0))
        grasp_configuration = (self.client.get_result()).grasp_configuration

        #current_joint_configuration
        rospy.loginfo("Waiting /arm_controller/state...")
        sub = rospy.Subscriber("/arm_controller/state",
                               JointTrajectoryControllerState,
                               self.get_joint_state)
        while sub.get_num_connections() == 0:
            time.sleep(0.3)
            continue
        rospy.loginfo("/arm_controller/state has finished.")

        rotacion = grasping_functions.rotation_matrix(obj)

        server_result = GraspFActionResult().result
        server_result.feasible_grasp_available = False
        server_result.grasp_configuration = []

        for i in range(0, len(grasp_configuration)):
            pre_trans = rotacion * grasping_functions.matrix_from_pose(
                grasp_configuration[i].pre_grasp.pose)
            grasp_trans = rotacion * grasping_functions.matrix_from_pose(
                grasp_configuration[i].grasp.pose)

            t = translation_from_matrix(pre_trans)
            q = quaternion_from_matrix(pre_trans)
            tg = translation_from_matrix(grasp_trans)
            qg = quaternion_from_matrix(grasp_trans)

            pre = PoseStamped()
            pre.header.stamp = rospy.Time.now()
            pre.header.frame_id = "/base_link"
            pre.pose.position.x = t[0]
            pre.pose.position.y = t[1]
            pre.pose.position.z = t[2]
            pre.pose.orientation.x = q[0]
            pre.pose.orientation.y = q[1]
            pre.pose.orientation.z = q[2]
            pre.pose.orientation.w = q[3]

            g = PoseStamped()
            g.header.stamp = rospy.Time.now()
            g.header.frame_id = "/base_link"
            g.pose.position.x = tg[0]
            g.pose.position.y = tg[1]
            g.pose.position.z = tg[2]
            g.pose.orientation.x = qg[0]
            g.pose.orientation.y = qg[1]
            g.pose.orientation.z = qg[2]
            g.pose.orientation.w = qg[3]

            offset_x = 0  #(g.pose.position.x - pre.pose.position.x)/3
            offset_y = 0  #(g.pose.position.y - pre.pose.position.y)/3
            offset_z = 0  #(g.pose.position.z - pre.pose.position.z)/3

            pre.pose.position.x += offset_x
            pre.pose.position.y += offset_y
            pre.pose.position.z -= offset_z
            g.pose.position.x += offset_x
            g.pose.position.y += offset_y
            g.pose.position.z -= offset_z

            sol = False
            for w in range(0, self.ik_loop_reply):
                (pre_grasp_conf, error_code) = grasping_functions.callIKSolver(
                    current_joint_configuration, pre)
                if (error_code.val == error_code.SUCCESS):
                    for k in range(0, self.ik_loop_reply):
                        (grasp_conf,
                         error_code) = grasping_functions.callIKSolver(
                             pre_grasp_conf, g)
                        if (error_code.val == error_code.SUCCESS):
                            new_valid_grasp = GraspSubConfiguration()
                            new_valid_grasp.sdh_joint_values = grasp_configuration[
                                i].sdh_joint_values
                            new_valid_grasp.grasp = g.pose
                            new_valid_grasp.pre_grasp = pre.pose
                            new_valid_grasp.category = grasping_functions.get_grasp_category(
                                pre.pose.position, g.pose.position)

                            sol = grasping_functions.valid_grasp(
                                new_valid_grasp)

                            if sol:
                                server_result.feasible_grasp_available = True
                                server_result.grasp_configuration.append(
                                    new_valid_grasp)
                                break
                    if sol:
                        break
            #for
        #while

        #order grasps
        if len(server_result.grasp_configuration) > 0:
            (server_result.grasp_configuration).sort()

        rospy.loginfo(
            str(len(server_result.grasp_configuration)) +
            " valid grasps for this pose.")
        rospy.loginfo("/get_grasps_from_position call has finished.")
        print "Time employed: " + str(time.time() - x)
        print "---------------------------------------"
        self.get_grasps_from_position_server.set_succeeded(server_result)