Tuesday, 24 March 2015

Focus Stacking Coins of the Realm.

This page leads on from Testing the Focus Stacking System.

Today I've been doing a bit more testing of my focus stacking system, photographing a 5p coin.

The purpose of this was to test the effect of taking shots at larger and smaller increments by controlling the scanner differently.

The script scanprogramme.txt gives larger increments (about 0.08mm I think). The script smooth.py gives smaller increments (about 0.02mm-ish, probably).

According to the documenation of my camera lens, the depth of field at the maximum 5x magnificaion is about 0.1mm, so the bigger increments should really be enough.

Here are some results:

With larger increments the individual shots look like this:

and the focus stacked image looks like this:

With smaller increments the individual shots look like this:

and the focus stacked image looks like this:

I find it hard to say whether there is any improvement from using the tiny increments over the larger ones.

This is the setup that I used (The train set in the background is the important part, obviously):

This project eventually took wings and now has a website of its own. To see more about it please go to http://www.chlorophyllosophy.uk.

Monday, 23 March 2015

Testing the Focus Stacking system

This post follows on from Focus Stacking - Connecting the wires.

I just tested my new focus stacking system and it was fairly successful. In the end I mounted the subject on the scanner and just triggered the camera manually using the shutter release button. The scanner ticks loudly every time the arm moves so it wasn't too hard to know when to release the shutter.

I used this python script to drive the scanner. The camera setup was a Canon 5d MkII with a Canon MP-E 5x macro lens. The camera was just resting on a box and working in auto mode. The battery was stuck to the arm of the scanner with a bit of blue tack to stop it wobbling.

Here are a few sample images:

This is the focus stacked image, compiled by Helicon Focus:

There's some strange fuzzy stuff going on at the top, but otherwise that seems to have been fairly successful. 

Good stuff. 

This post follows on from Focus Stacking - Connecting the wires.

The next page in the series is Focus Stacking Coins of the Realm.

This project eventually took wings and now has a website of its own. To see more about it please go to http://www.chlorophyllosophy.uk.

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.

This project eventually took wings and now has a website of its own. To see more about it please go to http://www.chlorophyllosophy.uk.

Sunday, 1 February 2015

Focus stacking - connecting the wires

This is a follow-on from "Setting up focus stacking".

This page describes how I made the following connections:

laptop --> Raspberry Pi --> chip --> stepper motor. 

Sites and books that I learned from:

The motor is a unipolar stepper motor, with five wires. The final setup is shown below.

Connecting the wires in possibly the right order...

The closer photo below shows how I have connected the components via a breadboard.

The power for the motor comes from the mains cord that originally powered the scanner. I cut the plug off that had previously plugged into the scanner, and I stripped the wires to fit them into the breadboard (Labelled + and - in the photo.). The plug steps the electricity supply down to 15V so the bare wires are not dangerous.

In the middle of the breadboard is the black ULN2803 chip that the raspberry pi will be driving to control the stepper motor.

At the back of the photo is the raspberry pi with 5 wires plugged into its GPIO (output) ports. These wires lead to the breadboard to feed signal into the chip. The signal comes out of the other side of the chip into the other set of five wires that are attached to the motor.

The hard part was to know in which order the coloured wires in each set should go. Here's what I did:

The Raspberry Pi Cookbook explained what to do with the wires that plugged into the pi. That was easy. There are 13 pairs of pins and you can see which colour of wire went onto each pin here. The yellow wire is directly behind the brown wire. So far so good.

The other end of the pi wires plugged into the breadboard like this. Four at the top end and the last one at the bottom end of the chip.

The inside of the chip looks a lot this like diagram that I have copied from Colvins.ca, and which he got from the manufacturer. We are using a different chip, but it is very similar. To get the wires in the right order we need to understand what is going on in here.

What is going on in this maze of wires? 

As I understand it, this is how the business works:

The box on the left is the pi. The box in the middle is the chip. The web diagram on the right is the motor. It's the magnets in the motor that really matter.

