~hp/chimaera

#Firmware

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 (1−distance

  • 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 = c0⋅Bn3+c1⋅Bn+c2⋅Bn(1)

fit

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 =(BT⋅B)−1⋅BT⋅y(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+S⋅B(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+(Vs−Vr)⋅A (2)Vo = Vr+(Vq−Vr+S⋅B)⋅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+(Vq−Vr)⋅A(3)Vq = Voq−Vr A +Vr (4)Vo = Voq+A⋅S⋅B (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=A⋅S
  • 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+AS⋅B1 (2) Vo2 = Voq+AS⋅B2 (3) Vo1Vo2 = Voq+AS⋅B1Voq+AS⋅B2 (4) Vo1⋅(Voq+AS⋅B2) = Vo2⋅(Voq+AS⋅B1) (5)Vo1⋅Voq+Vo1⋅AS⋅B2 = Vo2⋅Voq+Vo2⋅AS⋅B1(6)AS⋅(Vo1⋅B2−Vo2⋅B1) = Voq⋅(Vo2−Vo1) (7) AS = Voq⋅(Vo2−Vo1)Vo1⋅B2−Vo2⋅B1 (8) AS = Voq⋅(Vo2−Vo1)|B|⋅(−Vo1−Vo2) (9) AS = Voq⋅(Vo1−Vo2)|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 = c0⋅Bn3+c1⋅Bn+c2⋅Bn(1)Bn = f−1(yn) (2)Bn = B−BminBmax−Bmin (3) B = Bn⋅(Bmax−Bmin)+Bmin(4)

Let us get one step further. In the end we want vicinity to be mapped continuously from [0−1]

  • 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 = Vo−Voq AS (1) B = (Vo−Voq)⋅AS−1 (2)N = Bmax−Bmin (3)Bn = (B−Bmin)⋅N−1 (4)Bn = ((Vo−Voq)⋅AS−1−Bmin)⋅N−1 (5)Bn = (Vo−Voq)⋅AS−1⋅N−1−Bmin⋅N−1(6) U = AS−1⋅N−1 (7)W = Bmin⋅N−1 (8)Bn = (Vo−Voq)⋅U−W (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) = A⋅x+B (1)yn(x) = A⋅x2+B⋅x+C (2)yn(x) = A⋅x3+B⋅x2+C⋅x+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.

#Summary

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.

About this wiki

commit efcd31785f5abcb00e74f650c419e9e4e8c052b4
Author: Hanspeter Portner <dev@open-music-kontrollers.ch>
Date:   2023-03-19T11:16:31+01:00

Decrease header depths by one
Clone this wiki
https://git.open-music-kontrollers.ch/~hp/chimaera_man (read-only)
git@git.open-music-kontrollers.ch:~hp/chimaera_man (read/write)