The use of the Kalman Filter Algorithm is common in robotics as a control method. More importantly, it can be used to control spacecraft - and who doesn’t love a good spaceship? Preferably one with an invisibility field… but we won’t be discussing spaceships, or invisibility fields. For this case we will imagine a humble cart on a track. The cart can control its movement by applying a force. The cart will also have the ability to detect it’s distance along the track using a lazer.

It is simple to understand a linear motion model. For every time step, let’s call the time step , we move some distance, let’s call the distance , across the track. We will imagine that this track is the axis. The amount of distance () that the cart moves in a certain amount of time () is its velocity. We will say the the robots instantaneous change in position, the derivative that results in velocity, is represented as . If we remember the form definition of the derivative we see:

We can see that any movement that our cart makes along our track is given by some change in time multiplied by the instantaneous change in position over that time, so . If we recall from the great teacher Newton, we can write an equation of motion as follows:

Here the value represents the starting position of the cart along the track. In the simplest cast, the starting position can be . We can also see that the term is the acceleration term . I will be helpful to again remember . We can rearrange this to see , thus:

We can then view the velocity in the same way. With the term the initial velocity of the system:

Next, we want to view the system in a more compact way. To do this we see that the cart has a state at any given time . In other words, at any given time the state of the cart can be described by its position and velocity. We can represent this state with the existing functions for and :

And if we continue to expand this equation we get…

Now We apply some basic linear algebra:

Thus,

It is useful to think of our system in this way. It allows us to have a single force acting upon the cart and allows us to derive its state given an initial and with an arbitrary time step .

The last step is to realize that, in practice, nothing is perfect. To make our motion model more practical we must assume some error in our ability to move in a purely deterministic way. If we could, after all, move in a perfectly deterministic way, then there would be no need for the Kalman filter or complex computational physics. The world would be deterministic and boring. But reality is stochastic, and we want our model to reflect reality. This is discussed more in section 3.2 below.

To start this, we characterize the true accuracy of our motion. For this case we will assume a normal distribution of error - our friend the bell curve. This is represented by the equation:

We imagine that our cart, when it moves, has some small error in movement that is within the bell curve above. Let’s call a randomly sampled value from this bell curve . Each time we move we are effectively selecting a random value of error. To represent this in our motion equations, we simply add the terms (simply errors in position and velocity):

The cart has a laser that is shot to a wall and it receives a distance to that wall.
For convenience we allow this distance to align perfectly with the axis of movement.
We will call the measurement returned by the lazer and, because we are getting the
distance to the wall at the start of the track, we

As you may have suspected, it is not that simple. But, don’t worry, it still is pretty simple. Instead of considering only the position value we want to consider the entire state of the cart in the sensor model. To do this we will have to use the state vector . We do this because, in some models, we may have sensor that detect velocity rather than position. Or, we may have an accelerometer that detects acceleration rather than position or velocity. To make our model general, so that we can truly understand the kalman filter, we use the vector for with :

notice that the matrix results in a scalar. We are using this to ignore the velocity component of the state.

Let’s look back at our motion model. Notice that it is of the form:

Then we see that our sensor model is of the form:

This gives us the matrices:

Next we will want to view and with some matrix representation.
To do this we will find their covariance matrices. Let’s let Remember that covariance
is the measurement of the

See that, for a finite set of random numbers , the variance with expected value is:

We can actually do the same thing for two variables, let’s say and . This is what we call the covariance, and for a finite set of random numbers and we get the equation:

Notice that we can also find the variance of if we just find its covariance with itself. Covariance would just give us the variance of .

The Covariance Matrix is a matrix representation of the calculated covariances of all variables in the system. It is a square, symmetric matrix, which contains the possible covariances. If the matrix is diagonal, then the variables used are independent. If it is not, then then the variables outside of the diagonal are not independent.

In practice, we would discover this experimentally using the steps above, but for the purposes of this example I will assume that the motion and sensing model have reasonably compact covariance matrices that are diagonal. The Kalman filter is optimal if the covariance matrices are known perfectly, but it will still give good results if diagonal estimates of the covariance matrices are used. So, I will give the covariance matrix and the covariance matrix :

This is me assuming that we have a decent model for our movement model and that our the sensor is relatively inaccurate in comparison. Notice that our sensor model’s a covariance matrix has higher variance for its entries than the motion model’s does.

The main idea with the Kalman filter is to combine the predicted measurement, predicted motion, and real measurement. Then, adjust the state so that so that the better model or measurements are trusted more. This becomes more clear when the following equation is considered:

Here the term is the estimated state of the cart system. The term is generated from or motion model, is the measured distance and is the predicted measurement of distance. Notice that all of these terms would be known. The term is known as the Kalman Gain.

Note, that if the values are exact, there is no Kalman Gain. This is intuitive,
because if our movement was perfectly modeled then we would not need any adjustment.
However, if this was not the case - and it usually isn’t - then there will be some
optimal Kalman gain generated that

First, Recall the matrices that we are using:

The Kalman Filter Algorithm also requires two inputs. These inputs are the initial state and the covariance . The covariance is estimated to start and, for our purposes, we will treat it as the identity matrix and let the algorithm modify it. Remember the equation for a gaussian distribution: , note that this requires a mean and variance as input. It just so happens that the state and the covariance are these values! This is because the Kalman filter algorithm is - at its core - a way to modify gaussian distributions. These gaussian distributions are modified based on what estimates / measurements (which are also gaussian distributions) can be trusted more. The measure for trust is , the Kalman gain, explained in the section above.

The first step of the Kalman Filter algorithm is to generate the prediction of the state, this is done with our motion model. In this step, we take in the initial state . This also relies on the force of the system, which we can assume to be an arbitrary constant for simplicity:

The second equation takes the covariance that is given as an input and we attempt to predict what the new covariance should be. This step takes our previous covariance and estimates what our new one should be, we call this state .

The third step calculates the Kalman Gain for the current iteration of the system.
Notice that the larger the variance in the sensor measurements , the
small the Kalman Gain will be. This equation for the Kalman Gain

Just as before (in 4.1), we use the Kalman Gain to update the state prediction. Here the term is the newest estimated state. The term is the state previously estimated by the motion model. The term is the actual measurement make by the sensor and the value is the measurement predicted by the sensor model. This is done as follows:

The final step is to update covariance to represent the new estimation. Notice that if we had made a perfect prediction (which never happens) we would just have the identity matrix times the predicted covariance. Otherwise, the variance is updated based on the Kalman Gain:

All together this is the Kalman Filter Algorithm: br<> br<> Predict: Update:

The output of the Kalman filter is another state and another covariance , which are meant to be iteratively updated. This makes sense, because we want to improve the certainty of our measurements and movements. These measurements and movements are represented by gaussians, which are functions mean and variance.

Assume the initial conditions, constants, and above variables:

Preform 2 iterations of the Kalman Filter Algorithm