-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.pde
317 lines (275 loc) · 8.14 KB
/
bluetooth.pde
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
import processing.serial.*; //serial reading package
import controlP5.*; //GUI library
// Button variables
ControlP5 cp5;
int myColor = color(255);
int c1,c2;
float n,n1;
Boolean dataDisplay = false;
Boolean dataCapture = false;
PrintWriter output;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
int[] roll = new int[4]; // Roll value
int[] time = new int[4]; // Time in milliseconds as reference
float[] calca = new float[4]; // For holding ln(alpha) of peaks over time
float[] timea = new float[4]; // Corresponding time for those ln(alpha)'s
int counter = 0; // Counter for counter()
Boolean signal = false;
Boolean swtch = false;
float damp = 0; // Damping Ratio
// Auto-normalizing variables
float normByte1 = 20; //IR Sensor
float normByte2 = 0; //Roll
float[] temp1 = new float[5];
float[] temp2 = new float[5];
int axis1yPos = 200;
int axis2yPos = 500;
float norm = 0;
void setup () {
// set the window size:
size(1000, 800);
// set inital background:
background(0);
initialize(axis1yPos,"IR Sensor");
initialize(axis2yPos,"Roll");
// Place Buttons
cp5 = new ControlP5(this);
// Display Data Button
cp5.addToggle("Display Data",false,200,600,150,19);
// Capture Data Button
cp5.addToggle("Capture Data",false,400,600,150,19);
// Close Program Button
cp5.addBang("Close Program",600,600,150,19);
// List all the available serial ports
println(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, "/dev/tty.RN42-DECF-SPP", 115200);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
//Create file to write to
output = createWriter("positions.txt");
}
void draw () {
// everything happens in the serialEvent()
serialEvent(myPort);
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
if (inString.contains(",")) {
String[] inStr = inString.split(",");
String inString1 = inStr[0];
print("First Data: ");
println(inString1);
String inString2 = inStr[1];
print("Second Data: ");
Boolean neg = false;
if (inString2.contains("-")){ // Check if negative number
neg = true;
}
inString2 = inString2.replaceAll("\\D+",""); // Reduce to numbers only
if (neg) {
String minus = "-";
inString2 = minus.concat(inString2);
}
println(inString2);
roll[0] = roll[1];
roll[1] = roll[2];
roll[2] = roll[3];
roll[3] = Integer.parseInt(inString2); // Store new data value at end of array
time[0] = time[1];
time[1] = time[2];
time[2] = time[3];
time[3] = currentTimeMillis();
damping();
print("Damping Coefficient: ");
println(damp);
// Display Data if toggle on
if (dataDisplay) {
process(inString1, 1);
process(inString2, 2);
}
// Print data if button toggled on
if (dataCapture) {
// Print data to output file
output.print(inString1 + ",");
output.println(inString2);
}
} else {
process(inString,3);
}
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
// set inital background:
background(0);
initialize(axis1yPos,"IR Sensor");
initialize(axis2yPos,"Roll");
} else {
// increment the horizontal position:
xPos++;
}
}
}
void initialize (int yPos, String type) {
smooth();
// draw the y-axis
stroke(126);
line(0, yPos, width, yPos);
// bars every 15 px
for (int n=0; n<=width; n=n+15) {
line(n,yPos-5,n,yPos+5);
}
textSize(15);
textAlign(CENTER);
text(type,width/2,yPos-150); // 10 -> 30
text("Damping Coefficient: ",width*.4,700); //-40 -> 50
}
public float normalize(float temp[], float in, float norm) {
//Auto-normalize if last five values are around input value
temp[0] = temp[1];
temp[1] = temp[2];
temp[2] = temp[3];
temp[3] = temp[4];
temp[4] = (int) in;
boolean indicator = true;
for (int i=0; i<temp.length; i++) {
if (temp[i] != in) {
indicator = false;
}
}
if (indicator) {
norm = temp[0];
}
return norm;
}
void process(String string, int type) {
float[] temp;
int yPos = 0;
int lowTol = 0;
int highTol = 0;
int mapLow = 0;
// trim off any whitespace:
string = trim(string);
// convert to an int and map to the screen height:
float inByte = float(string);
if (inByte != 0) {
switch (type) {
case 1: norm = normByte1; // IR Sensor
temp = temp1;
yPos = axis1yPos;
lowTol = -10;
highTol = 10;
//inByte = random(10,30);
mapLow = 0;
break;
case 2: norm = normByte2; // Roll
temp = temp2;
yPos = axis2yPos;
lowTol = -50;
highTol = 50;
//inByte = random(-50,50);
norm = normalize(temp,inByte,norm);
mapLow = -150;
break;
case 3:
break;
default: break;
}
//println(inByte);
int min = (int) norm + lowTol;
int max = (int) norm + highTol;
inByte = map(inByte, min, max, mapLow, 150); //Change map to appropriate min and max
// draw the line:
stroke(127,34,255);
line(xPos, yPos, xPos, yPos - inByte);
}
}
void keyPressed() { // Will execute contents when any key is pressed
}
void damping() {
Boolean haveTime = true;
for (int i=0; i<3; i++) {
if ((time[i+1]-time[i]) == 0){
haveTime = false;
}
}
if (haveTime) { // check if time is filled up first, avoid div by zero
float drdt1 = (float) ( roll[3] - roll[2] )/( time[3] - time[2] );
float drdt2 = (float) ( roll[2] - roll[1] )/( time[2] - time[1] );
float drdt3 = (float) ( roll[1] - roll[0] )/( time[1] - time[0] );
println(drdt1);
if (drdt1 < 0 && drdt2 >= 0 && drdt3 >= 0) {
signal = true;
}
if (signal) {
count();
} else {
for (int i=0; i<calca.length; i++) {
calca[i] = 0;
}
for (int i=0; i<timea.length; i++) {
calca[i] = 0;
}
counter = 0;
}
}
}
void count() {
if (roll[3]>0 && roll[3] < roll[2] && swtch) {
swtch = false;
calca[0] = calca[1];
calca[1] = calca[2];
calca[2] = calca[3];
calca[3] = (float) Math.log(roll[2]);
timea[0] = timea[1];
timea[1] = timea[2];
timea[2] = timea[3];
timea[3] = currentTimeMillis();
counter = counter + 1;
println(calca[3]);
} else if ( roll[3]<0 ) {
swtch = true;
}
if (counter==5) {
float[] as = new float[4];
for (int i=0; i<3; i++) {
as[0] = as[1];
as[1] = as[2];
as[2] = (calca[i+1] - calca[i])/( (timea[i+1] - timea[i])*.01 );
}
float a = (as[3] + as[2] + as[1] + as[0])/4; // Averaging alphas
damp = -2*a;
String strDamp = Float.toString(damp);
text(strDamp,width*.5+50,700);
signal = false;
}
}
public static int currentTimeMillis() {
return (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
}
void controlEvent(ControlEvent theEvent) {
if(theEvent.isController()) {
if(theEvent.controller().name()=="Capture Data") {
if(theEvent.controller().value()==1) {
dataCapture = true;
} else {
dataCapture = false;
}
}
if(theEvent.controller().name()=="Display Data") {
if(theEvent.controller().value()==1) {
dataDisplay = true;
} else {
dataDisplay = false;
}
}
if(theEvent.controller().name()=="Close Program") {
output.flush();
output.close();
exit();
}
}
}