Connect a Kawasaki robot to a Python program on a computer

Did you already solve this problem?

Just edit this wiki post!

Please provide a step-by-stap solution, including all the code and other things someone else would like to know to make it work.

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

Connecting to the Kawasaki to send single locations

Full solution

Thanks @Shiyar!

Connecting a Kawasaki Robot to a Python program on your computer was challenging, but we developed a solution using sockets. To facilitate this connection, set up the Python program as a server, allowing other devices, including the robot, to connect to it. This can be achieved using the following code:

Python code on computer

import socket
import time
def start_server(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    print(f"Server is listening on {host}:{port}")

    while True:
        client_socket, addr = server_socket.accept()
        print(f"Connected by {addr}")
        while True:
            # data = client_socket.recv(1024)
            # if not data:
            #     break
            # print("Received:", data.decode())
            # You can send a response here if needed

            response = '1'
            response = '2'

if __name__ == '__main__':
    HOST = ''  # Your PC's IP address
    PORT = 10001  # Port to listen on (non-privileged ports are > 1023)
    start_server(HOST, PORT)

Note: My code includes a segment within the while loop to send data for testing purposes. It sends the number β€˜1’ every 3 seconds, alternating with the number β€˜2’ on the subsequent iteration.

Kawasaki robot code (AS language)

After that you have to connect the Kawasaki robot to your pc/python program.
I made an script to do that here is the code:

.PROGRAM smr2() ; Shiyar
  tout_open = 60
  ip[1] = 192
  ip[2] = 168
  ip[3] = 0
  ip[4] = 2  ; PC's IP address
  port = 10001
  er_count = 0

  TIMER (2) = 0
  TCP_CONNECT sock_id1, port, ip[1], tout_open
  IF sock_id1 < 0 THEN
    er_count = er_count + 1
    IF er_count >= 5 THEN
      PRINT "Client Communication with PC has failed"
      PRINT "TCP_CONNECT error id = ", sock_id1, ", error count = ", er_count
      GOTO connect
    PRINT "TCP_CONNECT OK id = ", sock_id1, ", with time elapsed = ", TIMER (2)

After that, you might want to receive data from the Python program :blush:. This can be done using my script, which receives data and performs actions based on the received data, such as executing Option 1 or 2, etc. You can customize it however you like.

.PROGRAM receive_data() ;
    numbytes = 10
    max_length = 10
    tout_rec = 60
    ret = 0
    TCP_RECV ret, sock_id1, $recv_buf[1], numbytes, tout_rec, max_length
    IF ret < 0 THEN
      PRINT "TCP_RECV error in RECV", ret
      $recv_buf[1] = "000"
      IF numbytes > 0 THEN
        ;PRINT "TCP_RECV OK in RECV", ret
        FOR i = 1 TO numbytes
        PRINT "RecBuff[", i, "]= ", $recv_buf[i]
        IF $recv_buf[1] == "1" THEN
          PRINT "IT IS 1"
          SPEED 80 ALWAYS
          JMOVE #[ -100.000,-59.995,9.834,13.745,8.050,50.603]
        IF $recv_buf[1] == "2" THEN
          PRINT "IT IS 2"
          SPEED 80 ALWAYS
          JMOVE #[ -160.000,-59.995,9.834,13.745,8.050,50.603]
        $recv_buf[1] = "000"
        ret = -1

General solution

Thanks @Mathijs!

TCP or UDP communication is explained in the Kawasaki TCP/IP Communication Manual.

Some detailed examples are available starting at page 46. Please note that some functionality is not available out of the box, you need all the separate programs mentioned to make this work:

  • .PROGRAM open_socket() ;Starting communicatin
  • .PROGRAM send(.ret,.$data) ;Communication Sending data
  • .PROGRAM recv() ;Communication Receiving data
  • .PROGRAM close_socket() ;Closing communication

Please note that there might be differences between these functions when acting as server or client!

Example programs

If you want to use the robot as client: (3.4 KB)
If you want to use the robot as server: (3.6 KB)

Directly controlling the full Kawasaki movements with a bare Python library

Kawapai library by @GuusParis (experimental)

Python library for interfacing with Kawasaki D, E and F series controllers

Warning: API is in experimental stage

Note from @thijs: The Tetrisbot group made improvements to it, but we don’t have that code. I am currently trying to get it and will update this document when I have it.

Update: @tom was kind enough to provide the entire codebase of the Tetrisbot, including improved kawapai lib! Download here: tetris bot code incl. improved kawapai (3.7 MB)

ROS library

Official, unsupported KHI (Kawasaki Heavy Industry) Ros drivers. Currently only for these robots:

  • duaro1
  • rs007l
  • rs007n
  • rs013n
  • rs020n
  • rs025n
  • rs030n
  • rs80n
1 Like