-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLaunchCerebellumSimulinkExternalForce.m
375 lines (348 loc) · 11.3 KB
/
LaunchCerebellumSimulinkExternalForce.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
%*****************************************************************************************
% Launch m file for Simulink Cerebellar Abstract with an applied external Force
%****************************************************************************************
%% Initializing parameters
% cleaning all the possible variables
clear all
close all
% Simulation parameters of the simulink file
TrajectoryTime=1;%1sec
TimeStep=0.002; % Step Size
SimulationTime=10; %Total simulation time
% This is for storing into a File all the obtained results from
% the Simulink file
FileSuffix = 'AllLearningExternalForces';
TorqueFile = strcat('CorrectiveTorque',FileSuffix,'.mat');
TorqueIndFile = strcat('CorrectiveTorque1',FileSuffix,'.mat');
PCActivityFile = strcat('PCActivity',FileSuffix,'.mat');
MFDCNWeightFile = strcat('MFDCNWeight',FileSuffix,'.mat');
PCDCNWeightFile = strcat('PCDCNWeight',FileSuffix,'.mat');
IODCNWeightFile = strcat('IODCNWeight',FileSuffix,'.mat');
KPWeightFile = strcat('KPWeight',FileSuffix,'.mat');
IOActivityFile = strcat('IOActivity',FileSuffix,'.mat');
ErrorPositionFile = strcat('ErrorPosition',FileSuffix,'.mat');
Delay=0;
%Parameters for plotting the results
NumStep=round(TrajectoryTime/TimeStep);
NumTrajectories=round(SimulationTime/TrajectoryTime);
%% Robot load
load MANIPULATORS
%load the manipulator file with 05kg mass at the ending efector
RRedKuKa=RRedKuKa;
RRedKuKadet=RRedKuKadet05;
%% Simulink simulation framework
% launch the simulink file, at least the path an time per trajectory
% should be specified.
open_system('CerebellumIODCNAbstractFext');
set_param('CerebellumIODCNAbstractFext/ToFileTorque','Filename',TorqueFile);
set_param('CerebellumIODCNAbstractFext/ToFileTorqueInd','Filename',TorqueIndFile);
set_param('CerebellumIODCNAbstractFext/ToFilePC','Filename',PCActivityFile);
set_param('CerebellumIODCNAbstractFext/ToFileMFDCN','Filename',MFDCNWeightFile);
set_param('CerebellumIODCNAbstractFext/ToFilePCDCN','Filename',PCDCNWeightFile);
set_param('CerebellumIODCNAbstractFext/Subsystem/ErrorModel/ToFileErrorPos','Filename',ErrorPositionFile);
set_param('CerebellumIODCNAbstractFext/ToFileIO','Filename',IOActivityFile);
set_param('CerebellumIODCNAbstractFext/ToFileKP','Filename',KPWeightFile);
set_param('CerebellumIODCNAbstractFext/ToFileIODCN','Filename',IODCNWeightFile);
open_system('CerebellumIODCNAbstractFext/Subsystem');
open_system('CerebellumIODCNAbstractFext/Subsystem/ErrorModel');
save_system('CerebellumIODCNAbstractFext');
mkdir('Register');
sim('CerebellumIODCNAbstractFext',SimulationTime+Delay);
%% Plots
Torque = load(TorqueFile);
Torque = Torque.ans;
% Torque Values
figure(1)
subplot(3,1,1)
title('Corrective Torque Values')
hold on
plot(Torque(1,:),Torque(2,:),'b')
hold on
plot(Torque(1,:),Torque(3,:),'r')
hold on
plot(Torque(1,:),Torque(4,:),'k')
hold on
subplot(3,1,2)
hold on
title('Ideal Torque Values')
plot(Torque(1,:),Torque(8,:),'b')
hold on
plot(Torque(1,:),Torque(9,:),'r')
hold on
plot(Torque(1,:),Torque(6,:),'k')
subplot(3,1,3)
hold on
title('Error Torque Values')
plot(Torque(1,:),Torque(8,:)-Torque(2,:),'b')
hold on
plot(Torque(1,:),Torque(9,:)-Torque(3,:),'r')
hold on
plot(Torque(1,:),Torque(10,:)-Torque(4,:),'k')
clear Torque;
% Partial Torque Values
Torque1 = load(TorqueIndFile);
Torque1 = Torque1.ans;
figure(2)
subplot(3,1,1)
hold on
title('Corrective partial Torque Values')
hold on
plot(Torque1(1,:),Torque1(2,:),'b')
hold on
plot(Torque1(1,:),Torque1(3,:),'r')
legend('torque +q1', 'torque -q1')
hold on
subplot(3,1,2)
hold on
title('Corrective partial Torque Values')
hold on
plot(Torque1(1,:),Torque1(4,:),'b')
hold on
plot(Torque1(1,:),Torque1(5,:),'r')
legend('torque +q2', 'torque -q2')
hold on
subplot(3,1,3)
hold on
title('Corrective partial Torque ')
hold on
plot(Torque1(1,:),Torque1(6,:),'b')
hold on
plot(Torque1(1,:),Torque1(7,:),'r')
legend('torque +q3', 'torque -q3')
hold on
% Stiffnes
figure(3)
subplot(3,1,1)
title('Stiffness Joint 1')
hold on
plot(Torque1(1,:),Torque1(2,:)+Torque1(3,:),'b')
hold on
subplot(3,1,2)
title('Stiffness Joint 2')
hold on
plot(Torque1(1,:),Torque1(4,:)+Torque1(5,:),'r')
hold on
subplot(3,1,3)
title('Stiffness Joint 3')
hold on
plot(Torque1(1,:),Torque1(6,:)+Torque1(7,:),'k')
hold on
clear Torque1;
% Activity at Purkinje Cells
PCActivity = load(PCActivityFile);
PCActivity = PCActivity.ans;
figure(4)
subplot(3,1,1)
hold on
title('PC Activity Joint 1')
plot(PCActivity(1,:),PCActivity(2,:),'b')
hold on
title('PC Activity Joint 1')
plot(PCActivity(1,:),PCActivity(3,:),'r')
legend('+Q1', '-Q1')
hold on
subplot(3,1,2)
hold on
title('PC Activity Joint 2')
plot(PCActivity(1,:),PCActivity(4,:),'b')
hold on
title('PC Activity Joint 2')
plot(PCActivity(1,:),PCActivity(5,:),'r')
legend('+Q2', '-Q2')
hold on
subplot(3,1,3)
hold on
title('PC Activity Joint 3')
plot(PCActivity(1,:),PCActivity(6,:),'b')
hold on
title('PC Activity Joint 3')
plot(PCActivity(1,:),PCActivity(7,:),'r')
legend('+Q3', '-Q3')
hold on
clear PCActivity;
% Mossy Fiber -Deep Cerebellar Nuclei synaptic weights
MFDCNWeight = load(MFDCNWeightFile);
MFDCNWeight = MFDCNWeight.ans;
figure(5)
subplot(3,1,1)
title('MFDCN Weight Joint 1')
hold on
plot(MFDCNWeight(1,:),MFDCNWeight(2,:),'b')
hold on
title('MFDCN Weight Joint 1')
plot(MFDCNWeight(1,:),MFDCNWeight(3,:),'r')
legend('+Q1', '-Q1')
hold on
subplot(3,1,2)
title('MFDCN Weight Joint 2')
plot(MFDCNWeight(1,:),MFDCNWeight(4,:),'b')
hold on
title('MFDCN Weight Joint 2')
plot(MFDCNWeight(1,:),MFDCNWeight(5,:),'r')
legend('+Q2', '-Q2')
hold on
subplot(3,1,3)
hold on
title('MFDCN Weight Joint 3')
plot(MFDCNWeight(1,:),MFDCNWeight(6,:),'b')
hold on
title('MFDCN Weight Joint 3')
plot(MFDCNWeight(1,:),MFDCNWeight(7,:),'r')
legend('+Q3', '-Q3')
hold on
clear MFDCNWeight;
% Purkinje Cell- Deep Cerebellar Nuclei synaptic weights
PCDCNWeight = load(PCDCNWeightFile);
PCDCNWeight = PCDCNWeight.ans;
figure(6)
subplot(3,1,1)
title('PCDCN Weight Joint 1')
hold on
plot(PCDCNWeight(1,:),PCDCNWeight(2,:),'b')
hold on
title('PCDCN Weight Joint 1')
plot(PCDCNWeight(1,:),PCDCNWeight(3,:),'r')
legend('+Q1', '-Q1')
hold on
subplot(3,1,2)
title('PCDCN Weight Joint 2')
plot(PCDCNWeight(1,:),PCDCNWeight(4,:),'b')
hold on
title('PCDCN Weight Joint 2')
plot(PCDCNWeight(1,:),PCDCNWeight(5,:),'r')
legend('+Q2', '-Q2')
hold on
subplot(3,1,3)
title('PCDCN Weight Joint 3')
plot(PCDCNWeight(1,:),PCDCNWeight(6,:),'b')
hold on
title('PCDCN Weight Joint 3')
plot(PCDCNWeight(1,:),PCDCNWeight(7,:),'r')
legend('+Q3', '-Q3')
hold on
clear PCDCNWeight;
% Inferior Olive -Deep Cerebellar Nuclei synaptic weights
IODCNWeight = load(IODCNWeightFile);
IODCNWeight = IODCNWeight.ans;
figure(7)
subplot(3,1,1)
title('IODCN Weight Joint 1')
hold on
plot(IODCNWeight(1,:),IODCNWeight(2,:),'b')
hold on
title('IODCN Weight Joint 1')
plot(IODCNWeight(1,:),IODCNWeight(3,:),'r')
legend('+Q1', '-Q1')
hold on
subplot(3,1,2)
title('IODCN Weight Joint 2')
plot(IODCNWeight(1,:),IODCNWeight(4,:),'b')
hold on
title('IODCN Weight Joint 2')
plot(IODCNWeight(1,:),IODCNWeight(5,:),'r')
legend('+Q2', '-Q2')
hold on
subplot(3,1,3)
title('IODCN Weight Joint 3')
plot(IODCNWeight(1,:),IODCNWeight(6,:),'b')
hold on
title('IODCN Weight Joint 3')
plot(IODCNWeight(1,:),IODCNWeight(7,:),'r')
legend('+Q3', '-Q3')
hold on
% kp synaptic weights Evolution
KPWeight = load(KPWeightFile);
KPWeight = KPWeight.ans;
figure(8)
subplot(3,1,1)
title('KP Weight Joint 1')
hold on
plot(KPWeight(1,:),KPWeight(2,:),'b')
hold on
title('KP Weight Joint 1')
plot(KPWeight(1,:),KPWeight(3,:),'r')
legend('+Q1', '-Q1')
hold on
subplot(3,1,2)
title('KP Weight Joint 2')
plot(KPWeight(1,:),KPWeight(4,:),'b')
hold on
title('KP Weight Joint 2')
plot(KPWeight(1,:),KPWeight(5,:),'r')
legend('+Q2', '-Q2')
hold on
subplot(3,1,3)
title('KP Weight Joint 3')
plot(KPWeight(1,:),KPWeight(6,:),'b')
hold on
title('KP Weight Joint 3')
plot(KPWeight(1,:),KPWeight(7,:),'r')
legend('+Q3', '-Q3')
hold on
% KP*Inferior Olive -Deep Cerebellar Nuclei synaptic weights
figure(9)
subplot(3,1,1)
title('KP*IODCN Weight Joint 1')
hold on
plot(KPWeight(1,:),KPWeight(2,:).*IODCNWeight(2,:),'b')
hold on
title('KP*IODCN Weight Joint 1')
plot(KPWeight(1,:),KPWeight(3,:).*IODCNWeight(3,:),'r')
legend('+Q1', '-Q1')
hold on
subplot(3,1,2)
title('KP*IODCN Weight Joint 2')
plot(KPWeight(1,:),KPWeight(4,:).*IODCNWeight(4,:),'b')
hold on
title('KP*IODCN Weight Joint 2')
plot(KPWeight(1,:),KPWeight(5,:).*IODCNWeight(5,:),'r')
legend('+Q2', '-Q2')
hold on
subplot(3,1,3)
title('KP*IODCN Weight Joint 3')
plot(KPWeight(1,:),KPWeight(6,:).*IODCNWeight(6,:),'b')
hold on
title('KP*IODCN Weight Joint 3')
plot(KPWeight(1,:),KPWeight(7,:).*IODCNWeight(7,:),'r')
legend('+Q3', '-Q3')
hold on
clear KPWeight;
clear IODCNWeight;
% Position Error Evolution
ErrorPos = load(ErrorPositionFile);
ErrorPos = ErrorPos.ans;
figure(10)
title('Position Error')
plot(ErrorPos(1,:),ErrorPos(2,:),'b')
hold on
plot(ErrorPos(1,:),ErrorPos(3,:),'r')
hold on
plot(ErrorPos(1,:),ErrorPos(4,:),'k')
legend('joint 1', 'joint 2','joint3')
ErrorPos1=ErrorPos(2,:)';
ErrorPos2=ErrorPos(3,:)';
ErrorPos3=ErrorPos(4,:)';
clear ErrorPos;
for i=1:NumTrajectories,
Mae1(i)=mae(ErrorPos1(NumStep*(i-1)+1:(NumStep*(i))-1));
Mae2(i)=mae(ErrorPos2(NumStep*(i-1)+1:(NumStep*(i))-1));
Mae3(i)=mae(ErrorPos3(NumStep*(i-1)+1:(NumStep*(i))-1));
end
figure(11)
subplot(3,1,1)
title('MAE error Joint1 (Position)')
hold on
plot(Mae1,'b')
hold on
subplot(3,1,2)
title('MAE error Joint2 (Position)')
hold on
plot(Mae2,'r')
hold on
subplot(3,1,3)
title('MAE error Joint3 (Position)')
hold on
plot(Mae3,'k')
hold on
save_system('CerebellumIODCNAbstractFext');
close_system('CerebellumIODCNAbstractFext');