-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.m
147 lines (104 loc) · 4.12 KB
/
simulation.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
%% Configure Simulation Parameters
segment_dt = .001;
total_dt = 1;
n = floor(total_dt/segment_dt);
%% Ball trajectory
% z0 = [-2.1; 1.8; -5.3; 2; -2; g_];
% z0 = [-1.5; 3.9; -4.7; 2; -2; g_];
% z0 = [-.5; .9; -4.7; 1; -1; g_];
% z0 = [-.5; .9; -4.7; 3; -3; g_];
v0 = 2;
z0 = [.2-v0; 0; -g_/2-Ls_; v0; 0; g_];
ball_position = zeros(3,n);
ball_velocity = zeros(3,n);
time = 0:segment_dt:total_dt;
t_apex = z0(6)/g_;
z_apex = z0(1:3) + z0(4:6)*t_apex - 1/2*g_*t_apex^2*e3;
z_d_apex = z0(4:6) - g_*t_apex*e3;
for i=1:size(time,2)
t = time(i);
ball_position(:,i) = z0(1:3) + z0(4:6)*t - 1/2*g_*t^2*e3;
end
%% Plan Trajectory
trajectory.x = find_coefficients([0;0;0;0],[z_apex(1);0;0;0],total_dt);
trajectory.y = find_coefficients([0;0;0;0],[z_apex(2);0;0;0],total_dt);
trajectory.z = find_coefficients([0;0;0;0],[z_apex(3);0;0;0],total_dt);
trajectory.a = find_coefficients([0;0;0;0],[0;0;0;0],total_dt); % wrist pronation a: + pi, - pi
trajectory.b = find_coefficients([pi/2;0;0;0],[pi/2;0;0;0],total_dt); % swing b: 0, pi
trajectory.g = find_coefficients([0;0;0;0],[0;0;0;0],total_dt); % yaw -2*pi,2*pi
% aggressive example
% trajectory.x = find_coefficients([0;0;0;0],[.3;0;0;0],total_dt);
% trajectory.y = find_coefficients([0;0;0;0],[.2;0;0;0],total_dt);
% trajectory.z = find_coefficients([0;0;0;0],[.8;0;0;0],total_dt);
% trajectory.a = find_coefficients([0;0;0;0],[pi/4;0;0;0],total_dt);
% trajectory.b = find_coefficients([pi/2;0;0;0],[pi/2+pi/4;0;0;0],total_dt);
% trajectory.g = find_coefficients([0;0;0;0],[pi/4;0;0;0],total_dt);
% conservative example
% trajectory.x = find_coefficients([0;0;0;0],[.03;0;0;0],total_dt);
% trajectory.y = find_coefficients([0;0;0;0],[.05;0;0;0],total_dt);
% trajectory.z = find_coefficients([0;0;0;0],[.4;0;0;0],total_dt);
% trajectory.a = find_coefficients([0;0;0;0],[pi/10;0;0;0],total_dt);
% trajectory.b = find_coefficients([pi/2;0;0;0],[pi/2+pi/4;0;0;0],total_dt);
% trajectory.g = find_coefficients([0;0;0;0],[pi/10;0;0;0],total_dt);
stacked = [
trajectory.x; trajectory.y; trajectory.z;
trajectory.a; trajectory.b; trajectory.g;
];
[stacked, minval, retcode] = trajectory_optimization(z0,stacked);
minval
retcode
show_trajectory;
pause
%
% figure(3)
% clf;
% names = {'x','y','z','\alpha','\beta','\gamma'};
% for coord=1:6
% derivatives = zeros(5,n);
% for i=1:n
% derivatives(:,i) = compute_derivatives(stacked((coord-1)*8+(1:8)),i*segment_dt,total_dt);
% end
% subplot(2,3,coord);
% plot(segment_dt*(1:n),derivatives.')
% title(names{coord});
% end
% drawnow;
%% Tracking
[~,current_state] = compute_control(stacked,0,total_dt);
state = zeros(n,size(current_state,1));
state_des = zeros(size(state));
state(1,:) = current_state;
us = zeros(n,6);
[xs, Rg, th1, th2, xs_d, w, th1d, th2d] = state_from_vector(current_state);
%% Dynamic Simulation
xe_rec = zeros(6,n); % records the planned trajectory of end effector
xs_rec = zeros(6,n); % records the trajectory computed with diff. flatness
w_rec = zeros(3,n); % records the planned gripper ang. vel
% Om_rec = zeros(3,n); % records the ang. vel. computed with diff. flatness
percent_done = -1;
% options = odeset('AbsTol',1e-8,'RelTol',1e-4, 'MaxStep',0.00001);
for j=1:n
% progress indicator
percent = floor((j / n)*100);
if(percent > percent_done)
fprintf('simulating dynamics: %d%% done.\n',percent);
percent_done = percent;
end
t = segment_dt * j;
% compute feedforward control
[u_ff, current_state_des] = compute_control(stacked, t, total_dt,current_state);
% integrate dynamics
tspan=segment_dt*(j-1)+[0 segment_dt];
[~,qs] = ode45(@(t,x) ode(x,u_ff),tspan,current_state);
current_state = qs(end,:)';
% reorthonormalize rotation matrices (project back onto manifold)
[xs, Rg, th1, th2, xs_d, w, th1d, th2d] = state_from_vector(current_state);
[U, ~, V] = svd(Rg);
Rg = U * V';
% record state and control inputs for plotting
state(j,:) = vector_from_state(xs, Rg, th1, th2, xs_d, w, th1d, th2d);
state_des(j,:) = current_state_des;
us(j,:) = u_ff.';
end
%% Visualization
plot_sim_results