SHARE
TWEET

Motor_control.py

a guest Jan 2nd, 2015 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import sqrt
  2. from Bipolar_Stepper_Motor_Class import Bipolar_Stepper_Motor
  3. from time import sleep
  4. from threading import Thread
  5.  
  6. #Anti-backlash direction logging - the motor at home position last went in the -1 direction toward home
  7. stepper1_last_direction=-1
  8. stepper2_last_direction=-1
  9.  
  10. def sign(a): #translate integer signed value into integer direction
  11.     if a>0: #positive movement, 1
  12.         return 1
  13.     elif a<0:#negative movement, -1
  14.         return -1
  15.     elif a==0:
  16.         return 0
  17.     else:
  18.         print "Motor control function sign got a bad step value!"
  19.  
  20. def Motor_Step(stepper1, step1, stepper2, step2, speed):
  21.     global stepper2_last_direction,stepper1_last_direction
  22.     dir1=sign(step1) #make signed step integer into which direction
  23.     dir2=sign(step2)
  24.     if abs(dir1-stepper1_last_direction)==2: #If this is true, the motor has gone from one direction to the opposite, not just to 0
  25.         stepper1.correctBacklash(dir1) #correct for backlash
  26.         stepper1_last_direction=dir1 # set last direction moved in to the direction the motor will now be moving in
  27.     if abs(dir2-stepper2_last_direction)==2:
  28.         stepper2.correctBacklash(dir2)
  29.         stepper2_last_direction=dir2
  30.     step1=abs(step1) #turn step value into # of steps
  31.     step2=abs(step2)
  32.     duration=sqrt(step1**2+step2**2)/speed #Pythagorean theorem to figure out total distance moved
  33.     stepper1_thread=Thread(target=stepper1.move,args=(step1,dir1,duration,))#set up a thread for one motor
  34.     stepper2_thread=Thread(target=stepper2.move,args=(step2,dir2,duration,))
  35.     stepper1_thread.daemon=True #if the program shuts down, these threads will, too
  36.     stepper2_thread.daemon=True
  37.     stepper1_thread.start()#starts both threads at the same time - the motors move together
  38.     stepper2_thread.start()
  39.     sleep(duration)
  40.     while stepper1_thread.isAlive()==True or stepper2_thread.isAlive()==True: #ascertain that the motors are done before moving on
  41.         sleep(0.0001)
RAW Paste Data
Top