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!
#!/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) try: motor1.set_mode(motor1.MODE_SINGLE) motor1.set_degrees(5.625/64) # Default degreess by step for stepper motor 28BYJ-48 motor1.set_speed(15) motor2.set_mode(motor2.MODE_DUAL) motor2.set_degrees(5.625/64) # Default degreess by step for stepper motor 28BYJ-48 motor2.set_speed(15) count = 5 while count: if not motor1.alive() and not motor2.alive(): print 'Running...', count if count % 2: motor1.forward(degrees=180) motor2.forward(degrees=180) else: motor1.backward(degrees=180) motor2.backward(degrees=180) count -= 1 time.sleep(0.5) time.sleep(1.5) for angle in (-90, 180, -180, 180, -90): motor1.angle_to(angle) motor2.angle_to(angle) while motor1.alive() and motor2.alive(): time.sleep(1) except KeyboardInterrupt: print '\nScript stopped...' except Exception, error: print 'Error :', error finally: motor1.stop() motor2.stop() manager.delete(iface1) manager.delete(iface2) manager.cleanup()