ECE4160/5160-MAE 4190/5190: Fast Robots course, offered at Cornell University in Spring 2023
This project is maintained by CEI-lab
The objective of Lab 7 is to implement a Kalman Filter, which will help you execute the behavior you did in Lab 6 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 1foot from the wall (if you chose Task A, position control) or turn within 2ft of the wall (if you chose Task B, orientation control). Note that part of the lab 8 grade is based on the speed of your solution.
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 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
Note that due to the snow day, we have decided to make edits to the lab from this point forward. If you are low on time, consider doing task 4.A; if you are all caught up, consider doing 4.B instead for up to 4 bonus points.
Now that you understand how the Kalman Filter works, you could implement this on your robot and use it to speed up sampling of the estimated distance-to-the-wall (Task 4.B). However, getting the Kalman Filter to work in practice takes time. If you are low on time, do extrapolation instead.
Write a function to extrapolate based on recent ToF sensor values, such that you can drive your robot quickly towards the wall and initiate your stunt at just the right time. While your solution in this lab does not have to be fast, part of your lab 8 grade will be based on the speed of your solution relative to your class mates. Be sure to demonstrate that your solution works by uploading videos and by plotting corresponding raw and estimated data in the same graph.
(Original task)
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. Be sure to demonstrate that your solution works by uploading videos and by plotting corresponding raw and estimated data in the same graph.
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)
If you want to get a head start on Lab 8, try speeding up your robot using your new KF (or extrapolator) to increase the execution time of your control loop.
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.