How to get your Pi to go

Optional Add-ons

Pi2Go-Lite currently has two optional extras that further add to its already impressive repertoire. The first is a cleverly designed Pan & Tilt kit designed specifically to work with the Raspberry Pi camera module. It comes with everything you need, including a longer camera cable, all the screws and pillars, and servomotors (Pi2Go-Lite can control servos with two on-board connectors). With the kit, you can create three types of camera mount: a simple fixed design (so the camera simply faces the front), a single-servo left- and right-panning mount, or a full pan and tilt mount (allowing the camera to move up and down, left and right), which would be great for following objects or remotely controlling the camera to look around a new environment. Not bad for £12 and probably a worthy investment, even if you don't have a Pi2Go.

The second optional extra is a wheel encoder kit, which plays on the fact that no two DC motors, regardless of how accurate or well made they are, will run at exactly the same speed. This ultimately means that your robot wants to turn in a big circle, no matter how carefully you select your motors. Not only is this annoying, but it will also play havoc with scripts that rely on the robot having a rough idea how far it's travelled and in what direction.

Using two more IR reflectance sensors and alternating black and white striped disks that attach to the inside of the Pi2Go's wheels, 4tronix has an affordable (<£4) encoder system for Pi2Go-Lite that lets you measure exactly how many revolutions each wheel has made so you can make exacting maneuvers without worrying about the robot drifting off course.

Controlling Pi2Go-Lite with Python

Like most Raspberry Pi peripherals, the preferred method of coding Pi2Go-Lite is Python, and the kit comes with a Python library and examples [4] designed to give you access to all of its functionality with easy-to-use methods.

To initialise the Pi2Go-Lite you simply add pi2go.init() to the top of your script, and when you want to close everything down at the end, you simply call pi2go.cleanup() at the end.

The motor functions are very simple and can be controlled by calling pi2go.forward(speed), where speed is an integer from 0 to 100. You can replace forward with reverse or spinLeft and spinRight. You can also control wheels individually, make sweeping turns by controlling the speed of both motors from the same function call, and more.

Sensors are equally easy to control. For example, in my line-following robot code (Listing 1), I've used the downward-facing infrared reflectance sensors to see if the robot is following a black line (usually made from duct tape or something similar) by calling irLeftLine() or irRightLine().

Listing 1

Line-Following Robot Python Script

01 #!/usr/bin/python
02
03 import sys, time
04 import pi2go
05
06 pi2go.init()
07
08 slowspeed = 20
09 fastspeed = 100
10
11 lastleft = 0
12 lastright = 0
13
14 # Let's get going
15 pi2go.forward(fastspeed)
16
17 # main loop
18 try:
19   while True:
20     left = pi2go.irLeftLine()
21     right = pi2go.irRightLine()
22     if left==0 and right==0:
23       pi2go.stop()
24     if left == 0 and lastleft == 1:
25       pi2go.turnForward(slowspeed,fastspeed)
26       pi2go.setAllLEDs(0, 4095, 4095)
27     elif right == 0 and lastright == 1:
28       pi2go.turnForward(fastspeed,slowspeed)
29       pi2go.setAllLEDs(4095, 0, 4095)
30     lastleft = left
31     lastright = right
32     time.sleep(0.01)
33
34 except KeyboardInterrupt:
35        pi2go.setAllLEDs(0, 0, 0)
36        pi2go.cleanup()
37        sys.exit()

Similar function calls are available for the IR sensors on the front of the robot to see if Pi2Go-Lite is in danger of bumping into anything as it ambles around a room. Testing the state of these sensors is as easy as calling pi2go.irAll(), which returns True if either sensor has been triggered. Of course, you really need to know which sensor has been triggered, so you can call irLeft() or irRight in your script to test them.

These examples only scratch the surface of what the Pi2Go-Lite's excellent Python library can do. With it, you can also control servos, take charge of the momentary button switch, and change the brightness of the various LEDs dotted around the chassis.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content