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

Re: Raspberry Pi Car Camera

Wed Jun 10, 2015 7:47 pm

12458 wrote:What do u mean by the gpios can u post a pic
I cannot use pin 5 as it is for the rtc
On mine l've ditched the led and moved the shutdown switch to gpio pin 26, as you say rtc tend to use gpio pin 5, this also gives the benefit you can hold down the shutdown switch as long as you want and it won't reboot.

I'll upload the code tomorrow, when back at my pi, including flipping the video

camera.hflip = True
camera.vflip = True


Gordon

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

Re: Raspberry Pi Car Camera

Thu Jun 11, 2015 9:14 am

Latest version. Now use pin13, to gnd (pin14), for shutdown and pin26, to gnd (pin25), to stop the program, this means it will be compatible with all Pis.

Also horizontally and vertically flips video.

If using gps it will take the date and time from that, and show lat , long ,speed in Km/h and direction.

Gordon
Last edited by gordon77 on Sat Jun 13, 2015 5:08 pm, edited 1 time in total.

12458
Posts: 20
Joined: Wed May 27, 2015 4:13 am
Location: Singapore

Re: Raspberry Pi Car Camera

Sat Jun 13, 2015 7:15 am

scotty101 wrote:
12458 wrote:My camera is upside down. How do I flip the video?
Turn the camera up the correct way. :ugeek:
I've changed the rotation by using camera.rotation = 180

12458
Posts: 20
Joined: Wed May 27, 2015 4:13 am
Location: Singapore

Re: Raspberry Pi Car Camera

Sat Jun 13, 2015 7:19 am

gordon77 wrote:Latest version. Now use pin13, to gnd (pin14), for shutdown and pin26, to gnd (pin25), to stop the program, this means it will be compatible with all Pis.

Also horizontally and vertically flips video.

If using gps it will take the date and time from that, and show lat , long ,speed in Km/h and direction.

Gordon

Code: Select all

#!/usr/bin/env python
import picamera
import datetime as dt
import os
import RPi.GPIO as GPIO
import serial
import glob

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
# set input for shutdown button
GPIO.setup(13,GPIO.IN,pull_up_down = GPIO.PUD_UP)
# set input for software stop button
GPIO.setup(26,GPIO.IN,pull_up_down = GPIO.PUD_UP)

#set file record tme in seconds
record_time = 300

startfile = 100
maxfiles = 20

filestamp = startfile

#write filestamp file if first time
if os.path.exists('last.txt') == False:
    file = open("last.txt", "w")
    file.write(str(filestamp))
    file.close()
#read last filestamp file stored
file = open("last.txt", "r")
filestamp = int(file.read()) + 1
if filestamp > startfile + maxfiles:
    filestamp = startfile
file.close()

# check for gps on USB0
if os.path.exists('/dev/ttyUSB0') == True:
   ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 20
    camera.start_preview()
    camera.vflip = True
    camera.hflip = True
    camera.annotate_background = picamera.Color('black')
    camera.annotate_text = ""
    camera.annotate_text1 = ""
    camera.annotate_text2 = ""

    fix = 0
    
    while GPIO.input(26) == 1:
        start = dt.datetime.now()
        if filestamp > startfile + maxfiles:
           filestamp = startfile
        camera.start_recording(str(filestamp) + '.h264')
        # write filestamp file
        file = open("last.txt", "w")
        file.write(str(filestamp))
        file.close()
        while (dt.datetime.now() - start).seconds < record_time and GPIO.input(26) == 1:
           # check for gps connected
            if os.path.exists('/dev/ttyUSB0') == True:
                gps = ser.readline()
                # get location from gps
                if gps[1 : 6] == "GPGGA":
                    gps1 = gps.split(',',12)
                    fix = gps1[6]
                    print fix
                if gps[1 : 6] == "GPRMC" and fix != '0':
                    gps2 = gps.split(',',12)
                    camera.annotate_text1 = " " + gps2[9] + " " + gps2[1][:6] + " " + gps2[3] + " " + gps2[4] + " " + gps2[5] + " " + gps2[6] + " "
                if gps[1 : 6] == "GPVTG" and fix != '0':
                    gps3 = gps.split(',',9)
                    camera.annotate_text2 = gps3[7] + " " + gps3[1]
                if fix != '0':
                    camera.annotate_text = camera.annotate_text1 + camera.annotate_text2
                else:
                    camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            else:
                camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            camera.wait_recording(0.2)
            # shut down if pin 13 to gnd
            if GPIO.input(13) == 0:
                path = 'sudo shutdown -h now '
                os.system (path)
        camera.stop_recording()
        filestamp +=1
        # stop program if pin 26 to gnd
        if GPIO.input(26)== 0:
           break
