-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomActuatorStrength.m
150 lines (110 loc) · 5.46 KB
/
CustomActuatorStrength.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
%Function to calculate actuator strength as a function of system dynamics
%Last modified by Anup Teejo Mathew 02.03.2022
function u = CustomActuatorStrength(Tr,q,g,J,t,qd,Jd,M,C,F,Bq,lsqoptions)
s=10; %lengthofcurvature
Q=[pi,4,3,0]; %quaternion vector
kappa= curvat(Q,s); %curvature
phi = bendangle(Q); %angle
%transition matrix
T = transition(phi, kappa, s);
%Quaternion in terms of transition matrix (we prolly dont need this)
%Q= [sqrt(T(1,1)+T(2,2)+T(3,3)+1)/2; (T(3,2)-T(2,3))/(2*A); (T(1,3)-T(3,1))/(4*A); (T(2,1)-T(1,2))/(4*A)];
%p is the tip position (x,y,z)
p = tippos(kappa, s, phi);
%%%%%%%%%%%%%%%
eta = J*qd;
%lsqoptions = optimoptions('lsqlin','Display','off'); defined inside dynamics.m file to be used for lsqlin or lsqnonneg
%Tr: Linkage element,
%q and qd: joint coordinates and their time derivatives,
%g, J, Jd, and eta: transformation matrix, Jacobian, time derivative of jacobian, and screw velocity at every significant point of the linkage
%t: time
%M,C,F,Bq: generalized mass, coriolis, force, and actuation matrices.
%u should be (nactx1) column vector where nact is the total number of actuators.
% global us ts es
control_type = 6; % 3 for 3D, or 6 for 6D control
control_target = 'T'; % 'P' for desired point, or 'T' for desired trajectory control
%% Desired Tip Trajectory / position and orientation
if control_target == 'T' % Controller follows a set trajectory
w = pi/8;
g_fk = [0.8094 -0.5872 0 0.9574;...
0.5872 0.8094 0 0.2162;...
0 0 1 0;...
0 0 0 1.0000];
R = [1 0 0;0 cos(w*t) -sin(w*t);0 sin(w*t) cos(w*t)];
Rdot = [0 0 0;0 -w*sin(w*t) -w*cos(w*t);0 w*cos(w*t) -w*sin(w*t)];
Rdotdot = [0 0 0;0 -w^2*cos(w*t) w^2*sin(w*t);0 -w^2*sin(w*t) -w^2*cos(w*t)];
g_bar = [R,zeros(3,1);0,0,0,1]*g_fk*[R',zeros(3,1);0,0,0,1];
g_bar_dot = [Rdot,zeros(3,1);0,0,0,0]*g_fk*[R',zeros(3,1);0,0,0,1]+[R,zeros(3,1);0,0,0,1]*g_fk*[Rdot',zeros(3,1);0,0,0,0];
g_bar_dotdot = [Rdotdot,zeros(3,1);0,0,0,0]*g_fk*[R',zeros(3,1);0,0,0,1]+2*[Rdot,zeros(3,1);0,0,0,0]*g_fk*[Rdot',zeros(3,1);0,0,0,0]+[R,zeros(3,1);0,0,0,1]*g_fk*[Rdotdot',zeros(3,1);0,0,0,0];
elseif control_target == 'P' % Controller finds a desired point
% g_bar = [ 0.8759 0.3218 0.3595 0.6733;...
% -0.3502 0.9366 0.0148 -0.6516;...
% -0.3319 -0.1388 0.9330 0.0707;...
% 0 0 0 1.0000];
g_bar = [-0.6017 -0.2149 0.7693 0.1468
0.7349 0.2283 0.6386 0.5236
-0.3129 0.9496 0.0206 -0.6633
0 0 0 1.0000];
g_bar_dot = zeros(4,4);
g_bar_dotdot = zeros(4,4);
end
%% Tip position and orientation
g_tip = g(end-3:end,:);
g_error = ginv(g_tip)*g_bar;
%% Tip velocity and acceleration
eta_tip = eta(end-5:end,:);
%% Desired Tip Velocity and Accleration (in the target frame)
eta_bar_hat = (g_bar^-1)*g_bar_dot;
eta_bar_hat_dot = -eta_bar_hat^2+g_bar^-1*g_bar_dotdot;
eta_bar = [eta_bar_hat(3,2);eta_bar_hat(1,3);eta_bar_hat(2,1);eta_bar_hat(1,4);eta_bar_hat(2,4);eta_bar_hat(3,4)];
eta_bar_dot = [eta_bar_hat_dot(3,2);eta_bar_hat_dot(1,3);eta_bar_hat_dot(2,1);eta_bar_hat_dot(1,4);eta_bar_hat_dot(2,4);eta_bar_hat_dot(3,4)];
%% Error in tip velocity and Accelaration (in tip frame)
e_eta = dinamico_Adjoint(g_error)*eta_bar-eta_tip; %in tip frame
eta_bar_dot = dinamico_adj(e_eta)*dinamico_Adjoint(g_error)*eta_bar+... %in tip frame
dinamico_Adjoint(g_error)*eta_bar_dot;
%% Tip jacobian and it's derivative
J_tip = J(end-5:end,:);
Jd_tip = Jd(end-5:end,:);
%% Dynamic Equation Coeffecients
D = Tr.D;
K = Tr.K;
%% Controller
if control_type == 3 %3D
%%
J_tip_pos = J_tip(4:6,:);
Jd_tip_pos = Jd_tip(4:6,:);
e_pos = g_error(1:3,4);
e_eta_pos = e_eta(4:6);
eta_bar_dot_pos = eta_bar_dot(4:6);
m_n = size(J_tip_pos);
full_rank = min(m_n);
m = rank(J_tip_pos);
if m ~= full_rank
fprintf('Rank = %d \n',m)
fprintf('\n Jacobian is not full rank\n')
% pause
end
K_p = 120*eye(3);
K_d = 120*eye(3);
options = optimoptions('lsqlin','Algorithm','interior-point','Display','off');
u = lsqlin(J_tip_pos*(M^-1)*Bq,eta_bar_dot_pos+K_d*(e_eta_pos)+(K_p*(e_pos))+J_tip_pos*(M^-1)*((C+D)*qd+K*q-F)-Jd_tip_pos*qd,[],[],[],[],-200*ones(Tr.n_sact,1),0*ones(Tr.n_sact,1),[],options);
elseif control_type == 6 %6D
e = piecewise_logmap(1,g_error);
% m_n = size(J_tip);
% full_rank = min(m_n);
% m = rank(J_tip);
%
% if m ~= full_rank
% fprintf('Rank = %d \n',m)
% fprintf('\n Jacobian is not full rank\n')
%
% end
K_p = 100*eye(6);
K_d = 100*eye(6);
% lsqoptions = optimoptions('lsqlin','Display','off'); is defined in dynamics.m
u = lsqlin(J_tip*(M^-1)*Bq,eta_bar_dot+K_d*(e_eta)+(K_p*(e))+J_tip*(M^-1)*((C+D)*qd+K*q-F)-Jd_tip*qd,[],[],[],[],-50*ones(Tr.n_sact,1),0.001*ones(Tr.n_sact,1),[],lsqoptions);
% u = lsqnonneg(J_tip*(M^-1)*Bq,eta_bar_dot+K_d*(e_eta)+(K_p*(e))+J_tip*(M^-1)*((C+D)*qd+K*q-F)-Jd_tip*qd,[],lsqoptions);
% us=[us;u'];
% ts=[ts;t];
% es=[es;e'*e];
end