Advertisement
Guest User

Bipolar_Stepper_Motor_Class.py

a guest
Jan 2nd, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. from time import sleep
  3.  
  4. class Bipolar_Stepper_Motor:
  5.     def __init__(self,step_pin,dir_pin,limit_pin,backlash=0): #initializing some basic stuff to keep track of things
  6.         GPIO.setmode(GPIO.BOARD)
  7.         self.position=0
  8.         self.step_pin=step_pin
  9.         self.dir_pin=dir_pin
  10.         GPIO.setup(self.step_pin,GPIO.OUT)
  11.         GPIO.setup(self.dir_pin,GPIO.OUT)
  12.         self.limit_pin=limit_pin
  13.         self.backlash=backlash
  14.     def move(self,steps,direction,duration):
  15.         if steps==0: #don't do anything!
  16.             return 0
  17.         else:
  18.             interval=duration/(steps*1.0) #divide time into time in between step
  19.         if direction==1: #change int direction to EasyDriver direction input
  20.             GPIO.output(self.dir_pin,0)
  21.         elif direction==-1:
  22.             GPIO.output(self.dir_pin,1)
  23.         else:
  24.             print "Bipolar Stepper Motor class move function got a bad direction value!"
  25.         for step in xrange(steps):#xrange is leaner and meaner for # iterations than range
  26.             GPIO.output(self.step_pin,1)#do a step
  27.             sleep(interval/2.0) #wait half the time
  28.             GPIO.output(self.step_pin,0)#the step is done
  29.             self.position+=direction
  30.             sleep(interval/2.0)#finish sleeping - this is the time between low and high pulse
  31.     def correctBacklash(self,new_dir):
  32.         if new_dir==1:#change int direction to EasyDriver-friendly direction
  33.             GPIO.output(self.dir_pin,0)
  34.         elif new_dir==-1:
  35.             GPIO.output(self.dir_pin,1)
  36.         else:
  37.             print "Bipolar Stepper Motor class correctBacklash function got a bad direction value!"
  38.         for step in range(self.backlash):#do steps, but don't update position - the axis hasn't moved
  39.             GPIO.output(self.step_pin,1)
  40.             sleep(0.00005)
  41.             GPIO.output(self.step_pin,0)
  42.             sleep(0.00005)
  43.         print "Corrected for some axis backlash."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement