Pages

Sunday, 28 July 2013

Raspberry Pi Car Cam overlaid with OBD data

I like motorsport and love driving my car around race tracks, the only problem is when you capture this on film, using a dashcam or similar, it looks rubbish! I wanted to add some data about what was going on.

I recently blogged about how to use a raspberry pi to read car diagnostics data, I modified this program to log the data to a file, I then went for a drive around Castle Combe racetrack using the raspberry pi camera board to capture HD video alongside the OBD-II diagnostics data from the car

I then created a program which converted the OBD-II data into a subtitle file and used mencoder to overlay the data (RPM, MPH, Temperature & Throttle position) as subtitles on top of the video.

Create Subtitle File
I created a program to read the OBD data file and convert this into a SubRip subtitle file. The SubRip files are simple text files which allow you to define subtitles which can be imported into or played alongside movies.

The first number (675) is the number of the subtitle in the sequence, the 2 times are the times that the subtitle should appear and then disappear within the movie, the text after this is the subtitle, the subtitle is then terminated with a carriage return.

Overlay Subtitles onto Video
I used MP4Box to put the h264 video stream created by raspivid into an mp4 file:

MP4Box -add 20130715_214854.h264 -fps 30 20130715_214854.mp4

The -fps 30 option is important, without it MP4Box will assume the video is taken in 25fps and the resulting output will be playback slower than the original. It took me a while to work this out! I couldn't work out why my video and my data wouldn't sync. If your short on space on your SD card you can output to a USB or NAS drive but you will also have to use the -tmp /folder option as MP4Box uses the sd card for temporary storage while creating the video.

I then used mencoder to encode the mp4 video and the subtitles together into one mp4 video:

Be prepared to wait though, encoding a 13 minute full HD video took 9 hours on the Pi!

This is very much in the prototype stage at the moment, when I have tided up the code, automated it and made it more resilient, Ill provide all the code and more instructions on how to use it. It was also painfully difficult to get the data to sync with the video, so I need to think of a better solution for this.

Very Cool. Maybe for some further inspiration, look at the addons to Torque for Android. It also reads the ELM327 and one of the addons does guage overlays onto the recorded video from the android device.

If the camera module is available in the android install for Pi, might even be able to just reuse the current hardware config and just change out the software.

I've been thinking about OBDII computer project for a while now, 1st a 8051 and serial port based one, but was disappointed about how little information was available about the protocol. I could not find enough to write my own driver. Now you can buy a $10 device that will communicate with your PC or smartphone over USB or BT and use a free software for that. I like this project and I want to duplicate your path by doing my own car computer with video recording capabilities. Can you tell what kind of camera module did you use in your project? I see the ribbon cable going to camera and I wonder if a regular USB web cam can be used with Raspberry PI?

Good post, nice car, do you know of the kickstarter project for carputers "Raspberry Pi Car Power Supply Ignition Switch" http://www.kickstarter.com/projects/1312527055/raspberry-pi-car-power-supply-ignition-switch might be of interest.

It's quite a bit heavier but you might want to look at the OpenXC project as well. :) http://openxcplatform.com Right now Ford is the only company supporting it but I'm going to do a bit of work to try to reverse engineer some of the signals on my Nissan 370Z when I get time.

Very cool project. I can't wait to see further iterations as you develop it.

I have a question too. It's been my experience that OBD data is fairly laggy and doesn't have a very high sample rate. Has this been your experience or does the Lotus spit data at the OBD bus as fast as it drives?

In my project the end-to-end time to send a command to OBD, pull back the data, parse it and save it to a file was about 0.2 seconds. This is adequate if your only logging 1 peice of data, but if you want 5, you are making 5 separate requests, so suddenly my request time was 1 second, not great. I've not looked at baud rate tho, I suspect im using the default and slowest so there is probably improvement to be made.

Cozy up with a Lotus service tech and find out if Lotus uses other buses like CAN or a proprietary bus that is available on the OBD II port or elsewhere in the vehicle. While all manufacturers support the mandated OBD II functions and standards, many find it inadequate to their needs and actually use another bus topology along side the mandated ones. These often are not only faster and more responsive, but offer more detailed data and control options.That said, you're right to try exploring connection speed settings with your existing setup first.

Hello Martin, I am an avid reader of your blog and, like Nicholas, I am a young programmer. I have been working on a program that creates a menu GUI that can take you to other GUIs that are full of minecraft games/tools, including your snake game. Sorry if that's bad, I'll take it out. But, I did credit you in it and put your site in it. It would absolutely make my day for you to just look at it.But, I have no way of sending you the file. Pleas help

Hi Jakey,It sounds like a good project. Anyone and everyone is welcome to use my code, Im not precious about it, a mention is always nice tho, so thank you.Would love to look at your program. In terms of publishing it so people can access it, I would encourage you to look at github, although it can be a little daunting to start with, or , how about putting your program on dropbox or google drive and sharing it - they are really easy to use.Mart

Hello! Thank you for sharing your project and great integration using the power of Raspberry Pi! Out of curiosity, which camera did you choose for video capture?

Over the last couple of years I've developed the hardware and firmware for a rugged, high performance Open Source Hardware and Software data acquisition system for motorsports called RaceCapture/Pro http://autosportlabs.net/RaceCapturePro

We had a successful indiegogo campaign that completed at the beginning of this year: http://indiegogo.com/RaceCapture

The benefit of OBD2 is that while it's super convenient to interface, the sample rate is very low when many channels are enabled. Our goal with the RaceCapture/Pro project was to enable up to 100Hz logging across all channels and then build in support for an OBD2 interface for additional convenience when desired.

The camera is the standard raspberry pi camera board. It delivers 1080p at 30fps and plugs directly into the raspberry pi.

I had seen your product before, it looks really good, I had always wanted to make my own and for me data gather through OBD is good enough and doesnt involve the installation of sensors through-out the car.

My aim was always to overlay data on top of video as well, Im not using it to get detailed info about the car's performance, I just want to get some nice video for little money.

sorry i jsut wanted to grab the code thats all so ic an have a go, i have got my pi running, got it recording video via the terminal, i have my elm and need to get the data writing to csv for the subtitle bit, then the rest i think i can follow from your guide, so just wondered if you had a project i could download or if you could help me out a bit as i want to learn to develop more in python as i do c sharp / asp normally