Example #1
0
    def test_grasp_quality_functions(self):
        num_grasps = NUM_TEST_CASES
        of = ObjFile(OBJ_FILENAME)
        sf = SdfFile(SDF_FILENAME)
        mesh = of.read()
        sdf = sf.read()
        obj = GraspableObject3D(sdf, mesh)

        gripper = RobotGripper.load(GRIPPER_NAME)

        ags = UniformGraspSampler(gripper, CONFIG)
        grasps = ags.generate_grasps(obj, target_num_grasps=num_grasps)

        # test with grasp quality function
        quality_config = GraspQualityConfigFactory.create_config(
            CONFIG['metrics']['force_closure'])
        quality_fn = GraspQualityFunctionFactory.create_quality_function(
            obj, quality_config)

        for i, grasp in enumerate(grasps):
            success, c = grasp.close_fingers(obj)
            if success:
                c1, c2 = c
                fn_fc = quality_fn(grasp).quality
                true_fc = PointGraspMetrics3D.force_closure(
                    c1, c2, quality_config.friction_coef)
                self.assertEqual(fn_fc, true_fc)
Example #2
0
def grasp_quality_calculator(mesh, sdf, grasps, metric):
    obj = GraspableObject3D(sdf, mesh)

    quality_config = GraspQualityConfigFactory.create_config(
        CONFIG['metrics'][metric])
    quality_function = GraspQualityFunctionFactory.create_quality_function(
        obj, quality_config)

    metrics = []

    for grasp in grasps:
        quality = quality_function.quality(grasp)
        metrics.append(copy.deepcopy(quality.quality))
    return metrics
Example #3
0
    def antipodal_grasp_sampler(self):
        of = ObjFile(OBJ_FILENAME)
        sf = SdfFile(SDF_FILENAME)
        mesh = of.read()
        sdf = sf.read()
        obj = GraspableObject3D(sdf, mesh)

        gripper = RobotGripper.load(GRIPPER_NAME)

        ags = AntipodalGraspSampler(gripper, CONFIG)
        grasps = ags.generate_grasps(obj, target_num_grasps=10)

        quality_config = GraspQualityConfigFactory.create_config(
            CONFIG['metrics']['force_closure'])
        quality_fn = GraspQualityFunctionFactory.create_quality_function(
            obj, quality_config)

        q_to_c = lambda quality: CONFIG['quality_scale']

        i = 0
        vis.figure()
        vis.mesh(obj.mesh.trimesh, style='surface')
        for grasp in grasps:
            print(grasp.frame)
            success, c = grasp.close_fingers(obj)
            if success:
                c1, c2 = c
                fn_fc = quality_fn(grasp).quality
                true_fc = PointGraspMetrics3D.force_closure(
                    c1, c2, quality_config.friction_coef)
        #print(grasp)
            T_obj_world = RigidTransform(from_frame='obj', to_frame='world')
            color = plt.get_cmap('hsv')(q_to_c(CONFIG['metrics']))[:-1]
            T_obj_gripper = grasp.gripper_pose(gripper)
            #vis.grasp(grasp,grasp_axis_color=color,endpoint_color=color)
            i += 1
        #T_obj_world = RigidTransform(from_frame='obj',to_frame='world')
        vis.show(False)
Example #4
0
def antipodal_grasp_sampler_for_storing_stable_poses(mesh, sdf, stable_poses):
    mass = 1.0
    CONFIG['obj_rescaling_type'] = RescalingType.RELATIVE
    obj = GraspableObject3D(sdf, mesh)

    gripper = RobotGripper.load(
        GRIPPER_NAME, gripper_dir='/home/silvia/dex-net/data/grippers')

    ags = AntipodalGraspSampler(gripper, CONFIG)

    quality_config = GraspQualityConfigFactory.create_config(
        CONFIG['metrics']['force_closure'])
    quality_function = GraspQualityFunctionFactory.create_quality_function(
        obj, quality_config)

    max_poses = len(stable_poses)
    grasps = [None] * max_poses
    metrics = [None] * max_poses
    all_grasps = ags.generate_grasps(obj, target_num_grasps=200, max_iter=4)

    for id, stable_pose in enumerate(stable_poses):
        print('sampling for stable pose: ', id)
        if id == max_poses:
            break
        grasps_pose = filter(
            lambda x: grasp_is_parallel_to_table(x.axis, stable_pose.r),
            all_grasps)
        #grasps_pose = ags.generate_grasps(obj,target_num_grasps=20, max_iter=5, stable_pose=stable_pose.r)
        grasps[id] = []
        metrics[id] = []
        for grasp in grasps_pose:
            quality = quality_function.quality(grasp)
            #quality = PointGraspMetrics3D.grasp_quality(grasp, obj, quality_config)
            grasps[id].append(copy.deepcopy(grasp))
            metrics[id].append(copy.deepcopy(quality.quality))
    return grasps, metrics
