跳到主要内容

GF225Sensor

类路径: vitai_core.sensors.gf225.GF225Sensor

操作GF225视触觉传感器的核心类。


方法

所有方法都使用相同的参数模式:

参数名类型默认值说明
env_idxint0环境索引(多环境仿真时使用)

get_tactile_rgb()

获取触觉RGB图像(光学仿真生成)。

返回值: np.ndarray | None

  • 形状:(H, W, 3)
  • 数据类型:uint8
  • 通道:RGB

示例

sensor = vitai_scene.tactile_sensors["left"]["sensor"]
tactile_rgb = sensor.get_tactile_rgb(env_idx=0)

if tactile_rgb is not None:
print(f"形状: {tactile_rgb.shape}") # (480, 480, 3)
cv2.imwrite("tactile.png", cv2.cvtColor(tactile_rgb, cv2.COLOR_RGB2BGR))

get_camera_rgb()

获取传感器内置相机的RGB图像。

返回值: np.ndarray | None

  • 形状:(H, W, 3)
  • 数据类型:uint8

示例

camera_rgb = sensor.get_camera_rgb(env_idx=0)

get_camera_depth()

获取相机深度图(归一化)。

返回值: np.ndarray | None

  • 形状:(H, W)
  • 数据类型:uint8
  • 值范围:0-255
    • 0 = 远距离(无接触)
    • 255 = 近距离(最大压入)

示例

depth_img = sensor.get_camera_depth(env_idx=0)

if depth_img is not None:
max_depth = depth_img.max()
print(f"最大深度: {max_depth}")

# 接触检测
if max_depth > 4: # 阈值:4-10
print("检测到接触!")

💡 使用技巧

  • 典型接触检测阈值:4-10
  • 用于自适应夹爪控制
  • 左右传感器同时检测可提高可靠性

get_marker_motion()

获取Gelpad表面标记点的运动数据。

返回值: np.ndarray | None

  • 形状:(2, num_markers, 2)
    • [0] = 初始位置
    • [1] = 当前位置
    • 最后一维:(x, y)图像坐标

示例

marker_motion = sensor.get_marker_motion(env_idx=0)

if marker_motion is not None:
initial_pos = marker_motion[0] # (num_markers, 2)
current_pos = marker_motion[1] # (num_markers, 2)

# 计算位移
displacement = current_pos - initial_pos
avg_displacement = np.linalg.norm(displacement, axis=1).mean()
print(f"平均位移: {avg_displacement:.4f}像素")

get_indentation_depth()

获取物体压入Gelpad的深度(单位:米)。

返回值: float - 压痕深度(米)

示例

depth = sensor.get_indentation_depth(env_idx=0)
print(f"压入深度: {depth*1000:.2f}mm")

get_all_data()

一次性获取所有传感器数据。

返回值: dict - 包含所有数据的字典

字典键

  • tactile_rgb: 触觉RGB图像
  • camera_rgb: 相机RGB图像
  • camera_depth: 深度图
  • marker_motion: 标记点运动
  • indentation_depth: 压痕深度
  • frame: 帧编号
  • timestamp: 时间戳

示例

data = sensor.get_all_data(env_idx=0)

print(f"帧号: {data['frame']}")
print(f"时间戳: {data['timestamp']:.4f}s")
print(f"压入深度: {data['indentation_depth']*1000:.2f}mm")

if data['tactile_rgb'] is not None:
print(f"触觉图像: {data['tactile_rgb'].shape}")

完整示例:自适应夹爪控制

def adaptive_gripper_close(vitai_scene, depth_threshold=4, max_steps=30):
"""基于双传感器深度的自适应夹爪闭合"""
left_sensor = vitai_scene.tactile_sensors["left"]["sensor"]
right_sensor = vitai_scene.tactile_sensors["right"]["sensor"]

for i in range(max_steps):
# 读取双侧深度
left_depth = left_sensor.get_camera_depth(0)
right_depth = right_sensor.get_camera_depth(0)

if left_depth is not None and right_depth is not None:
left_max = left_depth.max()
right_max = right_depth.max()

# 左右都接触时停止
if left_max > depth_threshold and right_max > depth_threshold:
print(f"✅ [步骤{i}] 检测到双侧接触 (L:{left_max}, R:{right_max})")
return True

yield # 等待下一帧

print("⚠️ 达到最大步数未检测到接触")
return False

# 使用
contact_detected = yield from adaptive_gripper_close(vitai_scene)