-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurioDuinoGUI.pde
830 lines (684 loc) · 15.9 KB
/
CurioDuinoGUI.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
* David Keck
* CurioDuinoGUI.pde
* Graphical interface for operations of
* the CurioDuino Autonomous robot project.
* More information can be found here:
* davidskeck.wordpress.com
* or here http://github.com/davidskeck/CurioDuinoGUI
*/
import processing.serial.*;
// 1 for windows, 0 for mac bluetooth, 3 mac usb
final int PORT_NUMBER = 0;
// Number of samples in average
final int SAMPLE_SIZE = 150;
// Create object from Serial class
Serial port;
// Check for start button
boolean isStarted = false;
// Check for when to draw
boolean draw = false;
// Data from battery calculation
int batteryReading;
// Time measurements
long timeAtLink, timeSinceLink;
// Speed changer
final int SPEED_ONE = 75;
final int SPEED_TWO = 125;
final int SPEED_THREE = 175;
boolean buttonOne = true;
boolean buttonTwo = false;
boolean buttonThree = false;
// Edge sensors
boolean leftEdgeDetected = false;
boolean rightEdgeDetected = false;
// Obstacle sensors
boolean leftObstacleDetected = false;
boolean middleObstacleDetected = false;
boolean rightObstacleDetected = false;
// Motor variables
boolean leftForward = false;
boolean rightForward = false;
// Key input
boolean spacebar = false;
boolean keyOne = false;
boolean keyTwo = false;
boolean keyThree = false;
// Store battery averages
float[] batteryData = new float[SAMPLE_SIZE];
float batteryTotal = 0;
int batteryI = 0, batteryCnt = 0;
// Store time averages
long[] timeData = new long[SAMPLE_SIZE];
long timeTotal = 0;
int timeI = 0, timeCnt = 0;
// Make a font object
PFont font;
// Make an image object
PImage CurioDuinoImage;
float getCircularBatteryAverage(float inVal)
{
// Subtract oldest value from total
batteryTotal -= batteryData[batteryI];
// Replace old value with latest reading
batteryData[batteryI] = inVal;
// Increase total by latest reading
batteryTotal += inVal;
// Set index to compensate for number of values
batteryI = ++batteryI % batteryData.length;
// Increase counter if necessary
if(batteryCnt < batteryData.length)
{
batteryCnt++;
}
return batteryTotal/batteryCnt;
}
long getCircularTimeAverage(long inVal)
{
// Subtract oldest value from total
timeTotal -= timeData[timeI];
// Replace old value with latest reading
timeData[timeI] = inVal;
// Increase total by latest reading
timeTotal += inVal;
// Set index to compensate for number of values
timeI = ++timeI % timeData.length;
// Increase counter if necessary
if(timeCnt < timeData.length)
{
timeCnt++;
}
return timeTotal/timeCnt;
}
void drawBattery()
{
// Calculate battery percentage
float percentage = batteryReading/720.0;
percentage = getCircularBatteryAverage(percentage);
if (percentage > 1.0)
{
percentage = 1;
}
else if (percentage < 0.0)
{
return;
}
// This rectangle is under the string
fill(200);
stroke(1);
rect(289, 161, 95, 28);
// This rectangle is under the percentage bar
rect(150, 112, 563, 24);
// Fill bar color depends on percent
// Gradient from green to red
float redFader = map(percentage, 1.0, .5, 0, 255);
float greenFader = map(percentage, .5, 0, 255, 0);
fill(redFader, greenFader, 0);
// Draw the bar
rect(153, 115, (557*percentage), 18);
// Draw the string
fill(0);
text(nf(percentage*100, 3, 2), 293, 182);
}
void drawCommStatus()
{
// This circle is under the indicator LED
noStroke();
fill(200);
ellipse(990, 124, 26, 26);
// This rectangle is under the string
stroke(1);
fill(200);
rect(304, 211, 80, 28);
// Calculate time since last data link
timeSinceLink = millis();
timeSinceLink = getCircularTimeAverage(timeSinceLink -= timeAtLink);
// From zero to one second, change color
float greenFader = map(timeSinceLink, 500, 1000, 255, 0);
float redFader = map(timeSinceLink, 0, 500, 0, 255);
// Draw the LED
fill(redFader, greenFader, 0);
ellipse(990, 124, 20, 20);
// Draw the string
fill(redFader, 0, 0);
if(timeSinceLink < 9000)
{
text(nf(timeSinceLink/1000.0, 1, 3), 308, 233);
}
else
{
text("LOST", 314, 233);
}
}
void drawObstacleStatus()
{
// These are the rectangles under the strings
stroke(1);
fill(200);
rect(334, 261, 50, 28);
rect(334, 311, 50, 28);
rect(334, 361, 50, 28);
// Set fill
fill(0);
// Draw the strings
if(leftObstacleDetected)
{
fill(180, 0, 0);
text("YES", 338, 282);
fill(255, 0, 0, 190);
// Indication on CurioDuino
beginShape();
vertex(856, 500);
vertex(888, 500);
vertex(868, 600);
vertex(836, 600);
endShape(CLOSE);
}
else
{
fill(0);
text("NO", 345, 282);
fill(0, 255, 0, 125);
// Indication on CurioDuino
beginShape();
vertex(856, 500);
vertex(888, 500);
vertex(868, 600);
vertex(836, 600);
endShape(CLOSE);
}
if(middleObstacleDetected)
{
fill(180, 0, 0);
text("YES", 338, 332);
fill(255, 0, 0, 190);
// Indication on CurioDuino
beginShape();
vertex(688, 500);
vertex(723, 500);
vertex(723, 600);
vertex(688, 600);
endShape(CLOSE);
}
else
{
fill(0);
text("NO", 345, 332);
fill(0, 255, 0, 125);
// Indication on CurioDuino
beginShape();
vertex(688, 500);
vertex(723, 500);
vertex(723, 600);
vertex(688, 600);
endShape(CLOSE);
}
if(rightObstacleDetected)
{
fill(180, 0, 0);
text("YES", 338, 382);
fill(255, 0, 0, 190);
// Indication on CurioDuino
beginShape();
vertex(534, 500);
vertex(574, 500);
vertex(588, 600);
vertex(554, 600);
endShape(CLOSE);
}
else
{
fill(0);
text("NO", 345, 382);
fill(0, 255, 0, 125);
// Indication on CurioDuino
beginShape();
vertex(534, 500);
vertex(574, 500);
vertex(588, 600);
vertex(554, 600);
endShape(CLOSE);
}
}
void drawEdgeStatus()
{
// These are the rectangles under the strings
stroke(1);
fill(200);
rect(334, 411, 50, 28);
rect(334, 461, 50, 28);
// This is the rectangle under CurioDuino
noStroke();
fill(189);
rect(440, 280, 542, 470);
// Reset stroke width
stroke(1);
// Draw the strings
if(leftEdgeDetected)
{
fill(180, 0, 0);
text("YES", 338, 432);
fill(255, 0, 0, 195);
// Indication under CurioDuino
beginShape();
vertex(822, 608);
vertex(711, 608);
vertex(711, 725);
vertex(947, 725);
endShape(CLOSE);
}
else
{
fill(0);
text("NO", 345, 432);
fill(0, 255, 0, 145);
// Indication under CurioDuino
beginShape();
vertex(822, 608);
vertex(711, 608);
vertex(711, 725);
vertex(947, 725);
endShape(CLOSE);
}
if(rightEdgeDetected)
{
fill(180, 0, 0);
text("YES", 338, 482);
fill(255, 0, 0, 195);
// Indication under CurioDuino
beginShape();
vertex(711, 608);
vertex(600, 608);
vertex(475, 725);
vertex(711, 725);
endShape(CLOSE);
}
else
{
fill(0);
text("NO", 345, 482);
fill(0, 255, 0, 145);
// Indication under CurioDuino
beginShape();
vertex(711, 608);
vertex(600, 608);
vertex(475, 725);
vertex(711, 725);
endShape(CLOSE);
}
}
void drawCurioDuinoImage()
{
image(CurioDuinoImage, 460, 300);
}
void drawMovementStatus()
{
// This is the rectangle under the string
stroke(1);
fill(200);
rect(276, 511, 108, 28);
// Set fill for text
fill(0);
// Set fill and weight for line
stroke(255, 255, 0);
strokeWeight(5);
if (leftForward && rightForward && isStarted)
{
text("FORWARD", 280, 532);
drawArrow(553, 370, 100, 106);
drawArrow(873, 370, 100, 73);
}
else if (leftForward && !rightForward)
{
text("R-TURN", 287, 532);
drawArrow(523, 470, 100, 288);
drawArrow(873, 370, 100, 73);
}
else if (!leftForward && rightForward)
{
text("L-TURN", 287, 532);
drawArrow(553, 370, 100, 106);
drawArrow(903, 470, 100, 253);
}
else if (!leftForward && !rightForward && isStarted)
{
text("REVERSE", 280, 532);
strokeWeight(5);
drawArrow(523, 470, 100, 288);
drawArrow(903, 470, 100, 253);
}
else
{
text("STOPPED", 280, 532);
}
strokeWeight(1);
stroke(0);
}
void setup()
{
size(1024,768);
// Pick a font
font = loadFont("Monospaced.plain-48.vlw");
textFont(font);
// Pick an image
CurioDuinoImage = loadImage("CurioDuinoPicture.png");
// Background color
background(240);
// Main rectangle
fill(189);
rect(6, 100, 1010, 661, 9);
// Button rectangle
fill(0, 255, 0);
rect(20, 550, 364, 200);
// Speed selector buttons
rect(530, 205, 40, 40);
fill(200);
rect(640, 205, 80, 40);
rect(790, 205, 120, 40);
// Print out labels
fill(0);
textSize(54);
text("Start", 120, 660);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
textSize(48);
text("CurioDuino Mission Control", 145, 60);
textSize(24);
text("Battery%: ", 20, 131);
text("Comm. link status ", 728, 131);
text("Avg battery %: ", 20, 181);
text("Time(s) since link: ", 20, 231);
text("L obstacle detected: ", 20, 281);
text("M obstacle detected: ", 20, 331);
text("R obstacle detected: ", 20, 381);
text("L edge detected: ", 20, 431);
text("R edge detected: ", 20, 481);
text("Movement status: " , 20, 531);
text("Speed selector: ", 610, 181);
try
{
String arduinoPort = Serial.list()[PORT_NUMBER];
port = new Serial(this, arduinoPort, 9600);
port.bufferUntil('\n');
}
catch (Exception E)
{
// Couldn't open port, show message box and close program
javax.swing.JOptionPane.showMessageDialog(null, "Port could not be opened.",
"Serial port connection error", 0);
exit();
}
}
void draw()
{
if (draw)
{
// Draw dynamic indicators
drawBattery();
drawCommStatus();
drawEdgeStatus();
drawCurioDuinoImage();
drawObstacleStatus();
drawMovementStatus();
}
}
void serialEvent(Serial port)
{
// Required to make sure the
// entire data packet has been
// sent and recieved
if (port.available() < 27)
{
port.clear();
return;
}
// Get the time immediately after data has arrived
timeAtLink = millis();
// Read serial data in
String data = (port.readString());
int index = 0, index2 = 0;
index = data.indexOf("LE");
leftEdgeDetected = boolean(int(data.substring(0,index)));
index2 = data.indexOf("RE");
rightEdgeDetected = boolean(int(data.substring(index+2, index2)));
index = data.indexOf("B");
batteryReading = int(data.substring(index2+2, index));
index2 = data.indexOf("LO");
leftObstacleDetected = boolean(int(data.substring(index+1, index2)));
index = data.indexOf("MO");
middleObstacleDetected = boolean(int(data.substring(index2+2, index)));
index2 = data.indexOf("RO");
rightObstacleDetected = boolean(int(data.substring(index+2, index2)));
index = data.indexOf("LF");
leftForward = boolean(int(data.substring(index2+2, index)));
index2 = data.indexOf("RF");
rightForward = boolean(int(data.substring(index+2, index2)));
}
void mousePressed()
{
// Start/stop button coordinates
if (((mouseX > 20) && (mouseX < 20 + 364) && (mouseY > 550) && (mouseY < 550 + 200)))
{
// if mouse clicked inside square
isStarted = !isStarted;
draw = true;
// Send signal to CurioDuino
port.write(int(isStarted));
stroke(1);
// Check status to determine button display type
if(isStarted)
{
fill(255, 0, 0);
rect(20, 550, 364, 200);
textSize(54);
fill(0);
text("Stop", 134, 660);
}
else
{
fill(0, 255, 0);
rect(20, 550, 364, 200);
fill(0);
textSize(54);
text("Start", 120, 660);
}
}
if ((buttonOne == false) && ((mouseX > 530) && (mouseX < 530 + 40) && (mouseY > 205) && (mouseY < 205 + 40)))
{
buttonOne = true;
buttonTwo = false;
buttonThree = false;
port.write(SPEED_ONE);
// Speed selector one
fill(0, 255, 0);
rect(530, 205, 40, 40);
fill(200);
rect(640, 205, 80, 40);
rect(790, 205, 120, 40);
fill(0);
textSize(54);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
}
if ((buttonTwo == false) && ((mouseX > 640) && (mouseX < 640 + 80) && (mouseY > 205) && (mouseY < 205 + 40)))
{
buttonOne = false;
buttonTwo = true;
buttonThree = false;
port.write(SPEED_TWO);
// Speed selector one
fill(255, 255, 0);
rect(640, 205, 80, 40);
fill(200);
rect(530, 205, 40, 40);
rect(790, 205, 120, 40);
fill(0);
textSize(54);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
}
if ((buttonThree == false) && ((mouseX > 790) && (mouseX < 790 + 120) && (mouseY > 205) && (mouseY < 205 + 40)))
{
buttonOne = false;
buttonTwo = false;
buttonThree = true;
port.write(SPEED_THREE);
// Speed selector one
fill(255, 0, 0);
rect(790, 205, 120, 40);
fill(200);
rect(530, 205, 40, 40);
rect(640, 205, 80, 40);
fill(0);
textSize(54);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
}
textSize(24);
}
void drawArrow(int cx, int cy, int len, float angle)
{
pushMatrix();
translate(cx, cy);
rotate(radians(angle));
line(0,0,len, 0);
line(len, 0, len - 12, -12);
line(len, 0, len - 12, 12);
popMatrix();
}
void keyPressed()
{
// Spacebar
if (key == 32)
{
spacebar = true;
startButton();
}
// 1
else if (key == 49)
{
keyOne = true;
speedOne();
}
// 2
else if (key == 50)
{
keyTwo = true;
speedTwo();
}
// 3
else if (key == 51)
{
keyThree = true;
speedThree();
}
}
void startButton()
{
// Start/stop button coordinates
if (spacebar)
{
// if mouse clicked inside square
isStarted = !isStarted;
draw = true;
// Send signal to CurioDuino
port.write(int(isStarted));
stroke(1);
// Check status to determine button display type
if(isStarted)
{
fill(255, 0, 0);
rect(20, 550, 364, 200);
textSize(54);
fill(0);
text("Stop", 134, 660);
}
else
{
fill(0, 255, 0);
rect(20, 550, 364, 200);
fill(0);
textSize(54);
text("Start", 120, 660);
}
// Handle keyboard shortcut boolean
spacebar = false;
textSize(24);
}
}
void speedOne()
{
if (keyOne)
{
buttonOne = true;
buttonTwo = false;
buttonThree = false;
port.write(SPEED_ONE);
// Speed selector one
fill(0, 255, 0);
rect(530, 205, 40, 40);
fill(200);
rect(640, 205, 80, 40);
rect(790, 205, 120, 40);
fill(0);
textSize(54);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
// Handle keyboard shortcut boolean
keyOne = false;
textSize(24);
}
}
void speedTwo()
{
if (keyTwo)
{
buttonOne = false;
buttonTwo = true;
buttonThree = false;
port.write(SPEED_TWO);
// Speed selector one
fill(255, 255, 0);
rect(640, 205, 80, 40);
fill(200);
rect(530, 205, 40, 40);
rect(790, 205, 120, 40);
fill(0);
textSize(54);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
// Handle keyboard shortcut boolean
keyTwo = false;
textSize(24);
}
}
void speedThree()
{
if (keyThree)
{
buttonOne = false;
buttonTwo = false;
buttonThree = true;
port.write(SPEED_THREE);
// Speed selector one
fill(255, 0, 0);
rect(790, 205, 120, 40);
fill(200);
rect(530, 205, 40, 40);
rect(640, 205, 80, 40);
fill(0);
textSize(54);
text(">", 536, 241);
text(">>", 650, 241);
text(">>>", 805, 241);
// Handle keyboard shortcut boolean
keyThree = false;
textSize(24);
}
}