Localization Assignment
Localization Assignment
1
Robot Parameters
wheelRadius = 0.1; % Wheel radius (m)
wheelDistance = 0.5; % Distance between wheels (m)
Simulation Parameters
dt = 0.1; % Time step (s)
totalTime = 30; % Total simulation time (s)
timeSteps = 0:dt:totalTime;
numSteps = length(timeSteps);
2
Process and Measurement Noise
Q = diag([0.1, 0.1, 0.05].^2); % Process noise covariance
R = diag([0.1, 0.1].^2); % Measurement noise covariance
3
% Update state and covariance
x_est = x_pred + K * y;
P = (eye(3) - K * H) * P_pred;
% Store data
x_true_history(:, i) = x_true;
x_est_history(:, i) = x_est;
P_history(:, i) = diag(P);
z_history(:, i) = z;
% Plot measurements
plot(z(1), z(2), 'gx', 'MarkerSize', 6);
drawnow;
end
end
% Plot results
figure;
subplot(3, 1, 1);
plot(timeSteps, x_true_history(1, :), 'r-', timeSteps, x_est_history(1, :),
'b--');
legend('True', 'Estimated');
title('X Position');
grid on;
subplot(3, 1, 2);
plot(timeSteps, x_true_history(2, :), 'r-', timeSteps, x_est_history(2, :),
'b--');
legend('True', 'Estimated');
title('Y Position');
grid on;
subplot(3, 1, 3);
plot(timeSteps, x_true_history(3, :), 'r-', timeSteps, x_est_history(3, :),
'b--');
legend('True', 'Estimated');
title('Orientation (theta)');
grid on;
% Plot trajectory
figure;
show(occGrid);
4
hold on;
plot(x_true_history(1, :), x_true_history(2, :), 'r-', 'LineWidth', 2);
plot(x_est_history(1, :), x_est_history(2, :), 'b--', 'LineWidth', 2);
plot(z_history(1, :), z_history(2, :), 'g.', 'MarkerSize', 2);
legend('True Path', 'Estimated Path', 'Measurements');
title('Robot Trajectory with EKF Localization');
Helper Functions
function x_next = robot_motion_model(x, u, dt)
% Motion model for differential drive robot
% x = [x, y, theta]'
% u = [v, omega]'
v = u(1);
omega = u(2);
% Normalize angle
x_next(3) = mod(x_next(3) + pi, 2*pi) - pi;
end
v = u(1);
omega = u(2);
% Next state
x_next = robot_motion_model(x, u, dt);
5
else
% Circular motion
F(1, 3) = (v/omega) * (cos(x(3) + omega*dt) - cos(x(3)));
F(2, 3) = (v/omega) * (sin(x(3) + omega*dt) - sin(x(3)));
end
s = -2 * log(1 - p);
[V, D] = eig(P);
ellipse_x = a * cos(t);
ellipse_y = b * sin(t);
6
7
8
Published with MATLAB® R2023b