-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReseQ.m
297 lines (250 loc) · 9.45 KB
/
ReseQ.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
clear
close all
clc
%% Robot geometric parameters
WheelSpan=0.210;
r = 0.121/2;
l=WheelSpan+r*2
a = WheelSpan/2+0.0965;
b = WheelSpan/2+0.104;
w_e = (0.2046-0.0843)/2; % distance between the center of the 2 tracks
w_max = 0.2046; % total width of the robot
joint_to_joint=a+b
standard_stairs_diag=sqrt(0.17^2+0.29^2)
%theta_max = rad2deg(atan((b-l/2)/(w_max/2))+asin((a-l/2)/sqrt((w_max/2)^2+(b-l/2)^2)))
theta_max = 50;
%% Simulation parameters
v1_max = 0.5;
rc_max = 0.4;
w1_max = v1_max/rc_max;
x = [12 64 24];
%trajectory = 'Simple case';
%trajectory = 'Simple case smooth_w';
trajectory = 'S-curve';
%trajectory = 'U-curve';
%trajectory = '<3 shape';
N = 20000;
switch trajectory
case 'Simple case'
v1 = v1_max*ones(1,N);
w1 = w1_max*ones(1,N);
case 'Simple case smooth_w'
v1 = v1_max*ones(1,3*N);
w1 = [zeros(1,N),linspace(0,w1_max,N),w1_max*ones(1,N)];
case 'S-curve'
v1 = v1_max*ones(1,8*N);
w1 = [zeros(1,N), linspace(0,w1_max,N), w1_max*ones(1,N/2), linspace(w1_max,0,N), zeros(1,N), linspace(0,-w1_max,N), -w1_max*ones(1,N/2), linspace(-w1_max,0,N), zeros(1,N)];
case 'U-curve'
v1 = v1_max*ones(1,2*N+N);
w1 = [zeros(1,N), w1_max*ones(1,N), zeros(1,N)];
case '<3 shape'
v1_max = 0.56;
rc_min = 0.1;
w1_max = v1_max/rc_min;
v1 = v1_max*ones(1,37*N);
w1 = [zeros(1,N), linspace(0,w1_max/3,N), linspace(w1_max/3,0,N), zeros(1,5*N), linspace(0,w1_max/4,N), w1_max/4*ones(1,8*N), linspace(w1_max/4,0,N), -w1_max*ones(1,N), linspace(0,w1_max/4,N), w1_max/4*ones(1,8*N), linspace(w1_max/4,0,N), zeros(1,5*N), linspace(0,w1_max/3,N), linspace(w1_max/3,0,N), zeros(1,N)];
otherwise
v1 = v1_max*ones(1,N);
w1 = w1_max*ones(1,N);
end
ts = 10;
t = linspace(0,ts,length(v1));
dt = t(2)-t(1);
V1 = [v1; zeros(1,length(t)); zeros(1,length(t))];
W1 = [zeros(1,length(t)); zeros(1,length(t)); w1];
V2 = zeros(3,length(t));
W2 = zeros(3,length(t));
V3 = zeros(3,length(t));
W3 = zeros(3,length(t));
%% Initial conditions
% eta1_0 = [0,0,0]'; %Initial orientation and position [g0, x0, y0]'
% eta1d_0 = [0,0,0]'; %Initial angular and linear velocity [gd0, xd0, yd0]'
% eta2_0 = [0,-a-b,0]';
% eta2d_0 = [0,0,0]';
% eta3_0 = [0,-2*a-2*b,0]';
% eta3d_0 = [0,0,0]';
eta1_0 = [0,0,0]'; %Initial orientation and position [g0, x0, y0]'
eta1d_0 = [0,0,0]'; %Initial angular and linear velocity [gd0, xd0, yd0]'
eta2_0 = [deg2rad(theta_max),-a-b*cos(deg2rad(theta_max)),-b*sin(deg2rad(theta_max))]';
eta2d_0 = [0,0,0]';
eta3_0 = [0,-a-b*cos(deg2rad(theta_max))-a*cos(deg2rad(theta_max))-b,-b*sin(deg2rad(theta_max))-a*sin(deg2rad(theta_max))]';
eta3d_0 = [0,0,0]';
eta1 = nan(3,length(t)); eta1(:,1) = eta1_0;
eta1d = nan(3,length(t)); eta1d(:,1) = eta1d_0;
eta2 = nan(3,length(t)); eta2(:,1) = eta2_0;
eta2d = nan(3,length(t)); eta2d(:,1) = eta2d_0;
eta3 = nan(3,length(t)); eta3(:,1) = eta3_0;
eta3d = nan(3,length(t)); eta3d(:,1) = eta3d_0;
omega1 = nan(2,length(t)); %Angular velocities of the equivalent wheels [left;right]
omega2 = nan(2,length(t));
omega3 = nan(2,length(t));
% figure(1)
% plotModule1([eta1_0(2:3)',0]',eta1_0(1),l,w_max,a);
% plotModule2([eta2_0(2:3)',0]',eta2_0(1),l,w_max,a,b);
% plotModule3([eta3_0(2:3)',0]',eta3_0(1),l,w_max,a,b);
% axis equal
% grid
% return
%% Simulation
for i = 1:length(t)-1
th12(i) = eta1(1,i)-eta2(1,i);
th23(i) = eta2(1,i)-eta3(1,i);
omega1(:,i) = [(V1(1,i)-W1(3,i)*w_e/2)/r;
(V1(1,i)+W1(3,i)*w_e/2)/r];
V2(1,i) = V1(1,i)*cos(th12(i)) + a*W1(3,i)*sin(th12(i));
W2(3,i) = (V1(1,i)*sin(th12(i)) - a*W1(3,i)*cos(th12(i)))/b;
omega2(:,i) = [(V2(1,i)-W2(3,i)*w_e/2)/r;
(V2(1,i)+W2(3,i)*w_e/2)/r];
V3(1,i) = V2(1,i)*cos(th23(i)) + a*W2(3,i)*sin(th23(i));
W3(3,i) = (V2(1,i)*sin(th23(i)) - a*W2(3,i)*cos(th23(i)))/b;
omega3(:,i) = [(V3(1,i)-W3(3,i)*w_e/2)/r;
(V3(1,i)+W3(3,i)*w_e/2)/r];
eta1d(1,i) = W1(3,i);
eta2d(1,i) = W2(3,i);
eta3d(1,i) = W3(3,i);
eta1(1,i+1) = eta1(1,i) + dt*eta1d(1,i);
eta2(1,i+1) = eta2(1,i) + dt*eta2d(1,i);
eta3(1,i+1) = eta3(1,i) + dt*eta3d(1,i);
vs1 = Rotz(eta1(1,i))*V1(:,i);
eta1d(2:3,i) = vs1(1:2);
vs2 = Rotz(eta2(1,i))*V2(:,i);
eta2d(2:3,i) = vs2(1:2);
vs3 = Rotz(eta3(1,i))*V3(:,i);
eta3d(2:3,i) = vs3(1:2);
eta1(2:3,i+1) = eta1(2:3,i) + dt*eta1d(2:3,i);
eta2(2:3,i+1) = eta2(2:3,i) + dt*eta2d(2:3,i);
eta3(2:3,i+1) = eta3(2:3,i) + dt*eta3d(2:3,i);
end
%% Plot
figure(2)
subplot(121)
plot(eta1(2,:),eta1(3,:),'--k',"LineWidth",0.75)
grid on
axis equal
hold on
% subplot(222)
% plot(t,omega1(1,:)*60/(2*pi),'--k',"LineWidth",0.75);
% hold on
% plot(t,omega1(2,:)*60/(2*pi),'k',"LineWidth",0.75);
% plot(t,omega2(1,:)*60/(2*pi),'--r',"LineWidth",0.75);
% plot(t,omega2(2,:)*60/(2*pi),'r',"LineWidth",0.75);
% plot(t,omega3(1,:)*60/(2*pi),'--b',"LineWidth",0.75);
% plot(t,omega3(2,:)*60/(2*pi),'b',"LineWidth",0.75);
% grid on
% xlabel("time, s")
% ylabel("\omega_i, rpm")
%
% subplot(223)
% plot(t,V1(1,:),'--k',"LineWidth",0.75);
% hold on
% plot(t,W1(3,:),'k',"LineWidth",0.75);
% plot(t,V2(1,:),'--r',"LineWidth",0.75);
% plot(t,W2(3,:),'r',"LineWidth",0.75);
% plot(t,V3(1,:),'b',"LineWidth",0.75);
% plot(t,W3(3,:),'b',"LineWidth",0.75);
% grid on
% xlabel("time ,s")
% ylabel("linear and angular speed,m/s rad/s")
subplot(122)
plot(t,[0,rad2deg(th12)],'k',"LineWidth",0.75);
hold on
plot(t,[0,rad2deg(th23)],'b',"LineWidth",0.75);
plot([0 t(end)],[theta_max theta_max],'--r',"LineWidth",0.75);
plot([0 t(end)],[-theta_max -theta_max],'--r',"LineWidth",0.75);
grid on
xlabel("time ,s")
ylabel("\theta_i,gradi")
pause(5)
for ii = 1:length(t)/50:length(t)
subplot(121)
cla
plotModule1([eta1(2:3,ii)',0]',eta1(1,ii),l,w_max,a);
plotModule2([eta2(2:3,ii)',0]',eta2(1,ii),l,w_max,a,b);
plotModule3([eta3(2:3,ii)',0]',eta3(1,ii),l,w_max,a,b);
plot(eta1(2,:),eta1(3,:),'--k',"LineWidth",0.75)
% subplot(222)
% cla
% plot(t,omega1(1,:)*60/(2*pi),'--k',"LineWidth",0.75);
% plot(t,omega1(2,:)*60/(2*pi),'k',"LineWidth",0.75);
% plot(t,omega2(1,:)*60/(2*pi),'--r',"LineWidth",0.75);
% plot(t,omega2(2,:)*60/(2*pi),'r',"LineWidth",0.75);
% plot(t,omega3(1,:)*60/(2*pi),'--b',"LineWidth",0.75);
% plot(t,omega3(2,:)*60/(2*pi),'b',"LineWidth",0.75);
% xline(t(ii));
% legend("\omega_{1l}","\omega_{1r}","\omega_{2l}","\omega_{2r}","\omega_{3l}","\omega_{3r}",'Location','northwest')
%
% subplot(223)
% cla
% plot(t,V1(1,:),'--k',"LineWidth",0.75);
% plot(t,W1(3,:),'k',"LineWidth",0.75);
% plot(t,V2(1,:),'--r',"LineWidth",0.75);
% plot(t,W2(3,:),'r',"LineWidth",0.75);
% plot(t,V3(1,:),'b',"LineWidth",0.75);
% plot(t,W3(3,:),'b',"LineWidth",0.75);
% xline(t(ii));
% legend("v_1","\omega_1","v_2","\omega_2","v_3","\omega_3",'Location','northwest')
subplot(122)
cla
plot(t,[0,rad2deg(th12)],'k',"LineWidth",0.75);
plot(t,[0,rad2deg(th23)],'b',"LineWidth",0.75);
plot([0 t(end)],[theta_max theta_max],'--r',"LineWidth",0.75);
plot([0 t(end)],[-theta_max -theta_max],'--r',"LineWidth",0.75);
xline(t(ii));
legend("\theta_{1-2}","\theta_{2-3}","\theta_{max}",'Location','northwest')
drawnow;
end
%% Functions
function out = Rotz(th)
out = [cos(th) -sin(th) 0;
sin(th) cos(th) 0;
0 0 1];
end
function [] = plotModule1(p, th, l, w_max, a, b)
R = Rotz(th);
p1 = p + R*[-l/2,-w_max/2,0]';
p2 = p + R*[+l/2,-w_max/2,0]';
p3 = p + R*[+l/2,+w_max/2,0]';
p4 = p + R*[-l/2,+w_max/2,0]';
pQ1 = p + R*[-l/2,0,0]';
pQ = p + R*[-a,0,0]';
plot([p1(1) p2(1) p3(1) p4(1) p1(1)],[p1(2) p2(2) p3(2) p4(2) p1(2)], 'k', 'LineWidth',2)
hold on
plot([pQ1(1) pQ(1)], [pQ1(2) pQ(2)], 'k', 'LineWidth',2)
plot(pQ(1),pQ(2),'ok', 'LineWidth',2.5)
quiver(p(1),p(2), 0.15*l*cos(th), 0.15*l*sin(th),"Color",'red','LineWidth',1.0)
quiver(p(1),p(2), -0.15*l*sin(th), 0.15*l*cos(th),"Color",'green','LineWidth',1.0)
end
function [] = plotModule2(p, th, l, w_max, a, b)
R = Rotz(th);
p1 = p + R*[-l/2,-w_max/2,0]';
p2 = p + R*[+l/2,-w_max/2,0]';
p3 = p + R*[+l/2,+w_max/2,0]';
p4 = p + R*[-l/2,+w_max/2,0]';
pQ1 = p + R*[+l/2,0,0]';
pQ = p + R*[+b,0,0]';
pQ2 = p + R*[-l/2,0,0]';
pQ_2 = p + R*[-a,0,0]';
plot([p1(1) p2(1) p3(1) p4(1) p1(1)],[p1(2) p2(2) p3(2) p4(2) p1(2)], 'r', 'LineWidth',2)
hold on
plot([pQ1(1) pQ(1)], [pQ1(2) pQ(2)], 'r', 'LineWidth',2)
plot(pQ(1),pQ(2),'ok', 'LineWidth',2.5)
plot([pQ2(1) pQ_2(1)], [pQ2(2) pQ_2(2)], 'k', 'LineWidth',2)
plot(pQ_2(1),pQ_2(2),'ok', 'LineWidth',2.5)
quiver(p(1),p(2), 0.15*l*cos(th), 0.15*l*sin(th),"Color",'red','LineWidth',1.0)
quiver(p(1),p(2), -0.15*l*sin(th), 0.15*l*cos(th),"Color",'green','LineWidth',1.0)
end
function [] = plotModule3(p, th, l, w_max, a, b)
R = Rotz(th);
p1 = p + R*[-l/2,-w_max/2,0]';
p2 = p + R*[+l/2,-w_max/2,0]';
p3 = p + R*[+l/2,+w_max/2,0]';
p4 = p + R*[-l/2,+w_max/2,0]';
pQ1 = p + R*[+l/2,0,0]';
pQ = p + R*[+b,0,0]';
plot([p1(1) p2(1) p3(1) p4(1) p1(1)],[p1(2) p2(2) p3(2) p4(2) p1(2)], 'b', 'LineWidth',2)
hold on
plot([pQ1(1) pQ(1)], [pQ1(2) pQ(2)], 'b', 'LineWidth',2)
plot(pQ(1),pQ(2),'ok', 'LineWidth',2.5)
quiver(p(1),p(2), 0.15*l*cos(th), 0.15*l*sin(th),"Color",'red','LineWidth',1.0)
quiver(p(1),p(2), -0.15*l*sin(th), 0.15*l*cos(th),"Color",'green','LineWidth',1.0)
end