Example #5
0
def worker(i, sample_nums, grasp_amount, good_grasp):
    object_name = file_list_all[i][len(home_dir) + 35:]
    print('a worker of task {} start'.format(object_name))

    yaml_config = YamlConfig(home_dir +
                             "/code/grasp-pointnet/dex-net/test/config.yaml")
    gripper_name = 'robotiq_85'
    gripper = RobotGripper.load(
        gripper_name, home_dir + "/code/grasp-pointnet/dex-net/data/grippers")
    grasp_sample_method = "antipodal"
    if grasp_sample_method == "uniform":
        ags = UniformGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "gaussian":
        ags = GaussianGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "antipodal":
        ags = AntipodalGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "gpg":
        ags = GpgGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "point":
        ags = PointGraspSampler(gripper, yaml_config)
    else:
        raise NameError("Can't support this sampler")
    print("Log: do job", i)
    if os.path.exists(str(file_list_all[i]) + "/google_512k/nontextured.obj"):
        of = ObjFile(str(file_list_all[i]) + "/google_512k/nontextured.obj")
        sf = SdfFile(str(file_list_all[i]) + "/google_512k/nontextured.sdf")
    else:
        print("can't find any obj or sdf file!")
        raise NameError("can't find any obj or sdf file!")
    mesh = of.read()
    sdf = sf.read()
    obj = GraspableObject3D(sdf, mesh)
    print("Log: opened object", i + 1, object_name)

    force_closure_quality_config = {}
    canny_quality_config = {}
    fc_list_sub1 = np.arange(2.0, 0.75, -0.4)
    fc_list_sub2 = np.arange(0.5, 0.36, -0.05)
    fc_list = np.concatenate([fc_list_sub1, fc_list_sub2])
    for value_fc in fc_list:
        value_fc = round(value_fc, 2)
        yaml_config['metrics']['force_closure']['friction_coef'] = value_fc
        yaml_config['metrics']['robust_ferrari_canny'][
            'friction_coef'] = value_fc

        force_closure_quality_config[
            value_fc] = GraspQualityConfigFactory.create_config(
                yaml_config['metrics']['force_closure'])
        canny_quality_config[
            value_fc] = GraspQualityConfigFactory.create_config(
                yaml_config['metrics']['robust_ferrari_canny'])

    good_count_perfect = np.zeros(len(fc_list))
    count = 0
    minimum_grasp_per_fc = grasp_amount
    while np.sum(good_count_perfect < minimum_grasp_per_fc) != 0:
        grasps = ags.generate_grasps(obj,
                                     target_num_grasps=sample_nums,
                                     grasp_gen_mult=10,
                                     vis=False,
                                     random_approach_angle=True)
        count += len(grasps)
        for j in grasps:
            tmp, is_force_closure = False, False
            for ind_, value_fc in enumerate(fc_list):
                value_fc = round(value_fc, 2)
                tmp = is_force_closure
                is_force_closure = PointGraspMetrics3D.grasp_quality(
                    j, obj, force_closure_quality_config[value_fc], vis=False)
                if tmp and not is_force_closure:
                    if good_count_perfect[ind_ - 1] < minimum_grasp_per_fc:
                        canny_quality = PointGraspMetrics3D.grasp_quality(
                            j,
                            obj,
                            canny_quality_config[round(fc_list[ind_ - 1], 2)],
                            vis=False)
                        good_grasp.append((j, round(fc_list[ind_ - 1],
                                                    2), canny_quality))
                        good_count_perfect[ind_ - 1] += 1
                    break
                elif is_force_closure and value_fc == fc_list[-1]:
                    if good_count_perfect[ind_] < minimum_grasp_per_fc:
                        canny_quality = PointGraspMetrics3D.grasp_quality(
                            j, obj, canny_quality_config[value_fc], vis=False)
                        good_grasp.append((j, value_fc, canny_quality))
                        good_count_perfect[ind_] += 1
                    break
        print('Object:{} GoodGrasp:{}'.format(object_name, good_count_perfect))

    object_name_len = len(object_name)
    object_name_ = str(object_name) + " " * (25 - object_name_len)
    if count == 0:
        good_grasp_rate = 0
    else:
        good_grasp_rate = len(good_grasp) / count
    print('Gripper:{} Object:{} Rate:{:.4f} {}/{}'.format(
        gripper_name, object_name_, good_grasp_rate, len(good_grasp), count))
