main content

state estimation using time-凯发k8网页登录

this example shows how to estimate states of linear systems using time-varying kalman filters in simulink®. you use the kalman filter block from the control system toolbox™ library to estimate the position and velocity of a ground vehicle based on noisy position measurements such as gps sensor measurements. the plant model in kalman filter has time-varying noise characteristics.

introduction

you want to estimate the position and velocity of a ground vehicle in the north and east directions. the vehicle can move freely in the two-dimensional space without any constraints. you design a multi-purpose navigation and tracking system that can be used for any object and not just a vehicle.

$x_e(t)$ and $x_n(t)$ are the vehicle's east and north positions from the origin, $\theta(t)$ is the vehicle orientation from east and $u_\psi(t)$ is the steering angle of the vehicle. $t$ is the continuous-time variable.

the simulink model consists of two main parts: vehicle model and the kalman filter. these are explained further in the following sections.

open_system('ctrlkalmannavigationexample');

vehicle model

the tracked vehicle is represented with a simple point-mass model:

$$ \frac{d}{dt} \left[
\begin{array} {c}
 x_e(t) \\
 x_n(t) \\
 s(t) \\
 \theta(t)
\end{array} \right] = \left[
\begin{array} {c}
 s(t)\cos(\theta(t)) \\
 s(t)\sin(\theta(t)) \\
 (p \; \frac{u_t(t)}{s(t)} - a \; c_d \; s(t)^2) / m \\
 s(t) \tan(u_\psi(t)) / l
\end{array} \right]
$$

where the vehicle states are:

$$ \begin{array} {ll}
x_e(t) \; & \textnormal{east position} \; [m] \\
x_n(t) \; & \textnormal{north position} \; [m] \\
s(t) \; & \textnormal{speed} \; [m/s] \\
\theta(t) \; & \textnormal{orientation from east} \; [deg] \\
\end{array} $$

the vehicle parameters are:

$$ \begin{array} {ll}
p=100000 \; & \textnormal{peak engine power} \; [w] \\
a=1 \; & \textnormal{frontal area} \; [m^2] \\
c_d=0.3 \; & \textnormal{drag coefficient} \; [unitless] \\
m=1250 \; & \textnormal{vehicle mass} \; [kg] \\
l=2.5 \; & \textnormal{wheelbase length} \; [m] \\
\end{array} $$

and the control inputs are:

$$ \begin{array} {ll}
u_t(t) \; & \textnormal{throttle position in the range of -1 and 1} \; [unitless] \\
u_\psi(t) \; & \textnormal{steering angle} \; [deg] \\
\end{array} $$

the longitudinal dynamics of the model ignore tire rolling resistance. the lateral dynamics of the model assume that the desired steering angle can be achieved instantaneously and ignore the yaw moment of inertia.

the car model is implemented in the ctrlkalmannavigationexample/vehicle model subsystem. the simulink model contains two pi controllers for tracking the desired orientation and speed for the car in the ctrlkalmannavigationexample/speed and orientation tracking subsystem. this allows you to specify various operating conditions for the car and test the kalman filter performance.

kalman filter design

kalman filter is an algorithm to estimate unknown variables of interest based on a linear model. this linear model describes the evolution of the estimated variables over time in response to model initial conditions as well as known and unknown model inputs. in this example, you estimate the following parameters/variables:

$$ \hat{x}[n] = \left[
 \begin{array}{c}
 \hat{x}_e[n] \\
 \hat{x}_n[n] \\
 \hat{\dot{x}}_e[n] \\
 \hat{\dot{x}}_n[n]
 \end{array}
 \right]
$$

where

$$ \begin{array} {ll}
\hat{x}_e[n] \; & \textnormal{east position estimate} \; [m] \\
\hat{x}_n[n] \; & \textnormal{north position estimate} \; [m] \\
\hat{\dot{x}}_e[n] \; & \textnormal{east velocity estimate} \; [m/s] \\
\hat{\dot{x}}_n[n] \; & \textnormal{north velocity estimate} \; [m/s] \\
\end{array} $$

the $\dot{x}$ terms denote velocities and not the derivative operator. $n$ is the discrete-time index. the model used in the kalman filter is of the form:

$$ \begin{array} {rl}
\hat{x}[n 1] &= a\hat{x}[n]   gw[n] \\
y[n] &= c\hat{x}[n]   v[n]
\end{array} $$

where $\hat{x}$ is the state vector, $y$ is the measurements, $w$ is the process noise, and $v$ is the measurement noise. kalman filter assumes that $w$ and $v$ are zero-mean, independent random variables with known variances $e[ww^t]=q$, $e[vv^t]=r$, and $e[wv^t]=n$. here, the a, g, and c matrices are:

