A Class for Unipolar Motor Stepper - Part II
bentejuy Thu Jul 23, 2015 Tags InterfaceGPIO MotorStepper Unipolar

In the previous post, we saw that stepper motor control is relatively simple with Raspybot, although currently it only supports the class InterfaceGPIO as layer of connection with the Raspberry Pi. In this example I'll show how to control a stepper motor in a non-blocking manner.

The devices in Raspybot are based on threads to perform their duties in a transparent mode, although at the expense of losing some speed,but thanks to this, the main thread script can continue doing other tasks or control any other device.

Basically it is to avoid calling the MotorStepper's "join" method since this method blocks the main thread Python script until the motor stepper has finished the last task received. Sometimes this method may be useful but not when we want to continue performing other tasks in our script.

With the "alive" method we can know when the engine has stopped, provided that the last method called has received a number certain of steps or a certain angle. The "forward" and "backward" methods can receive three two types of parameters: "steps" or, "degrees" or time, and will stop when the received instruction is completed. If we want the motor to rotate indefinitely we just have to call "forward" or "backward" methods with a negative number of steps and the motor will rotate until the script ends or until the MotorStepper's "stop" method is called.

The example code is very simple and is composed of two parts: the first part with a "while" loop is shown using the "forward" and "backward" methods, and the second part with a "for" loop shows how use the "angle_to" method to move the stepper motor.

Have a nice day!

Download Code
#!/usr/bin/env python
# -+- coding: utf-8 -+-
# Name:         tests/motorstepper2
# Purpose:      Testing MotorStepperUnipolar object with mode not blocking
# Created:      04/01/2015
# Modified:     12/05/2015

import sys
import time

from raspybot.devices.motor import MotorStepperUnipolar
from raspybot.io.interface import InterfaceManager, InterfaceGPIO


def startinfo(motor):
    print 'Starting Motor => %s' % motor.get_name()

def stopinfo(motor):
    print 'Stopping Motor => %s' % motor.get_name()


manager = InterfaceManager()

iface1 = InterfaceGPIO(manager, pinout=(17, 18, 22, 23))
iface2 = InterfaceGPIO(manager, pinout=(9, 25, 11, 8))

motor1 = MotorStepperUnipolar(iface1, 'Motor 1', start=startinfo, stop=stopinfo)
motor2 = MotorStepperUnipolar(iface2, 'Motor 2', start=startinfo, stop=stopinfo)

    motor1.set_degrees(5.625/64)                #  Default degreess by step for stepper motor 28BYJ-48

    motor2.set_degrees(5.625/64)                #  Default degreess by step for stepper motor 28BYJ-48

    count = 5

    while count:
        if not motor1.alive() and not motor2.alive():
            print 'Running...', count

            if count % 2:

            count -= 1



    for angle in (-90, 180, -180, 180, -90):

        while motor1.alive() and motor2.alive():

except KeyboardInterrupt:
    print '\nScript stopped...'

except Exception, error:
    print 'Error :', error