Home Programming

Discussion

Left ArrowBack to discussions page
Tr1peiroTr1peiro Posts: 4 Apprentice

I was wondering if you cold help me in a proggram for the robot UR5, because I have been struggling in the creation of a socket/TCP that can run in the robot but at the same time can read the coordinates that have been sent by the computer.

 I have already the code for user/server already created but, the client program(URrobot) i can make a program that cand read this coordinates and I have already tried to use the scripts  socket_read_interger and the socket_read_ascii_float and none of them work





Tagged:

Best Answer

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    Answer ✓
    This is an example test socket that I created years ago to test some camera connection.  You can see here how the return is formatted, this is the required format per the UR documentation.  We read this as a string and then decoded it on the robot side since we wanted a mix of string and floats.

    import socket
    import time
    host = "127.0.0.1"
    port = 5001

    print("Program Start")
    tel = 0

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    s.listen(1)
    c, addr = s.accept()

    while (tel < 10):
      try:
        msg = c.recv(1024)
        print(msg)
        # c.send("Messsage '"+msg+"' was received")
        c.send("Pass,2.33,5.55,ok")
        if msg == "Stop":
        tel = 10
        print(tel)
      except socket.error as socketError:
        tel = 10
        print(socketError)
        print(tel)

    c.close()
    s.close()
    print("Program Finished")

Comments

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    When the server receives the read request what is it returning the to the robot and how is the data wrapped?  What is an example of the return value the server is sending?
  • Tr1peiroTr1peiro Posts: 4 Apprentice
    When the server receives the read request what is it returning the to the robot and how is the data wrapped?  What is an example of the return value the server is sending?

    the code i use on the robot is the following down

    def test2():
        flag = False
        sync_flag = False
        
        open = False
        while(open == False):
            open = socket_open("192.168.1.6",5050)
        end
        
        # popup("Connected to server!")
        while (sync_flag != True):
            data = socket_read_ascii_float(6)
            if (data[1] == 1):
                flag = True
            end
        end
        socket_close()
        flag = False

    end
    test2()


    but he can't read the information i send in the PC to the interface's robot.


  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    What is an example of the return from the PC server? If its not configured correctly the robot will not receive it.  That is why I asked to see the return value
  • Tr1peiroTr1peiro Posts: 4 Apprentice
    What is an example of the return from the PC server? If its not configured correctly the robot will not receive it.  That is why I asked to see the return value

    The code on pc server is write in that way:

    "import socket
    from struct import pack

    #Variaveis
    S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    IP = "192.168.1.6"
    PORTA =  5050
    FORMAT = "utf-8"

    server_address = (IP, PORTA)
    S.bind(server_address)
    S.listen(5)

    connection, client_address = S.accept()
    val = pack('i!', 4)

    #Escolha das coordenadas
    waypoint1 = input('Inserir coordenadas: ' + "\n")
    waypoint2 = input('Inserir coordenadas: ' + "\n")
    waypoint3 = input('Inserir coordenadas: ' + "\n")
    waypoint4 = input('Inserir coordenadas: ' + "\n")
    Ligar = input('Enviar coordenadas e ligaar Servidor? ')


    #Decidir se conecto ao servidor ou não
    if Ligar == ('yes' or 'Y'):
       connection.send(val)
       S.send(waypoint1.encode())
       S.send(waypoint2.encode())
       S.send(waypoint3.encode())
       S.send(waypoint4.encode())
           
    elif Ligar == ('no' or 'N'):
        connection.close()
        
    else:
        print("tenta outra vez")
        
    connection.close()"

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    Also, you are stuck in an endless loop on your robot code, you are performing a while loop against `sync_flag` but you are setting the value for `flag` in your logic if a value is returned. Also are you also always sending the first value of the data back from the server as `1`? A better check to know if data was received is to query index 0 in the array, if the value is `6` then you have gotten back the volume of data that you expected, if it's anything other than 6 you are not getting back the data you requested.  If you put the `global` identifier in front of the `data` variable you would be able to see that on the teach pendant in the variables list and you could therefore see what the data variable is holding.

    Remember the data variable will be an array of length = 7, you are requesting 6 values which will be indices 1 to 6 and the robot will place the size of the resulting read in index 0, if the read fails you will get a 0 in that position, otherwise it will be the number of values that were returned.


  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    Have you tried using a socket test program to send the request and see what value that server returns.  Also, the robot has a timeout of 2 seconds on the read value so if you are waiting for the robot to send a request and then typing the values back you are probably timing out on the robot read
  • Tr1peiroTr1peiro Posts: 4 Apprentice
    Have you tried using a socket test program to send the request and see what value that server returns.  Also, the robot has a timeout of 2 seconds on the read value so if you are waiting for the robot to send a request and then typing the values back you are probably timing out on the robot read
    I also test in SocketTest and sending the coordinates and work fine....

    My big trouble now is how i write the socket to connect the machine to the PC.....

    In the first time i create one simple code without the while loop but  this code doesn't work.....

    # def prog():
    #     open = socket_open("192.168.1.7", 5050)
    #     movel(Plane_1, accel_mss, speed_ms, 0,0)
    #     sendToServer = 'Coordenadas: '
    #     socket_send_string(sendToServer)
    #     data_1 = socket_read_byte_list(8)
    #     if data[0]=8
    # prog()


  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,267 Handy
    What does the return value that you get in socket test look like?  Can you send an example of what the server is sending to the request?
Sign In or Register to comment.
Left ArrowBack to discussions page