Wednesday, 4 February 2015

Triggering the camera with the Raspberry Pi

This page is a continuation from Focus stacking - connecting the wires.



The next stage in my project is to set up software called gPhoto2 to allow me to trigger the camera from the python programme that I have already been using on the Raspberry Pi. This will mean that the python programme on the pi can advance the scanner arm, then tell the camera to take a photo, then advance the arm, then take a photo, and so on, even up to hundreds of photos.

This part was very complicated because we were installing packages from source, so I had to phone a friend.

The pi runs Raspbian, which is a kind of linux operating system, and I control it entirely from the command line by ssh-ing through from my laptop. Installing packages on linux is normally fairly easy, but installing from sources is harder. Several of the packages that we had to install turned out to need other packages installed first, so we had to keep going back and installing those and then trying again. We got there in the end though.

We had to install three main things. libgphoto2, gphoto2, and the python bindings to allow us to trigger the camera from inside a python programme.

I'm going to describe it in some detail in case it is useful to others who are trying to do the same thing.

Here is what we did:

First to check that libgphoto2 was already installed:
Type: dpkg -l | grep gphot
ii  libgphoto2-2:armhf             2.4.14-2        armhf        gphoto2 digital camera library
ii  libgphoto2-port0:armhf       2.4.14-2        armhf        gphoto2 digital camera port library
This means that we already have libgphoto2. 

Then to install the programme called pip that would allow us to install python bindings:
Type: sudo apt-get install python-pip

Then to install gphoto2:
Type: sudo apt-get install gphoto2

Then to use pip to install the python bindings in gphoto2
Type: sudo pip install gphoto2
This did not work because of missing dependencies. 

Next we used wget to install libgphoto2.
Type: wget http://downloads.sourceforge.net/project/gphoto/libgphoto/2.5.7/libgphoto2-2.5.7.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fgphoto%2Ffiles%2F&ts=1423043960&use_mirror=kent

Rename the libgphoto2 file to make it end in tar.bz2.
Type: mv libgphoto2-2.5.7.tar.bz2\?r\=http\:%2F%2Fsourceforge.net%2Fprojects%2Fgphoto%2Ffiles%2F\&ts\=1423043960\&use_mirror\=kent libgphoto2.tar.bz2

Uncompress the tar file.
Type:  tar xjf libgphoto2.tar.bz2

Remove any previous installs of libgphoto2 as they are not working with pip.
Type: sudo apt-get remove 'libgphoto2*'

Install the libgphoto2 package.
Type: cd lib[Tab]
Type: ./configure
This did not work because we did not have libtool.

Install libtool.
Type: sudo apt-get install libltdl-dev
Type: sudo apt-get install libtool

Instal libgphoto2:
Type: ./configure
Type: make
Type: sudo make install

Try again to install the python bindings in gphoto2.
Type: sudo pip install gphoto2
This failed because of a missing file.

Type: dpkg -l | grep libpython
Type:  sudo apt-get install swig
Type:  sudo pip install gphoto2
This failed because of a missing file.

 Type: sudo apt-get install python2.7-dev

Try again to install the python bindings in gphoto2.
Type: sudo pip install gphoto2

Then it was all finished. All of the packages have been installed that will enable me to trigger the camera from the python programme that is already advancing the scanner arm. The next thing to do it to learn how to write the commands in the python programme. That will be for another day now. 

As an advance taster, this page explains how to trigger the camera from a python programme using the gphoto2 commands.
https://pypi.python.org/pypi/gphoto2/