Wednesday, 11 September 2013

Configuration

I had a spare Raspberry Pi and was wondering if I could turn it into a dashcam.
Originally I was using an old USB camera and although it kind of worked it wasn't great.
Some time later the Pi camera was announced and so I waited....
When it arrived I was very impressed and as the CPU usage was so very low
I wondered if I could combine it with my old USB version and maybe add a GPS.

Hardware - Raspberry Pi model B, 256Mb
OS - Arch Linux (or Raspbian)
Case - Stealth Black Raspberry Pi Case from modmypi
Front camera - Pi camera
Rear USB camera - Microsoft LifeCam HD-3000
GPS - Adafruit Ultimate GPS Breakout
LEDs - 3 x 3mm (red, green & yellow or blue)
Pi camera mount - From pimoroni
SD card adaptor - pIO microSD card adapter from modmypi
SD card - Samsung 32Gb micro SD PRO Card Class 10 UHS-I from dataio.nl

Here it is! A nice new case, some flat top LEDs and the GPS tucked inside. The Pi camera can also
be raised or lowered in the mount. All in all I'm pretty happy with the way it has turned out.

If you want to see the install instructions click here. Below is further information on how it was made.

Dashcam version 3.5
I managed to reduce the GPS / Pi camera interference issue and now everything is running really well.
I have moved the GPS unit back inside the Pi case and have updated a lot of the programs including
adding a global resource file. Still a few things to do but I am going to start making a nice new (slightly
less swiss cheese like) dashcam unit very soon.


Dashcam version 3
 I've now moved the GPS unit on top of the case to try and stop the interference from the Pi camera but
 it has not really helped. The readings are usually good but it can easily wander way off-course, if I
 disconnect the Pi camera it is consistently accurate. I'm not happy with it though and it defeats the
 purpose of why I changed the unit from version 1.

Front Pi camera
 The Pi camera is fixed in place above the SD card slot in the camera mount I bought from pimoroni.
 As it has 4 slots I thought it could be raised or lowered as desired but unfortunately the first time I
 moved it one of the holding tabs broke. No biggy though, it's fine where it is. I used the standard Pi
 cam. ribbon cable but it is slightly folded so it can run along the left inside the case.


 The video for the Pi camera records at 960 / 540 by default but is dependent on the size of the memory
 card. If it is more than 32Gb it can record at 1920 / 1080. If a USB memory stick is detected it can
 write to that instead of the internal card. I think this is actually more favourable as it seems to perform
 better (although I have not tested thoroughly). Between 10pm - 6am (or user defined) raspivid runs
 with the "-ex night"  option. Approximately every 15 minutes raspivid ends and immediately starts
 a new recording.

Rear USB camera
 I had a Microsoft HD-3000 camera for this. I've borrowed other USB cameras, some had slightly
 better resolution or faster frame rates but their was not much between them so I stuck with what
 I had spare. It is held in place on the rear right car headrest using velcro. It is quite discreet and barely
 visible from outside of the car. I put it there a long time ago and haven't had to touch it since.
 It connects to the Pi on the dashboard using a 3 metre long USB extension cable.


 The rear camera video is recorded using the excellent motion software program. It was hard to find
 some proper specs. regarding the HD-3000 but running at 512 / 288 @ 12fps gives good enough
 results. Every 10 minutes it starts a new video file.

Status LEDs
 There are three LEDs on the front right next to the USB slots. The red comes on when a power source
 is connected. The green lights up when the OS is running and available and the yellow is a general
 GPS and video recording status indicator. If the Pi is having problems with the GPS, video recording
 or a system problem the yellow and / or green will flash at different rates (I do need to document this
 properly).








