ECE4160/5160-MAE 4190/5190: Fast Robots course, offered at Cornell University in Spring 2024
This project is maintained by FastRobotsCornell
The objective of Lab 7 is to implement a Kalman Filter, which will help you execute the behavior you did in Lab 5 faster. The goal now is to use the Kalman Filter to supplement your slowly sampled ToF values, such that you can speed towards the wall as fast as possible, then either stop 1ft from the wall or turn within 2ft.
We will have a few setups in and just outside of the labs with crash-pillows mounted along the wall to limit damages. If you practice at home, be sure to do the same!
To build the state space model for your system, you will need to estimate the drag and momentum terms for your A and B matrices. Here, we will do this using a step response. Drive the car towards a wall at a constant imput motor speed while logging motor input values and ToF sensor output.
Compute the A and B matrix given the terms you found above, and discretize your matrices. Be sure to note the sampling time in your write-up.
Ad = np.eye(n) + Delta_T * A //n is the dimension of your state space
Bd = Delta_t * B
Initialize your state vector, x, e.g. like this: x = np.array([[-TOF[0]],[0]])
sig_u=np.array([[sigma_1**2,0],[0,sigma_2**2]]) //We assume uncorrelated noise, and therefore a diagonal matrix works.
sig_z=np.array([[sigma_3**2]])
def kf(mu,sigma,u,y):
mu_p = A.dot(mu) + B.dot(u)
sigma_p = A.dot(sigma.dot(A.transpose())) + Sigma_u
sigma_m = C.dot(sigma_p.dot(C.transpose())) + Sigma_z
kkf_gain = sigma_p.dot(C.transpose().dot(np.linalg.inv(sigma_m)))
y_m = y-C.dot(mu_p)
mu = mu_p + kkf_gain.dot(y_m)
sigma=(np.eye(2)-kkf_gain.dot(C)).dot(sigma_p)
return mu,sigma
Run your Kalman filter at a faster frequency than the ToF readings. In between readings, use the prediction step to estimate the state of the car. Similar to the linear extrapolation step from lab 6, but use the prediction step of your Kalman filter instead.
Make sure to use the appropriate delta t when discretizing the A and B matrices.
Plot the results.
If you have time, integrate the Kalman Filter into your Lab 6 PID solution on the Artemis. Before trying to increase the speed of your controller, use your debugging script to verify that your Kalman Filter works as expected. Make sure to remove the linear extrpolation step before doing this. Be sure to demonstrate that your solution works by uploading videos and by plotting corresponding raw and estimated data in the same graph.
If you have even more time, Rather than use linear extrapolation to estimate the position of the car between ToF readings, use the prediction step of the Kalman filter.
The following code snippets gives helpful hints on how to do matrix operations on the robot:
#include <BasicLinearAlgebra.h> //Use this library to work with matrices:
using namespace BLA; //This allows you to declare a matrix
Matrix<2,1> state = {0,0}; //Declares and initializes a 2x1 matrix
Matrix<1> u; //Basically a float that plays nice with the matrix operators
Matrix<2,2> A = {1, 1,
0, 1}; //Declares and initializes a 2x2 matrix
state(1,0) = 1; //Writes only location 1 in the 2x1 matrix.
Sigma_p = Ad*Sigma*~Ad + Sigma_u; //Example of how to compute Sigma_p (~Ad equals Ad transposed)
You’re off the hook in this lab.
To demonstrate that you’ve successfully completed the lab, please upload a brief lab report (<800 words), with code snippets (not included in the word count), photos, and/or videos documenting that everything worked and what you did to make it happen.