Home› Programming
Discussion
Back to discussions page
bcastets
Vacuum Beta tester Posts: 673 Expert
Control grippers from Python using MODBUS RTU |
3.4K views
|
Answered | |
/ Most recent by raj_usc_robot
in Programming
|
23 comments |

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.
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.
Support Tech
+01 418-380-2788 ext. 236
[email protected]
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)
It is a bit complex to control the gripper through the wrist camera. I would recommend to separate the connection.
You can mount a gripper coupling on top of the camera to separate communication.
Unfortunately we don't have API our documentation to make advance control of the camera and connected components.
The camera is made to be use in combinaison of UR robot and controlled via its URCAP on polyscope.
I have gotten my robot working with the camera and urscript in python, things are starting to be awesome.
However, accessing the gripper is not working.
Does the modbus go over the ethernet cable I already have in place? How do I set this up on the robot and computer end?
Did I understand correctly that this will not work if the robot has the camera wrist?
I have also looked at the RTDE library you linked (https://sdurobotics.gitlab.io/ur_rtde/index.html) but I do not get this to work in pycharm.
Have you managed to get this library working?
It is challenging.
The best reference for this is sdurobotics: https://sdurobotics.gitlab.io/ur_rtde/api/api.html#robotiq-gripper-api
I never tried it but I think that if the system communicate with the gripper via RTDE it should work even if the gripper is mounted on the camera.
This article is to control a gripper directly connected to a PC. In you case the gripper is connected to the camera and the camera is connected to the controller so it cannot work.
It works as the robot executes the same script that was made on the pendant, and the pendant does manage to control the grippers.
The guide is here: http://axisnj.com/controlling-a-universal-robots-cobot-using-python/
Sadly i must be doing it wrong as nothing happens. The author is a reseller so they do not actually share the code, i think they might just be baiting paid consultancy, but myself and I bet a lot of robotiq clients would be really happy to learn how to do this. I have several robots and it is really imperative that I get camera, robot and gripper controlled via openCV in python if I want to do advanced things.
Could this saved script trick work, do you know how to get this to work?
This method is described in this article:
https://dof.robotiq.com/discussion/1987/how-to-control-robotiq-product-through-port-30002-on-universal-robot#latest
The gripper is controlled by sending UR script to UR interface. It is easy, it works but it is a bit slow because there is a lot of code to send for one action.
Would you know any reason this might fail?
For some raison the connection failed.
It could that you don t have the correct connection settings in the program. Could you send you me your error and maybe details about your setup ? Maybe i will be able to understand this issue.
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:
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
I checked the code but I don't see any problem.
It looks like minimal modbus failed to establish the connection. Make sure you don t have other software communicating with the gripper like RUI (Robotiq user interface).
Further test are needed to debug this. This to make a simple activation using minimal modbus. This should help you to understand what is going wrong before using the full code.
Do you have some sample code for this? I am inexperienced using the minimal modbus library
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.write_registers(1000,[256,0,0]) #Activate the gripper
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
It is strange that the baudrate is not set correctly.
Could you precise what did you change in the code ?
I am running the code alongside the ur_robot_driver ur_control.launch.py package, which spins a node for the tooling /ur_tool_comm that has an argument that names the tool device at the value /tmp/ttyUR. I am receiving this error when I run your code:
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Caught exception when trying to load file of format [py]: [Errno 2] could not open port /dev/ttyUR: [Errno 2] No such file or directory: '/dev/ttyUR'