Getting your ChickBot Pi moving

Like the first light & tone program, there are some exciting bits which you need to take on trust. Copy & paste, get your ChickBot moving, make changes and you’ll soon be on your way.

Copy & paste may seem like a bit of a cheat, but once you understand a module, you don’t need to prove that you can type it out by hand.

Fire up the Geany IDE software which starts out with a blank program that you can add to.

We need to setup which modules we want to use and create some data needed to drive the motors, so add this to the top of the file :

import RPi.GPIO as GPIO
import time

STEPPER_R_PINS = [22,27,17,4]
STEPPER_L_PINS = [18,23,25,24]

STEPPER_SEQ = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]

Copy & paste is best – the commands have to be written exactly otherwise they won’t work. And don’t forget, the indents are important.

You can see that when we import the RPi.GPIO module, we add ‘as GPIO’ at the end. This allows us to use just the word ‘GPIO’ instead of having to use ‘RPi.GPIO’ every time.

After importing the modules that are needed, we create three data containers called arrays. The first two reference the output pins connected to the stepper motors and the third tells the program how the pins should be turned on & off in sequence.

We have two setup functions (like Lego blocks) that we need, one for using the GPIO and one to setup the outputs for the motors:

def SetupMotor(pins):
    for pin in pins:
       GPIO.setup(pin, GPIO.OUT)

For each entry in a pin list (array), we set it to be an output.

def Setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    SetupMotor(STEPPER_R_PINS)
    SetupMotor(STEPPER_L_PINS)

This turns off the warnings we get each time we re-run the program and tells the GPIO module we are using the CPU pin numbering system. It then calls our SetupMotor function for each of the two motors.

 

We then need to add the routine that uses the data for both the stepper pins and the sequence of operation to drive the motor:

def Step(step, pins):
    for c in range(0, 4):
       GPIO.output(pins[c], STEPPER_SEQ[step][c])

This takes each of the four pins used by the stepper motor output chip and outputs the 1 (on) or 0 (off) from the sequence data to the appropriate pin.

Now we have our ‘supporting cast’, we can get down to the main part of the program. First, we call the main setup routine and then setup both the Left and Right motor. Then, using a counting loop, we send the motors 1000 steps forward, with a 1mS delay after each step to allow time for the mechanical operation to take place:

Setup()

for count in range (0, 1000):
    Step(count % 8, STEPPER_R_PINS)
    Step(count % 8, STEPPER_L_PINS)
    time.sleep(0.001)

All your code should now look like this:

import RPi.GPIO as GPIO 
import time 

STEPPER_R_PINS = [22,27,17,4]
STEPPER_L_PINS = [18,23,25,24]

STEPPER_SEQ = [[1,0,0,1],
  [1,0,0,0],
  [1,1,0,0],
  [0,1,0,0],
  [0,1,1,0],
  [0,0,1,0],
  [0,0,1,1],
  [0,0,0,1]]

def SetupMotor(pins):
  for pin in pins:
    GPIO.setup(pin, GPIO.OUT)

def Setup():  
  GPIO.setwarnings(False) 
  GPIO.setmode(GPIO.BCM)
  SetupMotor(STEPPER_R_PINS) 
  SetupMotor(STEPPER_L_PINS) 

def Step(step, pins): 
  for c in range(0, 4):   
    GPIO.output(pins[c], STEPPER_SEQ[step][c]) 

Setup() 

for count in range (0, 1000): 
  Step(count % 8, STEPPER_R_PINS) 
  Step(count % 8, STEPPER_L_PINS) 
  time.sleep(0.001)

You can run it by clicking on the Execute button.

Let’s go further!

The ChickBot moves the number of steps that are set in the FOR loop – the 1000 is the number of steps that we programmed in. The stepper motor requires 4096 steps for a full rotation. Using the power of Maths, with a wheel diameter of 39mm, πD tells us that the circumference is 123mm. So 33.3 steps per millimetre or 333 steps per cm. If you change the 1000 to 3330, we can expect your ChickBot to move 10cm.

Feel free to change the number to see change the distance. You can get it to start moving again after it’s done a run by closing the terminal window and clicking Execute again.

You may want to add a ‘wait for button press’ line – this will allow you to start the program without the ChickBot starting to move straight away. This code will wait for the button on the top right of the driver board to be pressed before it starts:

RPi.GPIO.setup(2, RPi.GPIO.IN)
while(RPi.GPIO.input(2)):
    pass

It needs to go between the SetupMotor(STEPPER_L_PINS) and the for count loop.