The motor has four magnets in it. They are connected in series in pairs. Two are on a circuit with a yellow wire at one end and an orange wire at the other end. Two are on a circuit that has a brown wire at one end and a black wire at the other end. If both ends of the circuit are at 15V then the electricity does not flow and the magnets to not do anything. If one end of the circuit is at 15V and the other end is at 0V then the electricity flows and the magnets start to work. When they start to work, they move the motor, just one little step, and then they stop again. That is why it is a stepper motor.

The thing that we need to do is to make the magnets work, one after another, so that they pull round the circle. If they do this then the motor will work its way round over and over again and draw the scanner arm along. If we activate the magnets in the wrong order, then either nothing will happen, or the motor will go backwards then forwards, or it might just go one step and then stop.

But how does the pi control this?

In the normal state, all the pins on the left side of the chip are at 0V and all the pins on the right side of the chip are at 15V. Both ends of each magnet circuit are attached to the right side of the chip. This means that both ends of each magnet circuit are at 15V and the magnets are doing nothing.

When we tell the pi to act, it sends a signal down one of its wires. This switches the left pin on the chip to 15V. It triggers the NOT gate in the chip, which flicks the pin on the other side to 0V. Now, one of the magnet circuits has 15V at one end and 0V at the other end. There is a fifth (red) pin in the middle of the  motor that is also 15V so the circuit is actually 0V at the chip and 15V in the middle. Electricity starts to flow, and just one of the the magnets starts to pull the motor round.

We will tell the pi only to send a signal down one wire at a time, so only one NOT gate will be triggered, and only one magnet will pull. If we can trigger the magnets in the correct sequence, then they will pull one after another, and the motor will work its way round a complete circuit.

The photo above focused on the breadboard end of the pi wires. This photo below focuses on the breadboard end of the wires that lead to the stepper motor.

Once I had put the wires in like this, I had to figure out which order to trigger the gates in, so as to make the magnets work in the right order. I had no idea of the order until I did some experiments. Below are two tests that I found useful.


1) Test that the motor works. 

I held the two power wires against the red and one other colour of wire coming from the motor. The motor made a clicking noise, confirming that it was working.

2) Check the order of the pins

I wrote an experimental programme that told the pi to trigger the wires in pairs. The programme tested all possible pair combinations, and printed on the computer screen a list of which pair was being tested, as it happened. If the motor made a loud clunk, then I knew we had triggered the wires in the right order. If it made a quiet clunk then it meant the pairing had not worked well together.

Here's what the programme did: Programme rationale.

The code of the programme is here: Programme code
(It is adapted from the suggested programme in the Raspberry Pi Cookbook.)

To run a programme like this, I used WinSCP to put the programme on the pi. I used putty on my laptop to ssh to the pi. The programme was in my home directory and I had to type the following to run it:

> chmod +x test.txt (to change the permission so the programme can run.)
> sudo ./test.txt (to run the programme.)

When I ran this test, the scanner went clunk clunk clunk, and I could just see the cogs moving a bit. However, about every third clunk was louder than the others, and it was clear that these louder clunks were the good pairings. Conveniently they were the pairings 0-1. 1-2, 2-3, and that meant the fourth good pairing must be 3-1. Success!

What's next?

I re-wrote the programme to run these good pairings in the correct sequence, and the scanner arm smoothly worked its way along, with no backwards steps. Victory!

This is the new programme: Programme code
To reverse the arm I used this programme: Programme code

Now I have a programme that makes the scanner arm potter along the scanner in the direction I want and at a speed that I specify. Next I have to install a programme on the pi that will allow it to trigger the camera as well. Once I've done that, the system will be able to move the sample one tiny step towards the camera, then trigger the camera to take a shot, then move again. Once that's working, we are well on the way.

The next step of this project is continued on this blog page.

This project eventually took wings and now has a website of its own. To see more about it please go to http://www.chlorophyllosophy.uk.

Friday, 23 January 2015

Setting up focus stacking

I am trying to convert a flatbed scanner as a moving support for my camera for focus stacking. I'm going to document how it goes in case it helps others.

