# Gas station without pumps

## 2013 April 7

### Destroying a hard drive

Filed under: home school,magnetometer — gasstationwithoutpumps @ 13:08
Tags: , ,

Sorry that I’ve not posted in a week, but my laptop has been in the shop, getting the hard drive replaced.  The destruction of the old drive happened last Sunday in a physics lab I was doing with my son.  We were planning to measure the magnetic field of a coil as a function of current, distance, radius, or number of turns (but probably not all those variables, as that would have gotten tedious).  We weren’t getting a noticeable reading from the magnetometer, so I got out a neodynium magnet to see if the magnetometer was working.  It was—in fact the magnet saturated the magnetometer easily.  When I went to turn off the program that was streaming data from the magnetometer, I carelessly put the magnet down on my laptop—right over the hard drive. That mistake turned out to be an expensive one.

The computer continued to work that day, but the next time I tried to restart it, it wouldn’t boot up.  I took it down to the local computer shop on Monday, where I found out that the extended warranty had expired 6 months ago, so I had to pay labor as well as materials for replacing the disk, which ended up costing me $376 (as long as they were replacing the drive, I had them upgrade from a 500GB drive to a 1TB drive, since I was running out of disk space). When I finally got the computer back on Saturday, I spent most of the day restoring the system from my backup drive. Now I need to replace the backup drive, since Time Machine complains that the old drive does not have enough space to do a full backup. It looks like that will cost me another$100–$150 for a 1–2TB backup drive. I don’t have many choices of drive, since I need a Firewire 800 interface (my old MacBook Pro does not have USB 3 or Thunderbolt). So my moment’s carelessness cost me the use of my laptop for a week and about$500.

After having confirmed that the magnetometer was ok, we did a rough calculation of how strong the field from the coil should be, to see whether we ought to be detecting. (I know, we should have done that first.)  We were running about 33mA through a coil of 5 turns with a diameter of about 4.4cm. Using the formula $B(z) = \frac{\mu_0}{4\pi} \frac{2 \pi R^2 N I}{(z^2+R^2)^{3/2}}$, with I=0.033A, R=0.022m, and N=5, I computed that the magnetic field right at the center of the coil (z=0m) should be 4.7µT,  at 1cm (about as close as I could get the magnetometer) it should be 3.6µT, and at 2cm (where the measurements were being attempted) the field should be about 1.9µT.  The magnetometer has a resolution of 0.1µT per count, but the noise level was high enough that counts of 20 (2µT) would have been barely detectable. I suspect that a lot of the noise was because we had not immobilized the magnetometer.  According to the World Magnetic model, as displayed in Wikipedia, we should have about 49µT at a 60° inclination due to Earth’s field, so changes in orientation of 1° in the magnetometer would causes changes of about 0.9µT.

We’ll repeat the experiment (without having a strong magnet near the laptop!) using more current (say 300mA), more turns (40), and a smaller radius (a diameter of 1.25cm). With those values, we should be able to get a field of  1.2mT at the center of the coil, 180µT at 1cm, 32µT at 2cm, and 11µT at 3cm. We’ll also immobilize the magnetometer in my plastic-jawed Panavise, and make measurements by subtracting the field with the current off from the field with the current on. We may even double the signal by subtracting the field with the current in one direction from the field with the current in the opposite direction.

## 2012 November 27

### Magnetic fields with no lab

Last week we did measurements of the magnetic field around a single wire, and I had planned to “do a lab winding a helix of wire and measuring the field around it.  We’ll use the computational problem (18P79) to compute the expected field in different places, and try measuring the wound solenoid in corresponding locations.  This means that in setting up the program we’ll have to make the number of turns, the radius of the solenoid, its length, and the current through the solenoid all easily changed, to match the simulation to the coil that we wind.”

As it turned out, my son had the simulation finished and we spent most of an hour exploring what the program told us.  The initial picture showing magnetic field arrows near the coil looked fine, but I suggested trying a different visualization: having a particle trace out a magnetic field line.  We expected to see something like the classic pictures of iron filings around a bar magnet, and were surprised to see the magnetic field coiling out from the end of the solenoid.