It is no need. I solved this by soldering pins above the rtc.
Plus I still need the LED as I need it to tell me if it is at a Halted state

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

Re: Raspberry Pi Car Camera

Sat Jun 13, 2015 5:08 pm

Probably my final version for now. I may sort a UPS solution one day, save pressing button to shutdown safely.

LED re-established, ON when running program, pulses when using GPS.
GPS more reliable, kept stopping with certain strings.

Pictures showing mine on Tomtom windscreen mount

Gordon

Code: Select all

#!/usr/bin/env python
import picamera
import datetime as dt
import os
import RPi.GPIO as GPIO
import serial
import glob

#shutdown button gpio pin
SD = 13
#software stop button gpio pin
SS = 26
#running LED gpio pin
LD = 22

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
# set input for shutdown button
GPIO.setup(SD,GPIO.IN,pull_up_down = GPIO.PUD_UP)
# set input for software stop button
GPIO.setup(SS,GPIO.IN,pull_up_down = GPIO.PUD_UP)
# running LED
GPIO.setup(LD,GPIO.OUT)
GPIO.output(LD,GPIO.HIGH)

#set file record tme in seconds
record_time = 300

startfile = 100
maxfiles = 20
filestamp = startfile

#write filestamp file if first time
if os.path.exists('last.txt') == False:
    file = open("last.txt", "w")
    file.write(str(filestamp))
    file.close()
#read last filestamp file stored
file = open("last.txt", "r")
filestamp = int(file.read()) + 1
if filestamp > startfile + maxfiles:
    filestamp = startfile
file.close()

# check for gps on USB0
if os.path.exists('/dev/ttyUSB0') == True:
   ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 20
    camera.start_preview()
    #enable next 2 lines if picture inverted
    #camera.vflip = True
    #camera.hflip = True
    camera.annotate_background = picamera.Color('black')
    camera.annotate_text = ""
    camera.annotate_text1 = ""
    camera.annotate_text2 = ""

    fix = '0'
    
    while GPIO.input(SS) == 1:
        start = dt.datetime.now()
        if filestamp > startfile + maxfiles:
           filestamp = startfile
        camera.start_recording(str(filestamp) + '.h264')
        # write filestamp file
        file = open("last.txt", "w")
        file.write(str(filestamp))
        file.close()
        GPIO.output(LD,GPIO.HIGH)
        while (dt.datetime.now() - start).seconds < record_time and GPIO.input(SS) == 1:
           # check for gps connected
            if os.path.exists('/dev/ttyUSB0') == True:
                gps = ser.readline()
                
                # get location from gps
                if gps[1 : 6] == "GPGGA":
                    fix = '0'
                    gps1 = gps.split(',',12)
                    if len(gps) > 68 and (gps1[3] == "N" or gps1[3] == "S"):
                       fix = gps1[6]
                    #print fix
                if gps[1 : 6] == "GPRMC" and fix != '0':
                    gps2 = gps.split(',',12)
                    if len(gps) > 60 and (gps2[4] == "N" or gps2[4] == "S"):
                        camera.annotate_text1 = " " + gps2[9] + " " + gps2[1][:6] + " " + gps2[3] + " " + gps2[4] + " " + gps2[5] + " " + gps2[6] + " "
                if gps[1 : 6] == "GPVTG" and fix != '0':
                    gps3 = gps.split(',',8)
                    if len(gps) > 26 and gps3[2] == "T":
                        camera.annotate_text2 = gps3[7] + " " + gps3[1]
                if fix != '0':
                    GPIO.output(LD,GPIO.LOW)
                    camera.annotate_text = camera.annotate_text1 + camera.annotate_text2
                else:
                    camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            else:
                camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            camera.wait_recording(0.04)
            GPIO.output(LD,GPIO.HIGH)
            camera.wait_recording(0.16)
            # shut down if pin SD to gnd
            if GPIO.input(SD) == 0:
                GPIO.output(LD,GPIO.LOW)
                path = 'sudo shutdown -h now '
                os.system (path)
        camera.stop_recording()
        GPIO.output(LD,GPIO.LOW)
        filestamp +=1
        # stop program if pin SS to gnd
        if GPIO.input(SS)== 0:
            GPIO.output(LD,GPIO.LOW)
            break
