User Tools

Site Tools


rb5_advanced_programming

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rb5_advanced_programming [2023/01/20 14:02] – [Exernal Connection] bskimrb5_advanced_programming [2023/02/07 14:22] (current) – [Exercise] bskim
Line 15: Line 15:
  
 The control syntax provided in the teaching pendant / tablet UI can be implemented by the user directly from the external control device, and the robot operation commands / IO control commands are sent according to the user’s use case. The control syntax provided in the teaching pendant / tablet UI can be implemented by the user directly from the external control device, and the robot operation commands / IO control commands are sent according to the user’s use case.
- 
  
 The following document describes an example of driving a robot with the above concepts. The following document describes an example of driving a robot with the above concepts.
  
 + *[[rb5_advanced_programming#API Commands|API Commands]]
 =====External Connection===== =====External Connection=====
 In order to use external control, the external computer must be connected to the control box. The connection uses TCP / IP communication and the corresponding IP address can be set in the pendant. The result is displayed on the screen on control panel. Ports 5000 and 5001 open for external control. Port 5000 is a port for receiving commands, and port 5001 is a port for requesting and sending data indicating robot status. For convenience, port 5000 is called the command port and port 5001 is called the data port. In order to use external control, the external computer must be connected to the control box. The connection uses TCP / IP communication and the corresponding IP address can be set in the pendant. The result is displayed on the screen on control panel. Ports 5000 and 5001 open for external control. Port 5000 is a port for receiving commands, and port 5001 is a port for requesting and sending data indicating robot status. For convenience, port 5000 is called the command port and port 5001 is called the data port.
Line 29: Line 29:
 |  0x24  |  Size&0xFF  |  (Size>>8)&0xFF  |  0x03  |  Data  | |  0x24  |  Size&0xFF  |  (Size>>8)&0xFF  |  0x03  |  Data  |
 The format of the data is shown below. Depending on the system version, the size of the data may be different. However, the order is consistent, please refer to the table below. The format of the data is shown below. Depending on the system version, the size of the data may be different. However, the order is consistent, please refer to the table below.
- *[[rb5_advanced_programming#reqdata format|reqdata format]] 
  
-======Example====== 
-=====External Connect with Python===== 
-This is simple code for connect External Computer with RB5 Controller. 
  
 +[[rb5_advanced_programming#reqdata format|reqdata format]]
 +
 +==== Exercise ====
 +* Print out the RB5's current Position using script command.
 +
 +* Print out the RB5's current Joint angle using script command.
 +
 +* Move RB5 from joint angle(0,0,0,0,0,0) to angle(10,20,30,40,50,60), speed 30% using script command.
 +
 +* Move RB5 from joint Position(0.4,0,0.4,0,0,0) to Position(0.2,0.2,0.5,0,0,0), speed 30% using script command.
 +
 +
 +====Example====
 +===External Connect and Initialize with Python===
 +This is simple code for connect External Computer with RB5 Controller.
  
 <code python> <code python>
Line 82: Line 93:
         setRealMode()         setRealMode()
         print("set_real_mode")         print("set_real_mode")
 +        
 +    except KeyboardInterrupt:
 +        pass
 +    finally:
 +        os._exit(0) # not call close() ** workaround for native dead
 +</code>
 +
 +
 +===Get RB5 data from Controller with Python===
 +This is Example code for get RB5 data from RB5 Controller using Multi-thread.
 +you can make main program to control RB5 with RB5 data.
 +
 +<code python>
 +from queue import Queue 
 +import threading
 +
 +import socket
 +import sys
 +import struct
 +
 +####### RB5 API #######
 +#RB5 API to control RB5 Robot, 
 +#This RB5 API is based on RB5 manual
 +
 +def Rb5_decode(data):
 +    decode_data=[]
 +    for i in range(26):
 +        data_4byte=struct.unpack('f',data[i*4:i*4+4])[0]   
 +        decode_data.append(data_4byte)
 +    return decode_data
 +
 +def Rb5_getdata(sock):
 +    bufsize = 4096
 +    msg='reqdata'
 +    sdata=msg.encode()
 +    sock.send(sdata)
 +    data = sock.recv(bufsize)
 +    res=Rb5_decode(data)
 +        
 +    return res
 +            
 +def get_data_thread(sock, q_in, q_out):
 +    print('start')
 +    while True:
 +        cmd=q_in.get()
 +        if cmd == 1:
 +            rb5_data_ = Rb5_getdata(sock)
 +            q_out.put(rb5_data_)
 +            
 +def sendCommand(cmd):
 +    global cmd_socket
 +    send_cmd=cmd.encode()
 +    cmd_socket.send(send_cmd)
 +
 +def waitUntilMotionDone():
 +    usleep(10)
 +    # while(isMotionIdle() != True):
 +    #     print(isMotionIdle())
 +    #     usleep(10)
 +
 +def initRobot():
 +    sendCommand("mc jall init")
 +    time.sleep(1)
 +
 +#  <ProgramMode_Real>
 +#  change to 'real robot' mode -- robot will move
 +def setRealMode():
 +    sendCommand("pgmode real")
 +    time.sleep(1)
 +    # return *this;
 +
 +# <ProgramMode_Simulation>
 +# change to 'simulation' mode -- robot will not move except teaching
 +def setSimulationMode():
 +    sendCommand("pgmode simulation")
 +    time.sleep(1)
 +    # return *this;
 +
 +if __name__ == "__main__":
 +    q_in =Queue(1)
 +    q_out =Queue(2)
 +    global rb5_data
 +    global cmd_jnt
 +    global cmd_pose
 +    
 +    host = RB5_ip
 +    data_port = 5001
 +    cmd_port = 5000
 +    ###### Initializing #######
 +    if len(sys.argv)>1:
 +        host = sys.argv[1]
 +
 +    data_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 +    cmd_socket  = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 +    try:
 +        data_socket.connect((host, data_port))
 +        cmd_socket.connect((host, cmd_port))
 +        
 +        print("start")
 +
 +        ###### Start daemon thread to get RB5 data  #######
 +        t1 = threading.Thread(target=get_data_thread, args=(data_socket, q_in, q_out))
 +        t1.daemon = True 
 +        t1.start()
 +        print("start_tread")
 +        
 +        initRobot()
 +        print("init")
 +        setRealMode()
 +        print("set_real_mode")
 +        
 +        while True:
 +            time.sleep(step_time)
 +            q_in.put(1)
 +            rb5_data=q_out.get()
 +            get_robot_param()
 +            
 +        print("### End ###")
                  
     except KeyboardInterrupt:     except KeyboardInterrupt:
Line 207: Line 336:
  
 =====reqdata format===== =====reqdata format=====
 +
 +^             Header (4 Byte)             ||||  Data (n Byte)  |
 +|  0x24  |  Size&0xFF  |  (Size>>8)&0xFF  |  0x03  |  Data  |
 +
 {{ :bskim:rb5_tutorial:reqdata1.png?nolink&600 |}} {{ :bskim:rb5_tutorial:reqdata1.png?nolink&600 |}}
 {{ :bskim:rb5_tutorial:reqdata2.png?nolink&600 |}} {{ :bskim:rb5_tutorial:reqdata2.png?nolink&600 |}}
rb5_advanced_programming.1674252141.txt.gz · Last modified: by bskim