Open Music Kontrollers


So we have a lot of sensors and a micro controller. How do we get to our event signals? That is what this page is all about. One component of our event signal for a given magnetic source is its distance from the sensor. We will start right there and firstly look at the relationship of magnetic field and distance.

Distance - magnetic field - relationship

The strength of the magnetic field of a permanent magnet decreases with increasing distance from the sensor. This decrease is non-linear and the equations involved are non-trivial, it depends on the material and form of the permanent magnet. As a general rule, a cubic relationship is assumed. Nowadays, magnetic fields can be approximated with finite-element calculations, this however is overkill and not at all possible in realtime on a microcontroller. To approximate the distance of a given magnet from the sensor based on the sensed magnetic field flux at the latter in realtime it is therefore best to do some non-linear curve fitting for the given pair of sensor and magnet before-hand. In realtime, the fitted distance-function is simply applied to the sensed magnetic field flux.

Instead of distance, we use the concept of vicinity (1distance ) because in the end we are interested in how near we are to the sensors and not how far away from them. The following simple function to get normalized vicinity yn from the normalized magnetic flux Bn has been shown to be adequate for the type of sensors and magnets (Neodymium) we use:

yn = c0Bn3+c1Bn+c2Bn(1)


Measured and least-squares estimate between magnetic field at and vicinity to the sensor.

The relationship can vary considerably between different types of permanent magnets, the Chimaera therefore can be calibrated on-the-fly to different types of permanent magnets. This is done with a five point least squares fit and leads to an analytical solution with the help of some linear algebra.

B = 0 0 0b13b1b1b23b2b2b33b3b3 1 1 1 (1)y = 0y1y2y31 (2)c = c0c1c2 =(BTB)1BTy(3)

Sensor calibration

Get familiar first with the hardware of the Chimaera sensor unit, it will help a lot to understand all the following.

Although we now know the relationship between magnetic flux and vicinity we cannot yet approximate the distance of a magnet from its sensor. From the sensor unit we get a voltage which is linearly related to the magnetic flux the currently active sensor is in. However, the sensors do not all have the exactly same sensitivity and quiescent output, their values scatter over a given range. We have to take care of this firstly.

An ideal linear hall-effect sensor outputs a voltage Vs dependent on its sensitivity S and magnetic flux B relative to its quiescent voltage output Vq .

Vs = Vq+SB(1)

Both Vq and S vary between sensors. To make the voltage output of sensors comparable to each other (which is a prerequisite to interpolate between them later on), we need to calibrate them and apply the calibration data in a normalization step. But before we can do that we also have to consider the amplification of the voltage signal at the sensor unit.

The voltage output Vo after the amplification is dependent on the sensor voltage Vs , the reference voltage Vr at the OpAmp, and the amplification factor A of the OpAmp.

A = 1+RV1 R1 (1)Vo = Vr+(VsVr)A (2)Vo = Vr+(VqVr+SB)A(3)

Vo is what we measure with the analog-to-digital converters at the microcontroller. This is the only thing we can measure directly. We now have to apply some simple math to get to one value we want in the end: the relative vicinity normalized relative to the whole sensor array.

In the case of no magnetic field, we can measure the amplified quiescent voltage Voq of each sensor from which we can solve for Vq and rewrite Vo .

B = 0 (1)Vs = Vq (2)Voq = Vr+(VqVr)A(3)Vq = VoqVr A +Vr (4)Vo = Voq+ASB (5)

Vo we can measure, Voq we can measure, too. B we want to know finally. The next therefore is to decipher A and S . For this we define a new single parameter AS=AS . With a known B we could already calculate the latter, but we want to calculate it over both polarization domains concurrently to be more robust. We therefore look at the output voltages for an arbitrary magnetic field strength, once south polarized B1 and once north polarized B2 .

B2 = B1 (1) Vo1 = Voq+ASB1 (2) Vo2 = Voq+ASB2 (3) Vo1Vo2 = Voq+ASB1Voq+ASB2 (4) Vo1(Voq+ASB2) = Vo2(Voq+ASB1) (5)Vo1Voq+Vo1ASB2 = Vo2Voq+Vo2ASB1(6)AS(Vo1B2Vo2B1) = Voq(Vo2Vo1) (7) AS = Voq(Vo2Vo1)Vo1B2Vo2B1 (8) AS = Voq(Vo2Vo1)|B|(Vo1Vo2) (9) AS = Voq(Vo1Vo2)|B|(Vo1+Vo2) (10)

So, with measured Voq at B=0 and Vo1,Vo2 at a arbitrary |B| , we get to AS . But how do we know |B| ? It’s quite simple, we can numerically approximate it with the inverse of the magnetic-field-distance relationship, because vicinity is something we can easily measure, too. By doing so, we also have to define what the normalized magnetic flux Bn actually is.

yn = c0Bn3+c1Bn+c2Bn(1)Bn = f1(yn) (2)Bn = BBminBmaxBmin (3) B = Bn(BmaxBmin)+Bmin(4)

Let us get one step further. In the end we want vicinity to be mapped continuously from [01] . y=0 will be at a threshold magnetic flux Bmin , y=1 in turn at the maximal to expect magnetic flux Bmax . Bn will be the normalized measured magnetic flux B between the two extremes and depends on the three constants Voq , U and W :