Attachments
camera_rear.jpg
camera rear, OFF button, LED
camera_rear.jpg (35.91 KiB) Viewed 4496 times
Camera.jpg
Camera
Camera.jpg (45.84 KiB) Viewed 4496 times

FizzerUK
Posts: 5
Joined: Sat Jun 13, 2015 3:43 pm

Re: Raspberry Pi Car Camera

Sun Jun 14, 2015 7:12 pm

gordon77 wrote:
12458 wrote:What do u mean by the gpios can u post a pic
I cannot use pin 5 as it is for the rtc
On mine l've ditched the led and moved the shutdown switch to gpio pin 26, as you say rtc tend to use gpio pin 5, this also gives the benefit you can hold down the shutdown switch as long as you want and it won't reboot.

I'll upload the code tomorrow, when back at my pi, including flipping the video

camera.hflip = True
camera.vflip = True


Gordon
Gordon.. I buy you a virtual beer (if you are old enough, definitely wise enough)

This is great... JUST what I want.

Thanks.. Just got 4M picamera extension cable, appears to work.
Love the shutdown stuff, I have some touch pads I will be intergrating into the 'Zombie Surf Van' console to start shutdown etc.

Hope to get this recording in the background while having a media interface in the foreground.

Well anyway look forward to any updates and I will post my project pics soon... Hopefully.

Thanks again.

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

Re: Raspberry Pi Car Camera

Wed Jun 17, 2015 10:24 am

Cheers ! (I am old enough).

I'm doing an update as I found during much testing that the gps data lags, can be several minutes after a long run, presumably due to the serial buffer needing flushing.
Also I'm adding a shutdown if the supply volts go low, hopefully useful if running off a battery.

Gordon

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

Re: Raspberry Pi Car Camera

Mon Jun 22, 2015 12:56 pm

Hopefully this fixes the gps data lag by flushing the serial buffer.

It shuts down if it detects a low voltage alarm (GPIO35) for a few seconds.

It can now set the Pi time from the gps time, 'settime = 1', set your offset from UTC (whole hours only ! eg 'offset = 2'), useful when the gps reception drops in and out.

Also displays if 3D fix and number of sateliites recieved, can be switched off with 'add_gps_data = 0'

Code: Select all

#!/usr/bin/env python
import picamera
import datetime as dt
import os
import RPi.GPIO as GPIO
import serial

#shutdown button gpio 
SD = 27 # pin 13 (Set SD = 21 for PiB ver1)
#software stop button gpio 
SS = 7  # pin 26
#running LED gpio 
LD = 25 # pin 22
#low supply volts gpio (don't change)
LV = 35

GPIO.setwarnings(False)
GPIO.setmode (GPIO.BCM)
# set input for shutdown button
GPIO.setup(SD,GPIO.IN,pull_up_down = GPIO.PUD_UP)
# set input for software stop button
GPIO.setup(SS,GPIO.IN,pull_up_down = GPIO.PUD_UP)
# running LED
GPIO.setup(LD,GPIO.OUT)
GPIO.output(LD,GPIO.HIGH)
#Low supply volts
GPIO.setup(LV,GPIO.IN,pull_up_down = GPIO.PUD_UP)

#set file record tme in seconds
record_time = 300

startfile = 100
maxfiles = 20
filestamp = startfile

#set settime = 1 to allow Pi time to be updated by gps
settime = 1
#set your time offset, UTC = 0, used by settime
offset = 1

# set add_gps_data = 1 to include 3D fix and number of satellites
add_gps_data = 1

#write filestamp file if first time
if os.path.exists('last.txt') == False:
    file = open("last.txt", "w")
    file.write(str(filestamp))
    file.close()
#read last filestamp file stored
file = open("last.txt", "r")
filestamp = int(file.read()) + 1
if filestamp > startfile + maxfiles:
    filestamp = startfile
