# How to get position (X Y values) from a USB optical mouse

I want to use an optical mouse to measure displacement in X and Y direction of my robot. I'm using Ubuntu mate 18.04 on raspberry pi and wrote the following python code :

mouse = file('/dev/input/mouse0')
while True:
status, dx, dy = tuple(ord(c) for c in mouse.read(3))

def to_signed(n):
return n - ((0x80 &amp; n) &lt;&lt; 1)

dx = to_signed(dx)
dy = to_signed(dy)
print "%#02x %d %d" % (status, dx, dy)


But these displacement values are velocities dependent. I just want the displacement value, which is acceleration independent.