def worker(i, sample_nums, grasp_amount, good_grasp):  #主要是抓取采样器以及打分    100  20
    """
    brief: 对制定的模型,利用随机采样算法,进行抓取姿态的检测和打分
    param [in]  i 处理第i个mesh模型
    param [in]  sample_nums 每个对象模型返回的目标抓取数量
    param [in]  grasp_amount
    """

    #截取目标对象名称
    object_name = file_list_all[i][len(home_dir) + 35:]
    print('a worker of task {} start'.format(object_name))

    #读取初始配置文件,读取并在内存中复制了一份
    yaml_config = YamlConfig(home_dir + "/code/dex-net/test/config.yaml")
    #设置夹名称
    gripper_name = 'panda'
    #根据设置的夹爪名称加载夹爪配置
    gripper = RobotGripper.load(gripper_name,
                                home_dir + "/code/dex-net/data/grippers")
    #设置抓取采样的方法
    grasp_sample_method = "antipodal"
    if grasp_sample_method == "uniform":
        ags = UniformGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "gaussian":
        ags = GaussianGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "antipodal":
        #使用对映点抓取,输入夹爪与配置文件
        ags = AntipodalGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "gpg":
        ags = GpgGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "point":
        ags = PointGraspSampler(gripper, yaml_config)
    else:
        raise NameError("Can't support this sampler")
    print("Log: do job", i)
    #设置obj模型文件与sdf文件路径
    if os.path.exists(str(file_list_all[i]) +
                      "/google_512k/nontextured.obj") and os.path.exists(
                          str(file_list_all[i]) +
                          "/google_512k/nontextured.sdf"):
        of = ObjFile(str(file_list_all[i]) + "/google_512k/nontextured.obj")
        sf = SdfFile(str(file_list_all[i]) + "/google_512k/nontextured.sdf")
    else:
        print("can't find any obj or sdf file!")
        raise NameError("can't find any obj or sdf file!")

    #根据路径读取模型与sdf文件
    mesh = of.read()
    sdf = sf.read()
    #构建抓取模型类
    obj = GraspableObject3D(sdf, mesh)
    print("Log: opened object", i + 1, object_name)

    #########################################
    #设置
    force_closure_quality_config = {}  #设置力闭合  字典
    canny_quality_config = {}
    #生成一个起点是2.0终点是0.75   步长为-0.4  (递减)的等距数列fc_list_sub1 (2.0, 0.75, -0.4)
    fc_list_sub1 = np.arange(2.0, 0.75, -0.3)
    #生成一个起点是0.5终点是0.36   步长为-0.05的等距数列fc_list_sub2  (0.5, 0.36, -0.05)
    fc_list_sub2 = np.arange(0.5, 0.36, -0.1)

    #将上面两个向量接起来,变成一个长条向量,使用不同的步长,目的是为了在更小摩擦力的时候,有更多的分辨率
    fc_list = np.concatenate([fc_list_sub1, fc_list_sub2])
    print("判断摩擦系数")
    print(fc_list)
    for value_fc in fc_list:
        #对value_fc保留2位小数,四舍五入
        value_fc = round(value_fc, 2)
        #更改内存中配置中的摩擦系数,而没有修改硬盘中的yaml文件
        yaml_config['metrics']['force_closure']['friction_coef'] = value_fc
        yaml_config['metrics']['robust_ferrari_canny'][
            'friction_coef'] = value_fc
        #把每个摩擦力值当成键,
        force_closure_quality_config[
            value_fc] = GraspQualityConfigFactory.create_config(
                yaml_config['metrics']['force_closure'])
        canny_quality_config[
            value_fc] = GraspQualityConfigFactory.create_config(
                yaml_config['metrics']['robust_ferrari_canny'])

    #####################准备开始采样############################
    #填充一个与摩擦数量相同的数组,每个对应的元素都是0
    good_count_perfect = np.zeros(len(fc_list))
    count = 0
    #设置每个摩擦值需要计算的最少抓取数量 (根据指定输入值20)
    minimum_grasp_per_fc = grasp_amount
    #如果每个摩擦系数下,有效的抓取(满足力闭合或者其他判断标准)小于要求值,就一直循环查找,直到所有摩擦系数条件下至少都存在20个有效抓取
    while np.sum(good_count_perfect < minimum_grasp_per_fc) != 0:
        #开始使用antipodes sample获得对映随机抓取,此时并不判断是否满足力闭合,只是先采集满足夹爪条件的抓取
        #如果一轮多次随机采样之后,发现无法获得指定数量的随机抓取,就会重复迭代计算3次,之后放弃,并把已经找到的抓取返回来
        grasps = ags.generate_grasps(obj,
                                     target_num_grasps=sample_nums,
                                     grasp_gen_mult=10,
                                     max_iter=10,
                                     vis=False,
                                     random_approach_angle=True)
        count += len(grasps)
        #循环对每个采样抓取进行判断
        for j in grasps:
            tmp, is_force_closure = False, False
            #循环对某个采样抓取应用不同的抓取摩擦系数,判断是否是力闭合
            for ind_, value_fc in enumerate(fc_list):
                value_fc = round(value_fc, 2)
                tmp = is_force_closure
                #判断在当前给定的摩擦系数下,抓取是否是力闭合的
                is_force_closure = PointGraspMetrics3D.grasp_quality(
                    j, obj, force_closure_quality_config[value_fc], vis=False)
                #假设当前,1号摩擦力为1.6 抓取不是力闭合的,但是上一个0号摩擦系数2.0 条件下抓取是力闭合的
                if tmp and not is_force_closure:
                    #当0号2.0摩擦系数条件下采样的good抓取数量还不足指定的最低数量20
                    if good_count_perfect[ind_ - 1] < minimum_grasp_per_fc:
                        #以0号摩擦系数作为边界
                        canny_quality = PointGraspMetrics3D.grasp_quality(
                            j,
                            obj,
                            canny_quality_config[round(fc_list[ind_ - 1], 2)],
                            vis=False)
                        good_grasp.append((j, round(fc_list[ind_ - 1],
                                                    2), canny_quality))
                        #在0号系数的good抓取下计数加1
                        good_count_perfect[ind_ - 1] += 1
                    #当前抓取j的边界摩擦系数找到了,退出摩擦循环,判断下一个抓取
                    break
                #如果当前1号摩擦系数1.6条件下,该抓取j本身就是力闭合的,且摩擦系数是列表中的最后一个(所有的摩擦系数都判断完了)
                elif is_force_closure and value_fc == fc_list[-1]:
                    if good_count_perfect[ind_] < minimum_grasp_per_fc:
                        #以当前摩擦系数作为边界
                        canny_quality = PointGraspMetrics3D.grasp_quality(
                            j, obj, canny_quality_config[value_fc], vis=False)
                        good_grasp.append((j, value_fc, canny_quality))
                        good_count_perfect[ind_] += 1
                    #当前抓取j关于当前摩擦系数1.6判断完毕,而且满足所有的摩擦系数,就换到下一个摩擦系数
                    break
        print('Object:{} GoodGrasp:{}'.format(object_name,
                                              good_count_perfect))  #判断

    object_name_len = len(object_name)
    object_name_ = str(object_name) + " " * (25 - object_name_len)
    if count == 0:
        good_grasp_rate = 0
    else:
        good_grasp_rate = len(good_grasp) / count
    print('Gripper:{} Object:{} Rate:{:.4f} {}/{}'.format(
        gripper_name, object_name_, good_grasp_rate, len(good_grasp), count))