file.close()

gps_con = 0

# check for gps on USB0
if os.path.exists('/dev/ttyUSB0') == True:
   ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)
   gps_con = 1

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 20
    camera.start_preview()
    #enable next 2 lines if picture inverted
    camera.vflip = True
    camera.hflip = True
    camera.annotate_background = picamera.Color('black')
    camera.annotate_text = ""
    text0 = ""
    text1 = ""
    text2 = ""
    text4 = ""
    fix = '0'
    LVS = 0
    
    while GPIO.input(SS) == 1:
        start = dt.datetime.now()
        if filestamp > startfile + maxfiles:
           filestamp = startfile
        camera.start_recording(str(filestamp) + '.h264')
        # write filestamp file
        file = open("last.txt", "w")
        file.write(str(filestamp))
        file.close()
        GPIO.output(LD,GPIO.HIGH)
        while (dt.datetime.now() - start).seconds < record_time and GPIO.input(SS) == 1 :
           # check for gps connected
            if os.path.exists('/dev/ttyUSB0') == True:
                if gps_con != 1:
                    ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)
                    gps_con = 1
                gps = ser.readline()
                           
                
                # get data from gps
                                       
                if gps[1 : 6] == "GPGGA":
                    fix = '0'
                    gps1 = gps.split(',',12)
                    if len(gps) > 68 and (gps1[3] == "N" or gps1[3] == "S"):
                       fix = gps1[6]
                       text0 = " " + gps1[7] + " " + gps1[9] + "M"
                       
                    
                if gps[1 : 6] == "GPGSA":
                    gps4 = gps.split(',',12)
                    if len(gps) > 50:
                       text4 = " " + gps4[2] + "D"
                       
                       
                if gps[1 : 6] == "GPRMC" and fix != '0':
                    gps2 = gps.split(',',12)
                    if len(gps) > 60 and (gps2[4] == "N" or gps2[4] == "S"):
                        hour = int(gps2[1][0:2])
                        hour = hour + offset
                        if hour > 23:
                            hour = hour - 24
                        if hour < 0:
                            hour = hour + 24
                        timex = str(hour) + ":" + gps2[1][2:4] + ":" + gps2[1][4:6]
                        datex = "20" + gps2[9][4:6] + "-" + gps2[9][2:4] + "-" + gps2[9][0:2]
                        text1 = datex + " " + timex + " " + gps2[3] + " " + gps2[4] + " " + gps2[5] + " " + gps2[6]
                        if settime == 1:
                            path = "sudo date +%T -s '" + timex + "'"
                            os.system (path)
                            settime = 0
                        ser.flushInput()
                        
                if gps[1 : 6] == "GPVTG" and fix != '0':
                    gps3 = gps.split(',',8)
                    if len(gps) > 26 and gps3[2] == "T":
                        text2 = " " + gps3[7] + " " + gps3[1]
                        
                if fix != '0':
                    GPIO.output(LD,GPIO.LOW)
                    if add_gps_data == 1:
                        camera.annotate_text = text1 + text4 + text0 + text2 
                    else:
                        camera.annotate_text = text1 + " " + gps1[9] + "M" + text2 
                else:
                    camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            else:
                camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                
            camera.wait_recording(0.04)
            GPIO.output(LD,GPIO.HIGH)
                       
            # shut down if low volts (LV) or pin SD to gnd
            if GPIO.input(LV) == 0:
                LVS +=1
            else:
                LVS = 0
            if GPIO.input(SD) == 0 or LVS > 5:
                GPIO.output(LD,GPIO.LOW)
                path = 'sudo shutdown -h now '
                os.system (path)
        camera.stop_recording()
        GPIO.output(LD,GPIO.LOW)
        filestamp +=1
        # stop program if pin SS to gnd
        if GPIO.input(SS)== 0:
            GPIO.output(LD,GPIO.LOW)
            break

ankita061
Posts: 3
Joined: Mon Feb 08, 2016 7:21 am

Re: Raspberry Pi Car Camera

Fri Mar 18, 2016 11:44 am

can we connect gps module directly to the raspberry pi 2.

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

Re: Raspberry Pi Car Camera

Fri Mar 18, 2016 1:13 pm

Yes, normally via USB or serial

Return to “Other projects”