I want to work onrobot rg2 gripper in python

I succeeded in moving the robot using the urx library, but the operation of the gripper failed.
Currently, I am using an onrobot rg2 gripper, and I tried to operate it with the onrobot rg2 gripper api below, but it did not work.
The operating system is Windows and I want to run the gripper with Python.
I’ll be waiting for your help.
Below is my code.
ㅇ onrobot rg2 gripper API
(Support for OnRobot RG2 Gripper by Mofeywalker · Pull Request #45 · SintefManufacturing/python-urx · GitHub)

import urx
import time
from math import pi
from urx import onrobot_rg2_gripper

def rad(degree):
result = degree*(pi/180)
return result

rob = urx.Robot(“192.168.50.100”)

gripper = onrobot_rg2_gripper.OnRobotGripperRG2(rob)

a = 0.5
v = 1
rob.set_tcp((0,0,0.1,0,0,0))
rob.set_payload(2,(0,0,0.1))
time.sleep(2)

for i in range(2):
rob.movej((0,rad(-80),0,rad(-90),0,0),a,v)
time.sleep(2)
gripper.open_gripper(
target_width=110, # Width in mm, 110 is fully open
target_force=40, # Maximum force applied in N, 40 is maximum
payload=0.5, # Payload in kg
set_payload=False, # If any payload is attached
depth_compensation=False, # Whether to compensate for finger depth
slave=False, # Is this gripper the master or slave gripper?
wait=2 # Wait up to 2s for movement
)
rob.movej((0,rad(-100),0,rad(-90),0,0),a,v)

rob.close()

Hello, Unfortunately I dont know how exactly the code you sent is implemented in URX. The RG2 Gripper is not supported in the standard urx library you will have to use a fork that includes it.
What error are you getting if you try to run it?

Using the RG2 Gripper is possible even without dedicated functions in the urx API (for example if you dont want to use the fork of the library)
You need to add the “boilerplate script” before calling the RG2 command.
We just concatenate it as a long string before adding an RG2() function as a string as well.

This is the boilerplate script for the RG2 Grippers:

set_standard_analog_input_domain(0, 1)
  set_standard_analog_input_domain(1, 1)
  set_tool_analog_input_domain(0, 1)
  set_tool_analog_input_domain(1, 1)
  set_analog_outputdomain(0, 0)
  set_analog_outputdomain(1, 0)
  set_tool_voltage(0)
  set_input_actions_to_default()
  set_tcp(p[0.0,0.0,0.0,0.0,0.0,0.0])
  set_payload(0.0)
  set_gravity([0.0, 0.0, 9.82])
  # begin: URCap Installation Node
  #   Source: RG - On Robot, 1.9.0, OnRobot A/S
  #   Type: RG Configuration
  global measure_width=0
  global grip_detected=False
  global lost_grip=False
  global zsysx=0
  global zsysy=0
  global zsysz=0.06935
  global zsysm=0.7415
  global zmasx=0
  global zmasy=0
  global zmasz=0.18659
  global zmasm=0
  global zmasm=0
  global zslax=0
  global zslay=0
  global zslaz=0
  global zslam=0
  global zslam=0
  thread lost_grip_thread():
    while True:
      set_tool_voltage(24)
      if True ==get_digital_in(9):
        sleep(0.024)
        if True == grip_detected:
          if False == get_digital_in(8):
            grip_detected=False
            lost_grip=True
          end
        end
        set_tool_analog_input_domain(0, 1)
        set_tool_analog_input_domain(1, 1)
        zscale = (get_analog_in(2)-0.026)/2.9760034
        zangle = zscale*1.57079633+-0.08726646
        zwidth = 5.0+110*sin(zangle)
        global measure_width = (floor(zwidth*10))/10-9.2
      end
      sync()
    end
  end
  lg_thr = run lost_grip_thread()
  def RG2(target_width=110, target_force=40, payload=0.0, set_payload=False, depth_compensation=False, slave=False):
    grip_detected=False
    if slave:
      slave_grip_detected=False
    else:
      master_grip_detected=False
    end
    timeout = 0
    timeout_limit = 750000
    while get_digital_in(9) == False:
      if timeout > timeout_limit:
        break
      end
      timeout = timeout+1
      sync()
    end
    def bit(input):
      msb=65536
      local i=0
      local output=0
      while i<17:
        set_digital_out(8,True)
        if input>=msb:
          input=input-msb
          set_digital_out(9,False)
        else:
          set_digital_out(9,True)
        end
        if get_digital_in(8):
          out=1
        end
        sync()
        set_digital_out(8,False)
        sync()
        input=input*2
        output=output*2
        i=i+1
      end
      return output
    end
    target_width=target_width+9.2
    if target_force>40:
      target_force=40
    end
    if target_force<4:
      target_force=4
    end
    if target_width>110:
      target_width=110
    end
    if target_width<0:
      target_width=0
    end
    rg_data=floor(target_width)*4
    rg_data=rg_data+floor(target_force/2)*4*111
    rg_data=rg_data+32768
    if slave:
      rg_data=rg_data+16384
    end
    bit(rg_data)
    if depth_compensation:
      finger_length = 55.0/1000
      finger_heigth_disp = 5.0/1000
      center_displacement = 7.5/1000

      start_pose = get_forward_kin()
      set_analog_inputrange(2, 1)
      zscale = (get_analog_in(2)-0.026)/2.9760034
      zangle = zscale*1.57079633+-0.08726646
      zwidth = 5.0+110*sin(zangle)

      start_depth = cos(zangle)*finger_length

      sleep(0.016)
      timeout = 0
      while get_digital_in(9) == True:
        timeout=timeout+1
        sleep(0.008)
        if timeout > 20:
          break
        end
      end
      timeout = 0
      timeout_limit = 750000
      while get_digital_in(9) == False:
        zscale = (get_analog_in(2)-0.026)/2.9760034
        zangle = zscale*1.57079633+-0.08726646
        zwidth = 5.0+110*sin(zangle)
        measure_depth = cos(zangle)*finger_length
        compensation_depth = (measure_depth - start_depth)
        target_pose = pose_trans(start_pose,p[0,0,-compensation_depth,0,0,0])
        if timeout > timeout_limit:
          break
        end
        timeout=timeout+1
        servoj(get_inverse_kin(target_pose),0,0,0.008,0.01,2000)
        if point_dist(target_pose, get_forward_kin()) > 0.005:
          popup("Lower grasping force or max width",title="RG-lag threshold exceeded", warning=False, error=False, blocking=False)
        end
      end
      nspeed = norm(get_actual_tcp_speed())
      while nspeed > 0.001:
        servoj(get_inverse_kin(target_pose),0,0,0.008,0.01,2000)
        nspeed = norm(get_actual_tcp_speed())
      end
        stopj(2)
    end
    if depth_compensation==False:
      timeout = 0
      timeout_count=20*0.008/0.008
      while get_digital_in(9) == True:
        timeout = timeout+1
        sync()
        if timeout > timeout_count:
          break
        end
      end
      timeout = 0
      timeout_limit = 750000
      while get_digital_in(9) == False:
        timeout = timeout+1
        sync()
        if timeout > timeout_limit:
          break
        end
      end
      end
      sleep(0.024)
      if set_payload:
        if slave:
          if get_analog_in(3) < 2:
            zslam=0
          else:
            zslam=payload
          end
        else:
          if get_digital_in(8) == False:
            zmasm=0
          else:
            zmasm=payload
          end
        end
        zload=zmasm+zslam+zsysm
        set_payload(zload,[(zsysx*zsysm+zmasx*zmasm+zslax*zslam)/zload,(zsysy*zsysm+zmasy*zmasm+zslay*zslam)/zload,(zsysz*zsysm+zmasz*zmasm+zslaz*zslam)/zload])
      end
      master_grip_detected=False
      master_lost_grip=False
      slave_grip_detected=False
      slave_lost_grip=False
      if True == get_digital_in(8):
        master_grip_detected=True
      end
      if get_analog_in(3)>2:
        slave_grip_detected=True
      end
      grip_detected=False
      lost_grip=False
      if True == get_digital_in(8):
        grip_detected=True
      end
      zscale = (get_analog_in(2)-0.026)/2.9760034
      zangle = zscale*1.57079633+-0.08726646
      zwidth = 5.0+110*sin(zangle)
      global measure_width = (floor(zwidth*10))/10-9.2
      if slave:
        slave_measure_width=measure_width
      else:
        master_measure_width=measure_width
      end
      return grip_detected
    end
  set_tool_voltage(24)
  set_tcp(p[0,0,0.18659,0,-0,0])

So the python script that uses it with urx looks like this:

import urx

rob = urx.Robot("192.168.0.100")


boilerplate =  """
  set_standard_analog_input_domain(0, 1)
  set_standard_analog_input_domain(1, 1)
  set_tool_analog_input_domain(0, 1)
  set_tool_analog_input_domain(1, 1)
  set_analog_outputdomain(0, 0)
  set_analog_outputdomain(1, 0)
  set_tool_voltage(0)
  set_input_actions_to_default()
  set_tcp(p[0.0,0.0,0.0,0.0,0.0,0.0])
  set_payload(0.0)
  set_gravity([0.0, 0.0, 9.82])
  # begin: URCap Installation Node
  #   Source: RG - On Robot, 1.9.0, OnRobot A/S
  #   Type: RG Configuration
  global measure_width=0
  global grip_detected=False
  global lost_grip=False
  global zsysx=0
  global zsysy=0
  global zsysz=0.06935
  global zsysm=0.7415
  global zmasx=0
  global zmasy=0
  global zmasz=0.18659
  global zmasm=0
  global zmasm=0
  global zslax=0
  global zslay=0
  global zslaz=0
  global zslam=0
  global zslam=0
  thread lost_grip_thread():
    while True:
      set_tool_voltage(24)
      if True ==get_digital_in(9):
        sleep(0.024)
        if True == grip_detected:
          if False == get_digital_in(8):
            grip_detected=False
            lost_grip=True
          end
        end
        set_tool_analog_input_domain(0, 1)
        set_tool_analog_input_domain(1, 1)
        zscale = (get_analog_in(2)-0.026)/2.9760034
        zangle = zscale*1.57079633+-0.08726646
        zwidth = 5.0+110*sin(zangle)
        global measure_width = (floor(zwidth*10))/10-9.2
      end
      sync()
    end
  end
  lg_thr = run lost_grip_thread()
  def RG2(target_width=110, target_force=40, payload=0.0, set_payload=False, depth_compensation=False, slave=False):
    grip_detected=False
    if slave:
      slave_grip_detected=False
    else:
      master_grip_detected=False
    end
    timeout = 0
    timeout_limit = 750000
    while get_digital_in(9) == False:
      if timeout > timeout_limit:
        break
      end
      timeout = timeout+1
      sync()
    end
    def bit(input):
      msb=65536
      local i=0
      local output=0
      while i<17:
        set_digital_out(8,True)
        if input>=msb:
          input=input-msb
          set_digital_out(9,False)
        else:
          set_digital_out(9,True)
        end
        if get_digital_in(8):
          out=1
        end
        sync()
        set_digital_out(8,False)
        sync()
        input=input*2
        output=output*2
        i=i+1
      end
      return output
    end
    target_width=target_width+9.2
    if target_force>40:
      target_force=40
    end
    if target_force<4:
      target_force=4
    end
    if target_width>110:
      target_width=110
    end
    if target_width<0:
      target_width=0
    end
    rg_data=floor(target_width)*4
    rg_data=rg_data+floor(target_force/2)*4*111
    rg_data=rg_data+32768
    if slave:
      rg_data=rg_data+16384
    end
    bit(rg_data)
    if depth_compensation:
      finger_length = 55.0/1000
      finger_heigth_disp = 5.0/1000
      center_displacement = 7.5/1000
      start_pose = get_forward_kin()
      set_analog_inputrange(2, 1)
      zscale = (get_analog_in(2)-0.026)/2.9760034
      zangle = zscale*1.57079633+-0.08726646
      zwidth = 5.0+110*sin(zangle)
      start_depth = cos(zangle)*finger_length
      sleep(0.016)
      timeout = 0
      while get_digital_in(9) == True:
        timeout=timeout+1
        sleep(0.008)
        if timeout > 20:
          break
        end
      end
      timeout = 0
      timeout_limit = 750000
      while get_digital_in(9) == False:
        zscale = (get_analog_in(2)-0.026)/2.9760034
        zangle = zscale*1.57079633+-0.08726646
        zwidth = 5.0+110*sin(zangle)
        measure_depth = cos(zangle)*finger_length
        compensation_depth = (measure_depth - start_depth)
        target_pose = pose_trans(start_pose,p[0,0,-compensation_depth,0,0,0])
        if timeout > timeout_limit:
          break
        end
        timeout=timeout+1
        servoj(get_inverse_kin(target_pose),0,0,0.008,0.01,2000)
        if point_dist(target_pose, get_forward_kin()) > 0.005:
          popup("Lower grasping force or max width",title="RG-lag threshold exceeded", warning=False, error=False, blocking=False)
        end
      end
      nspeed = norm(get_actual_tcp_speed())
      while nspeed > 0.001:
        servoj(get_inverse_kin(target_pose),0,0,0.008,0.01,2000)
        nspeed = norm(get_actual_tcp_speed())
      end
        stopj(2)
    end
    if depth_compensation==False:
      timeout = 0
      timeout_count=20*0.008/0.008
      while get_digital_in(9) == True:
        timeout = timeout+1
        sync()
        if timeout > timeout_count:
          break
        end
      end
      timeout = 0
      timeout_limit = 750000
      while get_digital_in(9) == False:
        timeout = timeout+1
        sync()
        if timeout > timeout_limit:
          break
        end
      end
      end
      sleep(0.024)
      if set_payload:
        if slave:
          if get_analog_in(3) < 2:
            zslam=0
          else:
            zslam=payload
          end
        else:
          if get_digital_in(8) == False:
            zmasm=0
          else:
            zmasm=payload
          end
        end
        zload=zmasm+zslam+zsysm
        set_payload(zload,[(zsysx*zsysm+zmasx*zmasm+zslax*zslam)/zload,(zsysy*zsysm+zmasy*zmasm+zslay*zslam)/zload,(zsysz*zsysm+zmasz*zmasm+zslaz*zslam)/zload])
      end
      master_grip_detected=False
      master_lost_grip=False
      slave_grip_detected=False
      slave_lost_grip=False
      if True == get_digital_in(8):
        master_grip_detected=True
      end
      if get_analog_in(3)>2:
        slave_grip_detected=True
      end
      grip_detected=False
      lost_grip=False
      if True == get_digital_in(8):
        grip_detected=True
      end
      zscale = (get_analog_in(2)-0.026)/2.9760034
      zangle = zscale*1.57079633+-0.08726646
      zwidth = 5.0+110*sin(zangle)
      global measure_width = (floor(zwidth*10))/10-9.2
      if slave:
        slave_measure_width=measure_width
      else:
        master_measure_width=measure_width
      end
      return grip_detected
    end
  set_tool_voltage(24)
  set_tcp(p[0,0,0.18659,0,-0,0])

  """

grip_width_open = 40  
open_grip_command = f"RG2(target_width={grip_width_open}, target_force=1, payload=1, set_payload=False, depth_compensation=False) \n"

grip_width_closed = 0  
open_grip_command = f"RG2(target_width={grip_width_closed}, target_force=1, payload=1, set_payload=False, depth_compensation=False) \n"

robot_program = boilerplate + open_grip_command + close_grip_command

rob.send_program(robot_program)

let me know if this works

The program just hangs in my terminal but I get some errors on the teach pendant which are different errors upon subsequent runs.

Errors:

#value error: ‘timeout’ is not initalized to a value

#Value error: ‘input’ is not initalized to a value

#Value error: ‘i’ is not initalized to a value

#Value error: ‘rg_data’ is not initalized to a value

#Value error: ‘target_width’ is not initalized to a value

#RG-lag theshold exceeded, lower grasping force or max width

I checked all these variables and they are initialize. It must be close to correct. Any advice on how to debug this?

I have the same problem but with OnRobot SG. I’m trying to write a script to use the sg_grip and sg_release functions but when I try to send the script to the robot I receive a lot of “unitialization” messages even if the variables are initialized.
I even attempted to download the original script from polyscope (a working script) and send it later to the robot but I have the same problems.

Here use this library I developed to communicate with onRobot grippers with the Universal Robots (robots) through the standard m8 tool connector.

git@github.com:RyanPaulMcKenna/onRobot.git

Can pip install like so:

pip install onRobot

and use like so:

import onRobot.gripper as gripper

# Default id is zero, if you have multiple grippers, 
# see logs in UR Teach Pendant to know which is which :)
rg_id = 0
rg_gripper = gripper.RG2(rg_id)

rg_width = rg_gripper.get_rg_width()
pregrasp_width = 100

# force and width units described in onRobot RG2 Manual
target_width = 15.66
target_force = 40.00

if (rg_wdith == pregrasp_width):
    rg_gripper.rg_grip(target_width, target_force)

epsilon = 0.05 # Just an example of reasonable error may not be realistic. 
grip_success = abs(rg_gripper.get_rg_width() - target_width) < epsilon