Example #7
0
def worker(i, sample_nums, grasp_amount, good_grasp):
    object_name = file_list_all[i][len(home_dir) + 48:-12]
    print('a worker of task {} start'.format(object_name))

    yaml_config = YamlConfig(home_dir +
                             "/Projects/PointNetGPD/dex-net/test/config.yaml")
    gripper_name = 'robotiq_85'
    gripper = RobotGripper.load(
        gripper_name, home_dir + "/Projects/PointNetGPD/dex-net/data/grippers")
    grasp_sample_method = "antipodal"
    if grasp_sample_method == "uniform":
        ags = UniformGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "gaussian":
        ags = GaussianGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "antipodal":
        ags = AntipodalGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "gpg":
        ags = GpgGraspSampler(gripper, yaml_config)
    elif grasp_sample_method == "point":
        ags = PointGraspSampler(gripper, yaml_config)
    else:
        raise NameError("Can't support this sampler")
    print("Log: do job", i)
    if os.path.exists(str(file_list_all[i]) + "/nontextured.obj"):
        of = ObjFile(str(file_list_all[i]) + "/nontextured.obj")
        sf = SdfFile(str(file_list_all[i]) + "/nontextured.sdf")
    else:
        print("can't find any obj or sdf file!")
        raise NameError("can't find any obj or sdf file!")
    mesh = of.read()
    sdf = sf.read()
    obj = GraspableObject3D(sdf, mesh)
    print("Log: opened object", i + 1, object_name)

    force_closure_quality_config = {}
    canny_quality_config = {}
    fc_list_sub1 = np.arange(2.0, 0.75, -0.4)
    fc_list_sub2 = np.arange(0.5, 0.36, -0.05)
    fc_list = np.concatenate(
        [fc_list_sub1,
         fc_list_sub2])  # 摩擦系数列表  fc_list [2.  1.6  1.2  0.8  0.5  0.45 0.4 ]
    print("fc_list", fc_list, "fc_list[-1]", fc_list[-1])
    for value_fc in fc_list:
        value_fc = round(value_fc, 2)
        yaml_config['metrics']['force_closure']['friction_coef'] = value_fc
        yaml_config['metrics']['robust_ferrari_canny'][
            'friction_coef'] = value_fc

        force_closure_quality_config[
            value_fc] = GraspQualityConfigFactory.create_config(
                yaml_config['metrics']['force_closure'])
        canny_quality_config[
            value_fc] = GraspQualityConfigFactory.create_config(
                yaml_config['metrics']['robust_ferrari_canny'])

    good_count_perfect = np.zeros(len(fc_list))
    count = 0
    minimum_grasp_per_fc = grasp_amount

    # 各个摩擦系数生成一些抓取
    while np.sum(good_count_perfect < minimum_grasp_per_fc
                 ) != 0 and good_count_perfect[-1] < minimum_grasp_per_fc:
        print("[INFO]:good | mini", good_count_perfect, minimum_grasp_per_fc)
        print("[INFO]:good < mini", good_count_perfect < minimum_grasp_per_fc,
              np.sum(good_count_perfect < minimum_grasp_per_fc))
        grasps = ags.generate_grasps(
            obj,
            target_num_grasps=sample_nums,
            grasp_gen_mult=10,  # 生成抓取姿态
            vis=False,
            random_approach_angle=True)  # 随机调整抓取方向
        print("\033[0;32m%s\033[0m" %
              "[INFO] Worker{} generate {} grasps.".format(i, len(grasps)))
        count += len(grasps)
        for j in grasps:  # 遍历生成的抓取姿态, 判断是否为力闭合, 及其对应的摩擦系数
            tmp, is_force_closure = False, False
            for ind_, value_fc in enumerate(fc_list):  # 为每个摩擦系数分配抓取姿态
                value_fc = round(value_fc, 2)
                tmp = is_force_closure
                is_force_closure = PointGraspMetrics3D.grasp_quality(
                    j,
                    obj,  # 依据摩擦系数 value_fc 评估抓取姿态
                    force_closure_quality_config[value_fc],
                    vis=False)
                print("[INFO] is_force_closure:", is_force_closure,
                      "value_fc:", value_fc, "tmp:", tmp)
                if tmp and not is_force_closure:  # 前一个摩擦系数下为力闭合, 当前摩擦系数下非力闭合, 即找到此抓取对应的最小摩擦系数
                    print("[debug] tmp and not is_force_closure")
                    if good_count_perfect[ind_ - 1] < minimum_grasp_per_fc:
                        canny_quality = PointGraspMetrics3D.grasp_quality(
                            j,
                            obj,
                            canny_quality_config[round(fc_list[ind_ - 1], 2)],
                            vis=False)
                        good_grasp.append(
                            (j, round(fc_list[ind_ - 1],
                                      2), canny_quality))  # 保存前一个抓取
                        good_count_perfect[ind_ - 1] += 1
                    break

                elif is_force_closure and value_fc == fc_list[
                        -1]:  # 力闭合并且摩擦系数最小
                    print(
                        "[debug] is_force_closure and value_fc == fc_list[-1]")
                    if good_count_perfect[ind_] < minimum_grasp_per_fc:
                        canny_quality = PointGraspMetrics3D.grasp_quality(
                            j, obj, canny_quality_config[value_fc], vis=False)
                        good_grasp.append((j, value_fc, canny_quality))
                        good_count_perfect[ind_] += 1
                    break
        print('Worker:', i,
              'Object:{} GoodGrasp:{}'.format(object_name, good_count_perfect))

    object_name_len = len(object_name)
    object_name_ = str(object_name) + " " * (25 - object_name_len)
    if count == 0:
        good_grasp_rate = 0
    else:
        good_grasp_rate = len(good_grasp) / count
    print(
        'Worker:', i, 'Gripper:{} Object:{} Rate:{:.4f} {}/{}'.format(
            gripper_name, object_name_, good_grasp_rate, len(good_grasp),
            count))
