-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.m
76 lines (59 loc) · 1.84 KB
/
main.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
clear;
clc;
%% Parameters
% Controller gain
Kp_joint = eye(7)*20;
Kp_cart = eye(3)*15;
% Redundancy gain
k0 = 10; %0 for standard (non collision-free) motion planning
% Integration Time Step
Ts = 0.05;
% Obstacles
obs = [[-0.025;-0.55;0.8]];
% Time execution
T_tot = 20;
% Controller type
controller = "cartesian";
%% Trajectory setup
way_points = [[-0.525;-0.2;0.9],[-0.025;-0.6;0.5],[0.425;-0.3;0.7],[-0.025;-0.55;1.1],[-0.525;-0.2;0.9]];
x_d_des = [[0;0;0],[0;0;0],[0;0;0],[0;0;0],[0;0;0]];
% x_d_des = [[0;0;0],[0.9629;-0.0951;-0.2222],[0.0165;0.4465;0.2695],-[0.9629;-0.0951;-0.2222],[0;0;0]];
plot3(way_points(1,:),way_points(2,:),way_points(3,:), 'o','Color','r','MarkerSize',5);
[X,Y,Z] = sphere;
R = 0.15;
X = R*X;
Y = R*Y;
Z = R*Z;
surf(X+obs(1),Y+obs(2),Z+obs(3));
axis equal;
grid on;
hold on;
tvec = 0:Ts:T_tot;
tpts = 0:T_tot/(size(way_points,2)-1):T_tot;
[x,x_dot,x_dotdot,pp] = cubicpolytraj(way_points,tpts,tvec,...
'VelocityBoundaryCondition', x_d_des);
plot3(x(1,:),x(2,:),x(3,:), 'o','Color','b','MarkerSize',5);
grid on;
hold on;
%% Simulation
robot = VrepConnector(19999,0.05);
q = robot.GetState();
while(norm(directKinematics(q) - way_points(:,1)) > 1e-2 )
q = robot.GetState();
end
qd = robot.GetState();
for i=1:size(x_dot,2)
q = robot.GetState();
if controller == "joint"
[qd, q_control_dot] = jointSpaceController(qd, x_dot(:,i), q, Ts, Kp_joint, obs(:,1), k0);
else
[qd, q_control_dot] = cartesianSpaceController(x(:,i), x_dot(:,i), q, Ts, Kp_cart, obs(:,1), k0);
end
x_control = directKinematics(qd);
plot3(x_control(1),x_control(2),x_control(3), 'o','Color','r','MarkerSize',5);
hold on;
x_robot = directKinematics(robot.GetState());
plot3(x_robot(1),x_robot(2),x_robot(3), 'o','Color','g','MarkerSize',5);
hold on;
robot.ApplyControl(q_control_dot, Ts);
end