B = VoVoq AS (1) B = (VoVoq)AS1 (2)N = BmaxBmin (3)Bn = (BBmin)N1 (4)Bn = ((VoVoq)AS1Bmin)N1 (5)Bn = (VoVoq)AS1N1BminN1(6) U = AS1N1 (7)W = BminN1 (8)Bn = (VoVoq)UW (9)

Voq and U are different for each sensor (so they are actually two arrays of constants each), W is the same for the whole sensor array. By calibrating them once, we get from the sensor unit voltage output Vo directly to the normalized magnetic field strength Bn with only 3 mathematical operations (efficiency is important as we are on a microcontroller, e.g. we have to cope with limited computation power).

From the normalized magnetic flux Bn we then get to the vicinity y . As those vicinities are comparable over the whole sensor array, we are now ready to interpolate the position x of the magnetic source along the sensor array axis.

Blob event handling

With a blob we refer to a recognized magnetic field source (e.g. permanent magnet) that is to be tracked over space and time. Blob event handling can be divided into several stages: decipher polarity, narrowing down the area of interest on the sensor array, actual blob detection, interpolation of blob position, group association and blob tracking.

Magnetic flux polarity

First thing that is to be done is to discriminate between the polarities of the potentially present magnetic field sources. For each sensor value, we therefore subtract the previously calibrated quiescent value, so we get the difference of the present raw sensor value from its quiescent value. The sign of this difference gives us the polarity of the magnetic source we are encountering here, e.g. south and north polarity. The absolute value of the difference gives us the strength of the magnetic field (which is to be normalized with previously calibrated factors in a later step).

Area of interest

After each scan of the whole sensor array, areas of interest are marked on the array. These are regions, where the sensor value exceeds the threshold magnetic flux Bmin . The rest of the sensors are not of interest for the further steps. We do not apply the distance-magnetic-field relationship over all sensors at this step, as it would be computationally too costly. During calibration, for each sensor we precalculated its raw value Vo,min corresponding to threshold magnetic flux Bmin . This then can be efficiently checked against for each sensor at each update step to find out whether the threshold has been exceeded or not.

Blob detection

In the next step, we search for peaks in the areas of interest. A peak will correspond to an individual magnetic field source and is simply defined as a sensor with an absolute value which is greater than the values of a given number of adjacent sensors in the same area of interest. We save all found peaks along the sensor array for the blob’s positional interpolation in the next step.

Blob interpolation

As always more than one sensor is excited by an individual magnetic source, we can decipher its real position by interpolation around the peak sensor. We therefore fit curves yn(x) over a varying number of sensor values around the peak and find the maximum (X,Y) by finding the cross point of the curve’s derivative. Only at this step we convert the sensor values into normalized vicinities based on calibration data. For efficiency reasons, we use a precalculated lookup table for the distance-magnetic-field relationship, because the cube root and square root functions would be too costly.

yn(X) = Y(1)yn(X) x = 0(2)

We can use varying numbers of adjacent sensors to the peak and fit linear, quadratic, cubic or spline functions through the values. The coordinates (X,Y) of the maximum will be the current position of the blob on the sensor array.

yn(x) = Ax+B (1)yn(x) = Ax2+Bx+C (2)yn(x) = Ax3+Bx2+Cx+D(3)

interpolation order 0

Vicinities of four adjacent sensors and no fit (order 0), e.g. the highest value is its own fit.

interpolation order 1

Vicinities of four adjacent sensors and a linear fit (order 1) between the two highest values.

interpolation order 2

Vicinities of four adjacent sensors and a quadratic, aka hyperbolic fit (order 2) betwwen the three highest values.

interpolation order 3

Vicinities of four adjacent sensors and a cubic fit with Catmull-Rom splines (order 3) between all four values. Only the spline segment between the highest values is shown.

interpolation order 3

Vicinities of four adjacent sensors and a fit with a Lagrange polynomial (order 3) between all four values.

Group association

For convenience, we have introduced a concept of groups. Each blob has not only a given position on the sensor array, but is also affiliated to a group. A group is defined by a range (xmin..xmax ) on the sensor array and magnetic field flux polarity (south, north or both). Only blobs with a position in the range of - and a polarity corresponding to a given group will be associated with the latter.

Groups can overlap, e.g. there can be two groups for the same range with differing polarities. A blob can only be part of one group at a time though and will be affiliated to the first matching one.

Blob tracking

Up to now, everything we have done was based on the current state of the sensor array. However, to track blobs over time, there needs to be a means to associate identified blobs in the current iteration to blobs found in previous iterations to decipher whether new blobs have appeared or old ones have disappeared.

To link current blobs to previous blobs they are matched by position on the sensor array. Each new blob gets a unique ID, which will persist over time as long as the blob can be linked to itself in a previous iteration step, until the blob disappears. In the special case when a blob can be linked but has exited its previous group, it will be regarded as a new one, too.

Out of the blob tracking, we get a stream of event signals: When a new blob has appeared, an on event is triggered. When a blob could be linked to a previous iteration step, a set event is triggered to update the blobs position. When a blob has disappeared, an off event is triggered. All event signals will be triggered with complete set of current parameters: position (X,Y) , polarity (south or north) and group association.


So, what the firmware does in the end is: converting a raw array of sampled sensor values into a stream of blob event signals. The event signals now can be translated into a given transport protocol and sent over the network to drive other software or hardware. This is elaborated upon in a dedicated section, the usage.

Last update - 15 Jul 2019

Copyright © 2014-2019, Hanspeter Portner, Open Music Kontrollers, cc-by-sa 4.0.