-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrunJLSPPCExamples.m
More file actions
213 lines (149 loc) · 5.01 KB
/
Copy pathrunJLSPPCExamples.m
File metadata and controls
213 lines (149 loc) · 5.01 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
% Script to run a simple simulation of JLS-PPC
% Calls simJLSPPC (which calls functions in core)
% Basic setup:
% scalar or double integrator (SISO or MIMO) systems
% a few options for schedules
% handles varying lengths of ACK histories
% some simple plots for at end
% Brooks Reed
% brooksr8@gmail.com
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear variables
%close all
%clc
print_debug = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SYSTEM DEFINITION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SIM LENGTH
SIM_LENGTH = 300; % sim length
% MPC HORIZON:
N_HORIZON = 40; % (consider N_VEH, and T_S when setting this)
%%%%%%%%%%%
% SYSTEM (set up in setupSystemJPLPPC)
% scalar integrator
%system = 'SCALAR';N_VEH = 1;
% mass with force input, position feedback
%system = 'SISO_DOUBLE_INTEGRATOR';N_VEH = 1;
% mass with force and velocity input, position and velocity feedback
% separate comms channels for each input and output
%system = 'MIMO_DOUBLE_INTEGRATOR';N_VEH = 2;
% 1D "formation flying" - relative measurements
system = 'MIMO_RELATIVE_MEASUREMENTS';N_VEH = 10;
%%%%%%%%%%%
% SCHEDULE
% right now, "*_piggyback or *_noACK" are only options
if( ~isempty(strfind(system,'SISO')) || ~isempty(strfind(system,'SCALAR')))
% 'SISOALL' - [1],[1] - std. discrete time
% 'SISO2' - [1 0], [0 1] for pi, xi
% 'SISO4' - [1 0 0 0], [0 0 1 0] for pi, xi
%sched = 'SISO4_piggyback';
%sched = 'SISO4_noACK';
%sched = 'SISO2_noACK';
sched = 'SISO2_piggyback';
%sched = 'SISOALL_piggyback';
%sched = 'SISOALL_noACK';
elseif( ~isempty(strfind(system,'MIMO')))
% 'MIMO' options: MX, IL
%sched = 'MX_piggyback';
%sched = 'MX_noACK';
sched = 'IL_piggyback';
%sched = 'IL_noACK';
end
% DELAYS
TAU_C = 1; % control delay
TAU_M = 1; % meas delay (Also ACK delay with piggyback unless overwritten)
TAU_A = 1; % ACK delay
% ACK SETTINGS
% Length of ACK history sent
% Should be 1 + a multiple of schedule length = 1+T_S*N_ACKHISTORY_PERIODS
%N_ACKHISTORY_PERIODS = 0;
%N_ACHISTORY_PERIODS = 1;
N_ACKHISTORY_PERIODS = 5;
% adjustment to covariance priors due to no ACKs/control losses:
cov_prior_adj = 1;
%%%%%%%%%%%%%
% PACKET SUCCESS PROBABILITIES
% Nv is number of control AND meas channels (asymmetric # not implemented)
if( ~isempty(strfind(system,'SISO')) || ~isempty(strfind(system,'SCALAR')))
ALPHAC_BAR = .6; % controls
ALPHAM_BAR = .6; % measurements
ALPHAA_BAR = .6; % ACKs (if piggyback used, betaBar overrides gammaBar)
else
ALPHAC_BAR = .9*ones(N_VEH,1);
ALPHAM_BAR = .9*ones(N_VEH,1);
ALPHAA_BAR = .9*ones(N_VEH,1);
end
%% system setup
% (system params are in setupSystemJLSPPC script)
setupSystemJLSPPC
% 'raw' ACK history length as fcn of periods to be ACK'd
N_ACKHISTORY = 1+T_S*N_ACKHISTORY_PERIODS;
% initial conditions
x_IC = 5*randn(size(A,1),1);
if(size(A,1)>1)
% position only, no initial velocity (so like step resp from rest)
x_IC(2:2:end) = 0;
end
% (IF WANT TO DEBUG CONTROLLER - INIT ESTIMATOR PERFECTLY)
% xHat1 = xIC;P1 = 1*eye(2);
% (SOME HARDCODED DEBUG SEQUENCES FOR MEAS/ACKS)
%{
% tests w/ 1 then 2 then 3 missed ACKs
if(T_S==1)
alpha_m(1:12) = [1 1 1 0 1 0 0 1 0 0 0 1];
elseif(T_S==2)
alpha_m(1:22) = [0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1];
elseif(T_S==4)
alpha_m(1:23) = [0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1];
end
%}
if(strfind(sched,'piggyback'))
% ACK piggybacked to measurement
alpha_a = alpha_m; % overwrite
end
%% call sim fcn
tic
[r] = simJLSPPC(SIM_LENGTH,N_HORIZON,A,Bu,Bw,C,Q,Qf,R,W,V,TAU_M,TAU_C,...
TAU_A,TAU_AC,ALPHAC_BAR,PI_C,PI_M,PI_A,T_S,U_MAX,U_MIN,CODEBOOK,...
X_MAX,X_MIN,x_IC,P_1,x_hat_1,w,v,alpha_c,alpha_m,alpha_a,...
cov_prior_adj,N_ACKHISTORY,print_debug);
fprintf('\n TOTAL SIMULATION TIME: %f\n',toc)
r.sys.sched = sched;
r.sys.system =system;
r.sys.ALPHAM_BAR = ALPHAM_BAR;
r.sys.ALPHAA_BAR = ALPHAA_BAR;
% (save r struct here if want)
%{
fname = sprintf('results_%s',dateString('DHM'));
save(fname,'r')
%or rename r
uniquer = r;
save(fname,'uniquer')
%}
%% plots
% load results struct named "r"
if(size(r.sys.C,1)==1 && size(r.sys.Bu,2)==1)
plotJLSPPC_SISO(r)
else
% very simple MIMO plot:
NX_SYS = size(r.P,1); % underlying system states (no buffer)
SIM_LENGTH = size(r.X,2);
CPlot = r.sys.C;
if(strcmp(system,'MIMO_RELATIVE_MEASUREMENTS'))
CPlot = CPlot>0;
% (plots positions of each mass, as opposed to the measured output
% which is their relative positions)
end
figure
subplot(3,1,[1 2])
hx = plot(0:SIM_LENGTH-1,CPlot*r.X(1:NX_SYS,:));
hold on
hxh = plot(0:SIM_LENGTH-1,CPlot*r.Xh(1:NX_SYS,:),':');
legend([hx(1) hxh(1)],'X','XHat')
title('MIMO System (colors are i/o channels)')
subplot(3,1,3)
hu = stairs(repmat(0:SIM_LENGTH-1,[N_VEH,1])',r.u');
xlabel('time step')
ylabel('u')
end