-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf.m
More file actions
252 lines (214 loc) · 5.49 KB
/
f.m
File metadata and controls
252 lines (214 loc) · 5.49 KB
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Project: Simulation of a hybrid system
% Description: Flow map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function xdot_vec = f(state_vec)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%11/03/16
global n_states n_agents;
global x1_d_vec x1_dd_vec x3_d_vec fc_d_vec;
global kp_vec kd_vec kp_y_vec;
global gamma1_vec gamma2_vec;
global kc bc kf;
global epsilon1 epsilon2 threshold;
m = n_states;
n = n_agents;
global nFlag;
global initT maxT eta etaCheck bSetTimer alpha v0;
% states %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
states = zeros(m,n);
if(length(state_vec) == m*n)
for i = 1:n
for jj = 1:m
states(jj,i) = state_vec(jj + m*(i-1));
end
end
else
end % if(length(state_vec) == m*n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 07/31/17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global x0_vec;
y0 = 0.0; % initial y-position
global rho phi_d epsilon_ang phi v0;
% rho = 0.3;
% phi_d = 0.0;
% epsilon_ang = 0.001;
global bImpact tImpact bForce tForce;
global bTurning;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xdot_vec = [];
for k = 1:n
% x1 = x(1); % x1, x position
% x2 = x(2); % x2, x velocity
% x3 = x(3); % x3, y position
% x4 = x(4); % x4, y velocity
% qf = x(5);
% qh = x(6);
x1 = states(1,k);
x2 = states(2,k);
x3 = states(3,k);
qf = states(5,k);
qh = states(6,k);
tau = states(7,k);
time = states(8,k);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
omega = states(9,k);
v = states(10,k);
y0 = x0_vec(3,k); % initial y-position
% phi_d = phi(k);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_d(1) = x1_d_vec(k);
x_dd(1) = x1_dd_vec(k);
x_d(2) = x3_d_vec(k);
fc_d = fc_d_vec(k);
gamma1 = gamma1_vec(k);
gamma2 = gamma2_vec(k);
kp = kp_vec(k);
kd = kd_vec(k);
kp_y = kp_y_vec(k);
fc = 0;
u = 0;
if(x1 >= 0)
fc = kc*x1 + bc*x2;
else
fc = 0;
end
%%%%%%%%%%
if(bSetTimer == 0)
etaCheck = zeros(1,n_agents);
if (nFlag <= n_states)
if nFlag < n_states
maxT = 0;
end
initT = zeros(1,k);
for k = 1:n
kp = kp_vec(k);
kd = kd_vec(k);
x1_0 = states(1,k);
x2_0 = states(2,k);
x1_d = x1_d_vec(k);
z1 = states(1,k);
etaCheck(k) = eta_check(kp,kd,x1_0,v0*alpha,x1_d);
eta = etaCheck;
if(eta(k)>=maxT)
maxT = eta(k);
maxT;
end
initT(k) = maxT - eta(k);
end
end
% bSetTimer = 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add noise
global noise1 noise2 noise3 noise4 noise5 n1 n2 n3 n4 n5 maxCnt cnt_n time_old;
%
% n1 = noise1(cnt_n,1);
% n2 = noise2(cnt_n,1);
% n3 = noise3(cnt_n,1);
% n4 = noise3(cnt_n,1);
% n5 = noise3(cnt_n,1);
if (time - time_old) > 0.01
cnt_n = cnt_n + 1;
time_old = time;
end
n1 = 0;
n2 = 0;
n3 = 0;
n4 = 0;
n5 = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1_error = x1-x_d(1);
x3_error = x3-x_d(2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xdot = zeros(m,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% noise
% x1 = x1 + n3;
% x3 = x3 + n4;
% omega = omega + n1;
% v = v + n2;
% fc = fc + n5;
if omega <= -pi/2
omega = -pi/2;
elseif omega >= pi/2
omega = pi/2;
end
% horizontal - position controller
if ((qf == 0) && (qh == 1) && (fc <= gamma2))
xdot(1) = v;
xdot(3) = 0.0;
xdot(1) = v * cos(omega);
xdot(1) = xdot(1) + n1;
xdot(10) = -kp * x1_error -kd * x2 - fc;
%
xdot(10) = xdot(10) + n4; % v
xdot(2) = xdot(10);
xdot(1) = x2;
% horizontal - force controller
elseif ((qf == 1) && (qh == 1) && (fc >= gamma1))
xdot(1) = v;
v;
xdot(10) = kf * (fc_d - fc);
xdot(1) = v * cos(omega);
xdot(1) = xdot(1) + n1;
xdot(10) = xdot(10) + n4;
xdot(2) = xdot(10);
xdot(1) = x2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Turning - position controller
elseif((qf == 0) && (qh == 4) && (abs(x3_error) >= epsilon1) && (x1 <= x_dd(1) && (bTurning(k) == 1)))
if abs(x3_error) <= rho
if sign(omega) == sign(y0)
xdot(9) = 0.0;
else
xdot(9) = sign(y0);
end
end
xdot(1) = v * cos(omega);
xdot(3) = v * sin(omega);
xdot(1) = xdot(1) + n1; % position x
xdot(3) = xdot(3) + n2; % position y
% xdot(9) = xdot(9) + n3; % omega
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif((qf == 0) && (qh == 2) && (abs(x3_error) <= epsilon1) && (nFlag <= n_agents))
% xdot(1) = 0.0;
% xdot(3) = 0.0;
% xdot(9) = 0.0;
% xdot(10) = 0.0;
%
% vertical - position controller
elseif((qh == 0) && (qf == 0) && (abs(x3_error) >= epsilon1) && (x1 <= x_dd(1)))
xdot(1) = 0.0;
xdot(3) = -v * sign(y0);
xdot(1) = xdot(1) + n1; % position x
xdot(3) = xdot(3) + n2; % position y
%
elseif((qf == 0) && (qh == 3) && (tau >= 0) && (bSetTimer == 1))
xdot(7) = -1;
else
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xdot(9) = xdot(9) + n3; % omega
xdot(10) = xdot(10) + n4; % v
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% time
xdot(8) = 1;
xdot_vec = [xdot_vec; xdot];
if (bImpact(k) == 0) && (x1 >= 0.0)
bImpact(k) = 1;
tImpact(k) = time;
% k, time, x1, qf
end
if (bForce(k) == 0) && (qf == 1)
bForce(k) = 1;
tForce(k) = time;
% k
% time
% x1
%
end
end % for k = 1:n
end