We did a bunch of debugging.  We looked at at the contributions to the field from the different segments of the coil, by color coding arrows from a fixed observation position. The simulation had n segments for each turn of the helix, so we summed the segments mod n, to get the different contributions from the different parts of the helix.  We also tried varying the number of turns of the helix, and we played with the step size for the particle tracing out the field line.

We finally got some very nice drawings of the field lines coming out one end of the solenoid, spiraling out, then spiraling back in to the other end, and running through the center of the solenoid.  It took us a while to realize that the behavior was indeed what we should have been expecting, because the helix has current running parallel to the axis of the helix as well as around the helix.  A simulation (as the book suggests) using only circular rings would not have included this longitudinal current, and we would have missed some interesting views of the magnetic field.

I’m wondering whether we could have gotten a similar result by superimposing two fields: one computed from a stack of circular rings and the other from a wire down the axis, both with the same current.  I might try writing a program that compares the two approaches.

Because we spent an hour doing simulations and looking at the results, we did not get around to doing homework comparisons (a good thing, since I haven’t done the homework yet) nor did we get around to winding a coil and measuring the magnetic field, which I still want us to do.

## 2012 November 20

### Physics lab with magnetometer

Filed under: home school,magnetometer — gasstationwithoutpumps @ 16:10
Tags: , , ,

Today we did not go over homework problems in our weekly physics time, but did a lab instead.  I rewrote the code I had for using the Freescale MAG 3110 magnetometer chip on the Sparkfun breakout board, so that I could zero-out the measurement for easier differential measurement.  What we did then was to make a big loop of wire (26 gauge magnet wire—the same wire we used for our Young’s modulus experiment) from the floor to the ceiling beams, holding it to the floor with a couple of books, so that we had a straight vertical wire in the middle of the room, several feet from other wires.  We put the magnetometer near the wire, recording the distance and the orientation of the sensor, then zeroed the reading and measured the magnetic field 3 times. We then put a current through the wire (about 200 mA), measuring the current and the magnetometer reading (again, 3 sets of x,y,z values).  We did this for several distances and magnetometer orientations.

We also did one pair of measurements without the wire, zeroing the sensor in one orientation, then turning it around 180° and making another measurement.  This should give us an independent check of the units that the sensor readings are in, since we expect this measurement to be twice the strength of the Earth’s magnetic field (according to the World Magnetic model, as displayed in Wikipedia, we should have about 49µT at a 60° inclination, so the horizontal component should be about 24.5µT, and our measurements should show about a 49µT difference).  Of course, the rotation may not have been an exact 180° horizontal rotation, so we can’t really use this measurement to calibrate the sensor.

The manufacturer’s data sheet claims that the resolution is 0.1µT, but we recorded the sum of 10 successive readings, so our units are 0.01µT.  The repeatability of the measurements was not too bad—probably around ±0.5µT (I’ll have my son compute standard deviations for each set of 3 readings).  We only measured out to 5cm from the wire, since at that distance the field we were measuring seemed to be buried in noise.

My son’s task is to take the recorded field measurements and plot field strength as a function of distance for the measurements we made (probably correcting for differences in current, if those are large enough to matter).  He should also compute the expected magnetic field around a long wire for that field. There are several measurements at one distance, as we tried to verify that we were reading the orientation of the chip correctly—that distance might be a particularly good one for comparing the measured and computed field strengths.

Next week, we’ll try to do a little homework comparison, but we’ll also do a lab winding a helix of wire and measuring the field around it.  We’ll use the computational problem (18P79) to compute the expected field in different places, and try measuring the wound solenoid in corresponding locations.  This means that in setting up the program we’ll have to make the number of turns, the radius of the solenoid, its length, and the current through the solenoid all easily changed, to match the simulation to the coil that we wind.

## 2012 November 14

### Chapter 17 done, on to magnetic fields

Filed under: home school,magnetometer — gasstationwithoutpumps @ 20:53
Tags: , , , ,

Chapter 17 of Matter and Interactions, “Electric Potential”, went very quickly.  I had allocated 3 weeks to it back when I was making up a schedule, but we polished it off in a week.  When we went over the problems yesterday, there were a few discrepancies between the results my son got and the results I got.