Example #8
0
    def antipodal_grasp_sampler(self):
    	#of = ObjFile(OBJ_FILENAME)
    	#sf = SdfFile(SDF_FILENAME)
    	#mesh = of.read()
    	#sdf = sf.read()
        #obj = GraspableObject3D(sdf, mesh)
        mass = 1.0
        CONFIG['obj_rescaling_type'] = RescalingType.RELATIVE
        mesh_processor = MeshProcessor(OBJ_FILENAME, CONFIG['cache_dir'])
        mesh_processor.generate_graspable(CONFIG)
        mesh = mesh_processor.mesh
        sdf = mesh_processor.sdf
        obj = GraspableObject3D(sdf, mesh)

        gripper = RobotGripper.load(GRIPPER_NAME)

        ags = AntipodalGraspSampler(gripper, CONFIG)
        grasps = ags.sample_grasps(obj, num_grasps=100)
        print(len(grasps))
  
        quality_config = GraspQualityConfigFactory.create_config(CONFIG['metrics']['robust_ferrari_canny'])
        #quality_config = GraspQualityConfigFactory.create_config(CONFIG['metrics']['force_closure'])
        quality_fn = GraspQualityFunctionFactory.create_quality_function(obj, quality_config)
        

        vis.figure()
        #vis.points(PointCloud(nparraypoints), scale=0.001, color=np.array([0.5,0.5,0.5]))
        #vis.plot3d(nparraypoints)

        metrics = []
        vis.mesh(obj.mesh.trimesh, style='surface')
        for grasp in grasps:
            success, c = grasp.close_fingers(obj)
            if success:
                c1, c2 = c
                #fn_fc = quality_fn(grasp).quality
                true_fc = PointGraspMetrics3D.grasp_quality(grasp, obj, quality_config)
                true_fc = true_fc
                metrics.append(true_fc)

    	    #color = quality_fn(grasp).quality
            #if (true_fc > 1.0):
            #    true_fc = 1.0
            #color = (1.0-true_fc, true_fc, 0.0)

        low = np.min(metrics)
        high = np.max(metrics)
        print(low)
        print(high)
        if low == high:
            q_to_c = lambda quality: CONFIG['quality_scale']
        else:
            q_to_c = lambda quality: CONFIG['quality_scale'] * (quality - low) / (high - low)
        print(len(metrics))
        for grasp, metric in zip(grasps, metrics):
            color = plt.get_cmap('hsv')(q_to_c(metric))[:-1]
            print(color)
            vis.grasp(grasp, grasp_axis_color=color,endpoint_color=color)


        vis.show(False)
