User Tools

Site Tools


Writing /var/lib/dokuwiki/data/meta/teaching/ie0117/proyectos/2013/ii/raspberry_pi_dashcam.meta failed
teaching:ie0117:proyectos:2013:ii:raspberry_pi_dashcam

Raspberry Pi Dash-cam

Developers: Gabriel Alvarado Morales & Fabiola Muñoz Ugalde

Objective

The aim here is to provide continuous, high-quality video recording in a vehicle to capture significant events without human intervention.

These devices are sometimes called 'dash cams' and lately have become quite popular since many drivers now rely on dashboard cameras for insurance and legal purposes.

In case you're involved in an accident, you will have a video record of the event for your insurance company, or if you witness an accident or any other dangerous situation, your video may help law enforcement and other emergency responders.

Hardware Description

We have used the latest Raspberry Pi model B with an 8 GB SD card, along with the Raspberry Pi camera module to improve video-recording quality and simplify the programming due to compability terms.

For information and details about the Raspberry Pi and it´s camera module you can visit:

http://www.raspberrypi.org/

To provide internet access to our device we used an USB Wi-Fi dongle with the Raspberry Pi.

To keep the RPi running when the ignition of the vehicle is not on, we also added an external portable battery.

Getting Started

Installing OS Raspbian on the Raspberry Pi

The Raspberry Pi will not start without a properly formatted SD Card, containing the bootloader and a suitable operating system, you should install the OS and software to the Raspberry Pi before mounting it all together. We have chosen Raspbian, as it's one of the most advanced OS for the Raspberry with loads of help and tutorials on the internet.

You need to prepare the SD card to be able to run Raspbian on the Raspberry, in order to do this, we recommend following the instructions shown in the following link:

http://elinux.org/RPi_Easy_SD_Card_Setup

Enabling camera support in Raspbian

sudo apt-get upgrade 
sudo apt-get dist-upgrade
sudo raspi-config 

Navigate to 'camera' and select 'enable' then select 'Finish' and reboot.

Firmware Update

In order to keep your Raspberry Pi secure, and to get updated functionality, you should get the firmware and the software updated. The firmware is a small package of code that helps the software know how to talk to the hardware.

Install software needed to perform the update:

sudo apt-get install ca-certificates git-core binutils

Download the script:

sudo wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update

Copy the script to /usr/local/bin:

sudo cp rpi-update /usr/local/bin/rpi-update

Make the script executable:

sudo chmod +x /usr/local/bin/rpi-update

Run the script:

sudo rpi-update

Every time you want to check for firmware updates, then execute:

sudo rpi-update

Implementation

Auto Start & Auto Login

Edit the file /etc/inittab and comment out the line:

1:2345:respawn:/sbin/getty -noclear 38400 tty1

Below it add the line:

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

This does is an automatic login on tty1 (the first terminal), so a sudo reboot should now login the user pi automatically.

To start a script off when the RPi boots, follow the next steps.

Edit .bashrc and add this code to the end of the file:

if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then 
./dashcam.sh &
fi

After those modifications the Raspberry Pi will auto-boot when powered up and our dashcam application will then auto-run.

dashcam.sh

This is a shell script that is run automatically, when the Raspberry Pi boots up. It does a few initialisation tasks and runs our dashcam.py Python code in the background.

It has a control loop that records 5 minute video segments, each with a filename that is based on the start time and date (e.g. 2013_12_11_16:34:45.h264). After each recording the allocated storage space is measured and older files deleted to bring usage down below a pre-set limit (in our case it´s 6 GB).

The program loop runs until the contents of a file called dashcam.mode (which is created the first time the program runs in the directory /home/pi) are changed from 'exit' or 'shutdown' to 'record' or 'parked'.

#!/bin/sh
echo `date +%s` "! started dashcam.sh"
 
# Start the dashcam python code
sudo python dashcam.py &
 
cd /home/pi
# Set limit of rolling videos to 6GB
limit=6000000 
 
echo "record" > dashcam.mode  
# Creates a file named dashcam.mode and adds the content "record" on it 
 
mode=$(cat dashcam.mode) 
# The variable mode saves the content that is currently in the dashcam.mode file  
 
previous="dummy.h264"
current="dummy.h264"
# Variables destined to set the videos names 
 
# Main process
while [ true ]; do 
        # check Video folder disk space used  
	used=$(du Video | tail -1 | awk '{print $1}')
        echo `date +%s` "U Video" $used
	# Compare $used with $limit 
	# Free up disk space if needed
	while [ $limit -le $used ]; do
		remove=$(ls -1tr Video | grep .h264 | head -n 1)
		echo `date +%s` "-" $remove
		rm Video/$remove
		# Calculate disk space used
		used=$(du Video | tail -1 | awk '{print $1}')
	done
 
  # Check commands
  # if exit is requested, then stop and shutdown
  mode=$(cat dashcam.mode)
  if [ "$mode" = "exit" ]
	then
		echo `date +%s` "! stopped dashcam.sh"			
		# Shutdown the RPi, so we can safely remove power
		sudo shutdown -h now
		exit
  fi
 
  # if car ignition is on or if motion is detected then record video
  if [ "$mode" = "record" ] || [ "$mode" = "parked" ]
	then
		# New file name
		previous=$current
		current=$(date +"%Y_%m_%d_%H:%M:%S.h264")
		echo `date +%s` "+" $current
 
		# Capture a 5 minute segment of video
		raspivid -n -b 9000000 -w 1280 -h 720 -o Video/$current -t 300000
  fi
 
done

dashcam.py

This is a Python script that is also auto-started. It monitors the GPIO pins and writes control commands to the dashcam.mode file. This code uses the event_detected() function to detect changes to the switches and from the sensors.

import datetime
import RPi.GPIO as GPIO
import time
import os
import subprocess
import gps
 
def Log(t, s):
	print time.time(),t,s
 
Log("!", "started dashcam.py")
 
# Listen on port 2947 (gpsd) of local host
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
 
# Set mode
GPIO.setmode(GPIO.BOARD)
# Hide warnings
GPIO.setwarnings(False)
 
# Setup inputs
# Record = Button1 = GPIO15 = Board 10
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Parking = Button2 = GPIO17 = Board 11
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Ignition power = Button5 = GPIO22 = Board 15
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
# Intialise dashcam
mode = 'recording'
Log("M", "%s" % mode)
 
# Record button pressed
def mode_record(channel):
	global mode
	if mode != 'recording':
		mode='recording'
		Log("M", "%s" % mode)
GPIO.add_event_detect(10, GPIO.FALLING, callback=mode_record, bouncetime=500)
 
# Parked button pressed
def mode_parked(channel):
	global mode
	if mode != 'parked':
		mode = 'parked'
		Log("M", "%s" % mode)
GPIO.add_event_detect(11, GPIO.FALLING, callback=mode_parked, bouncetime=500)
 
# Ignition cut event
def ignition_cut(channel):
	global mode
	Log("=", "ignition cut")
	if mode != "parked":
		GPIO.cleanup()
		os.system("./exit.sh &")
		Log("!", "stopped dashcam.py")
		time.sleep(1)
		exit(0)
GPIO.add_event_detect(15, GPIO.FALLING, callback=ignition_cut, bouncetime=1000)
 
# Main program loop
while True:
	try:
		report = session.next()
 
	except KeyError:
		pass
	except KeyboardInterrupt:
		quit()
	except StopIteration:
		session = None
teaching/ie0117/proyectos/2013/ii/raspberry_pi_dashcam.txt · Last modified: 2022/09/20 00:08 (external edit)