Wheel encoders on a Raspberry Pi Robot
One of the problems with normal DC motors on robots is that you can’t accurately control them- you can vary the power to them using PWM but you’ve no real idea how far your’ bot will travel.
If you use stepper motors instead, then you can get very accurate results but the cheap ones don’t turn very fast and so are not suitable for a large floor area but ideal for table top use.
Recently, the Initio Robot Chassis from 4tronix has come along and has wheel encoders built in and I’ve attempted to incorporate them into my ScratchGPIO project.
Basically, I use the RPi.GPIO library to do all my GPIO in and out and it has a facility to generate a pseudo-interupt (a callback routine) every time it gets a pulse. The wheel encoders produce 36 pulses for every 1 revolution of the wheels so its just a case of using the callback mechanism to count the number of pulses.
So I tell the wheels to turn 1 revolution and the count goes up by 36 – well not quite 🙂
I have no idea what power for what length of time (but approx 0.5) will give 1 revolution so I just switch the motor on, wait until count exceeds 36 then stop the motor.
The problem with this is that the motor takes some time to stop and so the final count ends up at about 48 or so.
The trick at this point is to remember the over-shoot and use the difference to compensate for the next move.
At the moment only the left hand encoder is read and I only use one of the sensing wires.
To connect it up
Looking at your PirRoCon board with GPIO pins on the left and regulator nearest you, remove the blue link nearest you in the bank of 8. (This disables the level-shifter for pin 7 (GPIO 4) which is the one I’ve designated for using for this.
Connect the brown wire from the left hand encoder (that’s left hand as if you were driving the robot yourself) and connect it to the exposed pin nearest the GPIO pins on the link you have just removed
Connect the black wire to a 0V pin and connect the red wire to 3.3 (NOT 5V!!!!!!) (The 1.2 version has dedicated 3.3V pins but if you have an original PiRoCon then you need to connect to the 3.3V on the i2C sockets using a short male-male lead)