Home Programming

Discussion

Left ArrowBack to discussions page
bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
edited October 2020 in Programming
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.



Comments

  • nicolasmarcstephannicolasmarcstephan Posts: 5 Apprentice
    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!







  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    edited February 2021
    @nicolasmarcstephan
    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.
  • nicolasmarcstephannicolasmarcstephan Posts: 5 Apprentice
    edited February 2021
    Thank you for your answer.

    Is there an API for the wrist camera?
    Otherwise I will just use the gripper coupling.
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @nicolasmarcstephan
    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.
  • Ruben_SpeybrouckRuben_Speybrouck Posts: 18 Apprentice
    Hi again @bcastets

    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? 
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @Ruben_Speybrouck
    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.


  • Ruben_SpeybrouckRuben_Speybrouck Posts: 18 Apprentice
    @bcastets Thank you for clarifying that. I found a guide online where the author makes a program on the pendant that drives the grippers, saves it to usb stick inserted in the pendant and then uses a socket command (send via python) to send the same program back to the robot to control the gripper. 

    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?  
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @Ruben_Speybrouck
    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.

  • Ruben_SpeybrouckRuben_Speybrouck Posts: 18 Apprentice
    @bcastets once again you come to the rescue. Its a huge file to send for one command but I cannot say it is noticeably slow. THANK YOU for pointing me in the right direction! Seriously, thank you! 
  • NickAarsvoldNickAarsvold Posts: 8 Apprentice
    @bcastets I am having the same error as nicolasmarcstephan, but I am not connecting via wrist camera. I am able to get the code from the link @Guillaume_Charron posted working. Checking on the Robotiq User Interface, I can confirm I am using 'COM5' and Slave ID=9

    Would you know any reason this might fail?
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @NickAarsvold
    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.
  • NickAarsvoldNickAarsvold Posts: 8 Apprentice
    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
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @NickAarsvold
    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.
  • NickAarsvoldNickAarsvold Posts: 8 Apprentice
    @bcastets
    Do you have some sample code for this? I am inexperienced using the minimal modbus library
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @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
  • NickAarsvoldNickAarsvold Posts: 8 Apprentice
    edited June 2021
    That code was able to activate the gripper. I am not sure why the rest of the code doesn't work.
  • NickAarsvoldNickAarsvold Posts: 8 Apprentice
    @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
  • raj_usc_robotraj_usc_robot Posts: 3 Apprentice
    Using the Robotiq 2-finger gripper using your code @bcastets and it looks like minimalmodbus sets a baudrate of 19200 instead of the needed 115200. Had to modify that library to prevent the overwriting of the baudrate so that the code worked flawlessly. Hope this helps @NickAarsvold (even after a year)!
  • bcastetsbcastets Vacuum Beta tester Posts: 673 Expert
    @raj_usc_robot
    It is strange that the baudrate is not set correctly.
    Could you precise what did you change in the code ?

  • raj_usc_robotraj_usc_robot Posts: 3 Apprentice
    Your code is completely fine. It is the imported minimalmodbus library causing the issue. Specially at line 222, the baudrate is set to 19200. 
  • nxsgisnxsgis Unconfirmed Posts: 1 Recruit
    Benoit, apologies for digging up an old thread. I'm trying to run your code with a UR10e on ROS2 Humble using a Robotiq 2F-85 gripper that's connected through the flange. I'm able to move the gripper by pinging it through the console and sending commands directly, but keep having issues when trying to run scripts using modbus or socat.

    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'

    Do you have any pointers? I can include all of my files and cmd outputs if necessary.
  • raj_usc_robotraj_usc_robot Posts: 3 Apprentice
    I recommend checking which port your Robotiq Gripper is on. That error probably means that your code is looking at the wrong port (probably should be /dev/tmp/ttyUR). 
Sign In or Register to comment.
Left ArrowBack to discussions page