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.


Tests


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.