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/ )

Pibrella

20131128_223714These instructions refer to old 4 Please upgrade

 

The Pibrella is nice add-on board for the Raspberry Pi which consists of 3 large LEDs (Red/Amber/Green), 4 buffered outputs (OutE-F) with white LEDs to indicate their state, 4 inputs (InA-D) a built in large switch and a little piezo beeper.

Support for it has been added to the ScratchGPIO V4 developement version.

To use it, create a variable called AddOn and set its value to Pibrella in your On Green Flag event.

You can then use the usual AllOn/Off broadcasts and well as individual broadcasts for Red/Amber/Green/OutputE/OutputF/OutputG and OutputH

Also there is a special broadcast BeepXXXX where XXXX is the frequency in kHz that you want the beeper to make (The quality is very low so don’t expect Hi-Fi )

The 4 inputs are recognised as sensor values InA/InB/InC and InD and the switch is called “switch”

To control the brightness of the LEDs or the level of the outputs use redpower/amberpower/greenpower or powere/powerf/powerg or powerh.

ScratchGPIO V6 Alpha Testing

This blog is to introduce new versions of ScratchGPIO that might contain bugs or work in a different way to the normal version – so don’t use it to demonstrate ScratchGPIO to a class full of new users as it might change at anytime and not work properly.

It installs in a separate folder and doesn’t interfere with other versions

Copy the text below ( left click just before the s of sudo and drag right until all the text in the line as been selected) then right-click and select copy. Open up an LX Terminal window and select Edit and the Paste that into an LX Terminal window and run it to download the installer.

wget http://git.io/nFwadg -O isgh6alpha.sh

Once the installer has been downloaded then just type (or copy and paste the text below as before)

sudo bash isgh6alpha.sh

ChangeLog:

V6 Alpha Launched

Version =  ‘v5.3.23’ # 28June2014 NunChuck import wrapped in try except

Version =  ‘v5.3.22’ # 28June2014 PiGlow/Motor interaction bug fixed

Version =  ‘v5.3.20’ # 26June2014 NunChuck code released

Version =  ‘v5.3.05’ # 15June2014 Event detection and Triggering changed around

Version =  ‘v5.3.00’ # 11June2014 Pin trigger broadcasts

Version =  ‘v5.2.12’ # 29May14 – Changes to PiRoCon/MicRoCon motors

Version =  ‘v5.2.08’ # 23May14 – Ultra Threading for Pi2Go

Version =  ‘v5.2.07’ # 19May14 – Pibrella MotorE..H added and all motors new PWM code

Version =  ‘v5.2.06’ # 19May14 – Better Motor PWM by switching PWM pins

17May14 – 5.2.01 Major change – use much lower freq for motor PWM to improve torque at low speeds

11May14 – 5.1.41 PiDie bug fix with power

25Apr154 5.1.25  make sure all GPIO pins are turned off on program exit

21Apr14: 5.1.19 Event Triggering fixed/improved and back in.  Faster info flow from handler to Scratch as well 🙂

20Apr14:  V.5.1.17  Eventr triggering removed temp. Fix I2C issue with using normal scratchgpio PiMatrix removed temp

15Apr14: V.5.1.13 Lots of bux fixes and ADS581 support re-added

11Apr14: V5.1.04 – Change Github branch

9Apr14: V5.1.03 – More bug fix on inputs and change MotorPiTx to use output1/2 input1/2

9Apr14: V5.1.01 – Bug fix new input handling and add support for RC timing psuedo analog input

8Apr14 Input handling changed to use event triggers so no input change should be missed.  Also Out1/Out2 added for MotorPiTx

6Apr14 RasPiBot2 Added. 26Mar14 HapPi Added

 

PiRingo

The PiRingo from 4Tronix is a 12 LED  board arranged in a ring with a couple of switches.

To use it , launch ScratchGPIO Pluse create a variable called AddOn and use the Green Flag Event to set it to PiRingo

You can control the LEDs by using broadcasting Led1 .. Led12 on or Off e.g broadcast Led5On or broadcast Led1Off.  You also can use the usual AllOn and AllOff broadcasts.

PiRingoDocIf you create a variable called Power2, then you can vary the brightness of LED2 by changing its value from 0 to 100.

The switches states are available as on or off by using the sensor blocks in the sensing section.