$$ a = \left[
 \begin{array}{c c c c}
 1 & 0 & t_s & 0 \\
 0 & 1 & 0 & t_s \\
 0 & 0 & 1 & 0 \\
 0 & 0 & 0 & 1
 \end{array}
 \right]
$$

$$ g = \left[
 \begin{array}{c c}
 t_s/2 & 0 \\
 0 & t_s/2 \\
 1 & 0 \\
 0 & 1
 \end{array}
 \right]
$$

$$ c = \left[
 \begin{array}{c c c c}
 1 & 0 & 0 & 0 \\
 0 & 1 & 0 & 0
 \end{array}
 \right]
$$

where $t_s=1\;[s]$

the third row of a and g model the east velocity as a random walk: $\hat{\dot{x}}_e[n 1]=\hat{\dot{x}}_e[n] w_1[n]$. in reality, position is a continuous-time variable and is the integral of velocity over time $\frac{d}{dt}\hat{x}_e=\hat{\dot{x}}_e$. the first row of the a and g represent a discrete approximation to this kinematic relationship: $(\hat{x}_e[n 1]-\hat{x}_e[n])/ts=(\hat{\dot{x}}_e[n 1] \hat{\dot{x}}_e[n])/2$. the second and fourth rows of the a and g represent the same relationship between the north velocity and position.

the c matrix represents that only position measurements are available. a position sensor, such as gps, provides these measurements at the sample rate of 1hz. the variance of the measurement noise $v$, the r matrix, is specified as $r=50$. since r is specified as a scalar, the kalman filter block assumes that the matrix r is diagonal, its diagonals are 50 and is of compatible dimensions with y. if the measurement noise is gaussian, r=50 corresponds to 68% of the position measurements being within $\pm\sqrt{50}\;m$ or the actual position in the east and north directions. however, this assumption is not necessary for the kalman filter.

the elements of $w$ capture how much the vehicle velocity can change over one sample time ts. the variance of the process noise w, the q matrix, is chosen to be time-varying. it captures the intuition that typical values of $w[n]$ are smaller when velocity is large. for instance, going from 0 to 10m/s is easier than going from 10 to 20m/s. concretely, you use the estimated north and east velocities and a saturation function to construct q[n]:

$$f_{sat}(z)=min(max(z,25),625)$$

$$ q[n] = \left[
 \begin{array}{ c c }
 \displaystyle 1 \frac{250}{f_{sat}(\hat{\dot{x}}_e^2)} & 0 \\
 0 & \displaystyle 1 \frac{250}{f_{sat}(\hat{\dot{x}}_n^2)}
 \end{array}
 \right]
$$

the diagonals of q model the variance of w inversely proportional to the square of the estimated velocities. the saturation function prevents q from becoming too large or small. the coefficient 250 is obtained from a least squares fit to 0-5, 5-10, 10-15, 15-20, 20-25m/s acceleration time data for a generic vehicle. note that the diagonal q choice represents a naive assumption that the velocity changes in the north and east direction are uncorrelated.

kalman filter block inputs and setup

the 'kalman filter' block is in the control system toolbox library in simulink. it is also in system identification toolbox/estimators library. configure the block parameters for discrete-time state estimation. specify the following filter settings parameters:

  • time domain: discrete-time. choose this option to estimate discrete-time states.

  • select the use current measurement y[n] to improve xhat[n] check box. this implements the "current estimator" variant of the discrete-time kalman filter. this option improves the estimation accuracy and is more useful for slow sample times. however, it increases the computational cost. in addition, this kalman filter variant has direct feedthrough, which leads to an algebraic loop if the kalman filter is used in a feedback loop that does not contain any delays (the feedback loop itself also has direct feedthrough). the algebraic loop can further impact the simulation speed.

click the options tab to set the block inport and outport options:

  • unselect the add input port u check box. there are no known inputs in the plant model.

  • select the output state estimation error covariance z check box. the z matrix provides information about the filter's confidence in the state estimates.

click model parameters to specify the plant model and noise characteristics:

  • model source: individual a, b, c, d matrices.

  • a: a. the a matrix is defined earlier in this example.

  • c: c. the c matrix is defined earlier in this example.

  • initial estimate source: dialog

  • initial states x[0]: 0. this represents an initial guess of 0 for the position and velocity estimates at t=0s.

  • state estimation error covariance p[0]: 10. assume that the error between your initial guess x[0] and its actual value is a random variable with a standard deviation $\sqrt{10}$.

  • select the use g and h matrices (default g=i and h=0) check box to specify a non-default g matrix.

  • g: g. the g matrix is defined earlier in this example.

  • h: 0. the process noise does not impact the measurements y entering the kalman filter block.

  • unselect the time-invariant q check box. the q matrix is time-varying and is supplied through the block inport q. the block uses a time-varying kalman filter due to this setting. you can select this option to use a time-invariant kalman filter. a time-invariant kalman filter performs slightly worse for this problem, but is easier to design and has a lower computational cost.

  • r: r. this is the covariance of the measurement noise $v[n]$. the r matrix is defined earlier in this example.

  • n: 0. assume that there is no correlation between process and measurement noises.

  • sample time (-1 for inherited): ts, which is defined earlier in this example.

