-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExtendedKalmanFilter_arduino.m
91 lines (82 loc) · 3.08 KB
/
ExtendedKalmanFilter_arduino.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
classdef ExtendedKalmanFilter_arduino < handle
%ExtendedKalmanFilter Filter to estimate non-linear system state from
%noisy measurements.
% Class maintains state estimation, covariance of measurements and
% state covariance estimation. Update method takes sensor readings
% and performs the necessary calcs. Note it also takes a function
% handle to the state update function and measurement estimation
% function.
properties (SetAccess=protected)
P=zeros(4,4);
x=zeros(4,1);
Q=zeros(4,4);
R=zeros(1,1);
s1=serial('/dev/tty.usbserial-A400fQ5x');
%s1=serial('/dev/tty.usbserial-A1024PXR');
end
methods
function obj=ExtendedKalmanFilter_arduino(Pinit,xinit,Q,R)
obj.P=Pinit;
obj.x=xinit;
obj.Q=Q;
obj.R=R;
obj.s1.BaudRate=115200; %9600;
fopen(obj.s1);
end
function delete(obj)
fclose(obj.s1);
end
function update(ekf,z,Vxdt,Vydt)
%Estimate new state from old. Also obtain Jacobian matrix for
%later.
if (max([norm(z),norm(Vxdt),norm(Vydt)])>(2^15)/1000)
fprintf('Warning: overflow likely!\n');
end
fprintf(ekf.s1,'U ');
%fprintf(1,'U ');
fprintf(ekf.s1,sprintf('%i %i %i\n',int16(z*1000),int16(Vxdt*1000),int16(Vydt*1000)));
%fprintf(1,sprintf('%i %i %i\n',int16(z*1000),int16(Vxdt*1000),int16(Vydt*1000)));
%fprintf('Sent\n');
%arr=fscanf(ekf.s1,'%f\n')
try
ekf.x(1)=fscanf(ekf.s1,'%f\n');
ekf.x(2)=fscanf(ekf.s1,'%f\n');
ekf.x(3)=fscanf(ekf.s1,'%f\n');
ekf.x(4)=fscanf(ekf.s1,'%f\n');
%fscanf(ekf.s1,'Done.\n');
%fprintf('Received\n');
%ekf.x
catch
fprintf('Warning: read failed!\n')
end
end
function reset(ekf,x,P)
%Reset covariance and state.
%ekf.x=x;
%ekf.P=P;
fprintf(ekf.s1,'R ');
fprintf(ekf.s1,sprintf('%i %i %i %i\n',int16(x(1)*1000),int16(x(2)*1000),int16(x(3)*1000),int16(x(4)*1000)));
end
end
methods(Static)
function [w,A]=jacobian_h(x)
%This function computes the jacobian using equations from
%analytical derivation of Gaussian updraft distribution
%This expression gets used lots
expon = exp(-(x(3)^2+x(4)^2)/x(2)^2);
%Expected measurement
w = x(1)*expon;
%Elements of the Jacobian
A(1)=expon;
A(2)=2*x(1)*(x(3)^2+x(4)^2)/x(2)^3*expon;
A(3)=-2*x(1)*x(3)/x(2)^2*expon;
A(4)=-2*x(1)*x(4)/x(2)^2*expon;
end
function [xn,A]=jacobian_f(x,Vxdt,Vydt)
%Computes new state and jacobian
xn = [x(1);x(2);x(3)-Vxdt;x(4)-Vydt];
%Jacobian is identity
A=eye(4);
end
end
end