On 17p45 and 17p47, which involved electron guns with high voltage, he had computed the electron velocity without a relativistic correction, while I had done the calculation with the correction.  Since speeds were around a quarter the speed of light, the relativistic correction makes a difference, but not a huge one.

On three other problems, the discrepancies were all errors on my part (mainly sign of electric field), though in one case (17p80) it took us a while to find where I had goofed, though it was immediately obvious that his sign was right and mine wrong, just by looking at the extreme case, where the potential was being measured right at the charge whose sign was reversed.  I had forgotten that the field pointed down in the region of interest, and so left out one negative sign.

Chapter 17 went quickly in part because there were no experiments I could think of to do.  About the only relevant experiment I can think of would be to build a Van de Graaff generator (or other electrostatic generator).  That might be fun to do,  but would take more time than I want for one experiment. So we’re closing Chapter 17 after only one week, and racing on to Chapter 18, on magnetic fields.

I’ve not finished reading Chapter 18 yet, but I can see several experiments we can do.  We can do the 5th-grade science experiments that the book suggests—I even have some of the wires and compasses salvaged from the 5th grade classroom when my wife’s school did a massive cleanup this summer (half the building had to be vacated for construction after the July 4 fire). But there is no reason to stop there.  I have multimeters and a MAG3110 magnetometer, and we’ve previously written code to re-center the magnetometer readings, so we can actually measure currents and magnetic fields.   We might not even need to do the centering of the magnetometer, since we can do measurement of the magnetic field at a location as a series of different measurements with different currents, and look just at the differences in the readings.  The full-scale range of the magnetometer is supposed to be ±1000 µTesla (the Earth’s magnetic field is about 25 to 65 µTesla at the surface) with sensitivity of about 0.1µTesla, so the magnetometer should be far more sensitive than using little compasses.  It also measures the field along 3 axes, so we can look at the vector for the field, not just measure in one plane.

I’ve not finished reading Chapter 18 yet, so I’m not quite ready to assign problems, but I think that the computational problem for 18P79 (simulating the magnetic field of a solenoid) is worth doing, particularly if we compare the results from using a number of parallel rings to the results from using a helix, though I’d be satisfied with just the helical simulation.  We don’t get to inductance until Chapter 23, but we may want to wind a coil that matches our simulation and measure the magnetic field from it.  We’ll have to add some series resistance to make sure that we don’t fry our wall-wart power supplies (nor turn our solenoid into a fuse), but we should be able to wind a coil on a cardboard tube and measure the field in various locations with the magnetometer.  We should probably start with measuring the field around a straight wire first, though.

If we use a long piece of wire (say L=1m) and measure close to it, (say r=1cm), we should see a field of  about $\frac{\mu_0}{4 \pi}\frac{2 I}{r}$, so to see 2 µT, we’d need I = 0.5 * 2E-6 T * 0.01 m / (1e-7 T m/A) = 0.1 A, which is quite a reasonable value to produce from a battery or wall wart. With a 12v supply and a 22Ω resistor, I could provide 0.54A, except that the 22Ω resistor I have is only a 2w resistor and would get too hot trying to dissipate 6.5w.  Actually, I’m not really sure what the rating of the resistor is—it is 18mm long and 8mm in diameter, most likely a carbon resistor, and the only resistors that size I found online were 2W resistors. With a 5V supply, we’d get about 0.23A or 1.14w, and stay well below the 2w limit, while still getting a magnetic field that we could measure 1cm away (about 45 µT).  By using the dimensions of the MAG 3110 breakout board to set distances, we could measure fairly reliably at distances of 0.5 mm, 2mm, 6mm, and 8mm from the wire. (I’m not quite sure about the 0.5mm and 2mm—the breakout board+chip is 2.5mm, and the board alone is 1.5mm, but I don’t know where in the 1mm thick package the magnetometer sensor really is—we could use the 1/r dependence of the magnetic field strength to try to figure that out.)

So next week: string up a longish wire, add a 22Ω series resistor, a 5V power supply, an ammeter, and a switch, then measure the current and the magnetic field at various distances.  If we get that working, wind a solenoid, measure the field around it, and compare the measured field to a simulation.