Example #9
0
def antipodal_grasp_sampler(visual=False, debug=False):
    mass = 1.0
    CONFIG['obj_rescaling_type'] = RescalingType.RELATIVE
    mesh_processor = MeshProcessor(OBJ_FILENAME, CONFIG['cache_dir'])
    mesh_processor.generate_graspable(CONFIG)
    mesh = mesh_processor.mesh
    sdf = mesh_processor.sdf
    stable_poses = mesh_processor.stable_poses
    obj = GraspableObject3D(sdf, mesh)
    stable_pose = stable_poses[0]
    if visual:
        vis.figure()

    T_table_world = RigidTransform(from_frame='table', to_frame='world')
    T_obj_world = Visualizer3D.mesh_stable_pose(obj.mesh.trimesh,
                                                stable_pose.T_obj_world,
                                                T_table_world=T_table_world,
                                                color=(0.5, 0.5, 0.5),
                                                style='surface',
                                                plot_table=True,
                                                dim=0.15)
    if debug:
        print(len(stable_poses))
    #for stable_pose in stable_poses:
    #    print(stable_pose.p)
    #    print(stable_pose.r)
    #    print(stable_pose.x0)
    #    print(stable_pose.face)
    #    print(stable_pose.id)
    # glass = 22 is standing straight
    if debug:
        print(stable_pose.r)
        print(stable_pose.T_obj_world)
    gripper = RobotGripper.load(
        GRIPPER_NAME, gripper_dir='/home/silvia/dex-net/data/grippers')

    ags = AntipodalGraspSampler(gripper, CONFIG)
    stable_pose.id = 0
    grasps = ags.generate_grasps(obj,
                                 target_num_grasps=20,
                                 max_iter=5,
                                 stable_pose=stable_pose.r)

    quality_config = GraspQualityConfigFactory.create_config(
        CONFIG['metrics']['robust_ferrari_canny'])

    metrics = []
    result = []

    #grasps = map(lambda g : g.perpendicular_table(stable_pose), grasps)

    for grasp in grasps:
        c1, c2 = grasp.endpoints
        true_fc = PointGraspMetrics3D.grasp_quality(grasp, obj, quality_config)
        metrics.append(true_fc)
        result.append((c1, c2))

        if debug:
            success, c = grasp.close_fingers(obj)
            if success:
                c1, c2 = c
                print("Grasp:")
                print(c1.point)
                print(c2.point)
                print(true_fc)

    low = np.min(metrics)
    high = np.max(metrics)
    if low == high:
        q_to_c = lambda quality: CONFIG['quality_scale']
    else:
        q_to_c = lambda quality: CONFIG['quality_scale'] * (quality - low) / (
            high - low)

    if visual:
        for grasp, metric in zip(grasps, metrics):
            #grasp2 = grasp.perpendicular_table(stable_pose)
            #c1, c2 = grasp.endpoints
            #axis = ParallelJawPtGrasp3D.axis_from_endpoints(c1, c2)
            #angle = np.dot(np.matmul(stable_pose.r, axis), [1,0,0])
            #angle = math.tan(axis[1]/axis[0])
            #angle = math.degrees(angle)%360
            #print(angle)
            #print(angle/360.0)
            color = plt.get_cmap('hsv')(q_to_c(metric))[:-1]
            vis.grasp(grasp,
                      T_obj_world=T_obj_world,
                      grasp_axis_color=color,
                      endpoint_color=color)

        #axis = np.array([[0,0,0], point])
        #points = [(x[0], x[1], x[2]) for x in axis]
        #Visualizer3D.plot3d(points, color=(0,0,1), tube_radius=0.002)
        vis.show(False)

    pose_matrix = np.eye(4, 4)
    pose_matrix[:3, :3] = T_obj_world.rotation
    pose_matrix[:3, 3] = T_obj_world.translation
    return pose_matrix, result