Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt
- from Bipolar_Stepper_Motor_Class import Bipolar_Stepper_Motor
- from time import sleep
- from threading import Thread
- #Anti-backlash direction logging - the motor at home position last went in the -1 direction toward home
- stepper1_last_direction=-1
- stepper2_last_direction=-1
- def sign(a): #translate integer signed value into integer direction
- if a>0: #positive movement, 1
- return 1
- elif a<0:#negative movement, -1
- return -1
- elif a==0:
- return 0
- else:
- print "Motor control function sign got a bad step value!"
- def Motor_Step(stepper1, step1, stepper2, step2, speed):
- global stepper2_last_direction,stepper1_last_direction
- dir1=sign(step1) #make signed step integer into which direction
- dir2=sign(step2)
- 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
- stepper1.correctBacklash(dir1) #correct for backlash
- stepper1_last_direction=dir1 # set last direction moved in to the direction the motor will now be moving in
- if abs(dir2-stepper2_last_direction)==2:
- stepper2.correctBacklash(dir2)
- stepper2_last_direction=dir2
- step1=abs(step1) #turn step value into # of steps
- step2=abs(step2)
- duration=sqrt(step1**2+step2**2)/speed #Pythagorean theorem to figure out total distance moved
- stepper1_thread=Thread(target=stepper1.move,args=(step1,dir1,duration,))#set up a thread for one motor
- stepper2_thread=Thread(target=stepper2.move,args=(step2,dir2,duration,))
- stepper1_thread.daemon=True #if the program shuts down, these threads will, too
- stepper2_thread.daemon=True
- stepper1_thread.start()#starts both threads at the same time - the motors move together
- stepper2_thread.start()
- sleep(duration)
- while stepper1_thread.isAlive()==True or stepper2_thread.isAlive()==True: #ascertain that the motors are done before moving on
- sleep(0.0001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement