-
Notifications
You must be signed in to change notification settings - Fork 0
/
FINAL.pde
579 lines (513 loc) · 14.8 KB
/
FINAL.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//importing libraries
import oscP5.*; //Library which enables oscP5 communication
import netP5.*;
import processing.serial.*; //Library which enables serial communication
//declaring and initializing global variables
PFont myFont;
int portnum = 7400;
int i=0;
char val;
float heading;
int new_heading;
Serial myPort; //Declares object of type Serial, to send data using serial communication protocol (to Arduino)
OscP5 oscP5; //Decalres object of type OscP5 to receive data using OSCP5 communication protocol (from EEG)
OscP5 compass; //Decalres object of type OscP5 to receive data using OSCP5 communication protocol (compass ios app)
NetAddress myRemoteLocation;
NetAddress location; //compass ios app setting
char current_state = 'n'; //intializing current state to n (user is performing the neutral mental command)
char previous_state='u'; //intialiazing previous state to u (interface automatically selects forward at the begining)
// boolean variables to switch between BCI and autonomy mode in the interface
boolean send_bci=false;
boolean send_auto=false;
boolean after_yes=false;
boolean neutral=true;
char state='1';
boolean autonomy_q=true;
boolean autonomous_state= false;
boolean confirm_command=false;
boolean prompt_q=false;
//variables to used to later utilize the mental commands
float command ; //Float number to take the intensity of the command from the EEG headset
float thresh = 0.2; //Threshold of mental command
//variables used in the correction when in the autonomous mode
char point;
int orientation=0;
//setup
void setup() {
size(1000, 1000);
myPort = new Serial(this, "COM10",9600); //myPort sends and reads data using serial communication through port 9600
oscP5 = new OscP5(this, portnum ); //oscP5 listens to incoming messages at port 7400
compass= new OscP5(this, 1234); //compass listens to incoming messages from the compass app through port 1234
myRemoteLocation = new NetAddress("127.0.0.1", 7400);
location= new NetAddress("127.0.0.1", 1234);
myFont = createFont("Arial Black", 32); //used for formatting the font on the interface
textFont(myFont);
}
//serial event (function that is called whenever the Serial port myPort is busy
void serialEvent(Serial myPort) {
val=myPort.readChar(); //reads the message sent to myPort
if (val =='A'||val =='B'|| val =='C') { //switch to autonomous mode
autonomous_state = true;
point=val;
}
else if(val=='Z') //destination is reached (switch to BCI mode)
{
prompt_q=false;
autonomous_state=false;
confirm_command=false;
neutral=false;
orientation=0;
}
else if(val=='0') //predefine the orientation of the wheelchair within the user's household
{
orientation=230;
}
else if(val=='1') //predefine the orientation of the wheelchair within the user's household
{
orientation=133;
}
else if(val=='2') //predefine the orientation of the wheelchair within the user's household
{
orientation=313;
}
else if(val=='3') //predefine the orientation of the wheelchair within the user's household
{
orientation=50;
}
}
//Incoming OSC messages (port 7400) are forwarded to the oscEvent method and take the name of myMessage
void oscEvent (OscMessage myMessage) {
//////////////////////////////////////// check the following conditions in BCI mode
if (autonomous_state==false)
{
if(myMessage.checkAddrPattern("/COG/NEUTRAL") == true){
current_state='n';
confirm_command=false;
send_bci=true;
myPort.clear();
myPort.write('s');
}
else if (myMessage.checkAddrPattern("/COG/LEFT") == true) { //Checks if the osc message has the pattern "/COG/LEFT"
confirm_command=false;
command = myMessage.get(0).floatValue(); //Retrieves the intensity of the left command
if ((command>thresh) && (current_state=='n')) //declares a proper state based on teh previous information
{
if (previous_state == 'l')
{current_state = 'u';}
else if (previous_state == 'u')
{current_state = 'r';}
else if (previous_state == 'r')
{current_state = 'l';}
}
} else if (myMessage.checkAddrPattern("/COG/PUSH") == true) { //Checks if the osc message has the pattern "/COG/RIGHT"
command = myMessage.get(0).floatValue(); //Retrieves the intensity of the right command
if ((command>thresh) && current_state=='n') //Checks where the interface selection is to send a specific character to the Arduino and move the wheelchair
{
confirm_command=true;
if ( previous_state=='s' && send_bci==true) //STATE = STOP
{
myPort.clear();
myPort.write('s');
send_bci=false;
}
else if (previous_state=='u' && send_bci==true) //STATE = UP
{
myPort.clear();
myPort.write('f');
send_bci=false;
}
else if (previous_state=='r' && send_bci==true) //STATE = RIGHT
{
myPort.clear();
myPort.write('r');
send_bci=false;
}
else if (previous_state=='l' && send_bci==true) //STATE = LEFT
{
myPort.clear();
myPort.write('l');
send_bci=false;
}
}
else
{
confirm_command=false;
}
}
}
/////////////////////////////// check the following conditions in Autonmous mode
else
{
if (myMessage.checkAddrPattern("/compass/heading") == true) { //Checks if the osc message has the pattern
heading = myMessage.get(0).floatValue(); //continuously received the compass heading from the app
new_heading = round(heading);
myPort.clear();
myPort.write(new_heading);
//println(orientation);
if(orientation!=0) //corrects the wheelchair's orientation by comparing the predefined orientation
//in serila event and the new compass heading received from the app
{
if(new_heading<orientation-5)
{
myPort.write('i');
println("l");
// orientation=0;
}
else if(new_heading>orientation+5)
{
myPort.write('w');
println("r");
// orientation=0;
}
}
}
if(myMessage.checkAddrPattern("/COG/NEUTRAL") == true){ //also check for the following conditions in Autonomous mode
confirm_command=false;
neutral=true;
send_auto=true;
}
if(neutral==true) //change states to change the interface in Autonomous mode based on
//the user's current location
{
if (myMessage.checkAddrPattern("/COG/LEFT") == true)
{
confirm_command=false;
command = myMessage.get(0).floatValue();
if(command>thresh)
{
if(state=='1'){
state='2';
neutral=false;
}
else if(state=='2')
{
state='1';
neutral=false;
}
}
}
else if (myMessage.checkAddrPattern("/COG/PUSH") == true)
{
command = myMessage.get(0).floatValue();
if(command>thresh)
{
confirm_command=true;
if (point=='A' && send_auto==true && after_yes==true)
{
if(state=='1')
{
myPort.clear();
myPort.write('b');
send_auto=false;
//i++;
//println(i);
}
else if(state=='2')
{
myPort.clear();
myPort.write('c');
send_auto=false;
}
}
else if(point=='B' && send_auto==true && after_yes==true)
{
if(state=='1')
{
myPort.clear();
myPort.write('a');
send_auto=false;
}
else if(state=='2')
{
myPort.clear();
myPort.write('c');
send_auto=false;
}
}
else if(point=='C' && send_auto==true && after_yes==true)
{
if(state=='1')
{
myPort.clear();
myPort.write('a');
send_auto=false;
}
else if(state=='2')
{
myPort.clear();
myPort.write('b');
send_auto=false;
}
}
}
else{
confirm_command=false;
}
}
}
}
}
/////////////////////////////////////////////////////////////////draw the interface
void draw(){
//Background
background(255);
if(autonomous_state == false) //BCI mode interfacece / based on the state, call the corresponding interface function
{
after_yes=false;
if (current_state == 'u'||(current_state=='n'&& previous_state=='u')) //STATE = UP
{
forward_selected_confirmed(0,200,255);
previous_state='u';
if(confirm_command==true)
{
forward_selected_confirmed(112,128,144);
}
} else if (current_state == 'r'||(current_state=='n'&& previous_state=='r')) //STATE = RIGHT
{
right_selected_confirmed(0,200,255);
previous_state='r';
if(confirm_command==true)
{
right_selected_confirmed(112,128,144);
}
} else if (current_state == 'l'||(current_state=='n'&& previous_state=='l')) //STATE = LEFT
{
left_selected_confirmed(0,200,255);
previous_state='l';
if(confirm_command==true)
{
left_selected_confirmed(112,128,144);
}
}
}
////////////////////////////////////////////////
else{ //Autonomous state interface /based on the state, call the corresponding function interfacr
if(autonomy_q==true){
if( state=='1')
{
Auto_sc(0,200,255,0,0,128);
Auto_qtext();
if(confirm_command==true)
{
Auto_sc(112,128,144,0,0,128);
Auto_qtext();
prompt_q=true;
autonomy_q=false;
confirm_command=false;
neutral=false;
after_yes=true;
}
}
else if(state=='2')
{
Auto_sc(0,0,128,0,200,255);
Auto_qtext();
if(confirm_command==true)
{
Auto_sc(0,0,128,112,128,144);
Auto_qtext();
prompt_q=false;
autonomous_state=false;
confirm_command=false;
neutral=false;
myPort.clear();
myPort.write('n'); // tell arduino to go back to BCI mode
}
}
}
///////////////////////////////////////////////////check points(to ask the user a question based on the user's location)
else{
if(prompt_q)
{
if (point=='A')
{
if(state=='1')
{
Auto_sc(0,200,255,0,0,128);
Auto_atext();
if(confirm_command==true)
{
Auto_sc(112,128,144,0,0,128);
Auto_atext();
}
}
else if(state=='2')
{
Auto_sc(0,0,128,0,200,255);
Auto_atext();
if(confirm_command==true)
{
Auto_sc(0,0,128,112,128,144);
Auto_atext();
}
}
}
else if(point=='B')
{
if(state=='1')
{
Auto_sc(0,200,255,0,0,128);
Auto_btext();
if(confirm_command==true)
{
Auto_sc(112,128,144,0,0,128);
Auto_btext();
}
}
else if(state=='2')
{
Auto_sc(0,0,128,0,200,255);
Auto_btext();
if(confirm_command==true)
{
Auto_sc(0,0,128,112,128,144);
Auto_btext();
}
}
}
else if(point=='C')
{
if(state=='1')
{
Auto_sc(0,200,255,0,0,128);
Auto_ctext();
if(confirm_command==true)
{
Auto_sc(112,128,144,0,0,128);
Auto_ctext();
}
}
else if(state=='2')
{
Auto_sc(0,0,128,0,200,255);
Auto_ctext();
if(confirm_command==true)
{
Auto_sc(0,0,128,112,128,144);
Auto_ctext();
}
}
}
}
}
}
}
//////////////////////////////////////////////////////////////////interface features (functions used to draw the interface)
void Auto_sc(int r1, int g1, int b1, int r2, int g2, int b2)
{
stroke (255);
myFont = createFont("Arial Black", 32);
fill(r1,g1,b1);
rect(250, 400, 150, 150, 7);
fill(r2,g2,b2);
rect(600, 400, 150, 150, 7);
fill(0,0,128);
}
void Auto_qtext()
{
text("Do you want to switch to Autonomous Mode?",100,300);
fill (255);
text("Yes",292,480);
text("No",650,480);
}
void Auto_atext()
{
text("Where would you like to go?",255,300);
fill (255);
text("B",310,480);
text("C",660,480);
}
void Auto_btext()
{
text("Where would you like to go?",255,300);
fill (255);
text("A",310,480);
text("C",660,480);
}
void Auto_ctext()
{
text("Where would you like to go?",255,300);
fill (255);
text("A",310,480);
text("B",660,480);
}
/*void stop_selected_confirmed(int r, int g, int b)
{
//TRIANGLES
stroke(255);
fill(0, 0, 128);
triangle(350, 300, 650, 300, 500, 100);
fill(0, 0, 128);
triangle(300, 350, 300, 650, 100, 500);
fill(0, 0, 128);
triangle(700, 350, 700, 650, 900, 500);
//Circle
fill(r, g, b);
ellipse(500, 500, 250, 250);
//TEXT
fill(255);
text("FORWARD", 415, 250);
text("STOP", 455, 510);
text("LEFT", 185, 510);
text("RIGHT",730, 510);
}*/
void left_selected_confirmed(int r, int g, int b)
{
//TRIANGLES
stroke(255);
fill(0, 0, 128);
triangle(350, 300, 650, 300, 500, 100);
fill(r, g, b);
triangle(300, 350, 300, 650, 100, 500);
fill(0, 0, 128);
triangle(700, 350, 700, 650, 900, 500);
//Circle
fill(0, 0, 128);
ellipse(500, 500, 250, 250);
//TEXT
fill(255);
text("FORWARD", 415, 250);
text("STOP", 455, 510);
text("LEFT", 185, 510);
text("RIGHT",730, 510);
}
void forward_selected_confirmed(int r, int g, int b)
{
//TRIANGLES
stroke(255);
fill(r, g, b);
triangle(350, 300, 650, 300, 500, 100);
fill(0, 0, 128);
triangle(300, 350, 300, 650, 100, 500);
fill(0, 0, 128);
triangle(700, 350, 700, 650, 900, 500);
//Circle
fill(0, 0, 128);
ellipse(500, 500, 250, 250);
//TEXT
fill(255);
text("FORWARD", 415, 250);
text("STOP", 455, 510);
text("LEFT", 185, 510);
text("RIGHT",730, 510);
}
void right_selected_confirmed(int r, int g, int b)
{
//TRIANGLES
stroke(255);
fill(0, 0, 128);
triangle(350, 300, 650, 300, 500, 100);
fill(0, 0, 128);
triangle(300, 350, 300, 650, 100, 500);
fill(r, g, b);
triangle(700, 350, 700, 650, 900, 500);
//Circle
fill(0, 0, 128);
ellipse(500, 500, 250, 250);
//TEXT
fill(255);
text("FORWARD", 415, 250);
text("STOP", 455, 510);
text("LEFT", 185, 510);
text("RIGHT",730, 510);
}