-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterdigitationCharacterisation.pde
More file actions
435 lines (348 loc) · 12.7 KB
/
InterdigitationCharacterisation.pde
File metadata and controls
435 lines (348 loc) · 12.7 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// This program draws virtual pressure sensing strips with interdigitated
// shapes (zigzags for now), and a virtual finger that swipes it.
// To identify each strip, it uses different colors, and to simulate the
// effect of a finger on it, we just count how many pixels it hides.
int zzSpikeCount = 1; // zig-zag count
float zzSpacingRatio = 0.1; // space between strips
float fingerRatio = 1.25; // unit = distance between 2 strip centers
// The following global variables (in pixels) are calculated in setup()
int zzUnitWidth; // stripe + spacing width
int zzStrokeWidth; // stripe width - depends on screen size
int zzSpacingWidth;
int fingerWidth;
// The following global variables should not need to be modified
int stripNumber = 7;
int interStep = 10;
int colorRef = stripNumber * interStep;
int globalMax = 0;
int[] pressureIndices = new int[stripNumber];
boolean isCharacterizing = true;
int fingerPos, originalFingerPos, finalFingerPos;
int measureStep;
int retrievedPos = 0;
int[] errors;
int[] retrievedPositions;
/////////////////////////////////////////////////////////////////
void setup() {
colorMode(HSB, colorRef);
size(600, 250);
zzUnitWidth = Math.round(width / stripNumber);
zzSpacingWidth = Math.round(zzUnitWidth * zzSpacingRatio);
zzStrokeWidth = zzUnitWidth - zzSpacingWidth;
// TODO: USE FLOATS!!
// We want 71 data points across 3.5 stripes:
originalFingerPos = Math.round(1.75 * zzUnitWidth); // 1.75 to center the measures
finalFingerPos = Math.round(3.5 * zzUnitWidth + originalFingerPos);
measureStep = Math.round((finalFingerPos - originalFingerPos) / 71);
fingerWidth = Math.round(fingerRatio * zzStrokeWidth);
fingerPos = originalFingerPos;
errors = new int[width];
retrievedPositions = new int[width];
println("Partial characterization of line strip:");
println("normalized real position, normalized retrieved position");
// run the histogram once to initialize globalMax
drawBackground();
strokeWeight(0);
fill(0, 0, colorRef, 2*colorRef/3); // finger color
rect(0, 0, width, fingerWidth); // simulate a wide finger
histogram();
}
/////////////////////////////////////////////////////////////////
void draw() {
// draw strips
drawBackground();
// draw finger:
color white = color(0, 0, colorRef);
drawFinger(fingerPos, fingerWidth, white);
// analyse finger impact on sensor stripes and simulate raw sensor
int[] niceData = histogram();
// visualize the estimated finger position using raw sensor data
retrievedPos = drawCubicRetrievedFinger(niceData);
if (!isCharacterizing) {
fingerPos = mouseX;
} else {
characterization(fingerPos);
fingerPos += measureStep;
}
}
/////////////////////////////////////////////////////////////////
void characterization(int fingerPos) {
// Save characterization data to graph is later
if (retrievedPos > 0) {
// compute the error:
errors[fingerPos] = abs(fingerPos - retrievedPos);
retrievedPositions[fingerPos] = retrievedPos;
}
// is the simulation finished?
if (fingerPos >= finalFingerPos) {
// Plot characterization
drawBackground();
int count = 0;
stroke(0);
strokeWeight(6);
for (int i = measureStep; i < width; i++) {
line(i-1, height - errors[i-measureStep],
i, height - errors[i]);
if (retrievedPositions[i] > 0 && count++ < 71) {
float normalizedPos = float(i)/width;
float normalizedRetrivedPos = float(retrievedPositions[i])/width;
println(normalizedPos, ", ", normalizedRetrivedPos);
}
}
// draw finger in the middle as reference:
color c = color(0, 0, colorRef, 2*colorRef/3);
drawFinger(width/2, fingerWidth, c);
String fileName = "charact_count" + zzSpikeCount +
"_spacing" + zzSpacingRatio*100 + "percent.png";
saveFrame(fileName); // TODO: write parameters value in file
fill(colorRef);
rect(0,0, width, 80);
fill(0);
textSize(18);
text("Graph saved as: " + fileName, 20, 30);
text("Press any key = toggle mouse control", 20, 60);
noLoop();
}
}
/////////////////////////////////////////////////////////////////
void keyPressed() {
if (isCharacterizing) {
isCharacterizing = false;
loop();
} else {
isCharacterizing = true;
fingerPos = 0;
}
}
/////////////////////////////////////////////////////////////////
int[] histogram() {
// This function measures the effect of a finger on a strip.
// It counts the pixels with a color that changed.
int[] rawData = new int[colorRef];
// Calculate the histogram
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
// Only focus on the strips colors (discard white finger & bacground)
color c = get(i, j);
if (c != color(0, 0, colorRef)) {
int hue = int(hue(c));
rawData[hue]++;
}
}
}
// Extract from histogram and normalize
int[] niceData = preprocess(rawData);
// Visualization
drawHistogram(niceData); // simulated raw sensor data
return niceData;
}
/////////////////////////////////////////////////////////////////
int[] preprocess(int[] rawData) {
// Create an array only for the simulated pressure sensor data
int[] niceData = new int[stripNumber];
// Find the largest value in the histogram
int histMax = max(rawData);
globalMax = max(histMax, globalMax);
// Pressure positions counter
int indexCpt = 0;
// Remove irrelevant values
for (int i = 0; i < rawData.length; i++) {
if (rawData[i] < 0.06*globalMax) { // 0.6% is considered noise
rawData[i] = 0;
} else {
// Trick to initialize this array only once:
if (pressureIndices[pressureIndices.length-1] == 0) {
// get the index of the useful values
pressureIndices[indexCpt++] = i;
}
}
}
// Extract pressure sensor data and normalize:
for (int i = 0; i < stripNumber; i++) {
// count how many pixels are hidden by the finger
rawData[pressureIndices[i]] = globalMax - rawData[pressureIndices[i]];
// populate the array with normalized value
niceData[i] = colorRef * rawData[pressureIndices[i]] / globalMax;
}
return niceData;
}
/////////////////////////////////////////////////////////////////
void drawHistogram(int[] niceData) {
// Draw the histogram
for (int i = 0; i < niceData.length; i++) {
// Compute where lines should be traced
int x = int(map(i, 0,niceData.length+0.8, // TODO generic
0,width));
// shift to allign with stripe
x += 0.75 * width / niceData.length; // TODO generic
// Convert the histogram value to a location between
// the bottom and the top of the picture
int y = int(map(niceData[i],
0, colorRef,
height, 0));
//y *= globalMax/XXX; // TODO adapt!
stroke(0);
strokeWeight(5);
line(x,height, x,y);
}
}
/////////////////////////////////////////////////////////////////
int drawRetrievedFinger(int[] niceData) {
// This function aims to retrieve finger position
float retrievedPos = -1;
// Find max index
int maxArray = max(niceData);
int maxIndex = 0;
for (int i = 0; i < niceData.length; i++) {
if (maxArray == niceData[i]) {
maxIndex = i;
break;
}
}
// Retrieval method from Microchip TB3064 white paper (p12):
// microchip.com/stellent/groups/techpub_sg/documents/devicedoc/en550192.pdf
// Position is calculated as the centroid of 2 adjacent values:
int prev = (maxIndex==0)?
0 : niceData[maxIndex-1];
int next = (maxIndex>=niceData.length-1)?
0 : niceData[maxIndex+1];
retrievedPos = maxIndex + 0.5 * (next - prev) / niceData[maxIndex];
// Offset TODO?
retrievedPos += 0.5;
retrievedPos *= width / stripNumber;
// Draw finger at estimated position
color c = color(0, 0, 0, 2*colorRef/3);
drawFinger(int(retrievedPos), fingerWidth*4/5, c);
return int(retrievedPos);
}
/////////////////////////////////////////////////////////////////
int drawCubicRetrievedFinger(int[] niceData) {
// This function aims to retrieve finger position
int interFactor = interStep*2; // improves error smoothness
float retrievedPos = -1;
float[] y = new float[stripNumber*interFactor];
float scaling = float(width) / y.length;
strokeWeight(3);
// interpolate
for (int s = 0; s < stripNumber; s++) { // sensor strips
// avoid overflows
int s0 = (s-1 < 0)? -1 : s-1;
int s1 = s;
int s2 = (s+1 >= stripNumber)? s : s+1;
int s3 = (s+2 >= stripNumber)? s : s+2;
for (int i = 0; i < interFactor; i++) { // interpolation steps
y[i+s*interFactor] = CubicInterpolate(s0<0? 0 : niceData[s0],
niceData[s1],
niceData[s2],
niceData[s3],
float(i)/interFactor);
}
}
// Find max index
float maxArray = max(y);
for (int i = 0; i < y.length; i++) {
if (maxArray == y[i]) {
retrievedPos = i;
break;
}
}
retrievedPos *= scaling; // normalize to display
retrievedPos += zzUnitWidth/2;
// Draw finger at estimated position
color c = color(0, 0, 0, 2*colorRef/3);
drawFinger(int(retrievedPos), fingerWidth*4/5, c);
return int(retrievedPos);
}
/////////////////////////////////////////////////////////////////
float CubicInterpolate(float y0, float y1,
float y2, float y3, float mu) {
/* @article{bourke1999interpolation,
title={Interpolation methods},
author={Bourke, Paul},
journal={paulbourke.net/miscellaneous/interpolation},
year={1999} } */
float a0, a1, a2, a3, mu2;
mu2 = mu*mu;
// Breeuwsma approach:
a0 = -0.5*y0 + 1.5*y1 - 1.5*y2 + 0.5*y3;
a1 = y0 - 2.5*y1 + 2*y2 - 0.5*y3;
a2 = -0.5*y0 + 0.5*y2;
a3 = y1;
return (a0*mu*mu2 + a1*mu2 + a2*mu + a3);
}
/////////////////////////////////////////////////////////////////
float LinearInterpolate(float y1, float y2, float mu) {
return(y1*(1-mu)+y2*mu);
}
/////////////////////////////////////////////////////////////////
void drawFinger(int position, int size, color c) {
// This functions assumes that the pressure applied by a finger
// is similar to a disc
noStroke();
fill(c);
ellipse(position, height/2, size, size);
}
/////////////////////////////////////////////////////////////////
void drawBackground() {
// This function draws the strips, here they have a zigzag
// shape but a picture could be loaded with random shapes
// starting point
int startPosX = Math.round((zzStrokeWidth + zzSpacingRatio) / 2);
int startPosY = -zzStrokeWidth / 2;
// end point of the line
int endPosX = startPosX;
int endPosY = height-startPosY;
background(colorRef);
strokeJoin(MITER);
for (int i = 0; i<stripNumber; i++) {
strokeWeight(zzStrokeWidth);
// Hue, Saturation, Brightness
int hue = i*(colorRef/stripNumber) % colorRef;
stroke(color(hue, colorRef, colorRef));
drawZigZag(zzSpikeCount, zzStrokeWidth,
startPosX + zzUnitWidth * i, startPosY,
endPosX + zzUnitWidth * i, endPosY);
}
}
/////////////////////////////////////////////////////////////////
void drawZigZag(int segments, float radius, float aX, float aY, float bX, float bY) {
if (segments <= 1) {
beginShape();
vertex(aX, aY);
vertex(bX, bY);
endShape();
return;
}
// Calculate vector from start to end point
float distX = bX - aX;
float distY = bY - aY;
// Calculate length of the above mentioned vector
float segmentLength = sqrt(distX * distX + distY * distY) / segments;
// Calculate segment vector
float segmentX = distX / segments;
float segmentY = distY / segments;
// Calculate normal of the segment vector and multiply it with the given radius
float normalX = -segmentY / segmentLength * radius;
float normalY = segmentX / segmentLength * radius;
// Calculate start position of the zig-zag line
float StartX = aX + normalX;
float StartY = aY + normalY;
beginShape();
vertex(StartX, StartY);
// Render the zig-zag line
for (int n = 1; n < segments; n++) {
float newX = aX + n * segmentX + ((n & 1) == 0 ? normalX : -normalX);
float newY = aY + n * segmentY + ((n & 1) == 0 ? normalY : -normalY);
vertex(newX, newY);
}
// Render last line
vertex(bX + ((segments & 1) == 0 ? normalX : -normalX),
bY + ((segments & 1) == 0 ? normalY : -normalY));
// roll back to close the shape
for (int n = segments-1; n >= 1; n--) {
float newX = aX + n * segmentX + ((n & 1) == 0 ? normalX : -normalX);
float newY = aY + n * segmentY + ((n & 1) == 0 ? normalY : -normalY);
vertex(newX, newY);
}
endShape();
}