Would you mind highlighting with a comment this time.sleep to be replaces with the camera line?So no don't worry about the camera bit yet, you will be adding it in place of the time.sleep that you currently do on stepper X when you reach a stop so it wont affect the stepping.
Code: 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 = [0,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(6): #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("cwstop 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 )
#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 = 5 # 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("ccwstop at ", (stopsX[stop_countX]))
stop_countX = stop_countX - 1
time.sleep(5)
#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
# final 2 stepper version 3.2
import RPi.GPIO as GPIO
import time
# setup gpio
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
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)
# steps lists
# 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
# gpio ouput values list
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]
]
# stepper x starting halfstep value do not change
halfstep = 0
# stepper Z starting Halfstep value do not change
position = 0
# Stepper X cycles starting value
cycle = 1
# stop time after moving stepper Z you can reduce this if you want but not below 0.5
zstop = 5
# cmaera picture count start value
campic = 1
#step timer value adjust this value higher to slow stepping of motors typical values 0.001 0.001007
steptime = 0.001007
# delay for picture taking stop can be reduced once you have your picture taking code added but not below 0.5
picdelay = 5
def take_picture(campic):
print("place holder Take picture", campic)
# ----put you camera code in these lines to take pictures----
# campic is variable name for picture number counter
#
# --------------add more lines if need be -------------------
time.sleep(0.5)
def stepperz(Halfstep,stop_countZ):
# Stepper Z
start = stopsZ[stop_countZ]
if stop_countZ > 0:
start = start + 1
stop = stopsZ[stop_countZ + 1] + 1
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(steptime)
Halfstep = Halfstep + 1
if Halfstep == 8:
Halfstep = 0
#stop
print("Z moved to", (stopsZ[stop_countZ + 1]))
time.sleep(1)
return Halfstep
# main program loop
print ("Program Start")
start = time.time()
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(steptime)
halfstep = halfstep + 1
if halfstep == 8:
halfstep = 0
#stops
if i == (stopsX[stop_countX]):
print("X Moved to", (stopsX[stop_countX]))
time.sleep(picdelay)
#call to take picure
take_picture(campic)
campic = campic + 1
stop_countX = stop_countX + 1
# End of Clockwise rotation
print ("End of camera clockwise rotation cycle ", cycle )
# call stepper Z
stop_countZ = cycle - 1
Halfstep = position
position = stepperz(Halfstep,stop_countZ)
cycle = cycle + 1
stop_countX = 5 # reset stops
time.sleep(zstop)
# 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(steptime)
halfstep = halfstep - 1
if halfstep == -1:
halfstep = 7
#stops
if i == (stopsX[stop_countX]):
print("X moved to", (stopsX[stop_countX]))
time.sleep(picdelay)
#call to take picure
take_picture(campic)
campic = campic + 1
stop_countX = stop_countX - 1
#End of counter clockwise rotation
print ("End of counter clockwise camera 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(zstop)
GPIO.cleanup()
end = time.time()
runT = (end - start)
a=str(runT//3600)
b=str((runT%3600)//60)
c=str(round(((runT%3600)%60),3))
print ("End of program run it took {} hours {} mins {} seconds".format(a, b, c))
Code: Select all
camera.capture_sequence(['image%02d.jpg' % i for i in range(10)])Code: Select all
import time
import picamera
def outputs():
for i in range(10): # let's start with 10 pics to try
with picamera.PiCamera() as camera:
camera.resolution = (1920, 1080)
time.sleep(2)
start = time.time()
camera.capture_sequence(outputs(), 'rgb')
finish = time.time()
print('Captured 10 images at %.2ffps' % (10 / (finish - start)))
for filename in camera.capture_sequence('img{counter:03d}.jpg'):
print('Captured %s' % filename)
sleep(3) # wait 3 secondsCode: Select all
import time
import picamera
campic = 1
def take_picture(campic):
#----- copy this in to the function in the main program over writing what's there -------
print("Take picture", campic)
# the following line contains the path and file name that will be use to save file
filename = "/home/pi/insect" + str(campic) + ".jpg"
with picamera.PiCamera() as camera:
camera.resolution = (1920, 1080)
camera.start_preview()
time.sleep(2)
camera.capture(filename)
time.sleep(0.5)
camera.stop_preview()
#--------end of copy --------------------------------------------------------------------
for i in range(3): # let's start with 3 pics to try
take_picture(campic)
time.sleep(5)
campic = campic + 1
Code: Select all
filename = "/home/pi/insect" + str(campic) + ".jpg"
2 - Would this prevent overwriting pictures in case I forget to move the old ones to a different folder? Because this sounds so much like something I'll be doing at some point eheh So yep, might be useful.but we could add some more code so it asks you for a path and file name each time the program is run.
Code: Select all
import time
import picamera
campic = 1
def take_picture(campic):
#----- copy this in to the function in the main program over writing whats there -------
print("Take picture", campic)
# the following line contains the path and file name that will be use to save file
filename = "/home/pi/insect" + str(campic) + ".raw"
with picamera.PiCamera() as camera:
camera.resolution = (1920, 1088)
camera.start_preview()
time.sleep(2)
camera.capture(filename, 'rgb')
time.sleep(0.5)
camera.stop_preview()
#--------end of copy --------------------------------------------------------------------
for i in range(3): # let's start with 3 pics to try
take_picture(campic)
time.sleep(5)
campic = campic + 1
Code: Select all
# final 2 stepper version 4
import RPi.GPIO as GPIO
import time
import picamera
# setup gpio
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
control_pinsX = [7,11,13,15]
control_pinsZ = [19,21,23,29]
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)
# steps lists
# 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
# gpio ouput values list
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]
]
# stepper x starting halfstep value do not change
halfstep = 0
# stepper Z starting Halfstep value do not change
position = 0
# Stepper X cycles starting value
cycle = 1
# stop time after moving stepper Z you can reduce this if you want but not below 0.5
zstop = 5
# cmaera picture count start value
campic = 1
#step timer value adjust this value higher to slow stepping of motors typical values 0.001 0.001007
steptime = 0.001007
# delay for picture taking stop can be reduced once you have your picture taking code added but not below 0.5
picdelay = 5
def take_picture(campic):
print("Take picture", campic)
# the following line contains the path and file name that will be use to save file
filename = "/home/pi/" + name + str(campic) + ".jpg"
with picamera.PiCamera() as camera:
camera.resolution = (1920, 1080)
camera.start_preview()
time.sleep(2)
camera.capture(filename)
time.sleep(0.5)
camera.stop_preview()
def stepperz(Halfstep,stop_countZ):
# Stepper Z
start = stopsZ[stop_countZ]
if stop_countZ > 0:
start = start + 1
stop = stopsZ[stop_countZ + 1] + 1
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(steptime)
Halfstep = Halfstep + 1
if Halfstep == 8:
Halfstep = 0
#stop
print("Z moved to", (stopsZ[stop_countZ + 1]))
time.sleep(1)
return Halfstep
# file name entry
print ("Program Start")
print (" ")
print ("please input a file name for this program run")
name = input ("> ")
print(" ")
if name =="":
name = (time.strftime("%d%H%M_"))
# main program loop
start = time.time()
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(steptime)
halfstep = halfstep + 1
if halfstep == 8:
halfstep = 0
#stops
if i == (stopsX[stop_countX]):
print("X Moved to", (stopsX[stop_countX]))
time.sleep(picdelay)
#call to take picure
take_picture(campic)
campic = campic + 1
stop_countX = stop_countX + 1
# End of Clockwise rotation
print ("End of camera clockwise rotation cycle ", cycle )
# call stepper Z
stop_countZ = cycle - 1
Halfstep = position
position = stepperz(Halfstep,stop_countZ)
cycle = cycle + 1
stop_countX = 5 # reset stops
time.sleep(zstop)
# 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(steptime)
halfstep = halfstep - 1
if halfstep == -1:
halfstep = 7
#stops
if i == (stopsX[stop_countX]):
print("X moved to", (stopsX[stop_countX]))
time.sleep(picdelay)
#call to take picure
take_picture(campic)
campic = campic + 1
stop_countX = stop_countX - 1
#End of counter clockwise rotation
print ("End of counter clockwise camera 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(zstop)
GPIO.cleanup()
end = time.time()
runT = (end - start)
a=str(runT//3600)
b=str((runT%3600)//60)
c=str(round(((runT%3600)%60),3))
print ("End of program run it took {} hours {} mins {} seconds".format(a, b, c))