-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoombaPS3BTOwiArm.ino
388 lines (336 loc) · 10.2 KB
/
RoombaPS3BTOwiArm.ino
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
//
// This was developed with Arduino 1.0.2 // used in 1.0.0 seems OK DCW 12-28-2012
// USB_Host_Shield_2.0 from Circuits@home
// PS3 USB library - developed by Kristian Lauszus http://blog.tkjelectronics.dk/
//
// This code block is from PS3 example program
// #include <PS3USB.h>
#include <PS3BT.h>
#include <AFMotor.h>
USB Usb;
BTD Btd(&Usb);
/* You can create the instance of the class in two ways */
//PS3USB PS3(&Usb); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
PS3BT PS3(&Btd,0x00,0x1B,0xDC,0x00,0x3D,0x58); // Cirago tiny BT adapter
boolean printAngle;
uint8_t state = 0;
// end ps3 example program
int rxPin = 19;
int txPin = 18;
int ddPin = 24;
int ledPin = 13;
bool goingForward = false;
bool goingBack = false;
bool goingLeft = false;
bool goingRight = false;
boolean first = true;
AF_DCMotor motor1(1); // Instantiate all the motors
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
int val0 = 0; // variable to store the Arm 1 sensor
int val1 = 0; // variable to store the Arm 2 sensor
int val2 = 0; // variable to store the Arm 3 sensor
int val3 = 0; // variable to store the Arm 4 sensor
boolean armMode = false; // semaphore for analog control of the arm
void setup(){
pinMode(ddPin, OUTPUT); // sets the pins as output
pinMode(ledPin, OUTPUT); // sets the pins as output
Serial1.begin(57600);
Serial.begin(115200);
// This is here for on reset to put the Roomba back to passive mode so it can be woken up again.
// Allows for an arduino reboot to reset the Roomba if a sensor tripped.
Serial1.write(byte(130));
delay(50);
// PS3 stuff
// Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while(1); //halt
}
Serial.println(F("\r\nPS3 USB Library Started"));
// wake up the roomba
digitalWrite(ddPin, HIGH);
delay(100);
digitalWrite(ddPin, LOW);
delay(500);
digitalWrite(ddPin, HIGH);
delay(2000);
// set up ROI to receive commands
//Serial1.print(128, BYTE); // START
Serial1.write(byte(128));
delay(50);
//Serial1.print(130, BYTE); // CONTROL
Serial1.write(byte(130));
delay(50);
digitalWrite(ledPin, LOW); // say we've finished setup
} // end setup
void loop(){
Usb.Task();
if (first){
Serial.println("First entry into the loop, ready for action!!");
goForward();
delay(100);
goStop();
first = false;
digitalWrite(ledPin, HIGH);
} // end first
if(Serial.available()){
int inVal = Serial.parseInt();
if (inVal == 0){
motor1.setSpeed(0); // turn off motors
motor2.setSpeed(0);
motor3.setSpeed(0);
motor4.setSpeed(0);
} // end if
if (inVal == 1){ // motor 1 forward
motor1.setSpeed(200); // set the speed to 100/255
motor1.run(FORWARD);
delay(250);
motor1.setSpeed(0);
} // end if
if (inVal == 2){ // motor 1 backward
motor1.setSpeed(200); // set the speed to 100/255
motor1.run(BACKWARD);
delay(250);
motor1.setSpeed(0);
} // end if
if (inVal == 3){ // motor 2 forward
motor2.setSpeed(200); // set the speed to 100/255
motor2.run(FORWARD);
delay(250);
motor2.setSpeed(0);
} // end if
if (inVal == 4){ // motor 2 backward
motor2.setSpeed(200); // set the speed to 100/255
motor2.run(BACKWARD);
delay(250);
motor2.setSpeed(0);
} // end if
if (inVal == 5){ // motor 3 forward
motor3.setSpeed(200); // set the speed to 100/255
motor3.run(FORWARD);
delay(250);
motor3.setSpeed(0);
} // end if
if (inVal == 6){ // motor 3 backward
motor3.setSpeed(200); // set the speed to 100/255
motor3.run(BACKWARD);
delay(250);
motor3.setSpeed(0);
} // end if
if (inVal == 7){ // motor 4 forward
motor4.setSpeed(200); // set the speed to 100/255
motor4.run(FORWARD);
delay(250);
motor4.setSpeed(0);
} // end if
if (inVal == 8){ // motor 4 backward
motor4.setSpeed(200); // set the speed to 100/255
motor4.run(BACKWARD);
delay(250);
motor4.setSpeed(0);
} // end if
} // end keyboard input
if(PS3.getButtonClick(SELECT)) {
Serial.println("SELECT Press registered");
if(armMode){
armMode = false;
PS3.setLedOff(LED5);
Serial.println("ArmMode disengaged");
}
else{
PS3.setLedOn(LED6);
armMode = true;
Serial.println("ArmMode engaged!!");
}
}
if(armMode){
if(PS3.getAngle(Roll) < 150){ // move motor 1 left
Serial.println(PS3.getAngle(Roll));
motor1.setSpeed(255); // set the speed to 100/255
motor1.run(BACKWARD);
delay(5);
motor1.setSpeed(0);
} // end left
if(PS3.getAngle(Roll) > 210){ // move motor 2 right
Serial.println(PS3.getAngle(Roll));
motor1.setSpeed(255); // set the speed to 100/255
motor1.run(FORWARD);
delay(5);
motor1.setSpeed(0);
} // end right
if(PS3.getAngle(Pitch) < 150){ // move motor 2 up
Serial.println(PS3.getAngle(Pitch));
motor2.setSpeed(255); // set the speed to 100/255
motor2.run(FORWARD);
delay(5);
motor2.setSpeed(0);
} // end up
if(PS3.getAngle(Pitch) > 210){ // move motor 2 down
Serial.println(PS3.getAngle(Pitch));
motor2.setSpeed(255); // set the speed to 100/255
motor2.run(BACKWARD);
delay(5);
motor2.setSpeed(0);
} // end down
if(PS3.getAnalogHat(LeftHatY) < 117){ // move motor 3 up
Serial.println(PS3.getAnalogHat(LeftHatY));
motor3.setSpeed(255); // set the speed to 100/255
motor3.run(FORWARD);
delay(5);
motor3.setSpeed(0);
} // end
if(PS3.getAnalogHat(LeftHatY) > 138){ // move motor 3 down
Serial.println(PS3.getAnalogHat(LeftHatY));
motor3.setSpeed(255); // set the speed to 100/255
motor3.run(BACKWARD);
delay(5);
motor3.setSpeed(0);
} // end
if(PS3.getAnalogHat(RightHatY) < 117){ // move motor 4 up
Serial.println(PS3.getAnalogHat(LeftHatY));
motor4.setSpeed(255); // set the speed to 100/255
motor4.run(FORWARD);
delay(5);
motor4.setSpeed(0);
} // end
if(PS3.getAnalogHat(RightHatY) > 138){ // move motor 4 down
Serial.println(PS3.getAnalogHat(LeftHatY));
motor4.setSpeed(255); // set the speed to 100/255
motor4.run(BACKWARD);
delay(5);
motor4.setSpeed(0);
} // end
} // end armmode
if(PS3.getButtonPress(UP)) {
Serial.println("Up Press registered");
if(!goingForward){
goForward();
goingForward = true;
PS3.setAllOff();
PS3.setLedOn(LED1);
} //
}
if(!PS3.getButtonPress(UP)) {
if(goingForward){
goStop();
goingForward = false;
}
}
if(PS3.getButtonPress(DOWN)) {
if(!goingBack){
goBackward();
goingBack = true;
PS3.setAllOff();
PS3.setLedOn(LED2);
}
}
if(!PS3.getButtonPress(DOWN)) {
if(goingBack){
goStop();
goingBack = false;
}
}
if(PS3.getButtonPress(LEFT)) {
if(!goingLeft){
spinLeft();
goingLeft = true;
PS3.setAllOff();
PS3.setLedOn(LED3);
}
}
if(!PS3.getButtonPress(LEFT)) {
if(goingLeft){
goStop();
goingLeft = false;
}
}
if(PS3.getButtonPress(RIGHT)) {
if(!goingRight){
spinRight();
goingRight = true;
PS3.setAllOff();
PS3.setLedOn(LED4);
}
}
if(!PS3.getButtonPress(RIGHT)) {
if(goingRight){
goStop();
goingRight = false;
}
}
} // end loop
void goForward() {
/*
Serial1.print(137, BYTE); // DRIVE
Serial1.print(0x00,BYTE); // 0x00c8 == 200
Serial1.print(0xc8,BYTE);
Serial1.print(0x80,BYTE);
Serial1.print(0x00,BYTE);
*/
//Serial.println("Forward");
Serial1.write(byte(137));
Serial1.write(byte(0x00));
Serial1.write(byte(0xc8));
Serial1.write(byte(0x80));
Serial1.write(byte(0x00));
}
void goBackward() {
/*
Serial1.print(137, BYTE); // DRIVE
Serial1.print(0xff,BYTE); // 0xff38 == -200
Serial1.print(0x38,BYTE);
Serial1.print(0x80,BYTE);
Serial1.print(0x00,BYTE);
*/
Serial1.write(byte(137));
Serial1.write(byte(0xff));
Serial1.write(byte(0x38));
Serial1.write(byte(0x80));
Serial1.write(byte(0x00));
}
void spinLeft() {
/*
Serial1.print(137, BYTE); // DRIVE
Serial1.print(0x00,BYTE); // 0x00c8 == 200
Serial1.print(0xc8,BYTE);
Serial1.print(0x00,BYTE);
Serial1.print(0x01,BYTE); // 0x0001 == spin left
*/
Serial1.write(byte(137));
Serial1.write(byte(0x00));
Serial1.write(byte(0xc8));
Serial1.write(byte(0x00));
Serial1.write(byte(0x01));
}
void spinRight() {
/*
Serial1.print(137, BYTE); // DRIVE
Serial1.print(0x00,BYTE); // 0x00c8 == 200
Serial1.print(0xc8,BYTE);
Serial1.print(0xff,BYTE);
Serial1.print(0xff,BYTE); // 0xffff == -1 == spin right
*/
Serial1.write(byte(137));
Serial1.write(byte(0x00));
Serial1.write(byte(0xc8));
Serial1.write(byte(0xff));
Serial1.write(byte(0xff));
}
void goStop() {
/*
Serial1.print(137, BYTE); // DRIVE
Serial1.print(0x00,BYTE); //
Serial1.print(0x00,BYTE);
Serial1.print(0x00,BYTE);
Serial1.print(0x00,BYTE); //
Serial.println("Stop");
*/
Serial1.write(byte(137));
Serial1.write(byte(0x00));
Serial1.write(byte(0x00));
Serial1.write(byte(0x00));
Serial1.write(byte(0x00));
}