-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeometric_tracking_controller.m
378 lines (336 loc) · 13 KB
/
geometric_tracking_controller.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
% function[varargout] = geometric_tracking_controller(varargin)
%%% INITIALZING WORKSPACE
close all;
addpath('./Geometry-Toolbox/');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Using Geometry-Toolbox; thanks to Avinash Siravuru %%
% https://github.com/sir-avinash/geometry-toolbox %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%Implemeting The two cases mentioned in the paper
for iter = 1:2
switch(iter)
case 1
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% INITIALZING SYSTEM PARAMETERS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% moment of Inertia in Kg(m^2)
quad_params.params.mass = 0.5 ;
% moment of Inertia in Kg(m^2)
quad_params.params.J = diag([0.557, 0.557, 1.05]*10e-2);
% acceleration via gravity contant
quad_params.params.g = 9.81 ;
% interial fram axis
quad_params.params.e1 = [1;0;0] ;
quad_params.params.e2 = [0;1;0] ;
quad_params.params.e3 = [0;0;1] ;
% distance of center of mass from fram center in m
quad_params.params.d = 0.315;
% fixed constant in m
quad_params.params.c = 8.004*10e-4;
%defining parameters different for each trajectories
quad_params.params.x_desired = nan;
quad_params.params.gen_traj = 1; %change here
quad_params.params.vel = nan;
quad_params.params.acc = nan;
quad_params.params.b1d = nan;
quad_params.params.w_desired = [0;0;0];
quad_params.params.k1 = diag([5, 5 ,9]);
quad_params.params.k2 = diag([5, 5, 10]);
quad_params.params.kR = 200;
quad_params.params.kOm = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% INTIALIZING - INTIAL PARAMETERS x,v,R,w %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intial position
x_quad_0 = [0;0;0];
% xQ0 = [1;3;2];
% Intial velocity
v_quad_0 = zeros(3,1);
% Initial orientation
% R0 = RPYtoRot_ZXY(0*pi/180, 0*pi/180, 0*pi/180);
R0 = eye(3);
% Intial angular velocity
w0= zeros(3,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Concatenating the entire initial condition into a single vector
x0 = [x_quad_0; v_quad_0; reshape(R0,9,1); w0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% SIMULATION
odeopts = odeset('RelTol', 1e-8, 'AbsTol', 1e-9) ;
% odeopts = [] ;
[t, x] = ode15s(@odefun_quadDynamics, [0 20], x0, odeopts, quad_params) ;
% Computing Various Quantities
disp('Evaluating...') ;
index = round(linspace(1, length(t), round(1*length(t))));
% ind = 0:length(t);
for i = index
[~,xd_,f_,M_] = odefun_quadDynamics(t(i),x(i,:)',quad_params);
xd(i,:) = xd_';
pos_err_fx(i) = norm(x(i,1:3)-xd(i,1:3));
vel_err_fx(i) = norm(x(i,4:6)-xd(i,4:6));
f(i,1)= f_;
M(i,:)= M_';
end
%%% Plotting graphs
plott(t,x,xd,pos_err_fx,vel_err_fx);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% CASE 2 %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 2
clear;
%%% INITIALZING SYSTEM PARAMETERS %%%
% moment of Inertia in Kg(m^2)
quad_params.params.mass = 0.5 ;
% moment of Inertia in Kg(m^2)
quad_params.params.J = diag([0.557, 0.557, 1.05]*10e-2);
% acceleration via gravity contant
quad_params.params.g = 9.81 ;
% interial fram axis
quad_params.params.e1 = [1;0;0] ;
quad_params.params.e2 = [0;1;0] ;
quad_params.params.e3 = [0;0;1] ;
% distance of center of mass from fram center in m
quad_params.params.d = 0.315;
% fixed constant in m
quad_params.params.c = 8.004*10e-4;
% Desired position
quad_params.params.x_desired = [0;0;0];
quad_params.params.w_desired = [0;0;0];
%defining parameters different for each trajectories
quad_params.params.gen_traj = 0;
quad_params.params.vel = 0;
quad_params.params.acc = 0;
quad_params.params.b1d = [1;0;0];
quad_params.params.k1 = diag([5, 5 ,9]);
quad_params.params.k2 = diag([5, 5, 10]);
quad_params.params.kR = 50;
quad_params.params.kOm = 2.5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% INTIALIZING - INTIAL PARAMETERS x,v,R,w %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Intial position
x_quad_0 = [0;0;0];
% xQ0 = [1;3;2];
% Intial velocity
v_quad_0 = zeros(3,1);
% Initial orientation
R0 = [1 0 0;0 -0.9995 -0.0314; 0 0.0314 -0.9995];
% Intial angular velocity
w0= zeros(3,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Concatenating the entire initial condition into a single vector
x0 = [x_quad_0; v_quad_0; reshape(R0,9,1); w0];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% SIMULATION
odeopts = odeset('RelTol', 1e-8, 'AbsTol', 1e-9) ;
% odeopts = [] ;
[t, x] = ode15s(@odefun_quadDynamics, [0 20], x0, odeopts, quad_params) ;
% Computing Various Quantities
disp('Evaluating...') ;
index = round(linspace(1, length(t), round(1*length(t))));
% ind = 0:length(t);
for i = index
[~,xd_,f_,M_] = odefun_quadDynamics(t(i),x(i,:)',quad_params);
xd(i,:) = xd_';
pos_err_fx(i) = norm(x(i,1:3)-xd(i,1:3));
vel_err_fx(i) = norm(x(i,4:6)-xd(i,4:6));
f(i,1)= f_;
M(i,:)= M_';
end
%%% Plotting graphs
plott(t,x,xd,pos_err_fx,vel_err_fx);
otherwise
fprintf('\n invalid case');
end
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%% INTIALIZING - INTIAL PARAMETERS x,v,R,w %%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Intial position
% x_quad_0 = [0;0;0];
% % xQ0 = [1;3;2];
% % Intial velocity
% v_quad_0 = zeros(3,1);
% % Initial orientation
% % R0 = RPYtoRot_ZXY(0*pi/180, 10*pi/180, 20*pi/180);
% R0 = RPYtoRot_ZXY(0*pi/180, 0*pi/180, 0*pi/180);
% % Intial angular velocity
% w0= zeros(3,1);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Concatenating the entire initial condition into a single vector
% x0 = [x_quad_0; v_quad_0; reshape(R0,9,1); w0];
% %%%%%%%% SIMULATION
% odeopts = odeset('RelTol', 1e-8, 'AbsTol', 1e-9) ;
% % odeopts = [] ;
% [t, x] = ode15s(@odefun_quadDynamics, [0 20], x0, odeopts, quad_params) ;
%
% % Computing Various Quantities
% disp('Evaluating...') ;
% index = round(linspace(1, length(t), round(1*length(t))));
% % ind = 0:length(t);
% for i = index
% [~,xd_,f_,M_] = odefun_quadDynamics(t(i),x(i,:)',quad_params);
% xd(i,:) = xd_';
% pos_err_fx(i) = norm(x(i,1:3)-xd(i,1:3));
% vel_err_fx(i) = norm(x(i,4:6)-xd(i,4:6));
% f(i,1)= f_;
% M(i,:)= M_';
% end
%
% %%% Plotting graphs
% plott(t,x,xd,pos_err_fx,vel_err_fx);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Function definitions %%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[dx, xd, f,M] = odefun_quadDynamics(t,x,quad_params)
mass = quad_params.params.mass;
J = quad_params.params.J;
g = quad_params.params.g;
e1 = quad_params.params.e1;
e2 = quad_params.params.e2;
e3 = quad_params.params.e3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fetching desired states %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(quad_params.params.gen_traj)
[traj_desired] = return_traj(t);
% x_des = traj_desired.pos;
x_des = traj_desired.pos;
v_des = traj_desired.vel;
a_des = traj_desired.acc;
b1d = traj_desired.b1d;
w_desired = quad_params.params.w_desired;
w_desired_dot = zeros(3,1);
else
x_des = quad_params.params.x_desired;
v_des = [0;0;0];
a_des = [0;0;0];
b1d = quad_params.params.b1d;
Rd = eye(3);
w_desired = quad_params.params.w_desired;
w_desired_dot = zeros(3,1);
end
x_curr = x(1:3);
v_curr = x(4:6);
R = reshape(x(7:15),3,3);
w = x(16:18);
b3 = R(:,3);
dx = [];
%%%%%%%%%%%
% CONTROL %
%%%%%%%%%%%
% Position Control
err_x = x_curr - x_des;
err_v = v_curr - v_des;
% constants for force and moment
k1 = quad_params.params.k1;
k2 = quad_params.params.k2;
A = (-k1*err_x - k2*err_v + mass*a_des + mass*g*e3);
normA = norm(A);
b3_desired = A/normA;
f = vec_dot(A,b3);
%%%%%%%%%%%%%%%%%%%%
% Attitude Control %
%%%%%%%%%%%%%%%%%%%%
%b1d definded above;
b2d = vec_cross(b3_desired,b1d);
norm_b2d = norm(b2d);
b2d = b2d/norm_b2d;
projection_b1d = -vec_cross(b3_desired,b2d);
Rd = [vec_cross(b2d,b3_desired) b2d b3_desired];
%angluar velocity and rotation matrix constants
kR = quad_params.params.kR;
kOm = quad_params.params.kOm;
%calculating error in angular velocity and attitude
% psi_R = ;
err_R = 1/2 * vee_map(Rd'*R - R'*Rd) ;
err_Om = w - R'*Rd*w_desired ;
M = -kR*err_R - kOm*err_Om + vec_cross(w, J*w)...
- J*(hat_map(w)*R'*Rd*w_desired - R'*Rd*w_desired_dot) ;
% Equations of Motion
% -------------------
xQ_dot = v_curr;
vQ_dot = -g*e3 + (f/mass)*R*e3;
R_dot = R*hat_map(w) ;
Omega_dot = J\( -vec_cross(w, J*w) + M ) ;
% Computing xd
xd = [x_des; v_des; reshape(Rd, 9,1); w_desired];
% Computing dx
%-------------
dx = [xQ_dot;
vQ_dot;
reshape(R_dot, 9,1) ;
Omega_dot;];
% if nargout <= 1
% fprintf('Sim time %0.4f seconds \n',t);
% end
end
function[traj] = return_traj(t)
%desired trajectory xd(t) = [0.4t,0.4sin(pi*t), 0.6cos(pi*t)]
traj.pos = [0.4*t; 0.4*sin(pi*t); 0.6*cos(pi*t)];
traj.vel = [0.4; 0.4*pi*cos(pi*t); -0.6*pi*sin(pi*t)];
traj.acc = [0; -0.4*pi*pi*sin(pi*t); -0.6*pi*pi*cos(pi*t)];
traj.b1d = [cos(pi*t); sin(pi*t); 0];
% traj.b1d = [1; 0; 0];
end
function plott(t,x,xd,pos_err_fx,vel_err_fx)
disp('Plotting graphs...');
index = round(linspace(1, length(t), round(1*length(t))));
figure;
subplot(2,2,1);
plot(t(index),x(index,1),'-b','LineWidth',2); hold on;
plot(t(index),xd(index,1),':r','LineWidth',2); hold off;
grid on; title('x');legend('x pos','desired x pos');%axis equal;
xlabel('time');ylabel('x');
subplot(2,2,2);
plot(t(index),x(index,2),'-b','LineWidth',2); hold on;
plot(t(index),xd(index,2),':m','LineWidth',2); hold off;
grid on; title('y');legend('y pos','desired y pos');%axis equal;
xlabel('time');ylabel('y');
subplot(2,2,3);
plot(t(index),x(index,3),'-b','LineWidth',2); hold on;
plot(t(index),xd(index,3),':m','LineWidth',2); hold off;
grid on; title('z');legend('z pos','desired z pos');%axis equal;
xlabel('time');ylabel('z');
subplot(2,2,4);
plot3(x(index,1),x(index,2),x(index,3),'-b','LineWidth',2);hold on;
plot3(xd(index,1),xd(index,2),xd(index,3),':m','LineWidth',2); hold off;
grid on; title('trajectory');legend('trajectory','desired trajectory');%axis equal;
xlabel('x-axis');ylabel('y-axis');zlabel('z-axis');
%%%Plotting error functions
figure;
subplot(2,1,1);
plot(t(index),pos_err_fx(index),'-b','LineWidth',2);
grid on; title('error in position');
legend('{e}_x');
xlabel('Time');ylabel('{x}-{x}_d');
subplot(2,1,2);
plot(t(index),vel_err_fx(index),'-b','LineWidth',2);
grid on; title('error in velocity');legend('{e}_v');
xlabel('Time');ylabel('{v}-{v}_d');
%%%Plotting force and Moments
% figure;
% subplot(2,2,1);
% plot(t(index),f(index,1),'-b','LineWidth',2); %hold on;
% % plot(t(index),xd(index,1),':r','LineWidth',2); hold off;
% grid on; title('f');%legend('x','x_d');%axis equal;
% xlabel('time');ylabel('x [m]');
% subplot(2,2,2);
% plot(t(index),M(index,1),'-b','LineWidth',2); %hold on;
% % plot(t(index),xd(index,2),':m','LineWidth',2); hold off;
% grid on; title('M_x');%legend('y','y_d');%axis equal;
% xlabel('time');ylabel('M in x direction');
% subplot(2,2,3);
% plot(t(index),M(index,2),'-b','LineWidth',2); %hold on;
% % plot(t(index),xd(index,3),':m','LineWidth',2); hold off;
% grid on; title('z');%legend('z','z_d');%axis equal;
% xlabel('time');ylabel('M in y direction');
% subplot(2,2,4);
% plot(t(index),M(index,3),'-b','LineWidth',2); %hold on;
% % plot(t(index),xd(index,3),':m','LineWidth',2); hold off;
% grid on; title('M_z');%legend('z','z_d');%axis equal;
% xlabel('time');ylabel('M in z direction');
end