-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstraight_line.m
148 lines (118 loc) · 4.62 KB
/
straight_line.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
clc; clear; close all;
%% time parameter
dt = 0.1;
t0 = 0;
tf = 20-dt;
tspan = (t0:dt:tf)';
%% Define the Robot parameters
Lf = 0.2; % m
Lr = 0.2; % m
wheel_rad = 0.12; % m
W = 0.4; % 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)];
Reference = [xRef,yRef,thetaRef];
%% Initial Position
initialPosition = [2;1;deg2rad(45)];
% 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 = [20, 60, 10]; % k1, k2, k3
% lyapunovGain = [0.75, 1.75, 4]; % k1, k2, k3
lyapunovGain = [0.1, 0.2, 0.6]; % k1, k2, k3
%% 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);
% delta2(k,:) = delta(2);
% delta3(k,:) = delta(3);
% delta4(k,:) = delta(4);
% velocity1(k,:) = wheelVelocity(1);
% velocity2(k,:) = wheelVelocity(2);
% velocity3(k,:) = wheelVelocity(3);
% velocity4(k,:) = wheelVelocity(4);
k = k+1;
% refe = [refe; Reference(last_waypointIndex,:)];
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,yRef,'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',1.0),
hold on
end
light;
xlabel('X (m)'), ylabel('Y (m)'),axis ([-2 4 0 6])
%% Figure plotting
figure,plot(xRef,yRef,'r--'),hold on
plot(xAct,yAct,'-b'), grid on, legend('reference','actual')
xlabel('X (m)'), ylabel('Y (m)'),axis ([-2 4 0 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])