How to get your Pi to go
Line-Following Robot Script
As you can see in Listing 1, I've put some of what I learned about the Pi2Go-Lite Python library into action to make the Pi2Go-Lite follow a circuit with a simple Python script. This line-following robot circuit is easily created by sticking duct tape down on a very light floor surface (or large sheets of white paper) in pretty much any shape or formation you like. You would likely have to alter the script if the lines were to crossover in a figure eight or a similar pattern; otherwise, this code will work straight out of the box. You can find this script and other Pi2Go-Lite examples on Zachary Igielman's GitHub page [5].
The script initializes the Pi2Go library, and sets some variables that control the speed of the motors. Before entering into the main loop of the script that's repeated infinitely until you elect to cancel it with a keyboard interrupt, the Pi2Go-Lite is set to drive forward at full speed. After assigning the methods for checking both line-following IR sensors to variables, a number of if
statements check to see where the robot is on the line. If the IR sensors detect no line at all, the robot is set to stop, so it can be rescued. If, however, one IR sensor detects a line and other doesn't the script tells the robot to turn in order to seek out the line in the opposite direction.
Of course, like most things that utilize the Raspberry Pi, the sky really is the limit with Pi2Go-Lite. It's even possible to add ever more amazing stuff to your robot project, like the brilliant MeArm acrylic robot arm (Figure 3)!
Infos
- 4tronix storefront: http://www.4tronix.co.uk
- Pi2Go homepage: http://www.pi2go.co.uk
- Pi2Go-Lite build instructions: http://4tronix.co.uk/blog/?p=405
- Pi2Go Python library and instructions: http://4tronix.co.uk/blog/?p=475
- Example Pi2Go scripts by Zachary Igielman: https://github.com/ZacharyIgielman/pi2go
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)