Raspberry Pi based Pinewood Derby Instant Replay System

preview_player
Показать описание

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****
Рекомендации по теме
Комментарии
Автор

Andre - you are great! This system is awesome!! I tried making an RPi instant replay last year for our pack's Pinewood Derby but couldn't learn Python fast enough. In September I saw yours and I loaded your code (thank you!!!) and now I have a working instant replay system with an RPi camera module. I made my own laser/photo diode finish line trigger with an arduino and wired in a button so we can replay the last clip two more times for the really choice races.

KM-hnyz
Автор

Andre - Amazing work. I decided to adapt your work to a Champ timer. It was not too bad, but I did have to make some modifications.
1) I wired the speaker plug output (tip and ring (first two parts)) to the 3.3v and GPIO BCM pin 5.

2) I had to add the Pi's built in Pull Down resistor to the circuit. To modify the code for the setup line I changed it to:
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

3) I was getting inconsistent results so I needed to make sure the output time was increased to 100 ms in the terminal program. Directions on how to complete that are the champ timer by best track's manual.

4) I also decreased the time for the wait time "camera.wait_recording(0.2)" to "camera.wait_recording(0.02)"

5) Per K M " Install a package called gpac. In terminal, run the following commands one after the other:
sudo apt-get update
sudo apt-get install gpac "

6) To run the code just want to remind everyone you have to use sudo python filename.py or use Thorny. Else you will not have access to the GPIO pins.

Amazing work. Now my wife thinks I should have a four screen cast so the kids can see the whole race and then the instant replay just plays over it. I guess I better brush up on my python skills to make that happen.

Thanks again for all the hard work, just wanted to post my results to share with others in case they need the assistance.

jongooding
Автор

This is incredible! Thank you for sharing the resource! I thought I had this as an original thought. I guess it's true what they say, there's nothing new under the sun. I hope to get this kind of a set up going soon

AaronWieringa
Автор

Hello again Andre, just finally get my raspberry pi3 with the cammera same as you. Do you have a wiring diagram for this stuff that you can share? Your system is the only one I found in the web and is the only one that looks perfect for what I want to do with my PDT thanks for your time

antoniocaban
Автор

well what I have in mind now with your feedback is to connect the arduino output  3.3v pin (available) directly to the GPIO5 and use a photocell to trigger the ground. Not sure if I can trigger the 3.3v throught the cell and connect the ground directly to the GPIO30

antoniocaban
Автор

Andre, Thanks so much for sharing!!! I am prepping for our race and this will be such a crazy cool feature to have.

It is derby season, are you working on any upgrades for this year?

homegamerengineer
Автор

Hey there. Amazing work!! I had issues with getting the reply to work. And found the solution. I needed MP4Box installed. sudo apt-get install gpac, thought I would share :)

Lupierus
Автор

Would it be possible to get a schematic of the hardware you're using? There's a link posted elsewhere to it but the link is now dead. Also, could you explain what function the Arduino is serving?

AaronWieringa
Автор

hello Andre, just my last question related to this project.In the code, the following instruction line:  if the "directory" word is part of the instruction line or is just an "advice" that indicate that I need to write a specific "directory path" as configured in my raspi?thanks and have a good day!!

antoniocaban
Автор

Hi, this is awesome! My son's pack leader and I are wanting to do this exact thing, looks like you built a rockin' setup! How did it work out for you? I'd love to borrow from your design if you have time to make it available. Thanks!

justsayinit
Автор

oops, Andre, I think you sent to me the arduino PDT schematic, but what Im looking for is to see how did you hook up the raspberry PI to de arduino to get the trigger wun the cars reach finish line. I made the whole PDT from the miscjunk.org site and is fully funtional with 6 lanes. Now is when the instant relay go into the gameThanks

antoniocaban
Автор

Hello my friend, I am is Henry and I have one doubt. I would like your help if possible.
Is it possible to change the disp (7 seguiment) for to one pannel OLED SSD1306 I2C?
How is the possible make this change?

Thank you very much for your attention.

henrywood
Автор

Well Andre... I realized that my arduino code for a six lane track, it already have the arduino PIN11 already busy for the race status indicator lights. Looks like I will need to make the instant replay system completely apart from the arduino circuit. Looking in to your schematic, the raspberry trigger is hoked to a ground and the GPIO5 to the arduino PIN 11. In my case I will need to supply this signal from a photocell or any other sensing device. My question now is What is the voltage required for the GPIO5 pin? it is for 5V or it is for 3.3v? in my case what you suggest, a pull down resistor or a pull up resistor setup?

antoniocaban
Автор

hello Andre!!I'm still strugling loading this code to my instant reply raspi project. In an other forum, somebody tell me that my problem running the script could be associated to the code format. When I copy and paste the code listed in this URL, into my scrip editor, the line that contains the "dircreate is a mess and have all the code instructions togheter in one line, so probaby this is why I get a syntax error message during code execution. Can you provide to me the code you are using to sse if I can mine works?I was trying to separate each instructions line by line with the corresponding indentation but I'm lost in python programing and can't find someone near me that can work with this.Seeing What Kris comment, he made the code works for him and his feedback is that it is awsome!!

antoniocaban
Автор

hello Andre, can you re-write the code again to see what is wrong with it in the line of "while nor dircreated....". This is the line where I still having the syntax error. Trying to get some help in other forum but they told me that ask for help with codes designed for persons that are not part of the forum is an inadecuate action... :(

antoniocaban
Автор

Hi AndreOne more time :)Finally fix the syntax error and get the scrip code compiling well, but now when I run the script in the "Thonny" editor, the whole screen goes black as it supposed, but no video is seen and no way to go back to the desktop.I make sure that the omxplayer and gpac are both installed and get the pi3 updated and upgraded.What you think that can cause this  in this time?

antoniocaban
Автор

Hi Andre,   finally get the raspberry pi booted with the OS plus the cammera connected. I also finally understood the set up you sent to me. Now I'm just trying t run the python script in the program editor and there is a syntax error message. When I copied your code from this site and then paste it in a note pad, the line 42 appears like a long text stream with the program lines instruction in one program stream. What I'mdoing right now is to put the code exactly as your appear in this web site to see if it can be executed.Once this program is running in your track, it is necesary to start every race trough the key boar or mouse? oir it will automatically execute and then waiting for the next triger input from the arduino?. As a beginner, now I'm dealing witht the arduino analog output #11.

antoniocaban
Автор

Now I added a Bluetooth iPhone interface to the Arduino (and with more IO to the Pi) so I can replay the last clip while I'm at the starting line, and I added our district race graphic in the background using Pygame blit so when the camera preview sleeps, we see graphics instead of a blank screen. And then I added iPhone buttons to stop the camera preview so we see the race graphic during intermissions, and another button will bring it up again. (Kris)

KM-hnyz
Автор

what did you use for software to have the instant replay work?
I would love to do this at my derby but I am not sure exactly what to use. Also could I do it using a usb web cam instead of a pi cam?

donobrien
Автор

hello Andre, is possible to get a wiring schematic for this setup? or can you confirm if you are running the code in the Pi using NOOBS?thanks

antoniocaban