A Magnetometer Collides with Reality

So the magnetometer didn’t work.

The week of the competition, we were finally able to integrate the robots. I took a lovely working magnetometer, and spun it in a robot for the first time. What I got was useless noise, noting several revolutions per revolution.

As noted in my event report video, at the midnight hour we converted the magnetometer’s analog circuitry to read from an IR photodiode instead. This allowed us to at least compete

Most things don’t survive their first contact with reality, and we’re no exception. In this post I’m going break down exactly what went wrong, the steps we took to fix it, and where we ended up.

Motor Noise

There is also a particular noise component, but further testing indicated that it was introduced by the co-located brushed motor in my test rig. So I don’t expect it to be present in the arena.

- Spencer Allen, A Rotating Coil Magnetometer for Meltybrains

I was completely wrong about how much affect motor noise had. The bulk of my noise came from exactly this, my drive motor magnets were being picked up. Lets actually do some napkin math to investigate this.

Each drive motor has 14 magnets in the rotor. I don’t have an exact field measurement, but similarly sized magnets are rated at 0.225T, so we will use this. I originally discounted the effects of these magnets because of the distance from the coil, that being around 130mm center-to-center. According to this handy calculator from K&J Magnetics, we can expect a field strength of 10.84µT. As a reminder, we are expecting 50µT of earth field, so that doesn’t seem too bad. But the other thing to remember is the coil picks up changing fields, not static ones. The speed at which the field changes matters too.

The wheels are 2” diameter, and are 3” from the center of rotation.

$$p_\textrm{wheel} = \pi d = 159.6mm$$ $$p_\textrm{at wheel} = 2\pi r = 478.8mm$$ $$\textrm{Wheel turns per revolution} = \frac{p_\textrm{at wheel}}{p_\textrm{at wheel}} = 3$$

So for every revolution, the wheel turns three times.

To explain from here I’m referencing the paper “Filtering of Magnetic Noise Induced in Magnetometers by Motors of Micro-Rotary” [Unwin, 2015] They explain that brushless outrunner motors generate three categories of noise:

  1. Noise from the alternating magnet fields, at 14X the motor rotation speed

  2. Noise from uneven demagnetization of the magnets over time, at roughly 35X the motor rotation speed

  3. Noise from some magnets being stronger than others, at 1X the motor rotation speed.

The last bullet is bolded because it is the most dangerous to us. At only 3X the frequency of our signal of interest, it is nearly inseparable. To add injury to injury, the paper referenced above identifies this noise component as the strongest of the three.

Impact Analysis

In order to demonstrate the effect of this noise, lets run the numbers on source 2 from above. Source 2 is a little easier to calculate for, but keep in mind it’s not as a strongly interfering as source 3!

With 14 magnets in the rotor, the field changes 7 times per wheel turn. So in total the field change has a frequency 21 times the bot rotation speed. Lets plug these numbers into the equation from the previous part:

$$\varepsilon=-NAB_E\omega\cos(\theta)$$

$$\varepsilon=-(3000)(0.025m \cdot 0.038m)(0.00001084T)(157 \cdot 21 rad/s)\cos(42 \cdot \theta)$$

$$\varepsilon=-0.2037\cos(21*\theta)$$

Wowza, 200mV is 10X the signal strength of our actual signal! Though, slight wrinkle, it’s also at 42X the frequency of our actual signal. That makes the frequency $$25Hz \cdot 42Hz = 1050Hz$$, which is above the cutoff frequency of our filter. So to really understand what we’re up against, we need to evaluate the performance of our filter at that frequency.

Low Pass Filter Performance

According to our simulation, our filter attenuates by around 34.5dB at 1KHz, or 1 over 53. So On the output of our filter, our signal is 3.843mV in amplitude. That is ~5.7X smaller than our signal of interest, but still not great.

It’s also useful to evaluate at a different frequency, since our filter attenuation is logarithmic but our signal is linear. Lets take half the speed above, or 1250Hz

$$\varepsilon_{earth}=-(3000)(0.025m \cdot 0.038m)(0.00005T)(78.5 rad/s)\cos(\theta)$$

$$\varepsilon_{earth}=-0.011\cos(\theta)$$

$$\varepsilon_{magnet}=-(3000)(0.025m \cdot 0.038m)(0.00001084T)(78.5 \cdot 21 rad/s)\cos(21\theta)$$

$$\varepsilon_{magnet}=-0.0509\cos(21\theta)$$

The attenuation at 525Hz is roughly 26.5db, or one over 21.1.

$$V_{magnet}=-0.0024\cos(21\theta)$$

Sure enough, at this frequency, our noise component is now only 4.6 times less than our principle signal.

This is all pretty bad, so no wonder we were having trouble pulling the signal out. The noise is too close in frequency to be filtered, and it’s too strong to ignore.

Hard Consequences

As a result, the comparator was cut. There’s no way it could deal with the noise, no matter how much hysteresis was added. Instead, we attempted to sample analog voltages and filter it further digitally. This didn’t fare much better unfortunately.

Plot of raw magnetometer data

We gazed upon this plot, and despaired. We tried a couple filtering options, but in the end we were doomed. Realistically, we weren’t going to do much better than the analog filters. So this was the point we pivoted to the IR receiver.

The bright side here is that our analog front end could be easily re-purposed as a transimpedance filter for infrared. We got it running in record time.

Looking Forward

That all said, I wasn’t ready to give up on magnetometers.

I was recommended that paper after the event, and so could not benefit from it in time. It merely confirmed our experience, and definitively proved that simple filters weren’t going to work. With the strongest noise component only 3X the principle component, there’s basically only one technique that can separate them. More on that next time.

The other thing we learned is that while our rotating-coil magnetometer proved incredibly high-performance, this was ultimately wasted. It showed us in incredible detail how noisy we were, but math means that it didn’t make it any easier to pull out the signal we were looking for. At the same time, we identified a COTS magnetometer that had good enough specs. The BMM150 has sensitivity good for earth field sensing, and a sample rate well over Nyquist at 300Hz.

All that to say, goodby to the rotating-coil magnetometer. I learned a lot, it succeeded in its goal, but it’s too expensive for it’s worth.