Close

Controlling stepper motors with Gertbot and Python on the Raspberry Pi

The Gertbot is an add-on board for the Raspberry Pi, compatible with all existing models.

It brings power and motor control capabilities to the Raspberry Pi, but can also be used with other controllers supporting serial communication. Because the Gertbot has it’s own CPU dedicated to the real time activities, the Raspberry Pi is only required to pass high level commands such as “DC motor 1, ON”, “Stepper motor 2, 200 steps at 500Hz”.

The drivers come in two flavours: Python and C/C++. There is also an optional GUI, not requiring the use of code to use the Gertbot.

To know more about Gertbot, visit: http://www.gertbot.com

In this post, I’ll be demonstrating how to control two stepper motors using the Gertbot Python drivers.

Setting up

Getting everything set up is really easy and can be done in a matter of minutes.

The first step is to connect the Gertbot to the Raspberry Pi, but before doing so, ensure the Pi is powered off. Take the Gertbot and connect the 26-pin header, starting from pins 0 and 1. The Gertbot should slide in nicely next to the ethernet port, without touching it.

The result should be as follows:

photo 5

Next, the Gertbot Python drivers need to be installed on the Raspberry Pi. To do this, power on the Pi and using a terminal, browse to where you’d like to deploy the drivers.

The Gertbot Python drivers can be downloaded from the Gertbot website using following command:

  • wget http://www.gertbot.com/gbdownload/src/gertbot_py.tgz

The output of the command should be similar to this:

pi@PiDesk ~ $ wget http://www.gertbot.com/gbdownload/src/gertbot_py.tgz
--2015-05-04 19:28:43-- http://www.gertbot.com/gbdownload/src/gertbot_py.tgz
Resolving www.gertbot.com (www.gertbot.com)... 93.93.130.166
Connecting to www.gertbot.com (www.gertbot.com)|93.93.130.166|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8537 (8.3K) [application/x-gzip]
Saving to: `gertbot_py.tgz'

100%[================================================================================>] 8,537 --.-K/s in 0.01s

2015-05-04 19:28:43 (752 KB/s) - `gertbot_py.tgz' saved [8537/8537]

The downloaded drivers are compressed. In order to be able to use it, the file must be extracted:

  • tar -xvzf gertbot_py.tgz
pi@PiDesk ~ $ tar -xvzf gertbot_py.tgz
gertbot.py

There you have it, the “gertbot.py” file. By importing this file in other Python scripts, it is possible to call certain functions that will facilitate the control of the motors.

Script

At the bottom of the Gertbot’s download page, some example scripts are provided.

Starting off from the “Simple Rover” Python script and looking at the “Stepper Rover” C code, I came up with my own simple Python script to control two stepper motors. The script allows the control of two stepper motors to either go in one direction or the other. This is for example useful to control the Z-axis of a 3D printer.

It’s possible to define the number of steps and the frequency at which the steps are taken.

#!/usr/bin/python3

#################################################################
# stepper.py - Stepper motor control using Gertbot and Python #
# Based on Gertbot example code at http://www.gertbot.com/ #
#################################################################

# Use the Gertbot drivers
import gertbot as gb

# Using curses to repond to single keyboard keys
import curses

# This is for the development environment:
BOARD = 0 # which board we talk to
STEPPER_A = 0 # channel for first stepper motor
STEPPER_B = 2 # channel for second stepper motor
MODE = 24 # stepper control, gray code
FREQ = 900.0 # frequency

# Main program

# Get the curses screen
screen = curses.initscr()

# Open serial port to talk to Gertbot
gb.open_uart(0)

# Setup the channels for stepper motors
gb.set_mode(BOARD,STEPPER_A,MODE)
gb.set_mode(BOARD,STEPPER_B,MODE)
gb.freq_stepper(BOARD,STEPPER_A,FREQ)
gb.freq_stepper(BOARD,STEPPER_B,FREQ)

# Tell user what to expect
screen.addstr("Stepper motor control with Gertbot and python\n")
screen.addstr("\n")
screen.addstr("Press U to go up, D to go down, Q to quit\n")
screen.addstr("\n")

run = 1
while run==1 :
key = screen.getch() # Key?

if key==ord('q') :
run = 0 # stop running

if key==ord('u') :
gb.move_stepper(BOARD,STEPPER_A,200) # 200 steps, or one revolution
gb.move_stepper(BOARD,STEPPER_B,200) # 200 steps, or one revolution

if key==ord('d') :
gb.move_stepper(BOARD,STEPPER_A,-200) # 200 steps, or one revolution
gb.move_stepper(BOARD,STEPPER_B,-200) # 200 steps, or one revolution

# on exit stop everything
gb.emergency_stop()

# Set terminal behaviour normal again
curses.endwin()

The latest version of the code is available on GitHub.

As for the hardware, I used two 200 steps, bipolar, 12V NEMA17 stepper motors and connected them to the Gertbot as per the documentation. The underside of the Gertbot also indicates how to connect the motors.

photo 4 photo 3 photo 1

Demo

As a demo application, I’ve built a prototype of a lift to slide a screen in and out of place. The lift is controlled using the Python script documented earlier.

 

© Frederick Vandenbosch, 2014-2021. Unauthorised use and/or duplication of this material without express and written permission from this blog’s author and owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Frederick Vandenbosch with appropriate and specific direction to the original content.