Accurately determining the progress of a wheeled robot can be pretty tricky (and expensive!). Dead reckoning assumes that our motors are perfectly matched, our wheels don't slip and the surface we are running on is perfectly flat: most of these conditions are unattainable and are never guaranteed. Rotary Encoders on the wheels or motors are more accurate, they certainly remove the need for matched motors and can deal with incline changes, however slippage is still an issue. Ultrasonic or Laser range sensors circumvent all of the mechanical issues that plague dead reckoning and encoders but require stationary fixed points of reference, are subject to interference (objects coming between the robot and the reference point) and have issues with regard to range, accuracy and resolution (the more you want, the more it costs!).
Ideally, we want a sensor that can accurately measure positional changes in two dimensions, has a simple interface, has low power requirements, is easily obtainable and cheap into the bargain. The solution: that old optical mouse you have lying around! Computer mice are designed to do just what we want: very accurately track the two dimensional movement of an object in near to real time. They actually have a resolution 1000dpi which means we are able to measure a movement of ~0.03mm in any direction!
We do have to choose our mouse a little carefully however. We need one that either has a PS/2 connector (6 pin mini-DIN) or a USB mouse that is PS/2 compatible (should be stated on the bottom of the mouse somewhere, see picture). This is because the PS/2 protocol is really easy to implement (particularly for microcontrollers) whereas USB is somewhat more difficult!
I happened to have an old Microsoft USB1.1/PS/2 compatible mouse hanging around that I wasn't using, so I decided to sacrifice it to my robotical pursuits in the manner described herein.
Step 1: Tear Down
First things first, we need to open the case and liberate the electronic goodies within. I chose to do this because the mouse is far too big and bulky for my robot. If you don't want to this you just need a USB Type A or PS/2 socket that can be wired to the processor (Arduino in this case) into which the mouse can be plugged without modification, just skip this bit.
So, the process to deconstruct my mouse was as follows (also see pictures):
If you plug the mouse into a computer or laptop, ensuring the lens is correctly in place on the bottom, the mouse should work normally when you move it around.
Step 2: Initial Circuit Modifications
Now we've got our hands on the board we need to connect it to our microcontroller. To do this we can either use the existing cable (USB) in my case and wire an appropriate socket or we can replace this wire with some of our own. We also need to determine the function of each of our new wires. This is done simply by using a multimeters continuity setting and working out which connection on the board is connected to which pin on the USB (or PS/2) connector. The original wiring can be seen in the first image above and functions are, left to right, as follows:
I chose to desolder the existing cable form the board and replace it with some wires with Arduino friendly Dupont connectors. I also chose not to connect a wire to the shield connection as its not strictly necessary. We then connect the mouse to our microcontroller of choice, an Arduino Leonardo Nano clone in this case, as follows:
Step 3: Software
Now we've connected everything up we need some software for the microcontroller. Firstly we need some code that implements the PS/2 protocol. Helpfully there is an Arduino library available for this, however I found it to be a little outdated and I wanted to add some new functionality so I reimplemented and extended it a bit and its attached (ps2.h and ps2.cpp).
I then wrote a simple sketch (mouse.ino, attached) which requests the mouses status and position delta (the change in X and Y directions since we last asked) and prints it to the serial port once every second. If we program the Arduino, and everything has gone well, the mouse will light up and your favourite terminal application (or the Arduino IDE's serial monitor) will show a stream of status, X and Y deltas once a second. The X and Y data has a range of -127 to +127 and each step represents 0.0254mm which means we can measure up to 3.2mm change in any direction (for X positive values are to the right, for Y positive values are forwards).
Step 4: Further Modifications and Finishing Up
Once I'd got everything up and running I decided to remove the extra circuit board which has the electronics for the mouses buttons and scroll wheel as I didn't need these features and they were just taking up space. I desoldered both of the ribbon cables, plugged everything in and... nothing. The mouse wouldn't light up and I got nothing from the serial port. A little investigation with the multimeter told me that three of the pins (1,3, and 6) on the rightmost connector (looking from the top) were usually connected together when the button board was connected, so I just shorted them together with some scrap wire as shown in the picture. I then plugged everything back in and Hey Presto! We were back in business!
The final touch was to superglue the clear plastic lens in place on the circuit board to retain it and so I wouldn't lose it.
The next step will be to mount this on my robot and put it to use. I actually intend to cannibalise another mouse in the same way and fit both to the robot so as to be able to accurately determine orientation as well as progress (as described here).
I hope people find this little tutorial useful, please feel free to ask any questions. Thank you.