## 2012 April 21

### Fitting a sphere

Filed under: Accelerometer,magnetometer,Robotics — gasstationwithoutpumps @ 21:19
Tags: , , , , ,

Today I had to write a program to fit a sphere to a bunch of points that were supposedly near the surface of a sphere, but were noisy and sampled in a very biased way.  Since this is obviously not a new problem, I started out doing web research.  but I didn’t look for fitting a sphere, but for fitting a circle, since that is a simpler related problem.

I found a lot of papers, including several review papers, on how to fit a circle to a bunch of points.  The “obvious” method is to  do a least-squares fit to minimize the distance between the points and the circle, minimizing $\sum_i (r- \sqrt{ (x_i-a)^2+(y_i-b)^2})^2$, where $r$ is the radius and $(a,b)$ is the center of the circle.  Unfortunately, that is a difficult problem to solve, and even numerical methods require a lot of iterations to get decent solutions.  What most people do is to change to a slightly different problem that optimizes a different fitness function.  For example, Kåsa’s method minimizes $\sum_i (r^2 - (x_i-a)^2 - (y_i-b)^2)^2$.

There is a very nice, but very formal, presentation of the methods in a paper by Vaughn Pratt from 1987: Direct Least-Squares Fitting of Algebraic Surfaces.  This paper introduced Pratt’s method, which was later slightly improved to make Taubin’s method. I did not read these original papers (other than skimming Pratt’s paper).  Kåsa’s paper (A curve fitting procedure and its error analysis. IEEE Trans. Inst. Meas. 25: 8–14) does not seem to be available on-line.  The IEEE digital library is missing the whole 1976 year.

I did find a recent paper that does careful error analysis of both the geometric approach and several of the algebraic approaches (including the most popular ones: Kåsa, Pratt, and Taubin):

Error analysis for circle fitting algorithms
Electronic Journal of Statistics
Vol. 3 (2009) 886–911 ISSN: 1935-7524 DOI: 10.1214/09-EJS419

This paper shows that Taubin’s method is theoretically superior to Pratt’s which is theoretically superior to Kåsa’s (having less essential bias), and gives a very weak example showing it is also tru empirically.  More interestingly, it also gives a “hyperaccurate” algorithm that has less bias even than Taubin’s method.  I did not read the error analysis, but I did read the description of their Hyper algorithm and the implementations of it that Chernov has on his website.

Since I needed Python code, not Matlab code, and I needed spheres rather than circles, I spent a few hours today reimplementing Chernov’s Hyperfit algorithm.  I noticed that the basis suggested by Pratt for spheres, $(x^2+y^2+z^2,x,y,z,1)$, was a simple modification of the one used in both Pratt’s paper and Chernov’s paper for circles, $(x^2+y^2,x,y,1)$.  I decided to generalize to $n$ dimensions, and use the Numpy package in Python for all the matrix stuff.  I hope I got the generalization right!

From starting to look for papers until getting the code working was about 6 hours, but I had lunch in there as well, so this felt like pretty speedy development.  I’ve released the code with a Creative Commons Attribution-ShareAlike 3.0 Unported License, and would welcome corrections and improvments to it.

Of course, after all this buildup, you are probably wondering why I needed to fit a sphere to points—that is not a common problem for a bioinformatician to have.  Well, it is for the robotics club, of course.  They’ve been having a lot of trouble with the magnetometer calibration and heading code, so we decided to try doing an external calibration of the magnetometer, which has an enormous arbitrary 3D offset.  By waving the magnetometer around in different orientations (which means tumbling the ROV once the magnetometer is installed), we can sample the magnetic field in many orientations, though far from uniformly.  The center of  the sphere fitted to the readings gives us the 3D offset for the magnetometer.

My son and I tested it out with Python code and Arduino code that he had written to get the data from the magnetometer to the laptop, and the magnetometer readings do seem to be nicely centered around (0,0,0) after we do the correction.  We’re still having trouble using the accelerometer to get a tilt correction to give us clean compass headings, but that is a problem for tomorrow morning, I think.

Next Page »