-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicycle_simulate.m
185 lines (159 loc) · 6.43 KB
/
unicycle_simulate.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
% Trajectory generation and simulation for the unicycle model
clear all
clc
% Unicycle model parameters ────────────────────────────────────────────────────
r = 0.03; % wheel radius
L = 0.3; % distance between wheels
Ts = 0.1; % sampling time
x_constraints = 100*[
-2 2; % x position constraints
-2 2; % y position constraints
-pi 3*pi; % heading angle constraints
];
u_constraints = 100*[-30, 30]; % angular velocity constraints
states = 3; % number of states
outputs = 2; % number of outputs
Q_tilde = 0.75*1e-3*eye(states); % Process noise covariance
R_tilde = 1e-2*eye(outputs); % Measurement noise covariance
P0 = eye(states); % Initial state covariance
model = Unicycle(r, L, Ts, x_constraints, u_constraints, P0, Q_tilde, R_tilde);
% Reference Trajectory Generation ──────────────────────────────────────────────
% (comment / uncomment the desired trajectory)
% Circle trajectory
N_guide = 100;
radius = 0.5;
shape = "circle";
[x_ref, u_ref, Tend] = model.generate_trajectory(N_guide, shape, radius);
% % Leminscate trajectory
% N_guide = 100;
% a = 1;
% shape = "leminscate";
% [x_ref, u_ref, Tend] = model.generate_trajectory(N_guide, shape, a);
% % Arbitrary trajectory
% N_guide = 9;
% Z_guide = [
% 1, 1;
% 2.14, 1.85;
% 3.4, 1.91;
% 4.66, 1.41;
% 6, 1;
% 7.04, 1.43;
% 8, 2;
% 8.92, 2.63;
% 9.96, 2.37;
% ];
% N_points_filling = 3;
% shape = "arbitrary";
% N_basis = 2;
% order = 0;
% [x_ref, u_ref, Tend] = model.generate_trajectory(N_guide, shape, {N_points_filling, N_basis, order, Z_guide});
% % Multiply periodic references for multiple laps
% n_laps = 2;
% x_ref = repmat(x_ref, n_laps, 1);
% u_ref = repmat(u_ref, n_laps, 1);
% Tend = Tend*n_laps;
% Loop first reference as last reference
x_ref = [x_ref; x_ref(1, :)];
u_ref = [u_ref; u_ref(1, :)];
Tend = Tend + Ts;
% Simulate the system ──────────────────────────────────────────────────────────
Nsteps = length(x_ref); % number of steps
x = zeros(Nsteps, states); % state vector
x(1, :) = x_ref(1, :); % initial state
MSE_x = zeros(Nsteps-1, 1); % mean squared error for state
for i = 1:Nsteps-1 % Simulate the system
x(i+1, :) = model.simulate(x(i, :)', u_ref(i, :)', Ts);
MSE_x(i) = norm(x(i, :) - x_ref(i, :))^2;
end
max_MSE = max(MSE_x); % maximum mean squared error for state
disp(['Max Mean Squared Error for State: ', num2str(max_MSE)]);
MSE_x = mean(MSE_x); % mean squared error for state
disp(['Mean Squared Error for State: ', num2str(MSE_x)]);
% Plot ─────────────────────────────────────────────────────────────────────────
% Main trajectory plot
figure(1);
% Reference trajectory
ref_points = scatter(x_ref(:, 1), x_ref(:, 2), 5, 'filled', 'MarkerFaceColor', '#808080');
hold on;
arrow_length = 0.01;
for i = 1:length(x_ref)
x_arrow = arrow_length * cos(x_ref(i, 3));
y_arrow = arrow_length * sin(x_ref(i, 3));
quiver(x_ref(i, 1), x_ref(i, 2), x_arrow, y_arrow, 'AutoScale', 'off', 'Color', '#808080');
end
legend(ref_points,{'Reference trajectory'}, 'Location', 'northwest');
% Labels
title('Trajectory Simulation for the Unicycle Model');
xlabel('x'); ylabel('y');
grid on;
axis equal;
hold on;
% ────────────────────
% Wait for figure here
pause(1);
% Real trajectory
for i = 1:Nsteps
x_line = plot(x(1:i, 1), x(1:i, 2), 'blue', 'LineWidth', 1);
x_line.Color(4) = 0.5; % line transparency 50%
hold on;
x_points = scatter(x(1:i, 1), x(1:i, 2), 5, 'blue', 'filled');
hold on;
quiver(x(1:i, 1), x(1:i, 2), arrow_length * cos(x(1:i, 3)), arrow_length * sin(x(1:i, 3)), 'AutoScale', 'off', 'Color', 'blue');
legend([ref_points, x_points],{'Reference trajectory', 'Real trajectory'}, 'Location', 'northwest');
hold on;
pause(0.05);
if i < Nsteps
delete(x_line);
end
end
% % GIF ──────────────────────────────────────────────────────────────────────────
% % Main trajectory plot
% figure(1);
% filename = 'images/unicycle_simulation.gif'; % Output GIF filename
%
% % Reference trajectory
% ref_points = scatter(x_ref(:, 1), x_ref(:, 2), 5, 'filled', 'MarkerFaceColor', '#808080');
% hold on;
% arrow_length = 0.03;
% for i = 1:length(x_ref)
% x_arrow = arrow_length * cos(x_ref(i, 3));
% y_arrow = arrow_length * sin(x_ref(i, 3));
% quiver(x_ref(i, 1), x_ref(i, 2), x_arrow, y_arrow, 'AutoScale', 'off', 'Color', '#808080');
% end
% legend(ref_points,{'Reference trajectory'}, 'Location', 'northwest');
%
% % Labels
% title('Trajectory Simulation for the Unicycle Model');
% xlabel('x'); ylabel('y');
% grid on;
% axis equal;
% hold on;
%
% % Capture initial frame for GIF
% frame = getframe(gcf);
% img = frame2im(frame);
% [imind, cm] = rgb2ind(img, 256);
% imwrite(imind, cm, filename, 'gif', 'Loopcount', inf, 'DelayTime', 0.05);
%
% % Real trajectory animation and GIF capture
% for i = 1:Nsteps
% x_line = plot(x(1:i, 1), x(1:i, 2), 'blue', 'LineWidth', 1);
% x_line.Color(4) = 0.5; % line transparency 50%
% hold on;
% x_points = scatter(x(1:i, 1), x(1:i, 2), 5, 'blue', 'filled');
% hold on;
% quiver(x(1:i, 1), x(1:i, 2), arrow_length * cos(x(1:i, 3)), arrow_length * sin(x(1:i, 3)), 'AutoScale', 'off', 'Color', 'blue');
% legend([ref_points, x_points], {'Reference trajectory', 'Real trajectory'}, 'Location', 'northwest');
% hold on;
%
% % Capture frame for GIF
% frame = getframe(gcf);
% img = frame2im(frame);
% [imind, cm] = rgb2ind(img, 256);
% imwrite(imind, cm, filename, 'gif', 'WriteMode', 'append', 'DelayTime', 0.05);
%
% pause(0.05);
% if i < Nsteps
% delete(x_line);
% end
% end