Tutorial 02 Basic Rover

simonmonk edited this page Apr 8, 2013 · 4 revisions

In this tutorial, you will use a wireless keyboard to control a simple rover built on the Magician Chassis (https://www.sparkfun.com/products/10825).

This project is also one of the projects in my book 'Programming Raspberry Pi: Getting Started with Python' published by TAB.

Simple Rover

Here are a couple of YouTube videos of this. One that I and one from James Dinsmore:

http://www.youtube.com/watch?v=IO8a-2lrSns

http://www.youtube.com/watch?v=7oTjEB-Maeg

Before you start, assemble your Magician Chassis, and follow Tutorial 01 Getting Started.

The Magician Chassis motors connect directly to the motor outputs of the RaspiRobotBoard. If one of the motors spins in the wrong direction, then swap its leads over.

You may be okay with 4 fresh high power AA batteries in the battery box that comes with the Magician Chassis, but most likely, you will need to replace it with one that takes 6 x AA batteries. Rovers like this tend to eat batteries pretty quickly, so I suggest rechargeables.

You need to install the following program to control the rover. Perhaps the simplest way is to equip the Rover with a WiFi dongle (opening up other possibilites) and install the program using SSH.

See the following tutorial on getting SSH up and running: http://www.adafruit.com/blog/2012/12/20/tutorial-adafruits-raspberry-pi-lesson-6-using-ssh-raspberry_pi-raspberrypi/

When connected to the Pi by SSH, start an editor session by sending the command:

nano rover.py

Then paste the following code into it:

from raspirobotboard import *
import pygame
import sys
from pygame.locals import *

rr = RaspiRobot()

pygame.init()
screen = pygame.display.set_mode((640, 480))

pygame.display.set_caption('RaspiRobot')
pygame.mouse.set_visible(0)


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                rr.forward()
                rr.set_led1(True)
                rr.set_led2(True)
            elif event.key == K_DOWN:
                rr.set_led1(True)
                rr.set_led2(True)
                rr.reverse()
            elif event.key == K_RIGHT:
                rr.set_led1(False)
                rr.set_led2(True)
                rr.right()
            elif event.key == K_LEFT:
                rr.set_led1(True)
                rr.set_led2(False)
                rr.left()
            elif event.key == K_SPACE:
                rr.stop()
                rr.set_led1(False)
                rr.set_led2(False)

To run the program, type:

sudo python rover.py