Tuesday, March 18, 2014

Pi in the Sky



One of the many features available with the Raspberry Pi is the integral Pi Camera. One of the things I was very keen to try out was putting a Pi-Cam on the front of the microlight that I fly. This I have now done. The camera will capture High-resolution images and they look fantastic! Sadly for the purpose of putting the video on this blog I've had to massively reduce the resolution as well as editing the film! Anyway, hopefully you can get the idea with these 2 minutes of footage including take-off and landing. I will put much more interesting aerial footage up when I get it.

So how was this done?

Put simply, I used a Pi_A (for it's lower power consumption) and attached the camera to the front strut of a P&M aviation QuikR. Total cost of components ~£50.

For any techno-people out there, the following is the detailed run-down of how to do it:

I got my Pi_A with the camera from Farnell, who have a fantastic price of £27.98 (+vat) for the Pi and camera board combined.

This is how I got it all working. I spend some time reading what other people have done on the internet as well as referring to this book. One of the great things about the Pi community is that whilst no-one had done quite what I was trying to do there's so much information out there to help me learn how to do this. I have never even used a linux os before getting a Pi.

Component list:
RaspberryPi A
RaspberryPi Camera - link £33.58
Longer Ribbon Cable (30cm) - link £2.85
Pi Case - link £2.69
32Gb SD Card - link £10.99
Power lead - link £3.15

Wheezy Rasbian downloaded from the RaspberryPi official website. I didn't use the 'New-out-of-the-box' versions, instead opting for downloading the Raspbian image. A 32Gb SD Card enables about 4 hours of high-resolution video recording as well as holding the OS. I found that a longer ribbon cable was also useful, although not vital. The case I chose for it's cheapness and ease of assembly/reassembly. The other advantage of it is that because it's made for the Pi_B the ribbon cable can pass through the Ethernet port hole.

  1. Format SD card with SDFormatter on my PC
  2. Copy Wheezy-Raspbian image downloaded from the RaspberryPi site with win32diskimager onto the SD Card. The Pi is now ready to go.
  3. Activate camera and Maximize partition. When the Pi boots for the first time it runs config-Raspi which is needed at this stage for two operations. Firstly to activate the camera (all the new versions of Wheezy-Raspbian have the capability for the camera). And secondly to maximise the partition size so that all of the SD card space is available. These are done simply by selecting the options on the menu.
  4. Allow Pi to boot without login:  To do this involves modifying the inittab file. As with all things with the Pi, it's quite straightforward once you know how 'nano' is simply a text editor and 'sudo' allows a permitter user to execute a command as the superuser which is necessary for certain commands.
    In Terminal:
    sudo nano /etc/inittab
    Scroll down to:
    1:2345:respawn:/sbin/getty 115200 tty1
    and change to
    #1:2345:respawn:/sbin/getty 115200 tty1
    Under that line add:
    1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
    Ctrl+X to exit, Y to save followed by enter twice
  5. Create folder to save the video files in. This isn't strictly vital but helps when wanting to access the recorded videos later.In Terminal:
    mkdir video
  6. Create shell script. A 'shell script' is just a little program that can be made to run automatically at startup that uses the built-in commands to record video.In Terminal:
    sudo nano video.sh
    #takes video
    #-t 0 means keeps recording until stopped
    #-rot 180 as camera fixed upside down
    #saved as date and time based file name to folder 'video'
    while true
    filename=/home/pi/video/`date +%Y%m%dT%H%M%S`
    #creates filename
    sudo  raspivid -o $filename.h264 -t 0 -rot 180
    # takes video
    This script will then record video indefinitely unless stopped. I found the best way to mount the camera was upside down hence '-rot 180' makes the images the right-way up. The filename is created from the Pi's clock to be date and time. As this Pi is not connected to the internet the date and time is wrong. (Unlike PC's the Pi does not contain a battery to keep the correct time). However I used the method to ensure that the video files have an individual name, so it will record without error. Obviously the specific names are of no real significance.
    Make shell executable:
    In order to work, the shell needs to be executable.
    In Terminal:
    chmod + x video.sh
  7. Autorun on startup. This is achieved by adding a line to the .bashrc file. Once done the Pi will boot up and then automatically start recording video. In Terminal:
    sudo nano.bashrc
  8. Accessing files: Of course there's not much point in recording video that you can't then watch!
    The first way is directly on the pi:
       Hold down Ctrl + C this stops the program and returns to the command prompt
    omxplayer is the command for simply playing recorded video. To actually use or edit the files though it is important to be able to manipulate them. I decided that a good way of doing this was to get a separate SD card (8Mb) to run Rasbian on and then access the 32Mb card via a card reader. I also used this setup - so that I could then access my Pi files from my PC. (http://raspberrywebserver.com/serveradmin/share-your-raspberry-pis-files-and-folders-across-a-network.html)
  9. Converting  files: It's much easier to manipulate video files if you transfer them to MP4 format. MP4Box can be installed on the Pi and runs from the terminal. Once installed the easiest way is to open the folder containing the video files and then selecting Terminal from the Tools menu (or pressing F4).
  10. Camera Mount:  Mounting a forward-facing camera on a microlight is quite tricky. The Pi Camera is excellent because it's so small, but it took a little bit of thinking to work out how to mount it with the slight down-angle that I wanted. In the end this is what I came up with:

    The wire passing through the mounting holes on the camera board and through drilled holes in the plastic at the bottom to secure the board.


Just said...

Nice write up mate, but... you decided to share that landing?!!!

alienfromzog said...

It wasn't my best...