-
Notifications
You must be signed in to change notification settings - Fork 1
/
Drone.asv
222 lines (165 loc) · 5.8 KB
/
Drone.asv
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
classdef Drone < handle
%% MEMBERS
properties
g
t
dt
tf
m
l
I
x % [ X Y Z dX dY dZ phi theta psi p q r ]
r % [X Y Z]
dr % [dX dY dZ]
euler % [phi theta psi]
w % [ p q r]
dx
u % [ T M1 M2 M3 ]
T % T_sum
M % [ M1 M2 M3]'
end
properties
phi_des
phi_err
phi_err_prev
phi_err_sum
theta_des
theta_err
theta_err_prev
theta_err_sum
psi_des
psi_err
psi_err_prev
psi_err_sum
zdot_des
zdot_err
zdot_err_prev
zdot_err_sum
kP_phi
kI_phi
kD_phi
kP_theta
kI_theta
kD_theta
kP_psi
kI_psi
kD_psi
kP_zdot
kI_zdot
kD_zdot
end
%% METHODS
methods
%% CONSTRUCTOR
function obj = Drone(params, initStates,initInputs, gains, simTime)
obj.g = 9.81;
obj.t = 0.0;
obj.dt = 0.01;
obj.tf = simTime;
obj.m = params('mass');
obj.l = params('armLength');
obj.I = [params('Ixx') 0 0; 0 params('Iyy') 0; 0 0 params('Izz')];
obj.x = initStates;
obj.r = obj.x(1:3);
obj.dr = obj.x(4:6);
obj.euler = obj.x(7:9);
obj.w = obj.x(10:12);
obj.dx = zeros(12,1);
obj.u = initInputs;
obj.T = obj.u(1);
obj.M = obj.u(2:4);
obj.phi_des = 0;
obj.phi_err = 0;
obj.phi_err_prev = 0;
obj.phi_err_sum = 0;
obj.theta_des = 0;
obj.theta_err = 0;
obj.theta_err_prev = 0;
obj.theta_err_sum = 0;
obj.psi_des = 0;
obj.psi_err = 0;
obj.psi_err_prev = 0;
obj.psi_err_sum = 0;
obj.zdot_des = 0;
obj.zdot_err = 0;
obj.zdot_err_prev = 0;
obj.zdot_err_sum = 0;
obj.kP_phi = gains('P_phi');
obj.kI_phi = gains('I_phi');
obj.kD_phi = gains('D_phi');
obj.kP_theta = gains('P_theta');
obj.kI_theta = gains('I_theta');
obj.kD_theta = gains('D_theta');
obj.kP_psi = gains('P_psi');
obj.kI_psi = gains('I_psi');
obj.kD_psi = gains('D_psi');
obj.kP_zdot = gains('P_zdot');
obj.kI_zdot = gains('I_zdot');
obj.kD_zdot = gains('D_zdot');
end
function state= GetState(obj)
state = obj.x;
end
function obj = EvalEOM(obj)
bRi = RPY2Rot(obj.euler);
R = bRi';
% Translational Motions
obj.dx(1:3) = obj.dr;
obj.dx(4:6) = 1 / obj.m * ([0; 0; obj.m * obj.g] + R * obj.T * [0; 0; -1]);
% Rotational Motions
phi = obj.euler(1); theta = obj.euler(2);
obj.dx(7:9) = [1 sin(phi)*tan(theta) cos(phi)*tan(theta);
0 cos(phi) -sin(phi);
0 sin(phi)*sec(theta) cos(phi)*sec(theta)] * obj.w;
obj.dx(10:12) = (obj.I) \ (obj.M - cross(obj.w, obj.I * obj.w));
end
function obj = UpdateState(obj)
obj.t = obj.t + obj.dt;
obj.EvalEOM();
obj.x = obj.x + obj.dx .* obj.dt; % Euler method
obj.r = obj.x(1:3);
obj.dr = obj.x(4:6);
obj.euler = obj.x(7:9);
obj.w = obj.x(10:12);
end
function obj = AttitudeCtrl(obj, refSig)
% obj.u(1) = obj.m * obj.g;
% obj.u(2) = 0.0;
% obj.u(3) = 0.0;
% obj.u(4) = 0.0;
obj.phi_des = refSig(1);
obj.theta_des = refSig(2);
obj.psi_des = refSig(3);
obj.zdot_des = refSig(4);
% Error equations
obj.phi_err = obj.phi_des - obj.euler(1);
obj.theta_err = obj.theta_des - obj.euler(2);
obj.psi_err = obj.psi_des - obj.euler(3);
obj.zdot_err = obj.zdot_des - obj.dr(3);
% Error accumulators
obj.u(1) = ( obj.m * obj.g ) - (obj.kP_zdot * obj.zdot_err + ...
obj.kI_zdot * obj.zdot_err_sum + ...
obj.kD_zdot * (obj.zdot_err - obj.zdot_err_prev)/obj.dt);
obj.zdot_err_sum = obj.zdot_err_sum + obj.zdot_err;
obj.zdot_err_prev = obj.zdot_err;
obj.u(2) = (obj.kP_phi * obj.phi_err + ...
obj.kI_phi * obj.phi_err_sum + ...
obj.kD_phi * (obj.phi_err - obj.phi_err_prev)/obj.dt);
obj.phi_err_sum = obj.phi_err_sum + obj.phi_err;
obj.phi_err_prev = obj.phi_err;
obj.u(3) = (obj.kP_theta * obj.theta_err + ...
obj.kI_theta * obj.theta_err_sum + ...
obj.kD_theta * (obj.theta_err - obj.theta_err_prev)/obj.dt);
obj.phi_err_sum = obj.theta_err_sum + obj.theta_err;
obj.phi_err_prev = obj.theta_err;
obj.u(4) = (obj.kP_psi * obj.psi_err + ...
obj.kI_psi * obj.psi_err_sum + ...
obj.kD_psi * (obj.psi_err - obj.psi_err_prev)/obj.dt);
obj.psi_err_sum = obj.psi_err_sum + obj.psi_err;
obj.psi_err_prev = obj.psi_err;
% obj.u(1) = obj.m * obj.g;
obj.T = obj.u(1);
obj.M = obj.u(2:4);
end
end
end