-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlockClassifcationsDraft.m
358 lines (317 loc) · 9.58 KB
/
BlockClassifcationsDraft.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
clear; clc; close all; instrreset;
%% Connect to Device
r = MKR_MotorCarrier;
%% Define boxes, count, and setup for saving data
blockClass = [0 1 2]; % Zero bad, One good, 2 Excellent
trialCount = 25;
%trialCount = 6;
saveDirectory = [pwd, '\data'];
%% Collect Data
% Setup places to store data and start data stream
blockCount = length(blockClass);
data = cell(blockCount, trialCount);
r.startStream('analog');
% temp data holders
red = 0;
green = 0;
blue = 0;
ClawSize = 0;
% readings = zeros(3, 1);
readings = zeros(5, 1);
a = 1;
b = 1;
BadMin = zeros(5,1);
BadMax = zeros(5,1);
GoodMin = zeros(5,1);
GoodMax = zeros(5,1);
ExcelMin = zeros(5,1);
ExcelMax = zeros(5,1);
first = true;
for class = 1:3
% Begin gathering data
if(class == 1)
countdown("Beginning Recording Bad blocks in", 3);
elseif(class == 2)
countdown("Beginning Recording Good blocks in", 3);
else
countdown("Beginning Recording Excellent blocks in", 3);
end
for x = 1:trialCount
if(class == 1)
fprintf("Please Place Bad Block, Trial No. %d\n", x);
elseif(class == 2)
fprintf("Please Place Good Block, Trial No. %d\n", x);
else
fprintf("Please Place Excellent Block, Trial No. %d\n", x);
end
while(input("Start recording\n","s") ~= "start")
end
fprintf("reading data");
[red, green, blue] = r.rgbRead();
AnalogData = r.getAverageData('analog', 10);
ClawSize = CloseClaw(r);
pause(0.2)
readings(1, 1) = AnalogData(1);
readings(2, 1) = red;
readings(3, 1) = green;
readings(4, 1) = blue;
readings(5, 1) = ClawSize;
% readings(1, 1) = AnalogData(1);
% readings(2, 1) = red + green + blue;
% readings(3, 1) = ClawSize;
if(first == true)
BadMin = readings;
BadMax = readings;
first = false;
else
BadMin = CalcNewMins(readings, BadMin);
BadMax = CalcNewMaxs(readings, BadMax);
end
data{a, b} = readings;
b = b + 1;
pause(0.5);
clc;
end
a = a + 1;
b = 1;
first = true;
end
r.stopStream('analog'); % stop streaming accelerometer
r.close; % disconnect from the MKR
pause(1); % wait a second
clc; % clear command line
if ~exist(saveDirectory, 'dir') %check if a data folder already exists
mkdir(saveDirectory) %make folder if not
end
filename = ['data_2_', getenv('COMPUTERNAME'), '_', datestr(now,'yyyy-mm-dd-HH-MM-ss')];
save(filename,'data') %save data
% For Loading Data
% matObj = matfile('nameoffile.mat');
% then do
% data = matObj.data;
% this will read the cell from the struct into data
%% Normalize Data
% Normalize Data formula (X - Xmin) / (Xmax - Xmin)
dataCell = zeros(3,1);
% Normalize Bad Row of Cell
% Setup Denominators for Bad's RGB and HallEffect
DenomHe = BadMax(1, 1) - BadMin(1, 1);
DenomRGB = BadMax(2, 1) - BadMin(2, 1);
for b = 1:trialCount
dataCell = data{1, b};
dataCell(1,1) = (dataCell(1,1) - BadMin(1,1)) / DenomHe;
dataCell(2,1) = (dataCell(2,1) - BadMin(2,1)) / DenomRGB;
data{1,b} = dataCell;
end
% Normalize Good Row of Cell
DenomHe = GoodMax(1, 1) - GoodMin(1, 1);
DenomRGB = GoodMax(2, 1) - GoodMin(2, 1);
for b = 1:trialCount
dataCell = data{2, b};
dataCell(1,1) = (dataCell(1,1) - GoodMin(1,1)) / DenomHe;
dataCell(2,1) = (dataCell(2,1) - GoodMin(2,1)) / DenomRGB;
data{2,b} = dataCell;
end
%Normalize Excellent Row of Cell
DenomHe = ExcelMax(1, 1) - ExcelMin(1, 1);
DenomRGB = ExcelMax(2, 1) - ExcelMin(2, 1);
for b = 1:trialCount
dataCell = data{3, b};
dataCell(1,1) = (dataCell(1,1) - ExcelMin(1,1)) / DenomHe;
dataCell(2,1) = (dataCell(2,1) - ExcelMin(2,1)) / DenomRGB;
data{3,b} = dataCell;
end
%% Calculate Features
Features = zeros(blockCount, trialCount, 5);
for y = 1:blockCount
for z = 1:trialCount
blockType = data{y, z};
Features(y, z, :) = mean(blockType');
end
end
%% Plot Features
figure(); hold on; grid on;
for a = 1:blockCount
scatter3(Features(a,:,1), Features(a, :, 2), Features(a,:,3), Features(a,:,4), Features(a,:,5), 'filled');
end
%% Perform Linear Discriminate analysis
TrainingFeatures = reshape(Features, [trialCount*blockCount, 5]);
TrainingLabels = repmat(blockClass, [2, trialCount]);
LDA = fitcdiscr(TrainingFeatures, TrainingLabels);
%% Plot again
figure(); hold on; grid on;
for a = 1:blockCount
scatter3(Features(a,:,1), Features(a, :, 2), Features(a,:,3), Features(a,:,4), Features(a,:,5), 'filled');
end
limits = [xlim, ylim, zlim];
K = LDA.Coeffs(1, 2).Const;
L = LDA.coeffs(1, 2).Linear;
f = @(x1, x2, x3) K + L(1)*x1 + L(2)*x2 + L(3)*x3;
h2 = fimplicit(f, limits);
%% get raw data
training_examples_raw = zeros(5,75);
i = 1;
for row = 3:5
temp = cell2mat(data(row,:));
for col = temp
training_examples_raw(:,i) = col;
i = i+1;
end
end
training_examples = training_examples_raw;
%% Normalize data
MAX_SIZE = max(training_examples(5,:));
MIN_SIZE = min(training_examples(5,:));
MAX_HALL = max(training_examples(1,:));
MIN_HALL = min(training_examples(1,:));
CONSTS = [MAX_SIZE,MIN_SIZE,MAX_HALL,MIN_HALL];
colors = (training_examples_raw(2:4,:));
hall_effect = training_examples(1,:);
size_b = training_examples(5,:);
training_examples(5,:) = (size_b - min(size_b))./(max(size_b) - min(size_b)); %normalize size
training_examples(2:4,:) = training_examples(2:4,:)./255; %normalize colors
training_examples(1,:) = (training_examples(1,:) - min(hall_effect))./(max(hall_effect) - min(hall_effect));
colors = (training_examples(2:4,:));
hall_effect = training_examples(1,:);
size_b = training_examples(5,:);
% coef = pca(colors);
Y = zeros(1,75);
Y(1:25) = 0; %% bad == 0
Y(26:50) = 1; %% good == 1
Y(51:75) = 2; %% excellent == 2
%% Visualization red
% scatter3(hall_effect(1:25), size(1:25), colors(1,1:25),50, "red", 'o') % bad
% hold on
% scatter3(hall_effect(26:50), size(26:50), colors(1,26:50),50, "blue", '+') % good
% hold on
% scatter3(hall_effect(51:75), size(51:75), colors(1,51:75),50, "green", '*') % excellent
% hold on
% % Visualization green
% scatter3(hall_effect(1:25), size(1:25), colors(2,1:25),50, "red", 'o') % bad
% hold on
% scatter3(hall_effect(26:50), size(26:50), colors(2,26:50),50, "blue", '+') % good
% hold on
% scatter3(hall_effect(51:75), size(51:75), colors(2,51:75),50, "green", '*') % excellent
% hold on
% % Visualization blue
% scatter3(hall_effect(1:25), size(1:25), colors(3,1:25),50, "red", 'o') % bad
% hold on
% scatter3(hall_effect(26:50), size(26:50), colors(3,26:50),50, "blue", '+') % good
% hold on
% scatter3(hall_effect(51:75), size(51:75), colors(3,51:75),50, "green", '*') % excellent
%% pca
% scatter3(hall_effect(1:25), size(1:25), coef(1:25,1)',50, "red", 'o') % bad
% hold on
% scatter3(hall_effect(26:50), size(26:50), coef(26:50,1)',50, "blue", '+') % good
% hold on
% scatter3(hall_effect(51:75), size(51:75), coef(51:75,1)',50, "green", '*') % excellent
%% separate into training and testing sets
train_set = zeros(5,65);
test_set = zeros(5,10);
Y_train = zeros(1,65);
Y_test = zeros(1,10);
i = 1; j = 1;
for cat = 1:3
offset = 15*(cat-1);
for col = 1:15
train_set(:,i) = training_examples(:,col + offset);
Y_train(:,i) = Y(:,col + offset);
i = i+1;
end
for col = 16:25
test_set(:,j) = training_examples(:, col + offset);
Y_test(:,j) = Y(:,col + offset);
j = j+1;
end
end
classes = unique(Y);
%%
X(:,1:150) = training_examples_1;
X(:,151:300) = training_examples;
Result(:,1:150) = Y;
Result(:,151:300) = Y;
%% SVM
t = templateSVM('Standardize',true,'KernelFunction','polynomial');
model = fitcecoc(training_examples', Y,'Learners',t, 'FitPosterior',true,...
'ClassNames', classes);
%% Test
correct = 0;
for i = 1:10
result = model.predict(test_set(:,i)') == Y_test(i);
if(result == true)
correct = correct +1;
end
end
acc = round(correct./10*100);
fprintf("Accuracy %d\n", acc);
%% Claw Code Function
function size = CloseClaw(obj)
obj.servo(4,0);
previousAnalogVal = 0;
currentAnalogVal = 0;
for i = 10:10:180
obj.servo(4, i);
currentAnalogVal = obj.getAverageData('analog', 5);
pause(0.1);
highTresh = 0;
lowTresh = 0;
if( i < 50)
highTresh = (previousAnalogVal + 0.02);
lowTresh = (previousAnalogVal - 0.02);
else
highTresh = (previousAnalogVal + 10);
lowTresh = (previousAnalogVal - 10);
end
if( (currentAnalogVal(2) <= highTresh ) && (currentAnalogVal(2)>= lowTresh ) )
try
%s 47 i 140, m 35 i 110 , B 23 i 80
if (i <= 80 )
obj.servo(4, i - 23); %s 47 m 35 , B 23
disp('Big')
size = i - 23;
% size = 1;
break;
elseif(i > 80 && i < 115)
obj.servo(4, i - 30);
disp('Medium')
size = i - 30;
% size = 0.5;
break;
else
obj.servo(4, i - 25);
disp('Small')
size = i - 25;
% size = 0;
break;
end
catch
disp('not the end yet')
end
end
previousAnalogVal = currentAnalogVal(2);
pause(0.5);
end
obj.servo(4, 0);
end
function NewMins = CalcNewMins(readings, previousVals)
tempVals = zeros(3,1);
for i = 1:5
if(readings(i, 1) < previousVals(i, 1))
tempVals(i, 1) = readings(i, 1);
else
tempVals(i, 1) = previousVals(i ,1);
end
end
NewMins = tempVals;
end
function NewMaxs = CalcNewMaxs(readings, previousVals)
tempVals = zeros(3,1);
for i = 1:5
if(readings(i, 1) > previousVals(i, 1))
tempVals(i, 1) = readings(i, 1);
else
tempVals(i, 1) = previousVals(i ,1);
end
end
NewMaxs = tempVals;
end