-
Notifications
You must be signed in to change notification settings - Fork 1
/
objectTracker.m
87 lines (73 loc) · 2.88 KB
/
objectTracker.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
% init: [scalar] set to "1" once when object is first detected to initialize state vector, then set to "0"
% laneModel: [px1] state vector of the current lane model
% lanePieceLength: [scalar] length of linear pieces in the lane model
% r: [4x1] state vector of the object being tracked
% [ s_x (arc length from vehicle to object in the diection of the lane model);
% v_x (velocity of object in direction of lane);
% s_y (lateral position of object relative to middle lane);
% v_y (lateral velocity of object)]
% Pk:[4x4] covariance of the current state
% Q: [scalar] standard deviation of the state transition (how reliable is delta_x?)
% Rx: [scalar] standard deviation of the measurement in lane direction
% Ry: [scalar] standard deviation of the measurement lateral to the lane
% x_measure: [nx1] measured x values of the object being tracked
% y_measure: [nx1] measured y values of the object being tracked
% delta_x: [scalar] forward movement of the vehicle relative to the lane model
function [r, Pk] = objectTracker(init, laneModel, lanePieceLength, r, Pk, Q, Rx, Ry, x_measure, y_measure, delta_x, hasMeasurement)
% x und y Werte sind in Fzg-Koordinaten
%% find distance between lane model and object measurements to get the measurment of s (s_measure)
[xp, yp, phi] = getPointsFromState(laneModel, lanePieceLength);
P = [xp, yp, phi];
D = 10000*[1 1 1 1];
for s=1:numel(laneModel)
dist_point = sqrt((P(s, 1)-(x_measure))^2 + (P(s, 2)-(y_measure))^2);
if dist_point < D(3)
D(1) = s;
D(2) = 0;
D(3) = dist_point;
D(4) = -sign(laneModel(min([numel(laneModel), s+1]))); % positive Krümmung -> vorzeichen von d negativ (und anders rum)
end
if s > 1
[dist_line, lambda, sgn] = d_line_point([P(s-1, 1), P(s-1, 2)], [P(s, 1), P(s, 2)], [x_measure, y_measure]);
if dist_line < D(3) && lambda > 0 && lambda < 1
D(1) = s-1;
D(2) = lambda;
D(3) = dist_line;
D(4) = sgn; % vorzeichen von d
end
end
end
y_measure = D(4)*D(3);
x_measure = (D(1) + D(2)-1)*lanePieceLength;
% initialize the state vector on first object detection
if init == 1
r = [x_measure; 0; y_measure; 0];
return
end
%% state space model matrices
T = 0.01;
A = [1, T, 0, 0;
0, 1, 0, 0;
0, 0, 1, T;
0, 0, 0, 1];
B = [-1; 0; 0; 0];
H = [1, 0, 0, 0;
0, 0, 1, 0];
% covariance of the state transition
d = Q^2;
QQ = [ d, d*T, 0, 0;
d*T, d*T^2, 0, 0;
0, 0, d, d*T;
0, 0, d*T, d*T^2];
%% Kalman filter
% prediction
r = A*r + B*delta_x;
Pk = A*Pk*A' + QQ;
% kalman gain
S = H*Pk*H' + [Rx^2, 0; 0 Ry^2];
K = Pk*H'/S;
% update
if hasMeasurement
r = r + K*([x_measure; y_measure] - H*r);
end
Pk = Pk - K*S*K';