-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoesMySwiftBotWork.java
More file actions
477 lines (413 loc) · 14.6 KB
/
Copy pathDoesMySwiftBotWork.java
File metadata and controls
477 lines (413 loc) · 14.6 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
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
import swiftbot.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Scanner;
public class DoesMySwiftBotWork {
static SwiftBotAPI swiftBot;
public static void main(String[] args) throws InterruptedException {
try {
swiftBot = new SwiftBotAPI();
} catch (Exception e) {
/*
* Outputs a warning if I2C is disabled. This only needs to be turned on once,
* so you won't need to worry about this problem again!
*/
System.out.println("\nI2C disabled!");
System.out.println("Run the following command:");
System.out.println("sudo raspi-config nonint do_i2c 0\n");
System.exit(5);
}
// creates menu for user interaction
System.out.println("\n*****************************************************************");
System.out.println("*****************************************************************");
System.out.println("");
System.out.println("\t\t\tDOES MY SWIFTBOT WORK????");
System.out.println("");
System.out.println("*****************************************************************");
System.out.println("*****************************************************************\n");
Scanner reader = new Scanner(System.in); // Reading from System.in
// loops main menu after each user action
while (true) {
// outputs user options
// outputs user options
System.out.println("Enter a number to test a feature:\n");
System.out.println(
"1 = Test Camera \t|\t 2 = Test Button Lights\n" +
"3 = Test Ultrasound \t|\t 4 = Test Left Wheel\n" +
"5 = Test Right Wheel \t|\t 6 = Test Buttons\n" +
"7 = Test Underlighting \t|\t 8 = Test QR Code Detection\n" +
"0 = Exit\n");
System.out.print("Enter a number: ");
// reads user input and performs corresponding action
String ans = reader.next();
switch (ans) {
// Testing Camera
case "1":
System.out.println("Testing Camera.");
testCamera();
break;
// Testing Button lights
case "2":
System.out.println("Testing Button Lights");
testButtonLights();
break;
// Testing Ultrasound
case "3":
System.out.println("Testing Ultrasound");
testUltrasound();
break;
// Testing Left Wheel
case "4":
testWheel("left");
Thread.sleep(1000);
break;
// Testing Right Wheel
case "5":
testWheel("right");
Thread.sleep(1000);
break;
// testing Buttons
case "6":
testButtons();
break;
// Testing Under lights
case "7":
testAllUnderlights();
testIndividualUnderlights();
break;
// Testing QR Code
case "8":
testQRCodeDetection();
break;
// EXIT
case "0":
reader.close();
System.exit(0);
break;
default:
System.out.println("ERROR: Please enter a valid number.");
break;
}
}
}
public static void testCamera() {
try {
/*
* Taking 720x720 images for the test, however, it is recommended to use smaller
* sizes for faster
* code execution
*/
BufferedImage bwImage = swiftBot.takeGrayscaleStill(ImageSize.SQUARE_720x720);
BufferedImage image = swiftBot.takeStill(ImageSize.SQUARE_720x720);
if (image == null || bwImage == null) {
System.out.println("ERROR: Image is null");
System.exit(5);
} else {
// Save the bwImage to a directory.
ImageIO.write(bwImage, "png", new File("/data/home/pi/bwImage.png"));
ImageIO.write(image, "png", new File("/data/home/pi/colourImage.png"));
System.out.println("SUCCESS: Camera tests succeeded");
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("\nCamera not enabled!");
System.out.println("Try running the following command: ");
System.out.println("sudo raspi-config nonint do_camera 0\n");
System.out.println("Then reboot using the following command: ");
System.out.println("sudo reboot\n");
System.exit(5);
}
}
public static void testUltrasound() {
try {
double distance = swiftBot.useUltrasound();
System.out.println("Distance from front-facing obstacle: " + distance + " cm");
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR: Ultrasound Unsuccessful");
System.exit(5);
}
}
/*
* Testing each light for the SwiftBot Buttons. Each button is first
* individually
* turned on using the `testIndividualButtonLights()` method below this method.
* then, all button
* lights are turned on with `testAllButtonLights()`.
*/
public static void testButtonLights() {
try {
testIndividualButtonLights();
testAllButtonLights();
} catch (Exception e) {
System.out.println("ERROR: An error occurred testing button lights.");
e.printStackTrace();
System.exit(0);
}
}
/*
* This method tests each individual button light, by firstly turning them on
* with {@code setButtonLight();},
* before toggling them off with {@code toggleButtonLight();}. Afterwards, a for
* loop is ran that will turn up
* the brightness of the individual button light from 0 to 100 with {@code
* setButtonLightBrightness}, before
* turning it off completely with {@code setButtonLight();}.
*/
public static void testIndividualButtonLights() {
try {
Button buttons[] = { Button.A, Button.B, Button.X, Button.Y }; // Creating an array with all the Bot's
// buttons.
for (Button button : buttons) {
swiftBot.setButtonLight(button, true); // Turning it on
Thread.sleep(1000);
swiftBot.toggleButtonLight(button); // Toggling the button light off.
Thread.sleep(250); // Small wait so they're completely off.
// This snippet will adjust the brightness of an individual button light, from 0
// (off) to 100 (on)
for (int i = 0; i < 100; i++) {
swiftBot.setButtonLightBrightness(button, i);
Thread.sleep(5); // 5ms wait per iteration, so it's not too fast.
}
swiftBot.setButtonLight(button, false); // Completely turning off the button light.
System.out.println("SUCCESS: Changes for button " + button + " successful.");
}
} catch (Exception e) {
System.out.println("ERROR: An error occurred setting button lights.");
e.printStackTrace();
System.exit(5);
}
}
/*
* This method test all the button lights at once by using the {@code
* fillButtonLights();} method. After 2.5
* seconds, all the button lights will be turned off with {@code
* disableButtonLights();}.
*/
public static void testAllButtonLights() {
try {
swiftBot.fillButtonLights(); // Turns on all button lights
Thread.sleep(2500); // Waiting for 2.5 seconds
swiftBot.disableButtonLights(); // Turns off all button lights at once.
} catch (Exception e) {
System.out.println("ERROR: An error occurred setting all button lights.");
e.printStackTrace();
System.exit(5);
}
}
/*
* Tests the user-selected wheel, taking the wheel as a parameter: either "left"
* or "right".
* If the "left" wheel is selected, the function sets the left wheel velocity to
* 100%
* If the "right" wheel is selected, the function sets the right wheel velocity
* to 100%
*/
public static void testWheel(String wheel) {
// These variables represent the % velocity of each wheel.
// They are updated later in the method, depending on the chosen wheel
int leftWheelVelocity = 0;
int rightWheelVelocity = 0;
// If left wheel is chosen, set left wheel velocity to 100
if (wheel.equals("left")) {
System.out.println("Testing the left wheel");
leftWheelVelocity = 100;
}
// Otherwise, set the right wheel velocity to 100.
else {
System.out.println("Testing the right wheel");
rightWheelVelocity = 100;
}
try {
// Moves the wheels with the set left wheel and right wheel velocity, for 3
// seconds.
swiftBot.move(leftWheelVelocity, rightWheelVelocity, 3000);
testWheelReverse(wheel);
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR: Error while testing wheel");
System.exit(5);
}
}
/*
* Tests the user-selected wheel defined in the method above, but reverses the
* wheels instead.
* (Setting the velocity to -100 instead of 100)
*/
public static void testWheelReverse(String wheel) {
// These variables represent the % velocity of each wheel.
// They are updated later in the method, depending on the chosen wheel
int leftWheelVelocity = 0;
int rightWheelVelocity = 0;
// If left wheel is chosen, set left wheel velocity to 100
if (wheel.equals("left")) {
System.out.println("Testing the left wheel");
leftWheelVelocity = -100;
}
// Otherwise, set the right wheel velocity to 100.
else {
System.out.println("Testing the right wheel");
rightWheelVelocity = -100;
}
try {
// Moves the wheels with the set left wheel and right wheel velocity, for 3
// seconds.
swiftBot.move(leftWheelVelocity, rightWheelVelocity, 3000);
System.out.println("SUCCESS: wheel tests successful");
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR: Error while testing wheel");
System.exit(5);
}
}
/*
* Tests each button by making them all active for 10 seconds. You can extend,
* or shorten the time by changing
* the {@code endTime} variable. When you enable a button, you are free to
* encase a method inside of it that will
* only run once a button has been pressed. In this example, once a button has
* been pressed, it'll be disabled using
* {@code swiftBot.disableButton();}, meaning it cannot be pressed again. You
* are also able to disable all buttons
* by running {@code swiftBot.disableAllButtons();}
*/
public static void testButtons() throws InterruptedException {
System.out.println("All buttons on your Bot will be active for the next 10 seconds..");
try {
long endtime = System.currentTimeMillis() + 10_000;
swiftBot.enableButton(Button.A, () -> {
System.out.println("Button A Pressed.");
swiftBot.disableButton(Button.A);
});
swiftBot.enableButton(Button.B, () -> {
System.out.println("Button B Pressed.");
swiftBot.disableButton(Button.B);
});
swiftBot.enableButton(Button.X, () -> {
System.out.println("Button X Pressed.");
swiftBot.disableButton(Button.X);
});
swiftBot.enableButton(Button.Y, () -> {
System.out.println("Button Y Pressed.");
swiftBot.disableButton(Button.Y);
});
while (System.currentTimeMillis() < endtime) {
; // This while loop does nothing for 10 seconds.
}
swiftBot.disableAllButtons(); // Turns off all buttons now that it's been 10 seconds.
System.out.println("All buttons are now off.");
} catch (Exception e) {
System.out.println("ERROR occurred when setting up buttons.");
e.printStackTrace();
System.exit(5);
}
}
/*
* This method uses a range of colours to test all under lights, where each
* colour is represented
* as an integer array, with the first value being the amount of red, the second
* the amount of green
* and the third the amount of blue. These colours are then looped through with
* a for loop and assigned
* to all under lights using the {@code fillUnderlights();} method.
*/
public static void testAllUnderlights() throws InterruptedException {
int[][] colours = {
{ 255, 0, 0 }, // Red
{ 0, 255, 0 }, // Green
{ 0, 0, 255 }, // Blue
{ 255, 255, 255 } // White
};
try {
// This for loop iterates through all colours in the colours array.
for (int[] rgb : colours) {
swiftBot.fillUnderlights(rgb);
Thread.sleep(300);
}
swiftBot.disableUnderlights();
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR: failed to set all under lights");
System.exit(5);
}
}
/*
* This method uses three colours to test each individual under light: red,
* green and blue.
* An array of under lights are created, and then iterated through with a for
* loop.
* Inside this for loop, each under light is set to red, then 200 milliseconds
* later set to blue,
* then 200 milliseconds to green. After this, the colour of the under light is
* not removed, such that
* when the method finishes, all under lights should be green.
*/
public static void testIndividualUnderlights() throws InterruptedException {
// Declaring three variables containing the RGB values for red, green and blue.
int[] red = new int[] { 255, 0, 0 };
int[] blue = new int[] { 0, 0, 255 };
int[] green = new int[] { 0, 255, 0 };
try {
// Declaring an array of under lights.
Underlight[] underlights = new Underlight[] { Underlight.BACK_LEFT, Underlight.BACK_RIGHT,
Underlight.MIDDLE_LEFT,
Underlight.MIDDLE_RIGHT, Underlight.FRONT_LEFT, Underlight.FRONT_RIGHT };
for (Underlight underlight : underlights) { // Iterates through the array of under lights
System.out.println("Testing: " + underlight);
swiftBot.setUnderlight(underlight, red);
Thread.sleep(200);
swiftBot.setUnderlight(underlight, green);
Thread.sleep(200);
swiftBot.setUnderlight(underlight, blue);
Thread.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR: Unable to set under light");
System.exit(5);
}
System.out.println("SUCCESS: All under lights should be blue");
Thread.sleep(2000);
swiftBot.disableUnderlights();
}
/*
* This method takes a capture from the SwiftBot's camera to try and find a QR
* code in an image, and then
* decode it. If a QR code is found, the method will print the decoded message
* to the console.
* You could use these methods to have the SwiftBot take commands such as
* movement, lights, etc by scanning them!
*
* Note - the distance between the SwiftBot's camera and the QR code matters. If
* the QR code is too close or too far,
* the camera's focus might not be able to pick it up.
*/
public static void testQRCodeDetection() {
long startTime = System.currentTimeMillis();
long endTime = startTime + 10000; // 10 seconds in milliseconds
System.out.println("Starting 10s timer to scan a QR code");
try {
while (System.currentTimeMillis() < endTime) {
BufferedImage img = swiftBot.getQRImage();
String decodedMessage = swiftBot.decodeQRImage(img);
if (decodedMessage.isEmpty()) {
System.out.println(
"No QR Code was found. Try adjusting the distance of the SwiftBot's Camera from the QR code, or try another.");
} else {
System.out.println("SUCCESS: QR code found");
System.out.println("Decoded message: " + decodedMessage);
break;
}
System.out.println("Time elapsed: " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
Thread.sleep(1000);
}
System.out.println("Loop finished!");
} catch (Exception e) {
System.out.println("ERROR: Unable to scan for code.");
e.printStackTrace();
System.exit(5);
}
}
}