The idea of focus stacking is as follows:

  • I would like to take very close-up photographs of a gametophyte fern. 
  • I have a 5x Canon macro lens, but at the 5x magnification it only has 0.1mm depth of field.
  • In focus stacking the subject is photographed many times, and moved toward the camera a little each time. The many photographs are then combined into one single sharp photograph using commercial software (I'm using Helicon Focus).
  • With such a small subject it will be easier to make the moves using an electronic platform. 
  • I am going to convert a flatbed scanner so that I can put the fern on the arm and move the arm in tiny steps using a computer programme. 
  • The computer programme will run on a Raspberry Pi computer, which will also trigger the camera to take shots between moves. 

None of this was my idea. I learned it from the following sources:

It was recommended to me by Jim Haseloff of the Department of Plant Sciences in Cambridge. 

My hope is to set up a system like this and use it to support my own interest in photographing gametophyte ferns. 

I am hoping to take photos like this one of my own, but much better:

So how am I actually going to get this done? 

I'm using a Plustek OpticPro U12 scanner as it is quite an old model so is very solidly built, with a big strong platform on the scanner arm. This is it with the cover and glass platform taken off:

The motor inside is a unipolar one (shown by the fact that it has 5 wires).

The arm motor controls some cogs which push the platform along a metal pole.

There are two boards in the scanner as shown below:

The motherboard has a ULN2003 chip on it which we could use to control the motor, but I am going to buy a different one that will be easier to program.

I am going to hitch the scanner up to a Raspberry Pi by the method descriped in O'Reilly's "Raspberry Pi Cookbook".  To do that I have ordered:

- some male to female jumper wire
- an electronics breadboard,
- a chip called ULN2803
- a copy of the book.

We already have a Raspberry Pi and the means to connect it to my laptop via the network in our house.

I am going to work out how to set up the scanner arm to move forward in the correctly tiny steps at the correct speed, and then install software on the pi to trigger the camera to take shots in between steps.

This is just the first chapter of quite a long project. Hopefully more progress next week. :-)

The next page of this story is on this blog page.

This project eventually took wings and now has a website of its own. To see more about it please go to http://www.chlorophyllosophy.uk.

Monday, 5 January 2015

The complex romantic life of the fern.

A gametophyte generation plant of the species Dicksonia antarctica.

The plant above is a gametophyte fern, which is, beyond doubt, the finest thing that ever existed in nature.

It is only about half a centimetre across, and it will never get very much bigger than this. What is it though, and how does it relate to the big ferns (like the one below) that we are more familiar with from our gardens?

A sporophyte generation plant of the species Dryopteris wallichiana.

Ferns are not quite like people in their mode of their reproduction. They have a system called "alternation of generation". In this system, a tiny gametophyte plant (top photo above) reproduces to make a new large sporophyte plant (second photo above). Then the large sporophyte plant reproduces to make a new tiny gametophyte plant. And round they go again, alternating between sporophyte and gametophyte for ever.

The two plants are both individuals in their own right, and they can live entirely independently of one another.

What are these sporophytes and gametophytes though?

A sporophyte fern is a large plant that produces spores. We can easily see the spores clustered on the underside of the leaves of ferns that we meet in the garden. The spores each have only one set of chromosomes. When they fall off the leaves of the large sporophyte fern, they land on the ground and develop into tiny gametophyte plants.

The tiny gametophyte plants have only one set of chromosomes in each of their cells. The plants never grow much larger than a centimetre across and at maturity (a few weeks) they produce egg and sperm cells which also only have one set of chromosomes. The sperm cell swims across the plant's surface and possibly even onto an adjacent plant, until it finds an egg cell and fertilises it. This fertilisation event produces a cell with two sets of chromosomes, which then goes on to develop into a large sporophyte fern plant.

It always astonishes me that something as deceptively simple as a fern has such a complicated life cycle. It actually includes two completely different kinds of plants, which take turns in living their lives.

Since some sporophyte ferns are large trees, this alternation of generation is the size equivalent of a skyscraper giving birth to a shoe, which then gives birth to a new skyscraper. It's pretty odd, and pretty amazing. What's more, the process has been going on quietly and successfully for many many more years than human reproduction, in spite of its complexity.

If you would like to know more about the gametophyte fern, or even grow some yourself, then you can find the instructions in my post called Growing Fern Gametophytes.