-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQuaternionController_force.m
165 lines (127 loc) · 6.95 KB
/
QuaternionController_force.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
function output = QuaternionController(u,Params)
global prev Desired Integral
% Desired.p = u(1);
% Desired.q = u(2);
% Desired.r = u(3);
% Desired.yaw = u(4);
% Desired.phi = u(1);
% Desired.theta = u(2);
% Desired.psi = u(3);
Desired.x = u(1);
Desired.y = u(2);
Desired.z = u(3);
Desired.yaw = 0;
Psidt = 0;
x = u(1+4:17+4);
% Position
state.px = x(1);
state.py = x(2);
state.pz = x(3);
% Velocity
state.ui = x(4);
state.vi = x(5);
state.wi = x(6);
% Quaternion Pose
state.qw = x(7);
state.qx = x(8);
state.qy = x(9);
state.qz = x(10);
% Angular Velocity
state.p = x(11);
state.q = x(12);
state.r = x(13);
% Thrust Coefficients
state.CT1 = x(14);
state.CT2 = x(15);
state.CT3 = x(16);
state.CT4 = x(17);
T = Params.K*(state.CT1 + state.CT2 + state.CT3 + state.CT4);
l = Params.K*Params.d*(state.CT1 - state.CT2 - state.CT3 + state.CT4);
m = Params.K*Params.d*(state.CT1 + state.CT2 - state.CT3 - state.CT4);
n = (Params.K*Params.R/sqrt(2))*(state.CT1^3/2 - (state.CT2^3/2) + state.CT3^3/2 - (state.CT4^3/2));
Th = [Params.K Params.K Params.K Params.K;...
Params.K*Params.d -Params.K*Params.d -Params.K*Params.d Params.K*Params.d;...
Params.K*Params.d Params.K*Params.d -Params.K*Params.d -Params.K*Params.d;...
1.5*Params.K*Params.R*sqrt(state.CT1/2) -1.5*Params.K*Params.R*sqrt(state.CT2/2) 1.5*Params.K*Params.R*sqrt(state.CT3/2) -1.5*Params.K*Params.R*sqrt(state.CT4/2)];
pqrdt = [((Params.Jy-Params.Jz)/Params.Jx)*state.q*state.r;
((Params.Jz-Params.Jx)/Params.Jy)*state.p*state.r;
((Params.Jx-Params.Jy)/Params.Jz)*state.p*state.q] + [(1/Params.Jx)*l; (1/Params.Jy)*m; (1/Params.Jz)*n];
% Rotation of Velocity from Inertial Frame to Body FrameParams.
velocity.interial = [state.ui; state.vi; state.wi];
quat.scalar = state.qw;
quat.vector = [state.qx; state.qy; state.qz];
quat.full = [quat.scalar; quat.vector];
quat.inertial = transpose(quat.full)
omega.body = [state.p; state.q; state.r];
%[force.total; moment.body] = [1 1 1 1; Params.d 0 -Params.d 0; 0 Params.d 0 -Params.d; -Params.c Params.c -Params.c Params.c]*[f1; f2; f3; f4]
%force.body = [0; 0; force.total];
velocity.error = [Desired.vi(1); Desired.vi(2); Desired.vi(3)] - [state.ui; state.vi; state.wi];
% Find Desired acceleration from position error
Error.proportional = [Desired.x; Desired.y; Desired.z] - [state.px; state.py; state.pz];
Error.dot = velocity.error;
Integral.ErrorPos = Integral.ErrorPos + Error.proportional*Params.dt;
feedback.acceleration = Params.pos.kp*Error.proportional + Params.pos.ki*Integral.ErrorPos + Params.pos.kd*Error.dot;
% Calculate the change in force on the body
force.Inertial = Params.m * (feedback.acceleration - Desired.acceleration + [0; 0; Params.g]);
ForceBar.Inertial = force.Inertial/norm(force.Inertial);
% Mark Culter: Page 58
force.total = norm(force.Inertial);
% Mark Culter: Page 55
force.body = [0; 0; force.total];
ForceBar.body = force.body/norm(force.body);
% Calculate the Desired orientation
Desired.QuatNoYaw = 1/sqrt(2*(1 + ForceBar.Inertial' * ForceBar.body)) * [ 1 + ForceBar.Inertial' * ForceBar.body; cross(ForceBar.Inertial, ForceBar.body)];
Desired.quat = quatmultiply(Desired.QuatNoYaw',[cos(Desired.yaw) 0 0 sin(Desired.yaw/2)]);
feedback.jerk = (feedback.acceleration - prev.feedacclereation)/Params.dt;
Desired.jerk = (Desired.acceleration - prev.Desiredacceleration)/Params.dt;
prev.feedacclereation = feedback.acceleration;
prev.Desiredacceleration = Desired.acceleration;
xdt.ForceInertial = Params.m*(feedback.jerk + Desired.jerk);
xdt.ForceBarInertial = xdt.ForceInertial/norm(force.Inertial) - force.Inertial*(force.Inertial' * xdt.ForceInertial)/(norm(force.Inertial)^3);
% Calculate the Desired attitude rate
Desired.OmegaBody = [cross(ForceBar.Inertial,xdt.ForceBarInertial)];
Desired.OmegaBody(3) = Psidt;
% xdt.ForceBarInterialxy = cross(Desired.OmegaBody,ForceBar.Inertial);
% xdt.ForceBarInterialz = Psidt;
%% Desired vehicle orientation in the Inertial-frame
% Calulate Desired Angular Acceleration in the Body Frame
% velocity.error = [Desired.vi(1) - state.ui;
% Desired.vi(2) - state.vi;
% Desired.vi(3) - state.wi];
%
% velocity.xdt.error = [0; 0; 0];
%% Desired vehicle orientation in the Inertial-frame
%quat.inertial = quat.full';
%% Command filtered backstepping controller step 3
quat.error = quatmultiply(quat.inertial,quatconj(Desired.quat));
% Calulate Desired Angular Acceleration in the Body Frame
H = sign(quat.error(1))
WT = (force.total/Params.m)*(quat.error(1)*eye(3) - Params.Skew(quat.error(2:4)))*Params.Skew(Params.Quat2Rot(quat.inertial)*Params.E3)
%% Desired.pqr = -Params.K3 * H * transpose(quat.error(2:4)) - transpose(WT) * velocity.error - Params.Quat2Rot(quat.error) * omega.body
% Desired.pqr = -H * Params.att.Quat.kp * transpose(quat.error(2:4)) - transpose(WT) * velocity.error - Params.att.Quat.kd*(Params.Quat2Rot(quat.error)*omega.body)
%%
omega.error = Desired.OmegaBody - omega.body
quat.xdt.inertial = 0.5*Params.Iota(quat.inertial)*omega.body
quat.xdt.error = 0.5*Params.Iota(quat.error)*omega.error
velocity.xdt.error = feedback.acceleration;
[dq_0, dq_1, dq_2, dq_3] = deal_array(transpose(quat.xdt.inertial));
[dqe_0,dqe_1,dqe_2,dqe_3] = deal_array(quat.xdt.error);
[q_0,q_1,q_2,q_3] = deal_array(quat.inertial);
[qe_0,qe_1,qe_2,qe_3] = deal_array(quat.error)
dF = 0;
FuncWdt = Params.xdt.WT(force.total,dF,dq_0,dq_1,dq_2,dq_3,dqe_0,dqe_1,dqe_2,dqe_3,Params.m,q_0,q_1,q_2,q_3,qe_0,qe_1,qe_2,qe_3)
Desired.pqrdt = - H * Params.K * quat.xdt.error(2:4) - transpose(FuncWdt)*velocity.error + transpose(WT)*velocity.xdt.error + ...
Params.att.Quat.kd*(Params.Quat2Rot(quat.xdt.error)*omega.body + Params.Quat2Rot(quat.error)*pqrdt);
%% Integrate to get feedforward jerk
%moment.body = -sign(error.quat(1))*(Params.quat.kp*error.quat(2:4)') - Params.quat.kd*(omega.body - Desired.OmegaBody);
% Error.prop = [Desired.pqr(1) - state.p;
% Desired.pqr(2) - state.q;
% Desired.pqr(3) - state.r];
% Error.dot = [Desired.pqrdt(1) - pqrdt(1);
% Desired.pqrdt(2) - pqrdt(2);
% Desired.pqrdt(3) - pqrdt(3)];
%Integral.ErrorPQR = Integral.ErrorPQR + Error.prop*Params.dt;
%Desired.Thrust = force.total;
%pqrddt = Desired.pqrddt + Params.att.pqr.kp * Error.prop + Params.att.pqr.ki * Integral.ErrorPQR + Params.att.pqr.kd * Error.dot
pqrddt = -sign(quat.xdt.error(1))*(Params.att.pqr.kp*quat.xdt.error (2:4)) + Params.att.pqr.kd*(Desired.pqrdt - pqrdt)
output = [force.total, momentDt.body(1), momentDt.body(2), momentDt.body(3)];