mode Method

XenseGripper supports three control modes, which can be switched via the mode method (context manager) or the enable_mode/disable_mode methods:

  • POSITION: Position control mode (default), precisely controls the gripper to reach the specified position

  • SPEED: Speed control mode, drives the gripper to move at the set speed

  • SAFE: Safe control mode (sensor required), realizes safe operation with force feedback

XenseGripper.mode(self, control_mode, serial_number=None)

A context manager used to temporarily switch the gripper control mode; the original mode is automatically restored when exiting the context (recommended). Use this method with the with statement to ensure the mode is automatically reset after the code block is executed, avoiding exceptions caused by residual modes.

Parameters:
  • control_mode (XenseGripper.ControlMode) – Control mode enumeration values: XenseGripper.ControlMode.POSITION: Position control mode XenseGripper.ControlMode.SPEED: Speed control mode XenseGripper.ControlMode.SAFE: Safe control mode

  • serial_number (str, 可选) – Sensor serial number (required only for safe mode; the first scanned sensor is used by default)

Returns:

Context manager object, used to temporarily switch modes in the with statement block

Return type:

context manager

XenseGripper.enable_mode(self, control_mode, serial_number=None)

Manually enable the specified control mode

Parameters:
  • control_mode (XenseGripper.ControlMode) – Control mode enumeration values (same as the mode method)

  • serial_number (str, 可选) – Sensor serial number (required only for safe mode)

Returns:

Whether the operation is successful

Return type:

bool

XenseGripper.disable_mode(self)

Manually disable the current control mode, automatically restore to the default mode and stop the action

Returns:

Whether the operation is successful

Return type:

bool

Example Code

  1. Position control mode (default mode, no manual switching required)

from xensegripper import XenseGripper

gripper = XenseGripper.create(mac_addr="9a14e81bb832")
gripper.set_position(30)  # 直接设置位置(默认在 POSITION 模式)
  1. Speed Control Mode

# 使用上下文管理器 (推荐)
with gripper.mode(XenseGripper.ControlMode.SPEED):
    gripper.set_speed(50)   # 设置速度 50 mm/s
    time.sleep(2)
    gripper.set_speed(0)    # 停止

# 手动模式切换
gripper.enable_mode(XenseGripper.ControlMode.SPEED)
gripper.set_speed(30)
gripper.disable_mode()  # 自动停止并恢复原模式
  1. Safe Control Mode (Sensor Required)

# 使用上下文管理器 (推荐)
# 可指定传感器序列号,默认使用扫描到的第一个传感器
with gripper.mode(XenseGripper.ControlMode.SAFE, serial_number=None):
    # 设置控制参数 (可选,刚度范围 0.1~0.6)
    gripper.set_control_param(stiffness=0.35)

    # 位置控制 (带力反馈保护)
    gripper.set_position(20)

# 手动模式切换
gripper.enable_mode(XenseGripper.ControlMode.SAFE, serial_number="OG000285")
gripper.set_control_param(stiffness=0.4)
gripper.set_position(15)
gripper.disable_mode()  # 自动停止并恢复原模式