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.

penclode

encoderpicBut, as usual with ScratchGPIO, all this is hidden beneath the bonnet and all the Scratch has to do is the scripts on the left to move forward and back one wheel circumference.

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)

ScratchGPIO Documentation

This post is out-of-dat.-  Please update to latest version

This post is to hopefully document all the available broadcasts and variable usage that ScratchGPIO understands and responds to

DocPic

Scratch Variable “bug”

Although using variables is necessary once you start writing complex scripts, there is a “bug” or a “feature” (depending on yourpoint of view) in the way that Scratch sends out variable changes to any external programs.

If you say broadcast pin11On then Scratch will always send Pin11On out to any listening programs

But if you use a variable called Pin11 and say set Pin11 to On then it only sends this information out if the previous value of Pin11 variable was not On i.e. only changes are sent out

pindocThis code will not switch pin11 on the second time as the variable has not changed from its previous value.

One workaround is to use the green join block and broadcast any variables

joindoc

This method will ensure than any variable changes are always sent out

Fish Dish Raspberry Pi Addon Board

This post is out-of-date.- Please update to latest version

Fish Dish This board is available from PiSupply and is a simple board to use for a Traffic Light simulation with its 3 LEDs and buzzer and switch. It also has an added advantage of providing all of the GPIO pins for further expansion so any I2C device or any extra addon board can be piggy backed on top of it 🙂

The LEDS are called Led1..3, the buzzer is called buzzer and the switch is called switch (up and down)

The LEDs can also be referred to by name e.g red, yellow or green.

Ryanteck Motor Controller Board

This post is out-of-date.- Please update to latest version

This inexpensive motor controller board http://www.raspberrypi.org/archives/5269 gives full control over 2 motors and is one of the easiest ways to get into Robotics on the Raspberry Pi.

To use it in ScratchGPIO, simple set variable AddOn to RTKMotorCon and then you can use 2 variables – Motor1 and Motor 2 and vary the speed and direction of the motors from -100 to 100.

It also breaks out the 3.3V, GND and the I2C pins so it is very easy to add on other I2C devices as well.

(Support currently only available in the ScratchGPIO dev version https://cymplecy.wordpress.com/2013/11/26/scratchgpio-development-testing/ )