-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinter_rows.m
196 lines (160 loc) · 7.27 KB
/
inter_rows.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
clc; clear; close all;
%% time parameter
dt = 0.1;
t0 = 0;
tf = 60-dt;
tspan = (t0:dt:tf)';
%% Define the Robot parameters
Lf = 0.235; % m
Lr = 0.235; % m
wheel_rad = 0.12; % m
W = 0.42; % m
m = 20; % Kg
%% Define way points
% numWaypoints = (0:0.5:10)';
% xRef = [zeros(length(numWaypoints)-1,1); numWaypoints(1:end-1); 10*ones(length(numWaypoints),1);...
% 1*flipud(numWaypoints)];
%
% yRef = [numWaypoints(1:end-1); 10*ones(length(numWaypoints)-1,1); 1*flipud(numWaypoints);...
% zeros(length(numWaypoints),1)];
%
% thetaRef = [deg2rad(90)*ones(length(numWaypoints)-1,1); deg2rad(0)*ones(length(numWaypoints)-1,1);...
% deg2rad(-90)*ones(length(numWaypoints)-1,1); deg2rad(-180)*ones(length(numWaypoints)+1,1)];
numWaypoints = (0:0.25:5)';
xRef = [zeros(length(numWaypoints),1); 1*ones(length(numWaypoints),1); 2*ones(length(numWaypoints),1);...
3*ones(length(numWaypoints),1); 4*ones(length(numWaypoints),1); 5*ones(length(numWaypoints),1)];
yRef = [numWaypoints; 1*flipud(numWaypoints); numWaypoints; 1*flipud(numWaypoints); numWaypoints; 1*flipud(numWaypoints)];
thetaRef = [deg2rad(90)*ones(length(numWaypoints)-1,1); deg2rad(65); deg2rad(35);...
deg2rad(-90)*ones(length(numWaypoints)-2,1); deg2rad(-65); deg2rad(-35);...
deg2rad(90)*ones(length(numWaypoints)-2,1); deg2rad(65); deg2rad(35);...
deg2rad(-90)*ones(length(numWaypoints)-2,1); deg2rad(-65); deg2rad(-35);...
deg2rad(90)*ones(length(numWaypoints)-2,1); deg2rad(0); deg2rad(-10);...
deg2rad(-90)*ones(length(numWaypoints)-1,1);];
%% Reference Path
Reference = [xRef,yRef,thetaRef];
%% Initial Position
% initialPosition = [0.5;0;deg2rad(180)];
initialPosition = [-1;-1;deg2rad(0)];
xAct(1,:) = initialPosition(1);
yAct(1,:) = initialPosition(2);
thetaAct(1,:) = initialPosition(3);
vwMax = [0.5,1]; % maximum velocity, maximum omega
vwDes = [0.5,0]; % desired velocity, desired omega
%% Controller Gain
lyapunovGain = [0.5, 0.2, 1]; % k1, k2, k3
% lyapunovGain = [1 0.01 3]; % Model 1
% lyapunovGain = [200 500 100]; % Model 2
%% index toko indonesia
k = 1;
waypointIndex = 0;
last_waypointIndex = [];
numsinTolerance = 1e-2; % Numerical Singularity Tolerance
cycle = 1;
viz = Visualizer2D;
while tspan(k,:) < tf
%% Waypoints initializing
if waypointIndex == 0
waypointIndex = 1;
elseif waypointIndex == length(xRef)+1
waypointIndex = 1;
cycle = cycle + 1;
else
% do nothing
end
last_waypointIndex(k,:) = waypointIndex;
%% Kinematics Control
[vRef(k,:),wRef(k,:),waypointIndex,xe(k,:),ye(k,:),thetae(k,:)] = kinematics_control(lyapunovGain,[xAct(k,:),yAct(k,:),thetaAct(k,:)],Reference,vwMax,vwDes,waypointIndex);
% Numerical Singularity Solver in Speed Controller
if abs(vRef(k,:)) < numsinTolerance
waypointIndex = waypointIndex + ceil(length(numWaypoints)/10)+2;
else
% do nothing
end
%% Inverse Kinematics
[delta(:,k), wheelspeed(:,k)] = ugv_inverseKinematics(vRef(k,:),wRef(k,:),Lf,Lr,wheel_rad);
%% Forward Kinematics
[vxAct(k,:),omegaAct(k,:)] = ugv_forwardKinematics(delta(:,k),wheelspeed(:,k),Lf,Lr,W);
%% Conversion of local to global frame
xdotAct(k,:) = vxAct(k,:)*cos(thetaAct(k,:));% - vyAct*sin(thetaAct(k,:));
ydotAct(k,:) = vxAct(k,:)*sin(thetaAct(k,:));% + vyAct*cos(thetaAct(k,:));
thetadotAct(k,:) = omegaAct(k,:);
%% Get Tractor Position in global frame by integration
xAct(k+1,:) = xAct(k,:) + xdotAct(k,:)*dt;
yAct(k+1,:) = yAct(k,:) + ydotAct(k,:)*dt;
thetaAct(k+1,:) = thetaAct(k,:) + thetadotAct(k,:)*dt;
if thetaAct(k+1,:) < 2*pi
% do nothing
else
thetaAct(k+1,:) = thetaAct(k+1,:) - 2*pi;
end
%% Run the motion animation
delta1(k,:) = delta(1,k);
delta2(k,:) = delta(2,k);
delta3(k,:) = delta(3,k);
delta4(k,:) = delta(4,k);
velocity1(k,:) = wheelspeed(1,k);
velocity2(k,:) = wheelspeed(2,k);
velocity3(k,:) = wheelspeed(3,k);
velocity4(k,:) = wheelspeed(4,k);
refX(k,:) = xRef(waypointIndex);
refY(k,:) = yRef(waypointIndex);
refTheta(k,:) = thetaRef(waypointIndex);
k = k+1;
end
n = 10;
for i = 1:1:length(xAct)/n
xpose(i,:) = xAct(i*n,:);
ypose(i,:) = yAct(i*n,:);
thetapose(i,:) = thetaAct(i*n,:);
end
xpose = [xAct(1,:); xpose];
ypose = [yAct(1,:); ypose];
thetapose = [thetaAct(1,:); thetapose];
figure,
plot(xRef(1:21),yRef(1:21),'r--'),hold on,
plot(xRef(22:42),yRef(22:42),'r--'),hold on,
plot(xRef(43:63),yRef(43:63),'r--'),hold on,
plot(xRef(64:84),yRef(64:84),'r--'),hold on,
plot(xRef(85:105),yRef(85:105),'r--'),hold on,
plot(xRef(106:126),yRef(106:126),'r--'),hold on,
plot(xAct,yAct,'-b'),hold on,grid on
for i = 1:1:length(xpose)
xyzpose = [xpose(i,:) ypose(i,:) 0];
rotpose = axang2quat([0 0 1 thetapose(i,:)]);
% plotTransforms(xyzpose, rotpose,...
% 'MeshFilePath', 'groundvehicle.stl', "View", "2D",...
% 'MeshColor', 'blue','FrameSize',0.5);
ugv_shape(Lf+Lr,W,xpose(i,:),ypose(i,:),rad2deg(thetapose(i,:)),'k',0.5),
hold on
end
light;
xlabel('X (m)'), ylabel('Y (m)'),axis ([-2 6 -2 6])
%% Figure plotting
figure,
plot(xRef(1:21),yRef(1:21),'r--'),hold on,
plot(xRef(22:42),yRef(22:42),'r--'),hold on,
plot(xRef(43:63),yRef(43:63),'r--'),hold on,
plot(xRef(64:84),yRef(64:84),'r--'),hold on,
plot(xRef(85:105),yRef(85:105),'r--'),hold on,
plot(xRef(106:126),yRef(106:126),'r--'),hold on,
plot(xAct,yAct,'-b'), grid on, legend('reference','actual')
xlabel('X (m)'), ylabel('Y (m)'),axis ([-2 6 -2 6])
figure,subplot(2,1,1),plot(tspan(1:end),0.5*ones(length(vxAct)+1,1),tspan(1:end),[0;vxAct]),grid on
xlabel('time(sec)'), ylabel('velocity(m/s)'), legend('reference','actual'),axis([0 tf 0 0.6])
subplot(2,1,2),plot(tspan(1:end),zeros(length(omegaAct)+1,1),tspan(1:end),[0;omegaAct]),grid on
xlabel('time(sec)'), ylabel('omega(rad/s)'), legend('reference','actual'),axis([0 tf -1.3 1.3])
figure, subplot(2,1,1), plot(tspan(1:end-1),rad2deg(delta1),tspan(1:end-1),rad2deg(delta4)),grid on
xlabel('time(sec)'), ylabel('angle(degree)'), legend('front left (\delta_1)','back left (\delta_4)')
subplot(2,1,2), plot(tspan(1:end-1),rad2deg(delta2),tspan(1:end-1),rad2deg(delta3)),grid on
xlabel('time(sec)'), ylabel('angle(degree)'), legend('front right (\delta_2)','back right (\delta_3)')
figure, subplot(2,1,1), plot(tspan(1:end-1),velocity1,tspan(1:end-1),velocity4),grid on
xlabel('time(sec)'), ylabel('wheel velocity(m/s)'), legend('front left (v_1)','back left (v_4)')
subplot(2,1,2), plot(tspan(1:end-1),velocity2,tspan(1:end-1),velocity3),grid on
xlabel('time(sec)'), ylabel('wheel velocity(m/s)'), legend('front right (v_2)','back right (v_3)')
% figure, subplot(2,1,1), plot(tspan(1:end-1),velocity1,tspan(1:end-1),velocity4),grid on
% xlabel('time(sec)'), ylabel('angle(degree)'), legend('front left','back left')
% subplot(2,1,2), plot(tspan(1:end-1),velocity2,tspan(1:end-1),velocity3),grid on
% xlabel('time(sec)'), ylabel('angle(degree)'), legend('front right','back right')
figure,plot(refX),hold on,plot(xAct)
figure,plot(refY),hold on,plot(yAct)
figure,plot(refTheta),hold on,plot(thetaAct)