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 & n) << 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.


I am not sure what you mean by 'displacement value which is acceleration independent'. The conversion of relative positioning you're getting from /dev/input/mouse will always depend on the DPI of the mouse. All you'd have to do is figure out the DPI (either from the specs or from measurements, and hence the conversion between reported dx/dy values, and then accumulate them once you start moving from the origin. Here are some resources that could be useful to you:

http://kodedevil.com/2017/07/09/optical-mouse-odometer-rpi/ (notice the scaling factor in the code)


  • $\begingroup$ I mean I need displacement values which won't depend on velocity. You can notice that the mouse needs to be moved a longer distance to go end to end on your screen, while it takes lesser distance at a higher speed. So that means I do not get a true value of just the displacement $\endgroup$ – jash101 Aug 10 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.