Connect a Cognex camera to a Doosan robot

For this tutorial u need:

  • Doosan robot
  • Laptop with DRL studio - Homburger Hub, installed
  • Cognex camera

For getting data from the cognex camera on a Doosan robot you need to set the Doosan IP address and the cognex IP address in the same IP range. If they are both in the same IP range then you can use the code below.

Configure these parameters

port = 10000
ip = "192.168.137.10"

then open a TCP connection to the camera

socket = client_socket_open(ip, port)

After connecting, the camera sends a welcome message and asks for a username and password.

receive = client_socket_read(socket, -1, -1)[1].decode()
client_socket_write(socket, "admin\r\n".encode())   # username + carage return, new line character

### Asked for a password ###
receive = client_socket_read(socket, -1, -1)[1].decode()
client_socket_write(socket, "\r\n".encode())        # password(empty) + carriage return, newline character

If successful, then you will get a logged-in message.

receive = client_socket_read(socket, -1, -1)[1].decode()

After the login procedure, trigger the camera. This function only works if the Cognex camera is on Online Mode

client_socket_write(socket, "SE8\r\n".encode())
wait(3)

triggerstatus = client_socket_read(socket, -1, -1)[1].decode()[:-2]

if triggerstatus != "1":
    tp_log("Trigger failed: " + str(triggerstatus))
    return 0

After triggering the camera, We can get the cell value from the Cognex table cell.

cel_data = "GVC019"
client_socket_write(socket, (cel_data + "\r\n").encode())   # GVC013
getvaluestatus, rec, _empty = str(client_socket_read(socket, -1, -1)[1].decode()).split("\r\n")

if getvaluestatus == "1":
    # tp_log("GetValue successful: " + str(getvaluestatus))
    rec = str(rec).split(",")
else:
    tp_log("GetValue failed: " + str(getvaluestatus))

After get the cell value, we need to close the connection

client_socket_close(socket)

The Full Code

def get_data_from_cognex(cel_data):
    ### Configuration of camera settings, adjust where needed ###
    port = 10000
    ip = "192.168.137.10"

    ### Connect to camera and return status ###
    socket = client_socket_open(ip, port)

    ### After connecting the camera first sends a welcome message and asks for username and password ###
    receive = client_socket_read(socket, -1, -1)[1].decode()
    # tp_log(str(receive))
    client_socket_write(socket, "admin\r\n".encode())  # username + carage return, new line character

    ### Asked for a password ###
    receive = client_socket_read(socket, -1, -1)[1].decode()
    # tp_log(str(receive))
    client_socket_write(socket, "\r\n".encode())  # password(empty) + carage return, new line character

    ### user logged IN message ###
    receive = client_socket_read(socket, -1, -1)[1].decode()
    # tp_log(str(receive))

    ### Trigger camera, only works if camera is in ONLINE mode
    # client_socket_write(socket, "SW8\r\n".encode())     #SW set event and wait gives an error and dont know why
    client_socket_write(socket, "SE8\r\n".encode())  # SE set event does work but does not wait
    wait(3)

    triggerstatus = client_socket_read(socket, -1, -1)[1].decode()[:-2]

    if triggerstatus != "1":
        tp_log("Trigger failed: " + str(triggerstatus))
        return 0

    wait(1)  # Just to be sure, this can probably be removed

    client_socket_write(socket, (cel_data + "\r\n").encode())  # GVC013
    getvaluestatus, rec, _empty = str(client_socket_read(socket, -1, -1)[1].decode()).split("\r\n")

    if getvaluestatus == "1":
        # tp_log("GetValue successful: " + str(getvaluestatus))
        rec = str(rec).split(",")
    else:
        tp_log("GetValue failed: " + str(getvaluestatus))

    # tp_log("Received list:" + str(rec))

    client_socket_close(socket)

    return rec
Input Name Input Type Description
cel_data string GVxyyy this is the get value command. x is the letter of the cell where you want the data from. yyy is the number of the cell where you want the data from. For example GVC013 gets the value of cell C 13.

This function returns an array of all the values in the defined cell that were sepperated using a ‘,’. We used it for getting the X and Y position, rotation and the number of the mold from the cognex camera. If the cognex camera gives no data then there was no good detection of the object.

X_Y_R = get_data_from_cognex('GVC019')
    if (X_Y_R == ['']):
        tp_popup('No mold detected please place mold in the detectable square', pm_type=DR_PM_MESSAGE, button_type=1)
        return 0

    _pos_x = float(X_Y_R[0])
    _pos_y = float(X_Y_R[1])
    _pos_r = float(X_Y_R[2])
    _mold_number = int(X_Y_R[3])

This was the end of this tutorial, I hope it helped you. Good luck with your project!

If you want to discuss this topic or if you have questions, reply in the comments.

Code Writen by A.Vonk

1 Like