Here’s the newest (functioning!) code for the sake of documenting the process. I ditched the Arduino because I finally figured out how to use the PWM functions with the Big Easy Driver.
So it technically works. When the camera sees a blob of a specific area, it turns the stepper motor and the blue LED turns on for some visual feedback. There’s got to be some safety features and a reverse command added, but for a proof of concept, this is great. The only thing that I need to fix really is the sleep command. When the camera is connected VIA USB, the blobs show up in the IDE and on the LCD. There’s a pause because of the sleep command, but you know what blob the camera is seeing. When the camera is powered via an outside 5v power supply and not connected via USB, the blobs don’t appear on the LCD at all. It’s as if being connected to the computer slows it down just enough for the blob to appear before the loop resets.
All of this to say that I don’t think the ‘time.sleep’ and ‘Timer.OC_FORCED_INACTIVE’ lines are necessary. It’s only adding a delay that makes it difficult to actually see what’s happening. The motor is only going to move at the frequency of Timer 4 (2400hz) and stop turning anyway, so do I even need a sleep command? I could be wrong.
I also designed an enclosure based on the clear one that’s available for the camera with the LCD shield attached. I’m going to refine it a bit more, but I’ll post it somewhere and link it here when It’s finished.
import sensor, image, time, pyb, math, lcd
from pyb import Pin, Timer
blue_led = pyb.LED(3)
pin5 = pyb.Pin("P5", pyb.Pin.OUT_PP)
tim = Timer(4, freq=2400)
thresholds = (0, 22, -23, 8, -128, 19)
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE). RGB seems to work better for this application
sensor.set_framesize(sensor.LCD) # Set frame size to QVGA (320x240)
sensor.set_windowing((80, 160)) # The actual display window size
sensor.skip_frames(time = 2000) # Wait for settings take effect.
lcd.init()
while(True):
img = sensor.snapshot()
target = img.draw_rectangle((0,60,80,40), color=255)
lcd.display(img)
lcd.display(target)
img.draw_rectangle((0,60,80,40), color=255)
blob_list = img.find_blobs([thresholds],roi = (0,60,80,40), area_threshold=400, merge=True)
blob_count = len(blob_list)
tim = Timer(4, freq=2400) # Frequency in Hz
pin5.value(False)
for blob in img.find_blobs([thresholds],roi = (0,60,80,40), area_threshold=400, merge=True):
img.draw_rectangle(blob.rect(), color=255, fill = True)
if (blob_count == 0):
print(blob_count)
tim.channel(3, Timer.OC_FORCED_INACTIVE)
else:
if (blob_count > 0):
blue_led.on()
tim.channel(3, Timer.PWM, pin=Pin("P9"), pulse_width_percent=50)
print(blob_count)
time.sleep(800)
pin5.value(True)
tim.channel(3, Timer.OC_FORCED_INACTIVE)
blue_led.off()