Hi Dave,

I have a tiny bit of experience with Kalman filtering for state
estimation, and attached a toy example that might help get you
started. I have written it to work with Octave which is open source so
you can install it if you don't already have it.   The idea as I
understand it is that you have a model of how the state updates, like
Newton's laws.  You predict what you'll measure, compare that to your
actual measurement and the apply the Kalman gain matrix K to the
difference to update your estimate of the state.

You can run the example in Octave with

octave:1> kalman_example

In the example, I have an object moving in a parabolic arc, and in a
commeted-out line, an object moving in a straight line.  The
measurements that are the input the filter are stored in y. You can
plot the measurements out with

plot(y(1,:), y(2,:), 'o')

At the end of the script the estimate state positions are plotted with

plot(x_hat_plus(1,:), x_hat_plus(2,:), 'o')


The tricky parts seem to be what sort values to put in the initial P,
Q, and R covariance matrices.

Hope this helps.


Cheers,
Keith

On Sun, Aug 21, 2011 at 12:08 PM, <kth.cur...@yahoo.com> wrote:
>
>
> ---------- Forwarded message ----------
> Date: Wed, 17 Aug 2011 16:18:51 -0400
> From: Dave <e...@dc9.tzo.com>
> Reply-To: "Enhanced Machine Controller (EMC)" 
> <emc-users@lists.sourceforge.net>
> To: emc-users@lists.sourceforge.net
> Subject: [Emc-users] OT ? Kalman Filter
>
>
> I have an application that I believe could greatly benefit from a Kalman
> Filter.    Kalman filters are oftentimes used for guidance systems and
> that is what I need it for - except that I am guiding a boat!
> However the same filter I understand can also be used to predict
> (estimate) velocity and position based upon what would normally be
> considered incomplete information.  One example I heard what that a
> Kalman filter can
> be used to predict velocity when used with low count encoders - as in
> low count encoders used on servo motors.
>
> If you do a look up of "Kalman filter" on Youtube you can get an idea of
> their flexibility.
>
> Does anyone have any experience with Kalman filters?    I have searched
> the web for a simple explanation and John Crenshaw of embedded magazine
> started a series of articles in the March 2011 edition  (sort of a
> Kalman filter for dummies) but he stopped short of publishing the second
> article which gets into the actual filter software explanation.
>
> I found a "primer" on the web that I downloaded.  It is a PDF document
> that is 189 pages long!
>
> Where do I start with this??
>
> I get the feeling that if I just grab some source code and implement
> something it won't work since the filter has to be "setup" to allow the
> software to properly predict or estimate a value.
>
> Thanks,
>
> Dave
>
> ------------------------------------------------------------------------------
> Get a FREE DOWNLOAD! and learn more about uberSVN rich system,
> user administration capabilities and model configuration. Take
> the hassle out of deploying and managing Subversion and the
> tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2
> _______________________________________________
> Emc-users mailing list
> Emc-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/emc-users

### example to filter location measurements of a moving object
### build measurement data to filter

delta_t = 0.1;  # 1 second updates
# x state vector x-position, y-position, x-velocity, y-velocity
N = 1000;
t = (1:N)*delta_t;
# parabolic arc
x = [ 10*t ; 15*t - 3*t.^2 ; 10 + 0*t; 15 - 6*t];
#straight line
#x = [ 10*t ; -15*t  ; 10 + 0*t; 15 + 0*t];


# H is measurement matrix, only measures position for this example
H = [ 1 0 0 0;
      0 1 0 0 ];

#build measurement data
y = H*x + 30*randn(2,N);

plot(y(1,:), y(2,:), 'o')


### set up initial matrices for Kalman filter

# F is state update matrix, assuming Newton's Laws for model
F = [ 1  0 delta_t   0;
      0  1   0     delta_t;
      0  0   1       0;
      0  0   0       1      ] ;

# P is error covariance, set initial matrix
P = diag([1000 1000 100 100]);

# R measurement noise covariance, set initial matrix,
R = diag([1000 1000]);

# Q control noise covariance, set matrix for duration of filtering, for this example, assume only velocity changes

#try different control noise to allow for curves
#Q = diag([0 0 1000 1000]);
Q = diag([0 0 10 10]);
#Q = diag([0 0 1 1]);
#Q = diag([0 0 0 0]);   # seems pretty idealistic




### apply Kalman filter to measurements


x_hat_plus = zeros(4,N);
x_hat_plus(:,1) = [y(1,1) y(2,1) 0 0];  # initial estimate

for i = 2:N
  x_hat_minus = F * x_hat_plus(:,i-1) ;  # model extrapolation
  # x_hat_minus += G * u for control terms if known
  y_hat = H * x_hat_minus; # estimated measurement
  yvec = y(:,i);          # real, noisy, measurement
  P_minus = F * P * F' + Q;
  K = P_minus * H' * inv(H * P_minus * H' + R);
  # assimilate the new measurement
  x_hat_plus(:,i) = x_hat_minus + K*(yvec - y_hat);
  #update covariance
  A = eye(4,4)  - K*H;
  P = A*P_minus*A' + K*R*K';
endfor

plot(x_hat_plus(1,:), x_hat_plus(2,:), 'o')
------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system, 
user administration capabilities and model configuration. Take 
the hassle out of deploying and managing Subversion and the 
tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2
_______________________________________________
Emc-users mailing list
Emc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to