results

test the performance of the kalman filter by simulating a scenario where the vehicle makes the following maneuvers:

  • at t = 0 the vehicle is at $x_e(0)=0$, $x_n(0)=0$ and is stationary.

  • heading east, it accelerates to 25m/s. it decelerates to 5m/s at t=50s.

  • at t = 100s, it turns toward north and accelerates to 20m/s.

  • at t = 200s, it makes another turn toward west. it accelerates to 25m/s.

  • at t = 260s, it decelerates to 15m/s and makes a constant speed 180 degree turn.

simulate the simulink model. plot the actual, measured and kalman filter estimates of vehicle position.

sim('ctrlkalmannavigationexample');
figure;
% plot results and connect data points with a solid line.
plot(x(:,1),x(:,2),'bx',...
    y(:,1),y(:,2),'gd',...
    xhat(:,1),xhat(:,2),'ro',...
    'linestyle','-');
title('position');
xlabel('east [m]');
ylabel('north [m]');
legend('actual','measured','kalman filter estimate','location','best');
axis tight;

the error between the measured and actual position as well as the error between the kalman filter estimate and actual position is:

% east position measurement error [m]
n_xe = y(:,1)-x(:,1);
% north position measurement error [m]
n_xn = y(:,2)-x(:,2);
% kalman filter east position error [m]
e_xe = xhat(:,1)-x(:,1);
% kalman filter north position error [m]
e_xn = xhat(:,2)-x(:,2);
figure;
% east position errors
subplot(2,1,1);
plot(t,n_xe,'g',t,e_xe,'r');
ylabel('position error - east [m]');
xlabel('time [s]');
legend(sprintf('meas: %.3f',norm(n_xe,1)/numel(n_xe)),...
    sprintf('kalman f.: %.3f',norm(e_xe,1)/numel(e_xe)));
axis tight;
% north position errors
subplot(2,1,2);
plot(t,y(:,2)-x(:,2),'g',t,xhat(:,2)-x(:,2),'r');
ylabel('position error - north [m]');
xlabel('time [s]');
legend(sprintf('meas: %.3f',norm(n_xn,1)/numel(n_xn)),...
    sprintf('kalman f: %.3f',norm(e_xn,1)/numel(e_xn)));
axis tight;

the plot legends show the position measurement and estimation error ($||x_e-\hat{x}_e||_1$ and $||x_n-\hat{x}_n||_1$) normalized by the number of data points. the kalman filter estimates have about 25% percent less error than the raw measurements.

the actual velocity in the east direction and its kalman filter estimate is shown below in the top plot. the bottom plot shows the estimation error.

e_ve = xhat(:,3)-x(:,3); % [m/s] kalman filter east velocity error
e_vn = xhat(:,4)-x(:,4); % [m/s] kalman filter north velocity error
figure;
% velocity in east direction and its estimate
subplot(2,1,1);
plot(t,x(:,3),'b',t,xhat(:,3),'r');
ylabel('velocity - east [m/s]');
xlabel('time [s]');
legend('actual','kalman filter','location','best');
axis tight;
subplot(2,1,2);
% estimation error
plot(t,e_ve,'r');
ylabel('velocity error - east [m/s]');
xlabel('time [s]');
legend(sprintf('kalman filter: %.3f',norm(e_ve,1)/numel(e_ve)));
axis tight;

the legend on the error plot shows the east velocity estimation error $||\dot{x}_e-\hat{\dot{x}}_e||_1$ normalized by the number of data points.

the kalman filter velocity estimates track the actual velocity trends correctly. the noise levels decrease when the vehicle is traveling at high velocities. this is in line with the design of the q matrix. the large two spikes are at t=50s and t=200s. these are the times when the car goes through sudden deceleration and a sharp turn, respectively. the velocity changes at those instants are much larger than the predictions from the kalman filter, which is based on its q matrix input. after a few time-steps, the filter estimates catch up with the actual velocity.

summary

you estimated the position and velocity of a vehicle using the kalman filter block in simulink. the process noise dynamics of the model were time-varying. you validated the filter performance by simulating various vehicle maneuvers and randomly generated measurement noise. the kalman filter improved the position measurements and provided velocity estimates for the vehicle.

bdclose('ctrlkalmannavigationexample');

see also

related topics

网站地图