Re: Stepper 288yj-48 - stop and restart from the latest position
Posted: Sat Apr 18, 2020 11:46 am
It doesn't. The hardware only stops once.
A small, affordable computer with free resources to help people learn, make things, and have fun
https://www.raspberrypi.org/forums/
https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=270132
Code: Select all
# version 1
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
# Stops X
stopsX = [546,1091,1637,2182,2728]
stop_countX = 0
# Stops Z
stopsZ = [114,228,341,455,568,683,796,910,1024,1252,1365,1479,1593,1707,1820,1934,2048,2162,2276,2389,2503,2617,2731,2844,2958,3072,3186,3300,3413,3527,3755,3864,3982,4096]
stop_countZ = 0
for pin in control_pinsX:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pinsZ:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep = 0
start = time.time()
# stepper Z starting Halfstep
Halfstep = 0
# Stepper X
cycle = 1
# Stepper Z
for I in range(4096): # this now the number of steps for a 360 degrees rotation
#print(I)
for pin in range(4):
GPIO.output(control_pinsZ[pin], halfstep_seq[Halfstep][pin])
time.sleep(0.001)
Halfstep = Halfstep + 1
if Halfstep == 8:
Halfstep = 0
print(I, control_pinsZ[pin], halfstep_seq[Halfstep][pin], Halfstep)
#stops
if I == (stopsZ[stop_countZ]):
print("stop at ", (stopsZ[stop_countZ]))
stop_countZ = stop_countZ + 1
time.sleep(5)
GPIO.cleanup()
end = time.time()
#print (end - start)
Code: Select all
raspistill -o a%04d.jpg -t 645000 -tl 3600Code: Select all
# version 1.1
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
# Stops X
stopsX = [546,1091,1637,2182,2728]
stop_countX = 0
# Stops Z
stopsZ = [0,114,228,341,455,568,683,796,910,1024,1252,1365,1479,1593,1707,1820,1934,2048,2162,2276,2389,2503,2617,2731,2844,2958,3072,3186,3300,3413,3527,3755,3864,3982,4096]
stop_countZ = 0
for pin in control_pinsX:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pinsZ:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep = 0
start = time.time()
# stepper Z starting Halfstep
Halfstep = 0
position = 0
# Stepper X
cycle = 1
def stepperz(Halfstep,stop_countZ):
# Stepper Z
start = stopsZ[stop_countZ]
if stop_countZ > 0:
start = start + 1
stop = stopsZ[stop_countZ + 1] + 1
print (start, stop)
for I in range(start, stop): # this now the number of steps for one move
print(I)
for pin in range(4):
GPIO.output(control_pinsZ[pin], halfstep_seq[Halfstep][pin])
print(control_pinsZ[pin], halfstep_seq[Halfstep][pin])
time.sleep(0.001)
Halfstep = Halfstep + 1
if Halfstep == 8:
Halfstep = 0
print(Halfstep)
#stop
print("stop at ", (stopsZ[stop_countZ + 1]))
return Halfstep
time.sleep(5)
for stop_countZ in range(10):
Halfstep = position
position = stepperz(Halfstep,stop_countZ)
#print (position)
print((stop_countZ + 1), "stop completed")
time.sleep (5)
GPIO.cleanup()
end = time.time()
#print (end - start)
Are you running the right code ?
Code: Select all
# version 1
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
# Stops X
stopsX = [546,1091,1637,2182,2728]
stop_countX = 0
# Stops Z
stopsZ = [0,114,228,341,455,568,683,796,910,1024,1252,1365,1479,1593,1707,1820,1934,2048,2162,2276,2389,2503,2617,2731,2844,2958,3072,3186,3300,3413,3527,3755,3864,3982,4096]
stop_countZ = 0
for pin in control_pinsX:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pinsZ:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep = 0
start = time.time()
# stepper Z starting Halfstep
Halfstep = 0
# Stepper X
cycle = 1
for L in range(18): #loop for 18 times clockwise 18 counter clockwise
# Clock wise rotation including stops
for i in range(2729): # this now the number of steps for a 240 degrees rotation
print(i)
for pin in range(4):
GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
halfstep = halfstep + 1
if halfstep == 8:
halfstep = 0
#stops
if i == (stopsX[stop_countX]):
print("stop at ", (stopsX[stop_countX]))
stop_countX = stop_countX + 1
time.sleep(3)
# End of Clockwise rotation
#stop and wait 5 seconds
print ("End of Clockwise rotation cycle ", cycle )
#time.sleep(5) # uncomment this line if you want to stop for 5 seconds at the end of the motor travel before stating again
cycle = cycle + 1
stop_countX = 4 # reset stops
# rotate back to start with stops
for i in range(2728, -1, -1): # this now the number of steps for the required rotation
print(i)
for pin in range(4):
GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
halfstep = halfstep - 1
if halfstep == -1:
halfstep = 7
#stops
if i == (stopsX[stop_countX]):
print("stop at ", (stopsX[stop_countX]))
stop_countX = stop_countX - 1
time.sleep(3)
#End of reverse
print ("End of Counter Clockwise rotation cycle ", cycle)
time.sleep(5) # uncomment this line if you want to stop for 5 seconds at the end of the motor travel before stating again
cycle = cycle + 1
stop_countX = 0 # reset stops
GPIO.cleanup()
end = time.time()
print (end - start)
Code: Select all
# master code
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
# Stops X
stopsX = [546,1091,1637,2182,2728]
stop_countX = 0
# Stops Z
stopsZ = [0,4096]
stop_countZ = 0
for pin in control_pinsZ:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep = 0
start = time.time()
# Stepper X
cycle = 1
for i in range(4097): # this now the number of steps for a 360 degrees rotation
print(i)
for pin in range(4):
GPIO.output(control_pinsZ[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
halfstep = halfstep + 1
if halfstep == 8:
halfstep = 0
#stops
if i == (stopsZ[stop_countZ]):
print("This is stop at ", (stopsZ[stop_countZ]))
stop_countZ = stop_countZ + 1
time.sleep(10)
GPIO.cleanup()
end = time.time()
print (end - start)
Yes it doesSo if you mark your stepper motor and then run this code does the mark end up back at the starting position.
Code: Select all
# master code
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
# Stops X
stopsX = [546,1091,1637,2182,2728]
stop_countX = 0
# Stops Z
#stopsZ = [0,114,228,341,455,568,683,796,910,1024,1252,1365,1479,1593,1707,1820,1934,2048,2162,2276,2389,2503,2617,2731,2844,2958,3072,3186,3300,3413,3527,3755,3864,3982,4096]
stopsZ = [0,114,228,342,455,569,683,797,910,1024,1138,1252,1365,1479,1593,1707,1820,1934,2048,2162,2275,2389,2503,2617,2730,2844,2958,3072,3185,3299,3413,3527,3640,3754,3868,3982,4096]
stop_countZ = 0
for pin in control_pinsZ:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep = 0
start = time.time()
# Stepper X
cycle = 1
for i in range(4097): # this now the number of steps for a 360 degrees rotation
print(i)
for pin in range(4):
GPIO.output(control_pinsZ[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
halfstep = halfstep + 1
if halfstep == 8:
halfstep = 0
#stops
if i == (stopsZ[stop_countZ]):
print("This is stop at ", (stopsZ[stop_countZ]))
stop_countZ = stop_countZ + 1
time.sleep(10)
GPIO.cleanup()
end = time.time()
print (end - start)
Code: Select all
# final 2 stepper version 1.2
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
# Stops X
stopsX = [0,546,1091,1637,2182,2728]
stop_countX = 0
# Stops Z
stopsZ = [0,114,228,342,455,569,683,797,910,1024,1138,1252,1365,1479,1593,1707,1820,1934,2048,2162,2275,2389,2503,2617,2730,2844,2958,3072,3185,3299,3413,3527,3640,3754,3868,3982,4096]
stop_countZ = 0
for pin in control_pinsX:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
for pin in control_pinsZ:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
halfstep = 0
start = time.time()
# stepper Z starting Halfstep
position = 0
# Stepper X
cycle = 1
def stepperz(Halfstep,stop_countZ):
# Stepper Z
start = stopsZ[stop_countZ]
if stop_countZ > 0:
start = start + 1
stop = stopsZ[stop_countZ + 1] + 1
print (start, stop)
for I in range(start, stop): # this now the number of steps for one move
print(I)
for pin in range(4):
GPIO.output(control_pinsZ[pin], halfstep_seq[Halfstep][pin])
time.sleep(0.001) #0.001007
Halfstep = Halfstep + 1
if Halfstep == 8:
Halfstep = 0
#stop
print("Z stop at ", (stopsZ[stop_countZ + 1]))
return Halfstep
time.sleep(1)
for L in range(18): #loop for 18 times clockwise 18 counter clockwise
# Clock wise rotation including stops
for i in range(2729): # this now the number of steps for a 240 degrees rotation
print(i)
for pin in range(4):
GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
halfstep = halfstep + 1
if halfstep == 8:
halfstep = 0
#stops
if i == (stopsX[stop_countX]):
print("X stop at ", (stopsX[stop_countX]))
stop_countX = stop_countX + 1
time.sleep(5)
# End of Clockwise rotation
#stop and wait 5 seconds
print ("End of Clockwise rotation cycle ", cycle )
# call stepper Z
stop_countZ = cycle - 1
Halfstep = position
position = stepperz(Halfstep,stop_countZ)
cycle = cycle + 1
stop_countX = 4 # reset stops
time.sleep(5) # uncomment this line if you want to stop for 5 seconds at the end of the motor travel before stating again
# rotate back to start with stops
for i in range(2728, -1, -1): # this now the number of steps for the required rotation
print(i)
for pin in range(4):
GPIO.output(control_pinsX[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
halfstep = halfstep - 1
if halfstep == -1:
halfstep = 7
#stops
if i == (stopsX[stop_countX]):
print("X stop at ", (stopsX[stop_countX]))
stop_countX = stop_countX - 1
time.sleep(5)
#End of reverse
print ("End of Counter Clockwise rotation cycle ", cycle)
# call stepper Z
stop_countZ = cycle - 1
Halfstep = position
position = stepperz(Halfstep,stop_countZ)
cycle = cycle + 1
stop_countX = 0 # reset stops
time.sleep(5) # uncomment this line if you want to stop for 5 seconds at the end of the motor travel before stating again
GPIO.cleanup()
end = time.time()
print (end - start)