yaconsult
Posts: 33
Joined: Fri Mar 23, 2012 2:29 pm

Looking for sample python code driving stepper and servos

Tue May 10, 2016 5:52 am

I am new to working with servos and steppers but experienced with linux and python. I am helping a friend with a project that involves controlling three servos and a stepper motor using both the Adafruit servo board and the Adafruit stepper board.

I have been using the DualStepperTest.py (https://github.com/adafruit/Adafruit-Mo ... perTest.py) code as an example and assume that this is the best way to control multiple hardware devices at the same time by using threading?

I would love to see more example python code showing the control of multiple hardware devices like this. There will be one or two buttons that will be pushed and pushing the buttons will require the positions of the servos and stepper to be changed in some way at the same time. So I am thinking that the main loop will be sitting and watching for button presses and when they happen a call setting a new position will be sent to the thread controlling the appropriate servo or stepper?

Looking at working code would help a lot! So if you are willing to post or link to code examples showing this kind of hardware control - thank you very much! I've got to say that being able to control hardware from the Pi is fun!

MarcScott
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 81
Joined: Sat Aug 08, 2015 11:30 am

Re: Looking for sample python code driving stepper and servo

Wed May 11, 2016 7:33 am

You could have a look at gpiozero https://gpiozero.readthedocs.io/en/v1.2.0/index.html

There's no control for steppers or servos built-in as yet, although there is a generic pwm output device that you could use.

As for threading - it's built in. For instance, the code below will run and print hello every time the button is pushed.

Code: Select all

from gpiozero import Button

button = Button(4)

def say_hello():
    print('hello')

button.when_pressed = say_hello

User avatar
B.Goode
Posts: 9557
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Looking for sample python code driving stepper and servo

Wed May 11, 2016 8:02 am

pigpio is a library written and maintained by @joan, a regular contributor to these forums.

It includes support and examples for driving servos from python. Well worth a read...

http://abyz.co.uk/rpi/pigpio/python.html

User avatar
joan
Posts: 14747
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Looking for sample python code driving stepper and servo

Wed May 11, 2016 8:13 am

B.Goode wrote:pigpio is a library written and maintained by @joan, a regular contributor to these forums.

It includes support and examples for driving servos from python. Well worth a read...

http://abyz.co.uk/rpi/pigpio/python.html
Coincidentally I have been working on a script to drive multiple steppers. Currently it is working for an arbitrary number of bipolar steppers which need each phase driven in turn. I have added support for servos. I need to add support for PWM/DIR steppers and while I'm at it I will also allow single GPIO to be switched on or off.

The idea is that all the movements will be synchronised. It works in principle and the practice also looks fairly solid. Python on a Pi B+, with my non-optimised code, can handle about 2000 "events" per second. If I did the main event handler in C I think it would be a lot faster.
Last edited by joan on Sat May 21, 2016 1:51 pm, edited 1 time in total.

pootle
Posts: 351
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Looking for sample python code driving stepper and servo

Fri May 13, 2016 10:51 pm

joan wrote:Coincidentally I have been working on a script to drive multiple steppers. Currently it is working for an arbitrary number of bipolar steppers which need each phase driven in turn. I have added support for servos. I need to add support for PWM/DIR steppers and while I'm at it I will also allow single GPIO to be switched on or off.
I'm driving a couple of steppers via an Adafruit DC and Stepper Motor HAT for Raspberry Pi. It will drive stepper motors but can only do so pretty slowly, as everything goes down the i2c bus, andit has to send several commands just to perform 1 step. This limits the speed to about 70 steps per second or 1/2 that if running 2 steppers.

(Gory details)

What hardware interface are you using to drive your motors?

User avatar
joan
Posts: 14747
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Looking for sample python code driving stepper and servo

Sat May 14, 2016 9:22 am

I'm using the inexpensive unipolar steppers 28BYJ-48 (the 5V version). They tend to come with a ULN2003A driver board and cost less than a UK pound for stepper and driver (eBay).

Each driver board is driven by 4 GPIO. I guess your driver uses a pulse/dir interface so will only require 2 GPIO.

Connecting to the GPIO means you could send more pulses per second than your driver could cope with from the Pi. 100 thousand per second if you wanted.

The 28BYJ-48 top out at about 1000 pulses per second.
multi-stepper.png
multi-stepper.png (45.03 KiB) Viewed 2009 times
The above shows the pulses for 5 steppers and two servos (GPIO 2/3).

Short webm screencast.

Return to “Python”