state estimation with a kalman filter...example object falling in air we know the dynamics related...
TRANSCRIPT
State Estimation with a Kalman Filter
When I drive into a tunnel, my GPS continues to show me moving forward, even though it isn’t getting any new position sensing data How does it work?
A Kalman filter produces estimate of system’s next state, given noisy sensor data control commands with uncertain effects model of system’s (possibly stochastic) dynamics estimate of system’s current state
In our case, given a blimp with (approximately known) dynamics noisy sensor data control commands our current estimate of blimp’s state
How should we predict the blimp’s next state? How should we control blimp?
CSE 466 1State Estimation
Kalman Filter
Bayesian estimator, computes beliefs about state, assuming everything is linear and Gaussian Gaussian is unimodal only one hypothesis Example of a Bayes filter
“Recursive filter,” since current state depends on previous state, which depends on state before that, and so on
Two phases: prediction (not modified by data) and correction (data dependent)
Produces estimate with minimum mean-squared error Even though the current estimate only looks at the previous estimate,
as long as the actual data matches the assumptions (Gaussian etc), you can’t do any better, even if you looked at all the data in batch!
Very practical and relatively easy to use technique!
CSE 466 State Estimation 2
Example Object falling in air We know the dynamics
Related to blimp dynamics, since drag and inertial forces are both significant Dynamics same as driving blimp forward with const fan speed
We get noisy measurements of the state (position and velocity) We will see how to use a Kalman filter to track it
CSE 466 State Estimation 3
0 20 40 60 80 100 120 140 160 180 200-2
-1
0
1Position of object falling in air, Meas Nz Var= 0.0025 Proc Nz Var= 0.0001
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-1.5
-1
-0.5
0Velocity of object falling in air
observationsKalman outputtrue dynamics
Linear 1D Newtonian dynamics exampleObject falling in air
CSE 466 State Estimation 4
where dxvdt
State is ( , )x v
Linear 1D Newtonian dynamics exampleObject falling in air
CSE 466 State Estimation 5
2 (ideally we'd use instead of )
force due to drag a
g
a g
v v
dvf ma mdt
f kv
f gm
f f f kv mg
dvm kv mgdt
dv kv gdt m
Confused by the signs?If obj is falling down, v is a negative #
g (the gravitational const.) isa pos. number, but the directionof gravity is down, so the gravitational force is -mg (a neg. number).The frictional force –kv is a positivenumber (since v is negative).Even though we wrote –kv and –mg, these two forces are in opposite directions when the object is falling
How to solve the differential equation on the computer?
CSE 466 State Estimation 6
1
1
1
t t
t t
t t
dv kv gdt mv kv gt m
v v kv gt m
kvv v g tm
kvv v g tm
1
1
t t
t t
dx vdtx vt
x x vt
x x v t
Object falling in air
CSE 466 State Estimation 7
0 20 40 60 80 100 120 140 160 180 200-2
-1.8
-1.6
-1.4
-1.2
-1
-0.8
-0.6
-0.4
-0.2
0Falling object k/m = 10
xv
At terminalvelocity
Produced by iterating the difference equations on previous slide
In air, heavier objectsdo fall faster!
CSE 466 State Estimation 8
0 20 40 60 80 100 120 140 160 180 200-7
-6
-5
-4
-3
-2
-1
0Falling object k/m = 10
xv
0 20 40 60 80 100 120 140 160 180 200-7
-6
-5
-4
-3
-2
-1
0Falling object k/m = 5
xv
0 20 40 60 80 100 120 140 160 180 200-7
-6
-5
-4
-3
-2
-1
0Falling object k/m = 2.5
xv
And they take longer to reach theirterminal velocity
(Without air, all objects accelerate at thesame rate)
Matlab code for modeling object falling in air% falling.mx0 = 0.0;v0 = 0.0;TMAX = 200;x = zeros(1,TMAX);V = zeros(1,TMAX);g=9.8;m=1.0;k=10.0;x(1) = x0;v(1) = v0;dt=0.01;for t=2:TMAX,
x(t) = x(t-1)+(v(t-1))*dt;v(t) = v(t-1)+(-(k/m)*(v(t-1))-g)*dt;
endfigure();plot(x,'b'); hold on;title(['Falling object k/m = ' num2str(k/m)]);plot(v,'r')legend('x','v'); hold off
CSE 466 State Estimation 9
Multi-dimensional Gaussians
CSE 466 State Estimation 10
2 2 2
2
1/2
1( | , ) exp[ ( ) / 2 ]21 1( | , ) exp[ ( ) ( )]( ) 2
where
( ) det( / 2 )and is the inverse of the covariance matrix.
T
P x m x m
PZ
Z
x m R x m R x mR
R RR
One-dimensional (scalar)Gaussian
Vector Gaussian
Multi-dimensional Gaussians, Covariance Matrices, Ellipses, and all thatIn an N dimensional space is a sphere of radius R
Can write it more generally by inserting identity matrix
If we replace I by a more general matrix M, it will distort the sphere: for M diagonal, it will scale each axis differently, producing an axis-aligned ellipsoidWe could also apply rotation matrices to a diagonal M to produce a general, non-axis aligned ellipsoid The uncertainty “ball” of a multi-D Gaussian [e.g. the 1 std iso-surface] actually IS an ellipsoid!CSE 466 State Estimation 11
2T Rxx
2T T R xx xIx
2 2 2 21 2Note that ...T
Nx x x R xx x x
Example
CSE 466 State Estimation 12
-5 0 5-5
-4
-3
-2
-1
0
1
2
3
4
5
2 00 1
A
-5 0 5-5
-4
-3
-2
-1
0
1
2
3
4
5
1 00 2
A
Blue: circle of radius 1 (e.g. 1 std iso-surface of uncorrelated uniform Gaussian noise)Red: circle after transformation by A
CSE 466 State Estimation 13
-5 0 5-5
-4
-3
-2
-1
0
1
2
3
4
5
0.1 00 2
A
cos sinWith and ,
sin cos 16
0.1 0 0.172 0.3630 2 0.363 1.93
T
R
A R R
-5 0 5-5
-4
-3
-2
-1
0
1
2
3
4
5
End
CSE 466 State Estimation 14
Kalman filter variables
CSE 466 State Estimation 15
: state vector: observation vector: control vector
A: state transition matrix --- dynamics: input matrix (maps control commands onto state changes): covariance of state vector estimate: process n
xzu
BPQ oise covariance
: measurement noise covariance: observation matrix
RH
Kalman filter algorithm
CSE 466 State Estimation 16
1
Prediction for state vector and covariance:
Kalman gain factor:
( )
Correction based on observation:
( )
T
T T
x Ax Bu
P APA Q
K PH H PH R
x x K z H x
P P KH P
: state vector: observation vector: control vector
A: state transition matrix --- dynamics: control commands --> state changes: covariance of state vector estimate: process noise covariance: mea
xzu
BPQR surement noise covariance
: observation matrix H
Need dynamics in matrix form
CSE 466 State Estimation 17
1 1
1 1
1
1
1 11 1
1 11 1
x x v dt;v v v g dt;Want s.t.x xv v
Try1 x vx x x0 1 v vv v v
t t t
kt t tm
t t
t t
t tt t tk k
t tt t tm m
t tt t
A
A
A
Hmm, close but gravity is missing…we can’t put it into A because it will be multiplied by vt. Let’s stick it into B!
Need dynamics in matrix form
CSE 466 State Estimation 18
1 0 0 0Try
0 1Bu
g t g t
Now gravity is in there…we treated it as a control input.
1
1
1 11
1 11
x x 0v v 1
1 x vx 1 0 0
0 1 v ( v )v 0 1
t t
t t
t ttk k
t ttm m
B
t tt g tg t
A
1 1
1 1
Wantx x v dt;v v v g dt;
t t t
kt t tm
The command to turn ongravity!
Matlab for dynamics in matrix form% falling_matrix.m: model of object falling in air, w/ matrix notationx0 = 0.0; v0 = 0.0;TMAX = 200;x = zeros(2,TMAX);g=9.8;m=1.0;k=10.0;x(1,1) = x0; x(2,1) = v0;dt=0.01;u=[0 1]';for t=2:TMAX,
A=[[1 dt ]; ... [0 (1.0-(k/m)*dt) ]];
B=[[1 0 ]; ... [0 -g*dt ]];
x(:,t) = A*x(:,t-1) + B*u; end% plotting
CSE 466 State Estimation 19
Matlab for Kalman Filter
function s = kalmanf(s)s.x = s.A*s.x + s.B*s.u;s.P = s.A * s.P * s.A' + s.Q;% Compute Kalman gain factor:K = s.P * s.H' * inv(s.H * s.P * s.H' + s.R);% Correction based on observation:s.x = s.x + K*(s.z - s.H *s.x);s.P = s.P - K*s.H*s.P;
endreturn
CSE 466 State Estimation 20
The most computationallydifficult step
CSE 466 State Estimation 21
0 20 40 60 80 100 120 140 160 180 200-2
-1.5
-1
-0.5
0
0.5Position of object falling in air, Meas Nz Var= 0.01 Proc Nz Var= 0.0001
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-1.5
-1
-0.5
0
0.5Velocity of object falling in air
observationsKalman outputtrue dynamics
Calling the Kalman Filter (init)x0 = 0.0; v0 = 0.0;TMAX = 200;g=9.8;m=1.0; k=10.0;dt=0.01;
clear s % Dynamics modeled by As.A = [[1 dt ]; ...
[0 (1.0-(k/m)*dt)]];
CSE 466 State Estimation 22
Calling the Kalman Filter (init)% Measurement noise varianceMNstd = 0.4;MNV = MNstd*MNstd;% Process noise variancePNstd = 0.02;PNV = PNstd*PNstd;% Process noise covariance matrixs.Q = eye(2)*PNV;% Define measurement function to return the states.H = eye(2);% Define a measurement errors.R = eye(2)*MNV; % variance
CSE 466 State Estimation 23
Q, H, and R are all diagonal!
Calling the Kalman Filter (init)% Use control to include gravitys.B = eye(2); % Control matrixs.u = [0 -g*m*dt]'; % Gravitational acceleration% Initial state:s.x = [x0 v0]';s.P = eye(2)*MNV;s.detP = det(s.P); % Let's keep track of the noise by keeping detPs.z = zeros(2,1);
CSE 466 State Estimation 24
Calling the Kalman Filter% Simulate falling in air, and watch the filter track ittru=zeros(TMAX,2); % true dynamicstru(1,:)=[x0 v0];detP(1,:)=s.detP;for t=2:TMAX
tru(t,:)=s(t-1).A*tru(t-1,:)’+ s(t-1).B*s(t-1).u+PNstd *randn(2,1);s(t-1).z = s(t-1).H * tru(t,:)' + MNstd*randn(2,1); % create a meas.s(t)=kalmanf(s(t-1)); % perform a Kalman filter iterationdetP(t)=s(t).detP; % keep track of "net" uncertainty
end
CSE 466 State Estimation 25
The variable s is an object whose members are all the important data structures (A, x, B, u, H, z, etc)
tru: simulation of true dynamics. In the real world, this would be implementedby the actual physical system
Blimp estimation example
CSE 466 State Estimation 26
0 20 40 60 80 100 120 140 160 180 200-100
-50
0
50Position of blimp (in air), Meas Nz Var= 25 Proc Nz Var= 0.09
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-200
0
200Velocity of blimp (in air)
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 2000
500
1000Det(P)
0 20 40 60 80 100 120 140 160 180 200-10
0
10Commands
Here we are assuming we can switch gravity back and forth(from -10 to +10 and back)
Blimp P control using raw sensor data, no Kalman filter
CSE 466 State Estimation 27
0 100 200 300 400 500 600 700 800 900 1000-100
-50
0
50Position of blimp (in air), Meas Nz Var= 25 Proc Nz Var= 0.09
observationsKalman outputtrue dynamics
0 100 200 300 400 500 600 700 800 900 1000-100
-50
0
50Velocity of blimp (in air)
observationsKalman outputtrue dynamics
0 100 200 300 400 500 600 700 800 900 10000
500
1000Det(P)
0 100 200 300 400 500 600 700 800 900 1000-10
-5
0
5Commands
Servoing to setpoint -50Mean Squared Error: 0.143RMS: 0.379
Blimp P controlusing Kalman-filtered state estimate
CSE 466 State Estimation 28
0 100 200 300 400 500 600 700 800 900 1000-100
-50
0
50Position of blimp (in air), Meas Nz Var= 25 Proc Nz Var= 0.09
observationsKalman outputtrue dynamics
0 100 200 300 400 500 600 700 800 900 1000-100
-50
0
50Velocity of blimp (in air)
observationsKalman outputtrue dynamics
0 100 200 300 400 500 600 700 800 900 10000
500
1000Det(P)
0 100 200 300 400 500 600 700 800 900 1000-10
-5
0
5Commands
Servoing to setpoint -50Mean Squared Error: 0.0373RMS: 0.193
How to apply this for our blimps?
(* Let's do it in Mathematica! *)(* Here is the dynamics model *)ClearAll["Global`*"]
A = {{1,dt},{0,1-((k/m)* dt)}};B={{1,0},{0,-g dt}};state = {x,v};cmd = {0,1};A . state + B. cmd (* Dot is matrix multiply *)
{dt v+x,-dt g+(1-(dt k)/m) v}
CSE 466 State Estimation 29
Definitions
g=.;MNstd = 0.4;MNV = MNstd*MNstd;PNstd = 0.02;PNV = PNstd*PNstd;Q = {{PNV,0},{0,PNV}};H = {{1,0},{0,1}};R = {{MNV,0},{0,MNV}};P = {{MNV,0},{0,MNV}};
CSE 466 State Estimation 30
The Kalman filter
PredState = Simplify[A . state + B . cmd];PredUncertainty = Simplify[A . P . Transpose[A] + Q];Kgain = Simplify[PredUncertainty . Transpose[H] . Inverse[H.PredUncertainty.Transpose[H]+R]];CorrectedState = Simplify[state + Kgain . ({z1,z2} - H . PredState)];CorrectedUncertainty = Simplify[PredUncertainty + Kgain . H . PredUncertainty];
CSE 466 State Estimation 31
Result? Kind of a mess
etcCSE 466 State Estimation 32
Put in numerical values for constants
m=1;k = 1;dt = 0.01;
Kgain
{{0.500637,0.00249354}, {0.00249354,0.495599}}
CorrectedState
{x+0.500637 (-0.01 v - x+z1)+0.00249354 (-v+0.01 (g+v)+z2),v+0.00249354 (-0.01 v - x+z1)+0.495599 (-v+0.01 (g+v)+z2)}
The filter is a linear combination of previous state estimates, and sensor values (z1 and z2)!
CSE 466 State Estimation 33
Extensions
Extended Kalman Filter (EKF) Non-linear generalization…assumes linearization
around the currently estimated state Standard for GPS, robot navigation, etc
Information Filter A variant that allows N previous sensor values to
be processed in one filter update step Unscented Kalman Filter (UKF)…the
unscented Kalman Filter does not stink! Use a sampling of points to represent distributions
CSE 466 State Estimation 34
Extra examples…various noise settings
CSE 466 State Estimation 35
CSE 466 State Estimation 36
0 20 40 60 80 100 120 140 160 180 200-4
-2
0
2Position of object falling in air, Meas Nz Var= 0.16 Proc Nz Var= 0.0004
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-3
-2
-1
0
1Velocity of object falling in air
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 2000
0.01
0.02
0.03Det(P)
CSE 466 State Estimation 37
Process noise Q = 0.0
0 20 40 60 80 100 120 140 160 180 200-2
-1
0
1Position of object falling in air, Meas Nz Var= 0.01 Proc Nz Var= 0
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-1.5
-1
-0.5
0Velocity of object falling in air
observationsKalman outputtrue dynamics
CSE 466 State Estimation 38
0 20 40 60 80 100 120 140 160 180 200-3
-2
-1
0
1Position of object falling in air 2=0.04
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-1.5
-1
-0.5
0Velocity of object falling in air
observationsKalman outputtrue dynamics
Process noise Q = 0.0
CSE 466 State Estimation 39
0 20 40 60 80 100 120 140 160 180 200-4
-2
0
2
4Position of object falling in air 2=1
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-4
-2
0
2Velocity of object falling in air
observationsKalman outputtrue dynamics
Process noise Q = 0.0
CSE 466 State Estimation 40
0 20 40 60 80 100 120 140 160 180 200-2
-1
0
1Position of object falling in air 2=0.01
observationsKalman outputtrue dynamics
0 20 40 60 80 100 120 140 160 180 200-1.5
-1
-0.5
0Velocity of object falling in air
observationsKalman outputtrue dynamics
Process noise Q = 0.02