[英]Raspberry Pi + Camera + Stepper Motors + Open CV
I have written a program that basically brings a tray under a camera connected to a Raspberry Pi sequentially using a stepper motor+ a4988 driver.我编写了一个程序,该程序基本上使用步进电机 + a4988 驱动程序依次将一个托盘放在连接到 Raspberry Pi 的相机下。 The code brings a tray to a start position, takes a step, takes a photo and repeats this 10 times.
该代码将托盘带到开始位置,迈出一步,拍照并重复 10 次。 The tray is then returned to the start position.
然后托盘返回到起始位置。 What I should get is 10 photos of each section of the tray with whatever is on the tray.
我应该得到的是托盘每个部分的 10 张照片以及托盘上的任何东西。
However, what I get out is 7 photos that are exactly the same photo and then 3 that are different and I cant work out why.但是,我得到的是 7 张完全相同的照片,然后 3 张不同,我不知道为什么。
I think that the camera is taking the photos at a faster rate than the tray is moving but from the code I cant see why that would be.我认为相机以比托盘移动的速度更快的速度拍摄照片,但从代码中我看不出为什么会这样。
Im using openCV to get the photo because I plan to analyse each photo as it comes in.我使用 openCV 来获取照片,因为我计划在每张照片进来时对其进行分析。
Thanks!!谢谢!!
Heres my code:这是我的代码:
from time import sleep
import RPi.GPIO as GPIO
import cv2
cam = cv2.VideoCapture(0)
DIR = 20 # Direction GPIO Pin
STEP = 21 # Step GPIO Pin
CW = 1 # Clockwise Rotation
CCW = 0 # Counterclockwise Rotation
SPR = 200 # Steps per Revolution (360 / 1.8)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.output(DIR, CW)
MODE = (14, 15, 18) # Microstep Resolution GPIO Pins
GPIO.setup(MODE, GPIO.OUT)
RESOLUTION = {'Full': (0, 0, 0),
'Half': (1, 0, 0),
'1/4': (0, 1, 0),
'1/8': (1, 1, 0),
'1/16': (1, 1, 1),}
GPIO.output(MODE, RESOLUTION['1/16'])
delay2 = 0.0208/32
GPIO.output(DIR, CCW)
for x in range (1500): # Brings Tray to level of first photo
GPIO.output(STEP, GPIO.HIGH)
sleep(delay2)
GPIO.output(STEP, GPIO.LOW)
sleep(delay2)
sleep(.5)
for a in range (0,9): # Begins 10 photos
for b in range (250): #Motor steps between photos
GPIO.output(STEP, GPIO.HIGH)
sleep(delay2)
GPIO.output(STEP, GPIO.LOW)
sleep(delay2)
sleep(.5)
ret, frame = cam.read() #Sets up cam for photo
cv2.imwrite("image"+str(a)+".jpg", frame) #Write photo to file
sleep(2)
GPIO.output(DIR, CW)
for x in range(3750): # Pushes Tray out to original starting position
GPIO.output(STEP, GPIO.HIGH)
sleep(delay2)
GPIO.output(STEP, GPIO.LOW)
sleep(delay2)
sleep(.5)
cam.release()
GPIO.cleanup()
Solved it thanks to Mark and Matt.感谢 Mark 和 Matt 解决了这个问题。 Thanks for all your help.
感谢你的帮助。 Was solved by calling the video capture at each motor step and then releasing it each time.
通过在每个电机步骤调用视频捕获然后每次释放它来解决。
Heres the code:代码如下:
for a in range(1,10):
cam=cv2.VideoCapture(0)
ret, frame = cam.read()
cv2.imwrite('iamge'+str(a)+"jpg", frame)
cam.release
... etc etc. ……等等等等。
So for each step the camera is called and released which seems to solve it所以对于每一步,相机都会被调用和释放,这似乎解决了它
Thanks all谢谢大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.