I am using Opentrack to track head movements and the coordinates are sent through UDP to my Python program. The program works in the sense that it receives the coordinates correctly, but however I have noticed that there is a large delay before information arrives.

After observing the behaviour it seems to me that the tracking software sends the coordinates to some buffer that my program fetches the data from, but my program is slower to fetch the data than the speed which the buffer fills up. This means that if I move my head then those new coordinates have all been detected but the program has to gradually go through the buffer which causes the delay. This is a problem since I am using this as a real-time application that needs to send the current coordinates instantly to my program all the time.

I'm not sure if the problem is in the Opentrack software and if I should head over to that community for help, or if I can fix it in Python...

Basically, I just wish there wasn't a buffer but that it instead just sent the current coordinates (it doesn't matter if some measured coordinates are lost in my application).

def connect(self, PORT):
    HOST = ''   # Symbolic name meaning all available interfaces
    #PORT = 8888 # Arbitrary non-privileged port

    # Datagram (udp) socket
    try :
        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        print 'Socket created'
    except socket.error, msg :
        print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]

    # Bind socket to local host and port
        self.s.bind((HOST, PORT))
    except socket.error , msg:
        print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]

    print 'Socket bind complete'

#now keep talking with the client
def fetch(self):
    # receive data from client (data, addr)
    d = self.s.recvfrom(1024)
    data = d[0]
    addr = d[1]

    if data: 

        reply = 'OK...' + data

        self.s.sendto(reply , addr)

        unpacked_data = struct.unpack('dddddd', data)
        x = unpacked_data[0]
        y = unpacked_data[1]
        z = unpacked_data[2]
        return (x, y, z)
  • A network analyzer (e.g. Wireshark) should help you identify who is responsible for the buffering, sender or receiver. – jedwards Jul 20 '16 at 10:47

So I solved it by adding the line

self.s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1)

in my code. This sets the buffer to size 1 which solved my problem.

Your Answer

