A Simple Kalman Filter in Practice - Part 1

The Kalman Filter

There are a lot of great resources online that go into to detail to explain the Kalman Filter. In laymans terms, it’s a method that balances the weighting of new measurements and predictions based on some model of the system, given some knowledge about uncertainty in the measurements and uncertainty in the model. If you’ve ever taken a weighted average, you can intuitively think about them in similarly ways.

For more technical information, there are some great explanations from Steve Brunton, Michel van Biezen, and Alex Becker.

Using the Kalman Filter with a DC Motor

For the RC Truck Project, I’ve been slowing adding some engineering “improvements” as a fun way to explore some fun engineering topics. The DC motor with an IR optical, rotary encoder is a very approachable project to apply the Kalman Filter. DC motors are known and loved for their very linear behavior, which is great for the Kalman filter.

Modeling The Noise

The Set Up

Hardware

In order to run these experiments, I made a small bench top test stand to hold the motor, a bracket for the encoder sensor, and the tick-wheel.

motor_encoder.jpg

Electronics

The control set up is also fairly straight-forward and built from some off the shelf parts. The core is a Raspberry Pi 4, running with an Adrafruit PWM hat. The motor is a common, brushed, 35t motor used for RC rock crawlers. The PWM hardware is used to control the input signal to a standard RC motor electronic speed controller (ESC). In order to reduce the variation due to changes in battery level, the ESC is connected to a regulated power supply fixed at 8 volts.

ee_setup.png

Software

The software set up is pretty simple and using the existing libraries from Adafruit and pigpio. I wrote a small class to encapsulate each experiment. The class attributes include the profile of throttle values to sweep. The class uses the scipy stats package to calculate a basic linear regression results. I can then generate a number of test cases parameterized by the number of throttle sweeps to run, min throttle, max throttle, and number of steady state measurements to take.

Initial Model

In order to check some assumptions about the response of the motor and the motor controller, I developed a basic linear model by setting the throttle to a given value, allowing the motor to stabilize any transient dynamics and settle on a steady-state value, then took the average of several readings. A quick look reveals the the motor appears to behave linearly, at least between 10% and 50% throttle.

rpm = throttle * 30,655 - 1,981

rpm = throttle * 30,655 - 1,981

As one might expect, the motor isn’t perfectly linear and the motor ESC has it’s own peculiarities. As can be seen when sweeping the throttle from 0% to 100%, there is some deadband toward the beginning, and the motor speed saturates at the no-load max, commonly referred to as omega no load.

motor_saturation.png

When sweeping from 10% to 20% in 0.25% increments, it’s quite clear that there are some precision limits in the ESC controller itself. This and the motor speed saturation are interesting to note, but can be ignored as long as the test runs in the linear range and over a broad enough set of control inputs.

steps.png

Process Noise

The best way that I think about process noise is a quantification of the uncertainty in the model. If the model were perfect, then we wouldn’t need to consider any measurements. These uncertainties can be from non-linearities, hysteresis, deadbands, and other real world imperfections. A simple example of this could be the response of the motor or motor controller changing with temperature. My model currently ignores any temperature effects. Other process uncertainties can come from random disturbances on the system.

In order to make some estimate of the process noise, I set up an experiment to run the same throttle sweep described in creating the initial model. However, I now sweep the throttle up and down and repeat several times. To get an understanding of the “process noise” I simply look at the mean slope and intercept as well as the standard deviation from all the runs. Now I have some sort of description for the process noise in the model.

DC Motor Model: rpm as a function of motor throttlerpm = throttle * 30,848 - 1,629 slope: 30,848 (+/- 120.9)intercept: -1,629 (+/- 93.2)

DC Motor Model: rpm as a function of motor throttle

rpm = throttle * 30,848 - 1,629

slope: 30,848 (+/- 120.9)

intercept: -1,629 (+/- 93.2)

Measurement Noise

The measurement noise is a bit more direct to think about. Anyone who has looked at the time series output of nearly any digital sensor has observed the continuous small changes in the measurements, even if the quantity being measured is assumed to be constant. In order for this measurement noise quantification to be rigorous, the input to the motor should be very precisely controlled and understood. In other words, a better method than what I’ve done here would be to control the motor voltage with a high precision power supply and control for power noise. For the time being, I’m doing something a bit simpler.

In an obvious parallel to the process noise measurements, here I sweep across the throttle control values, but as opposed to looking at the variance in the linear motor model, I quantify the variance of the steady state values for each throttle step. In other words as opposed to taking all measurements at 25% throttle and looking at the variance, I’ve looked at the variance per throttle value and per run. In principle, the variance across runs shouldn’t change significantly, assuming that true steady-state values are being measured, the input control is fixed, and sufficient samples are taken.

Given this measurement set up, the measurement noise can now be quantified as a function of the input. This is really handy since the Kalman Filter is designed to accept either a fixed or dynamic input for the noise. Cool!

Motor Speed Variance as a function of throttle

Motor Speed Variance as a function of throttle

There are a couple of interesting observations in the plot above. First is that in general, the variance in the measurements seems to increase with motor speed. The plot above says as a function of throttle, but these are steady-state measurements and thus throttle and motor speed are interchangeable.

The second interesting observation is that the

More or Fewer Ticks?

One might assume that more ticks is better. For this project, I’ve not assumed that as a foregone conclusion. This is not out of a haute academic first principles reasoning, but rather another excuse for more nerdy tomfoolery. As such, I created two encoders, one with 10 ticks and one with 20 ticks. . Here are the results compared to the original experiment.

ticks_10v20.png

The results of the above plot seem to indicate that fewer ticks per revolution for a constant sampling period results in lower variance. That doesn’t really fit to my intuition, but I don’t have a good explanation yet for whats going on. It could have to do with the limits of the Linux OS on the Raspberry pi to count ticks or perhaps the precision of the python time module used for computing the RPM. I’ll have to spend some more time thinking about it and come back to it later.

The Implementation & Results

Now that I have the system crudely modeled and some idea about the noise, it’s time to implement the Kalman Filter! There are countless Kalman Filter libraries available, but I decided to roll my own in order to get a better intuition for the famed filter.

Open Loop Results

Looking at a few types of inputs and the response of the Kalman Filter, it seems to do a decent job. Sweet!

Kalman Filter results with step inputs

Kalman Filter results with step inputs

Future Work

This was a pretty basic start. Given the generally clean and precise response of the motor and generally low noise in the system, the Kalman Filter doesn’t seem to have a big impact. Where the filter has a bigger impact is when the gear box for the RC truck is added.The readings there are much noisier and the filter is much more useful. In fact for the motor alone, the variance in the model (process noise) is much, much higher than the measurement noise, despite both being low. The usefulness of the filter here is a bit questionable. Currently the process noise and measurement noise are fixed. From the data here, these can be improved as well.

In short, here are things to look forward to in the future:

  • Implementing the noise and process models

  • Applying the filter to a noisier system

  • Seeing how this work can improve the speed control of the RC Truck

Previous
Previous

Work from Home - Ergonomic Improvements

Next
Next

3D Printed Springs - Part 1