DoF - a Robotiq Community
Warning sign
The Dof Community was shut down in June 2023. This is a read-only archive.
If you have questions about Robotiq products please reach our support team.
bcastets

I share with you a python program I did to control Robotiq grippers from a PC using Python and MODBUS RTU protocol.

The code is hosted on github:
https://github.com/castetsb/pyRobotiqGripper

I did this program after receiving several questions about how to connect our gripper on different robots, PC,... If you want to make such prooject, I recommend that you check the user manual (https://robotiq.com/support/2f-85-2f-140/downloads-instruction-manual-cb-series?document=). Everything is explain in details with examples.
Robotiq grippers are using MODBUS RTU protocol and can be control just by writing command in their registers. I used  minimalMobus library to write in gripper register and request actions.

I hope this will help to understand how its working.



nicolasmarcstephan

Thank you for referring me here @bcastets !

You were right, this was exactly what I was looking for.
But unfortunately also this code doesn't work for me. MinimalModbus is installed and I checked the Robotiq User Interface where it shows me that the port is COM6 and the slave number is 9. The gripper is sitting on a Robotiq wrist camera. Could this be the problem?

This is the error message:

MinimalModbus debug mode. Will write to instrument (expecting 17 bytes back): '\t\x03\x07Ð\x00\x06Ä\r' (09 03 07 D0 00 06 C4 0D)
MinimalModbus debug mode. Clearing serial buffers for port COM6
MinimalModbus debug mode. No sleep required before write. Time since previous read: 7298031.00 ms, minimum silent period: 2.01 ms.
Traceback (most recent call last):
  File "...\pyRobotiqGripper-master\robotiqGripper.py", line 554, in <module>
    grip = RobotiqGripper("COM6")  # /dev/ttyUSB1
  File "...\pyRobotiqGripper-master\robotiqGripper.py", line 76, in __init__
    self.readAll()
  File "...\pyRobotiqGripper-master\robotiqGripper.py", line 235, in readAll
    registers = self.read_registers(2000, 6)
  File "...\pyRobotiqGripper-master\venv\lib\site-packages\minimalmodbus.py", line 844, in read_registers
    return self._generic_command(
  File "...\pyRobotiqGripper-master\venv\lib\site-packages\minimalmodbus.py", line 1170, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
  File "...\pyRobotiqGripper-master\venv\lib\site-packages\minimalmodbus.py", line 1240, in _perform_command
    response = self._communicate(request, number_of_bytes_to_read)
  File "...\pyRobotiqGripper-master\venv\lib\site-packages\minimalmodbus.py", line 1406, in _communicate
    raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
MinimalModbus debug mode. Response from instrument: '' () (0 bytes), roundtrip time: 47.0 ms. Timeout for reading: 50.0 ms.

Process finished with exit code 1


Thank you so much for your help!







bcastets
bcastets
NickAarsvold

Thank you for the quick response!

All files are included in this
google drive folder here

I have a 2F-85 connected to my laptop via the USB to RS-485 adapter and Robotiq cable included with the gripper.  After installing drivers for the adapter, the device appears on 'COM5'
Opening the Robotiq User Interface I see it has been recognized by the software and verify that it is on 'COM5' and has the default parameters (baud rate = 115200, stop bit = 1, no parity bit, slave ID = 9, and having terminal resister checked).

Downloading your code (which appears to be an excellent and useful tool), I change the port name in the test section (line 545) to 'COM5' and run the code. Here is the output:

C:\Users\nicka\PycharmProjects\GripperTest\venv\Scripts\python.exe C:/Users/nicka/PycharmProjects/GripperTest/robotiqGripper.py
{'gOBJ': {}, 'gSTA': {}, 'gGTO': {}, 'gACT': {}, 'kFLT': {}, 'gFLT': {}, 'gPR': {}, 'gPO': {}, 'gCU': {}}
MinimalModbus debug mode. Will write to instrument (expecting 17 bytes back): '\t\x03\x07Ð\x00\x06Ä\r' (09 03 07 D0 00 06 C4 0D)
MinimalModbus debug mode. Clearing serial buffers for port COM5
MinimalModbus debug mode. No sleep required before write. Time since previous read: 343288531.00 ms, minimum silent period: 2.01 ms.
Traceback (most recent call last):
  File "C:/Users/nicka/PycharmProjects/GripperTest/robotiqGripper.py", line 545, in <module>
    grip=RobotiqGripper("COM5")
  File "C:/Users/nicka/PycharmProjects/GripperTest/robotiqGripper.py", line 75, in __init__
    self.readAll()
  File "C:/Users/nicka/PycharmProjects/GripperTest/robotiqGripper.py", line 225, in readAll
    registers=self.read_registers(2000,6)
  File "C:\Users\nicka\PycharmProjects\GripperTest\venv\lib\site-packages\minimalmodbus.py", line 848, in read_registers
    payloadformat=_PAYLOADFORMAT_REGISTERS,
  File "C:\Users\nicka\PycharmProjects\GripperTest\venv\lib\site-packages\minimalmodbus.py", line 1170, in _generic_command
    payload_from_slave = self._perform_command(functioncode, payload_to_slave)
  File "C:\Users\nicka\PycharmProjects\GripperTest\venv\lib\site-packages\minimalmodbus.py", line 1240, in _perform_command
    response = self._communicate(request, number_of_bytes_to_read)
  File "C:\Users\nicka\PycharmProjects\GripperTest\venv\lib\site-packages\minimalmodbus.py", line 1406, in _communicate
MinimalModbus debug mode. Response from instrument: '' () (0 bytes), roundtrip time: 62.0 ms. Timeout for reading: 50.0 ms.

    raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)

Process finished with exit code 1


Afterward, I was able to get the code from https://dof.robotiq.com/discussion/92/controlling-the-robotiq-2-finger-gripper-with-modbus-commands-in-python running. Modifying the code slightly I have separated the close and open commands and have put them in callable functions.

Ideally, I would get your code running as it seems extremely useful and flexible in using the entirety of the gripper's functionality.

Thank you again for your help,
Nick

bcastets

@NickAarsvold
Minimal modbus is well documented:
https://minimalmodbus.readthedocs.io/en/stable/
You should be able to make it.

Here below is an example of what you could try:

instrument = minimalmodbus.Instrument('COM5', 9, debug = True)

instrument.serial.port                     # this is the serial port name
instrument.serial.baudrate = 115200         # Baud
instrument.serial.bytesize = 8
instrument.serial.parity   = serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout  = 0.2          # seconds
instrument.address                         # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU   # rtu or ascii mode
instrument.clear_buffers_before_each_transaction = True

print(instrument)

instrument.write_registers(1000,[0,0,0]) # Reset the gripper
instrument.write_registers(1000,[256,0,0]) #Activate the gripper

NickAarsvold

@bcastets I am now able to get the gripper working, thank you so much for your help. In this google drive folder is the code I got working.
Running the mmtest script, I am able to call functions from the robotiqGripper script. The original mmtest program which is mostly commented out worked flawlessly. Interestingly, I was able to call the robotiqGripper functions after running the program you have listed above. I then commented out line by line until I found the line of code that would break the program when absent. For whatever reason, when commenting out the line: 
instrument.serial.baudrate = 115200  
the gripper is unable to run in the robotiqGripper script called.
I am not sure why this is considering line 19 of robotiqGripper.py should do the same thing as instrument.serial.baudrate=115200

Nevertheless, thank you for your support,
Nick