Kalman Filter with Hall Sensors and EMF Readings

We're going to implement a sneaky Kalman filter from a Bayesian perspective.

Inputs

As inputs we have hall sensors and the EMF voltage readings. We might also have 1 or more magnetic rotary encoders with absolute position (multiple encoders allow us to measure internal torque on the transmission, if we know the elastic constant of the transmission mechanism).

  • 3 hall sensors with binary state, they are ideally placed 120 degrees apart, but as we shall measure, they are not that precise. The hall sensors toggle when the magnetic field over the sensor crosses 0 and switches sign (with some hysterisis). The result is that we have 6 valid regions for the motor position. The crossover point between each region is known, and it is different if moving clockwise or counter-clockwise. The rotor also tends to rest within each of these sectors, however note that the resting position is aligned with the stator iron cores not the hall sensors. We can notice this effect in the oscilations of the EMF voltage magnitude.

  • EMF voltage readings, which are a noisy measurement of the rotor position. The EMF angle is unknown below a certain speed where the EMF voltage readout is near the least significant bit (LSB) of the ADC and therefore quite noisy. The threshold speed is surprisingly low, about 2% of the maximum speed. Above this speed we can use the EMF voltage to estimate the rotor position.

  • A magnetic rotary encoder with absolute position (like AS5600 with 10bit resolution). This is a very precise measurement of the rotor position, but it is not fast enough for the current control loop. However we can use it for absolute position tracking of the output, after gearing. It will allow us to measure backlash of the gears and possibly internal torque on the transmission if compared with the EMF voltage readings.

Assumptions / Probabilistic setup

We assume all our reference angles are gaussian distributed, with a known/calibrated mean and standard deviation.

  1. The motor is stationary at the start.
  2. The initial position is the center of the current hall sector.
  3. We know/measure the hall sector crossover points and variances for both rotation directions.
  4. We can measure the EMF angle and its variance using an exponential moving average (EMA).

Model

To model the a crossover transition between hall sectors, we will assume the motor position is sampled from a prior gaussian distribution with the mean and variance predicted in the Kalman step. The hall crossover point will also be sampled from the calibrated gaussian distribution of the hall sector crossover points. The transition should occur when the sampled motor position is greater than the sampled hall crossover point. Then we measure the whether the transition actually occurred and calculate the posterior distribution of the motor position. We'll use the posterior distribution as the measurement input to the Kalman update. Above the threshold speed, we can incorporate the EMF angle and its variance as a secondary input.

Bayesian Inference

There are some challenges implementing the Kalman filter given the high frequency of the EMF readings and the low frequency and binary nature of the hall sensor readings. To account for the low frequency hall sensor we could set the confidence to 0 when no hall transition is detected. But we can do better! The solution will also solve our second problem, to slowly bring the position estimate back to the center of the current hall sector when no hall transition is detected. What we'll do is to predict how likely it is that we should have detected a hall transition given our position statistics. If we should have detected a transition but didn't we will adjust the position estimate towards the center of the current hall sector. This is an approximation to the actual posterior distribution of the position statistics. We can visualize the actual posterior distribution in the plot below because we have enough computing power on any device that can run a browser.

We consider the probability of the motor position being greater than the hall sector crossover point, given the current position estimate and its variance. Google Gemini has blessed us with a clear and accurate description:

It sounds like the project involves a Bayesian inference problem where our data (or evidence) comes from a probabilistic comparison.

  • We have a parameter of interest, let's call it . Your prior belief about is described by a Gaussian probability density function (PDF) with mean and standard deviation , which we can denote as .

  • We don't observe directly. Instead, you perform an experiment or make an observation that involves comparing to another quantity, . This is also a random variable, drawn from a Gaussian distribution with mean and standard deviation .

  • The "measurement" or data we obtain is the outcome of this comparison:

    1. Either (a sample from our prior is greater than a sample from the second Gaussian).
    2. Or (a sample from our prior is less than a sample from the second Gaussian).
  • We are constructing the likelihood function for these two possible outcomes, given a specific value of :

    • The likelihood of observing "" given is . This is precisely the cumulative distribution function (CDF) of evaluated at , let's call this .
    • The likelihood of observing "" given is . This is .
  • We are then using Bayes' theorem to find the posterior probability distribution for under each of these two data scenarios:

    1. If our measurement is "":
    2. If our measurement is "":

The "problem" can be described using terms like:

  • Bayesian inference with a probabilistic comparative likelihood: The likelihood is not based on a direct measurement of with some noise, but on the probability of satisfying an inequality with respect to another random variable.
  • Updating with binary outcome from stochastic comparison: Our data is binary (greater than / less than), and this outcome depends on a comparison where one of the terms () is stochastic.
  • A model with a Probit-type likelihood: The use of a Gaussian CDF as the likelihood function is characteristic of Probit models, which are often used for binary outcomes that depend on an underlying latent variable exceeding a threshold. Here, is like the latent variable, and is a stochastic threshold.

In essence, we're updating our beliefs about based on whether it "passed" or "failed" a test where the benchmark () was itself uncertain. The two posterior distributions represent our updated beliefs about in these two distinct scenarios.

The absence of evidence is evidence of the absence

Enough with the maths, the plot below shows how we should update our belief about the motor position every motor cycle. First we predict the our prior for the motor position then we check if a hall transition was detected.

  • If the detection matches our prediction, we gain little information, but confirm our prior is correct, thus the new estimate remains the same as the initial prediction.
  • When the prediction is wrong, we update the position for 2 cases:
    1. If the hall transition was detected when we didn't predict it, the motor must have sped up towards the detected sector. The new position is close to the trigger point (the position between 2 sectors).
    2. A transition was predicted but none was detected, then the motor must have slowed down. The updated position moves slightly towards the center of the current sector.

Combining Normal Distributions

Back to math, now that we have a corrected estimate of the motor position we can update our prior belief to incorporate as much information that we have from our measurement (or lack of). According to Bayes' rule the likelihood of the real position is proportional to our prior PDF multiplied by the measurment PDF. There is a special case when both PDFs are Gaussian, the result is also a Gaussian distribution. This special case is the Kalman filter update step derived from first principles. The mathematics of the Kalman gain work out to the same value as the weighting factor we use to combine the two PDFs. The mean and variance of the combined reading are vizualized below [1]:

We also consider the case where the confidence in each source is weighted by a factor . The mean can be calculated by iteratively applying to above formula formula for times the first source and times the second source. The variance however would shrink more than desired due to increased confidence in multiple measurements. To adjust this, we need to scale the weights to the interval [0, 1], we'll interpret this as confidence in the measurement. The mean and variance of the combined reading can then be calculated as follows:

References