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

Connect over TCP/IP (by @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))
    server_socket.listen(5)
    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'
            client_socket.sendall(response.encode())
            time.sleep(2)
            response = '2'
            client_socket.sendall(response.encode())
            time.sleep(3)
        client_socket.close()

if __name__ == '__main__':
    HOST = '192.168.0.2'  # 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

connect:
  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"
    ELSE
      PRINT "TCP_CONNECT error id = ", sock_id1, ", error count = ", er_count
      GOTO connect
    END
  ELSE
    PRINT "TCP_CONNECT OK id = ", sock_id1, ", with time elapsed = ", TIMER (2)
  END
.END

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() ;
  WHILE TRUE DO
    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"
     ELSE
      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]
        END
        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]
        END
      END
      ELSE
        $recv_buf[1] = "000"
        ret = -1
      END
    END
  END
.END

Connect over UDP (by @Shiyar)

Sometimes you need faster solutions, In that case you can use UTP protocol which is very easy to be used in python and also in Kawasaki Robot.

Here is code Robot Code:

.PROGRAM connect_to_pc_by_udp()
  timeout = 120
  answer_timeout = 3
  ip[1] = 192
  ip[2] = 168
  ip[3] = 0
  ip[4] = 10  
  port = 10010
  numbytes = 1
  ret = 0
  WHILE TRUE DO
    TWAIT 1
    UDP_RECVFROM ret, port, $cnt[0], numbytes, timeout, ip[1]
    IF ret <> 0 THEN
      PRINT "No data received within timeout period or error code: ", ret
      ; Continue to the next iteration without halting
    ELSE
        PRINT "Message: ", $cnt[0]
      ; Send confirmation message
      $cnt[0] = $ENCODE (/D, numbytes)
      UDP_SENDTO ret, ip[1], port, $cnt[0], 1, answer_timeout
      IF ret <> 0 THEN
        PRINT "Error with the UDP send, code: ", ret
        ; Optionally handle send error but do not halt
      END
    END
  END
.END

Python code:

import socket
import time

def udp_start(HOST, PORT, TRG_HOST):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind((HOST, PORT))
    print("UDP server started.")

    while True:
        input("Press Enter to send data...")  # Waits for Enter key press to proceed
        response = '1'
        print("Sending data")
        server_socket.sendto(response.encode(), TRG_HOST)
        time.sleep(2)  # Optional: delay between sends

if __name__ == '__main__':
    HOST = '192.168.0.2'  # Local IP address
    PORT = 10010  # Port to listen on
    TRG_HOST = ('192.168.0.1', PORT)  # Target host and port
    udp_start(HOST, PORT, TRG_HOST)

General solution (by @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:
tcp_client.as (3.4 KB)
If you want to use the robot as server:
tcp_server.as (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 library.zip (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

General solution (by @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:
tcp_client.as (3.4 KB)
If you want to use the robot as server:
tcp_server.as (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 library.zip (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

Sometimes you need faster solutions, In that case you can use UTP protocol which is very easy to be used in python and also in Kawasaki Robot.

Here is code Robot Code:

.PROGRAM connect_to_pc_by_udp()
  timeout = 120
  answer_timeout = 3
  ip[1] = 192
  ip[2] = 168
  ip[3] = 0
  ip[4] = 10  
  port = 10010
  numbytes = 1
  ret = 0
  WHILE TRUE DO
    TWAIT 1
    UDP_RECVFROM ret, port, $cnt[0], numbytes, timeout, ip[1]
    IF ret <> 0 THEN
      PRINT "No data received within timeout period or error code: ", ret
      ; Continue to the next iteration without halting
    ELSE
        PRINT "Message: ", $cnt[0]
      ; Send confirmation message
      $cnt[0] = $ENCODE (/D, numbytes)
      UDP_SENDTO ret, ip[1], port, $cnt[0], 1, answer_timeout
      IF ret <> 0 THEN
        PRINT "Error with the UDP send, code: ", ret
        ; Optionally handle send error but do not halt
      END
    END
  END
.END

Python code:

import socket
import time

def udp_start(HOST, PORT, TRG_HOST):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind((HOST, PORT))
    print("UDP server started.")

    while True:
        input("Press Enter to send data...")  # Waits for Enter key press to proceed
        response = '1'
        print("Sending data")
        server_socket.sendto(response.encode(), TRG_HOST)
        time.sleep(2)  # Optional: delay between sends

if __name__ == '__main__':
    HOST = '192.168.0.2'  # Local IP address
    PORT = 10010  # Port to listen on
    TRG_HOST = ('192.168.0.1', PORT)  # Target host and port
    udp_start(HOST, PORT, TRG_HOST)```
1 Like

Fantastic @Shiyar! I will copy it into the main doc (with attribution!)

1 Like