Pog49
Posts: 4
Joined: Fri Oct 09, 2015 7:47 am

car dash cam that will record my journey onto a USB stick

Wed Oct 14, 2015 10:18 am

Hello,

My project is to build a car dash cam that will record my journey onto a USB stick (16Gb). The Pi is powered via a 12V to 5V USB car charger (2A). I have two LEDs (red to indicate recording + blue to indicate the program is running) and two buttons (red to start/stop recording+ green to shutdown the pi).

I have a Python program that starts automatically via rc.local. I have made it executable and when I boot my Pi, the program runs and everything works as expected. I am able to start and stop recording video to the USB and shutdown the Pi via the buttons.

The problem comes when I have been recording for a while, the pi stops reacting to button presses and is effectively stuck.

My question is: Is there a better way of coding my project? Would interrupts be a better idea? Am I hitting a recursion limit? Any advice or ideas would be appreciated!

The Python Code:

Code: Select all


GPIO.cleanup()
import time
import picamera
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)       #Red Rec Button
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)       #Green Shutdown Button
GPIO.setup(24, GPIO.OUT)                                #Blue LED
GPIO.setup(23, GPIO.OUT)                                #Red LED
GPIO.output(24, 1)
GPIO.output(23, 0)

def choice():
        while 1:
                rec=GPIO.input(17)
                shut=GPIO.input(18)
                if rec==False:                          #If Pressed
                     camera()
                elif shut==False:                       #If Pressed
                     shutdown()
    
def camera():
    date=time.strftime("%d%m%Y%H%M%S")
    with picamera.PiCamera() as camera:
        #camera.start_preview()
        GPIO.output(24, 0)
        GPIO.output(23, 1)
        camera.resolution = (1024, 768)
        camera.framerate = 30
        camera.start_recording('/media/usb/%s.h264' % date )  #USB is set up correctly, mounted okay
        time.sleep(0.5)
        GPIO.wait_for_edge(17, GPIO.FALLING)
        camera.stop_recording()
        GPIO.output(23, 0)
        GPIO.output(24, 1)
        #camera.stop_preview()
        time.sleep(0.5)
    #Do I need to go back to choice() here instead?

                
def shutdown():
    GPIO.cleanup()
    subprocess.call(['shutdown -h now "System halted by GPIO action"'], shell=True)
    GPIO.cleanup()

choice()

The annoying thing is that it works until it freezes!

Pog

AndersM
Posts: 33
Joined: Sun Sep 06, 2015 1:18 pm

Re: A better way?

Fri Oct 16, 2015 8:59 am

Might have misunderstood your code but it looks like:
a) If you press Rec button more than half a second when pressing it to stop the recording it will restart the recording as it returns to the choice-loop with the Rec button down.
b) The program is stuck on the line GPIO.wait_for_edge(17, GPIO.FALLING) while recording. Therefore it will not respond to shutdown while recording. (Intentional?)

I suggest something like this:

Code: Select all

def choice() :
    recording=False
    while 1:
        rec=GPIO.input(17)
        shut=GPIO.input(18)
        if not rec:
            if recording:
                 #TODO function to stop recording and preview
                 GPIO.wait_for_edge(17, GPIO.RISING) #Waiting for release of Rec button
                 recordning=False
            else:
                 #TODO function to start recording and preview
                 GPIO.wait_for_edge(17, GPIO.RISING) #Waiting for release of Rec button
                 recordning=True
        if not shut:
            if recording:
                   #TODO function to stop recording and preview
                  recordning=False
                  shutdown()
            else:
                  shutdown()
        time.sleep(0.05) #Offers some bounce suppression and less CPU use
(Not tested!)

Pog49
Posts: 4
Joined: Fri Oct 09, 2015 7:47 am

Re: A better way?

Fri Oct 16, 2015 11:19 am

Thanks for the reply!

I like the idea of using a recording flag and acting on the release of the button. when the recording is set to true/false I assume i need to use that elsewhere in the code? i.e add a 'camera.stop_recording()'

The wait was intentional as I don't want the pi to shut down mid-record if the button gets pressed accidentally.

I will hopefully find time to test it out over the weekend and let you know how it goes.

Pog

AndersM
Posts: 33
Joined: Sun Sep 06, 2015 1:18 pm

Re: A better way?

Sat Oct 17, 2015 8:58 am

Pog49 wrote:I like the idea of using a recording flag and acting on the release of the button. when the recording is set to true/false I assume i need to use that elsewhere in the code? i.e add a 'camera.stop_recording()'
Second thoughts:
Since you would like to keep the

Code: Select all

with picamera.PiCamera() as camera: 
statement to handle closing and exceptions. I think my suggested choice loop should be included under the with statement and not in a separate function.
After answering I read in the camera API that there is a flag camera.recording that is True if the camera is recoding. You should probably use this instead of the "home made" i suggested.
Also saw that the API suggests calling camera.wait_recording() to catch any exceptions during recording.

Good luck!

gordon77
Posts: 4514
Joined: Sun Aug 05, 2012 3:12 pm

Re: A better way?

Sat Oct 17, 2015 10:41 am

Here's one l made earlier.....

viewtopic.php?f=41&t=111605&p=778276#p778276

Return to “Python”