filmov
tv
Raspberry Pi based Pinewood Derby Instant Replay System

Показать описание
Raspberry Pi based instant replay system, triggered by Arduino Pinewood Derby Timer. The Pi uses GPIO pin 5 attached to a voltage splitter on Arduino output 11 (and ground-ground) to detect when a car crosses the finish line, which triggers the replay. The Pi is recording a constant stream of video, and when the replay is triggered, it records another half second of video, then takes the last second and a half and saves it in slow motion (recording is done at 90fps), before replaying. Some work is still needed to optimize focus on the camera and perhaps repeat the replay one or two times, but overall it is functional.
Note: Mp4Box, omxplayer and pygame are required for the code to work.
Also Note: If you are using a newer Pi, you can really tighten up the timing. The code below was for a Pi B+, so processing the video took a few seconds. Trying this on a Pi3 the processing happens almost instantaneously, so the sleep times can be reduced. Also note that the recording wait time in the loop should be reduced, since in the case that all three cars finish within .2 seconds of each other, it is possible that the loop will miss the trigger. With a Pi3 changing this to 0.01 seems to work well. And finally, 1.5 seconds of video is really more than is needed. Most races have the finishes easily within half a second if not tighter. You can experiment with the postseconds and totalseconds (I'm liking .3 and .4 respectively at the moment, but haven't yet tested on the track).
Edit: The Python code I am using is below. This new version blacks the screen behind the replay and also shows the replay twice prior to reverting to the live feed:
****CODE BEGIN****
import sys
import io
import picamera
import RPi.GPIO as GPIO
import time
import os
from subprocess import Popen
#Set up confirguration
isoVal = 0
expmode = 'sports'
postseconds = 1.0
totalseconds = 1.5
thisframerate = 90
playbackframerate = 15
theoplaytime = totalseconds * thisframerate / playbackframerate
firstsleep = theoplaytime + 1
secsleep = firstsleep + 5
#set blank screen behind everything
#set up GPIO using BCM Numbering
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
camera = picamera.PiCamera()
stream = picamera.PiCameraCircularIO(camera, seconds=7)
videocount = 1
#set up directory for files
directory = dirpref + "/"
dircreated = False
dirsuf = 0
while not dircreated:
#add suffix and check again
dirsuf = dirsuf + 1
directory = dirpref + "/" + str(dirsuf) + "/"
else:
#create directory
dircreated = True
running = True
try:
while running:
if GPIO.input(5):
filemp4 = directory + "race" + str(videocount) + ".mp4"
filename = directory + "race" + str(videocount) + ".h264"
convertstring = "MP4Box -fps " + str(playbackframerate) + " -add " + filename + " " + filemp4
#playerstring = "omxplayer " + filemp4
omxc = Popen(['omxplayer', filemp4])
omxc = Popen(['omxplayer', filemp4])
videocount = videocount + 1
finally:
GPIO.cleanup()
****CODE END****
Комментарии