I am trying to build a project where I move two stepper motors one step each than take and image in a loop. I would like the loop to go as quickly as possible (maybe 20 loops per second). Eventually, I will do image processing on these images. I've used sample code that I've found online for both the steppers and the PiCam, but I've been very surprised how slowly the pi is taking images. If I only run the steppers and comment out the camera code, the Python reports about 20 loops per second, though it seems to slow with time. With only the camera taking images in the loop, the rate goes down to between 2 -3 loops per second, whether or not I display the images. I fear there is something that I am doing wrong.
I am using Python 2.7.9 on a Raspberry Pi 3.
This is the stepper HAT that I am using.
https://www.amazon.com/gp/product/B0721 ... UTF8&psc=1
I am using a standard PiCam for the camera
Here is my code. You can see that for now I have commented out the stepper motors.
Any help is greatly appreciated.
Code: Select all
#!/usr/bin/python #import Raspi_MotorHAT, Raspi_DCMotor, Raspi_Stepper from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor, Raspi_StepperMotor import time import atexit from picamera.array import PiRGBArray from picamera import PiCamera import cv2 import datetime import numpy as np #initialize the camera and grab a reference to the raw camera caputure camera = PiCamera() camera.resolution = (640, 480) rawCapture = PiRGBArray(camera, size=(640, 480)) #allow the camera to warm up time.sleep(1) # create a default object, no changes to I2C address or frequency mh = Raspi_MotorHAT(0x6F) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Raspi_MotorHAT.RELEASE) mh.getMotor(2).run(Raspi_MotorHAT.RELEASE) mh.getMotor(3).run(Raspi_MotorHAT.RELEASE) mh.getMotor(4).run(Raspi_MotorHAT.RELEASE) atexit.register(turnOffMotors) myStepper1 = mh.getStepper(200, 1) # 200 steps/rev, motor port #1 myStepper2 = mh.getStepper(200, 2) # 200 steps/rev, motor port #1 t_end = time.time() + 10 print('Starting Trial') start_time = datetime.datetime.now() while time.time() < t_end: #Perform minor timing calculations to get timestamps doneFrameTime=datetime.datetime.now() diffTime = doneFrameTime-start_time frameRate = 1/diffTime.total_seconds() start_time=datetime.datetime.now() print frameRate #myStepper1.oneStep(Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.MICROSTEP) #myStepper2.oneStep(Raspi_MotorHAT.FORWARD, Raspi_MotorHAT.MICROSTEP) #grab an image from the camera camera.capture(rawCapture, format="rgb") image = rawCapture.array #display the image on the screen and wait for a keypress #cv2.imshow("Image", image) #cv2.waitKey(1) rawCapture.truncate(0) #time.sleep(.01) turnOffMotors()