As you can see this case has been through a lot. If / when I can resolve the GPS problem
I will make a shiny new dashcam.
GPS
 For this I wrote a C program (with thanks to Blog of Adam for getting me started) which uses gpsd to
 read the GPS units data. The time, location, speed etc. are all written to one main logfile. Another file
 is written at the same time in gpx format with tracking information which can then be viewed in google
 earth for example. I have had the most problems with the GPS though. It is possible to fit the GPS unit
 inside the Pi case in a couple of places but the Pi camera interferes with the GPS unit very badly. I did
 trying using some copper plate to shield it but to no avail. It would be so nice to have the GPS inside
 the case but I just can not get it to work well together with the Pi camera.

Monitoring
 A status file is updated every minute with the uptime, CPU clock speed (usually 700Mhz or 900Mhz)
 and temperature. If the unit temperature rises above 82°C it will stop the motion process as this uses
 a fair chunk of CPU. Once the temperature falls below 78°C motion starts up again. I am not sure how
 useful this is yet but it was easy enough to implement. (As far as I can tell, if the temperature rises
 above 85°C the Pi will switch itself of immediately but don't quote me on that.)

 A script for each camera runs every 2 or 3 minutes to keep an eye on the video space. Once the
 thresholds are reached the oldest files are deleted until sufficient space is available again.

 If a network connection is detected SAMBA starts up so you can access all the files.

General
 I added new larger soft button (well, I had a spare hole) just to make it easier to reach. A quick press
 will issue a system power-off command and a longer 2+ second press will send a system reboot.

 This will probably be the final setup, at least using this Pi case. It has taken a lot of punishment. I have
 opened and closed it countless times. Many holes have been drilled and cut into it but it's still holding
 together really well. The plastic is also really nice as it is very flexible and fairly easy to drill or cut.
 If you are looking for a basic, cheap case then I would recommend it.

Issues
 The GPS works well, the Pi camera is great but the interference issue when they are running together
 is frustrating. I might try the version 1 setup with the GPS at the other end to see if that's any better.

 When the unit is shutdown using the button the green and yellow LEDs switch off to indicate it's
 stopped but the Pi writes/flushes to the SD card for a second or two longer.

 On a sunny day the unit can get pretty cooked on the dashboard and could overheat. Further testing
 (and some sunshine) is needed.

Some things I am thinking of doing:-
  • Creating some kind of global resource file you can change all the settings in.
  • Having NTP sync with the GPS time.
  • If a USB stick is inserted have all camera and log data write to it (not just the front camera only).
  • Maybe moving the power connector to somewhere more convenient.
  • Text overlay on the Pi camera video recording with date, time, location, speed etc.
  • A fisheye lens to get a wider view of the road.
  • Adding a microphone

Dashcam version 2
 The Pi camera is inside the case laying flat on top of the ethernet socket and connected via a 5cm
 ribbon cable. The 32Gb SD card has been swapped for a 32Gb micro card (in an adapter). The idea
 was to have the unit vertically mounted on the dashboard but after a USB memory stick, power and the
 USB camera are all connected the thing looked very large in the screen grip and takes up too much
 space for my liking.


 The status LEDs were moved to the rear underneath the PCB. It was a little fiddly especially as
 I wanted to try and hide the wiring.


 In the end I wasn't really that happy with how the unit looked or how much space it took up on the
 windscreen when it was all connected up and so, onto version 3...


Dashcam version 1
 The Pi camera sits on top of the case over the top of the CSI slot facing towards the ethernet and USB
 connections. It is held in place with blue tack and connected via a 5cm ribbon cable. I also installed
 a RTC so the video file creation names would be correct and also so that the time text overlay on the
 motion software video for the rear camera would be right.

 Three coloured LEDs are placed above the SD card slot (which faces the driver) to show the device's
 status. A click button for halting or rebooting the OS sits above the power connector on the right
 corner.
Sadly I never took any pictures of the original (blue tack and all), but this version 2 picture shows the
location of the LEDs above the SD card and the poweroff / reboot momentary button.

 Although this worked really well I could not find a robust way to hold the Pi camera unit in place and
 with it always being upright it was not very easy to store in a glove box. I toyed with the idea of a flip-
 up hinge mechanism but this was beyond my means (and skill).

25 comments:

  1. Erg mooi project! Is het een idee om er ook een tracker in te bouwen? Alle componenten zitten er al op.. Misschien nog een 3G optie? Ik ben bezig met een tracker waaraan ik een camera wil toevoegen.. Ik werk nu nog op basis van een android toestel, maar ik zou een van de camera's uit willen bouwen en verlengen, wat erg lastig is.. De camera voor raspberry is makkelijk te verlengen.. Ga je nog verder met het ontwikkelen? Gr Sebastiaan

    ReplyDelete
    Replies
    1. Thanks Sebastiaan,

      A 3G option is a nice idea. You could have the Pi send a text message with its
      current GPS location if requested or possibly some small images from the camera.
      I think I read about some 3G modules for the Pi so it should be fairly easy to setup,
      time permitting I will look into this further.
      How will your tracker / camera communicate with your phone? Will it be on-
      demand or a more continuous feed?

      Groetjes,
      Alan.

      Delete
  2. hola soy de mexico podrias ayudarme

    ReplyDelete
    Replies
    1. Hi There, sorry for the slow reply. Do you still need help or is it working okay for you?

      Delete
  3. Have you tried to send this data via 3G ?

    ReplyDelete
    Replies
    1. Hi, I have not tried using 3G. These pages have some good information about adding a shield:-
      http://www.cooking-hacks.com/documentation/tutorials/raspberry-pi-3g-gprs-gsm-gps
      http://www.thefanclub.co.za/how-to/how-setup-usb-3g-modem-raspberry-pi-using-usbmodeswitch-and-wvdial
      I would like to try it but It adds a lot of size to the to Pi and is also fairly expensive. I do not think I will
      be trying one anytime soon.

      Delete
  4. I'm curious how you got the motion program to use the USB camera. When I try setting the motion.conf file's videodevice parameter to /dev/video1 (the USB camera) it insists on using the pi camera. Did you have to work through a problem like that?

    ReplyDelete
    Replies
    1. Hi Steve, I haven't heard of an issue like this before. Motion at present does not recognise the Pi camera
      without some additional modification. How come /dev/video1 and not /dev/video0? Which OS are you using?

      Delete
    2. Very sorry I lost touch with this issue and didn't check back for your answer for so long.

      I'm using the raspicam-driven web interface described this project: http://www.raspberrypi.org/forums/viewtopic.php?f=43&t=63276&hilit=Rpi+web. The raspicam C module makes a system call to run motion to do motion detection. Motion starts up and reports on the command line that it is using configuration from /etc/motion/motion.conf, but changing parameters in that configuration file does not change the results of the videos produced as expected. One option in motion.conf is to specify the 'videodevice'. When I set that parameter to /dev/video1 it has no effect, and motion detection and the video produced continues to come from the pi camera at video0.

      The pi camera is on /dev/video0. It is recognized and works fine as the live cam in raspicam's web pages. I put another web camera on a USB port, which then gets mounted as /dev/video1.

      Steve

      Delete
  5. I googled this and found you. First question I have: what is the boot time on this?

    ReplyDelete
    Replies
    1. I have never timed it. At a rough guess it is probably around 10 - 15 seconds for boot up and both cameras to start recording. The GPS, from a warm start, takes an additional 10 -30 seconds to get a fix.

      Delete
  6. If anyone has gotten this to work and upload to the cloud I'd like to hear from you and see your plans. Thanks !

    ReplyDelete
  7. Have you had any issues with heat? I know you mentioned it gets pretty cooked on a hot sunny day, just sitting on the dash. Have you come up with a solution? Or is it best to just take the two cameras out? Maybe get a little drawstring bag to place the units in, and toss them in a backpack or laptop bag when going inside? The wiring could stay in place for quick re-connection.

    Just a thought............... :)

    ReplyDelete
  8. Have you had any issues with heat? I know you mentioned it gets pretty cooked on a hot sunny day, just sitting on the dash. Have you come up with a solution? Or is it best to just take the two cameras out? Maybe get a little drawstring bag to place the units in, and toss them in a backpack or laptop bag when going inside? The wiring could stay in place for quick re-connection.

    Just a thought............... :)

    ReplyDelete
    Replies
    1. Hi Jon, I ended up moving the Pi into the interior light housing for ease of use but as you can imagine it can get even hotter up there, plus it's no longer portable. It has survived pretty well for over a year but it's not ideal. I've been thinking about trying the UPS PIco with the fan kit or just a fan by itself. This could work nicely on a portable unit too.

      Currently the dashcam script monitors the temperature every minute. If it gets too hot it'll first stop the motion program as it's CPU intensive. If the temperature goes over 84 °C the script shuts-down the Pi immediately for safety. I'll update the blog if I get the opportunity to add a UPS/fan.

      All the best,
      Alan.

      Delete
  9. Would there be a way to setup an encryption system so that if the data card is removed or tried to access the data is encrypted. Because the last thing I'd want to do is to be speeding and have myself get pulled over, and have proof right there. Ideally, entering the "wrong password" 2 or 3 times would wipe the content. Cool project. Excited to play with this!

    ReplyDelete
    Replies
    1. Hehe, this is something that has slightly concerned me too. The encryption would have to work on the Linux filesystem or USB storage and ideally be in realtime. Time permitting I'll see what I can find out but can't promise anything. Glad you like the project. Cheers.

      Delete
  10. Awesome. Now, to take it to the extreme is it possible to have the whole, "enter this password" and it erases the data on the card? Very interesting project. I am going to have to keep an eye on this. How much to build one for me? ;-)

    ReplyDelete
    Replies
    1. Doing the 'enter password' thing is simple enough but protecting it from someone else using the 'rm' command or similar to delete stuff on your removable storage instead of needing the password would be difficult.

      Building one is time consuming and I'm not so into making them, I've only done three. I still have the one shown at the top of this page which I use for occasional testing. I'd sell it but it won't be cheap :)

      Delete
  11. Nice project
    How much USB bandwidth is consumed during recording?
    I was thinking to make something similar with a the Pi 2 But together a wifi access point and media server for the kids in the backseats. Was wondering if there is enough usb bandwidth to do it all at once.

    ReplyDelete
    Replies
    1. Thanks! I've never really measured the USB bandwidth. On my old single core Pi I had a 64Gb USB memory stick recording the Pi cameras video and the rear USB camera recording to the Pi's internal memory card, it coped very well. If I had both video feeds recording to the same location it would occasionally miss frames. I think a Pi 2 can deal with this better and should be able to handle a media server too. It's hard to say for certain though as card and USB device speeds can vary considerably. If you check the 'Summary' section of the blog I had some transfer speeds which I recorded quite a while ago. Should be a good project either way. All the best.

      Delete
  12. Hi,

    Nice Project. I was wondering if you tried both the cameras @ 30fps. Was Pi able to handle streams from both cameras @ 30fps?

    I am trying to do a similar project using 2 cameras @ 30fps and wondering if I can send the streaming from them via Wi-Fi.

    ReplyDelete
  13. Hi, how did you manage to stop picam interference to gps? I am working on a similar project but when I start taking photos the gps loses signal. Thanks

    ReplyDelete
    Replies
    1. Hello there, I was able to reduce the Pi camera interference slightly by double wrapping the ribbon cable with tin foil and then wrapping that with insulating tape. Also I put more tin foil behind the camera housing, insulating any possible contact points. Copper foil might be better but it is more expensive. Doing this noticeably helped my setup. Sadly the only really good solution seems to be the distance between the camera and GPS unit being 20cm+.

      Delete
  14. Hallo, we need a usb webcam (dashcam) solyrion for raspberry pi (source and compiled) - if possible for rear and front camera(. What would be the price in USD? Could You realize this?

    dimitri.krauss@gmail.com

    ReplyDelete