-
Notifications
You must be signed in to change notification settings - Fork 0
/
_2_007_autonomous.ino
488 lines (428 loc) · 10.2 KB
/
_2_007_autonomous.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
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
#include <Servo.h>
#include <PS2X_lib.h>
#define PHOTOR A0
#define WINCHSERVO 2
#define STRETCHERANGLESERVO 3
#define FRONTPINGSENSOR 4 //hooked up to a servo pin
#define SIDEPINGSENSOR 5 //hooked up to a servo pin
#define LEFTSERVO 6
#define RIGHTSERVO 7
#define STRETCHERMOTOR 8
#define SIDEPIN 12
#define LED 13
//REMAINING EMPTY PINS:
//0,1 : dont use if possible, for main TX/RX
//8 : servo connection
//10,11 : nothing special
//13 : nothing special
//A5-A7 : analog
//winch constants
#define W_UP 0
#define W_DOWN 180
#define W_STOP 70
//drive constants
#define FORWARD_L 170
#define FORWARD_R 0
#define BACKWARDS_L 0
#define BACKWARDS_R 180
#define LEFTZERO 98
#define RIGHTZERO 95
//amount the light value has to change before robot considers LED to be lit
#define LIGHTCHANGETHRESHOLD 180
//adjust the rubber band stretcher angle
#define STRETCHERBACK 120
#define STRETCHEROUT 40
//motor stretching controls
#define MOTOROUT 1000
#define MOTORIN 2000
#define MOTORSTOP 1500
//#################################################
//CHANGE THIS VALUE
boolean leftSide = false;
//################################################
int relativeLightValue;
int PS2PINS[] = {A4,A2,A3,A1}; //{CLOCK=blue, COMMAND=orange, ATTENTION=yellow, DATA=brown}
PS2X ps2x; // create PS2 Controller Class
Servo leftWheel;
Servo rightWheel;
Servo winch;
Servo stretcherAngle;
Servo stretcherMotor;
void setup() {
//set up servos
leftWheel.attach(LEFTSERVO);
rightWheel.attach(RIGHTSERVO);
winch.attach(WINCHSERVO);
stretcherAngle.attach(STRETCHERANGLESERVO);
stretcherMotor.attach(STRETCHERMOTOR);
pinMode(LEFTSERVO, OUTPUT);
pinMode(RIGHTSERVO, OUTPUT);
pinMode(WINCHSERVO, OUTPUT);
pinMode(STRETCHERANGLESERVO, OUTPUT);
pinMode(STRETCHERMOTOR, OUTPUT);
pinMode(SIDEPIN, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(57600);
delay(100);
relativeLightValue = analogRead(PHOTOR);
stretcherAngle.write(STRETCHERBACK);
stretcherMotor.writeMicroseconds(MOTORSTOP);
setupPS2controller();
digitalWrite(LED, HIGH);
}
void loop() {
waitForLight();
stretcherMotor.writeMicroseconds(MOTOROUT);
if(leftSide == true)
{
getWrenchLeft(); //autonomously drive and attempt to get wrench
}
else
{
getWrenchRight();
}
stretcherMotor.writeMicroseconds(MOTORSTOP);
PS2control();
}
void waitForLight()
{
double lightValue = analogRead(PHOTOR);
while(!(abs(lightValue-relativeLightValue) > LIGHTCHANGETHRESHOLD))
{
lightValue = analogRead(PHOTOR);
delay(10);
}
}
void getWrenchLeft()
{
forward();
delay(800);
spinRight();
delay(1800);
followEdge(12, 35);
hardBackLeft();
delay(1000);
spinRight();
delay(2100);
backwards();
while(pingDistanceCM(FRONTPINGSENSOR) < 44)
{
//do nothing. Ping already has a delay 100ms built in
}
stopDriving();
liftWrench();
}
void getWrenchRight()
{
forward();
delay(800);
spinRight();
delay(2000);
backwards();
delay(14000); //10 seconds
//now we should be against the far wall
forward();
delay(1800);
spinRight();
delay(1700);
backwards();
while(pingDistanceCM(FRONTPINGSENSOR) < 34)
{
//do nothing. Ping already has a delay 100ms built in
}
stopDriving();
liftWrench();
}
void followEdge(int distFromEdge, int endDist)
{
Serial.println("entered followEdge");
int lastValue = pingDistanceCM(SIDEPINGSENSOR);
delay(100);
int change = 0;
forward();
while(pingDistanceCM(FRONTPINGSENSOR) > endDist)
{
int currentValue = pingDistanceCM(SIDEPINGSENSOR);
Serial.println("currentValue: " + String(currentValue));
change = lastValue - currentValue;
Serial.println("change: " + String(change));
if(change > 0)
{//we are getting close to wall
if(currentValue - change < distFromEdge)
{//the next timestep should take us slightly too close. slow down outside wheel
Serial.println("will get too close. turn halfLeft");
halfLeft();
}
else
{//next timestep will not take us too close, but we are getting closer. keep going straight
Serial.println("getting closer, wont get too close. go straight");
forward();
}
}
else if(change < 0)
{//we are getting further from wall
if(currentValue - change < distFromEdge)
{//the next timestep should take us slightly too far. slow down outside wheel
Serial.println("getting farther. will get too far. turn halfRight");
halfRight();
}
else
{//next timestep will not take us too far, but we are getting farther. keep going straight
Serial.println("getting farther, wont get too far. go straight");
forward();
}
}
else
{//same distance from wall.
Serial.println("good distance. go straight");
forward();
}
lastValue = currentValue;
delay(400);
}
Serial.println("left followEdge");
}
void liftWrench()
{
//run winch
winch.write(W_DOWN);
delay(2500);
winch.write((W_STOP-W_UP)/2); //go up half speed
delay(3500);
winch.write(W_STOP);
}
int pingDistanceCM(int pinNum)
{
//send a ping
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, LOW);
delayMicroseconds(2);
digitalWrite(pinNum, HIGH);
delayMicroseconds(5);
digitalWrite(pinNum, LOW);
//wait for return
pinMode(pinNum, INPUT);
long duration = pulseIn(pinNum, HIGH);
//convert to cm
Serial.println((int)duration / 58);
delay(100); //help prevent interference
return (int)duration / 58; //its a magic number for converting
}
void forward()
{
rightWheel.write(FORWARD_R);
leftWheel.write(FORWARD_L);
}
void backwards()
{
rightWheel.write(BACKWARDS_R);
leftWheel.write(BACKWARDS_L);
}
void spinLeft()
{
rightWheel.write(FORWARD_R);
leftWheel.write(BACKWARDS_L);
}
void spinRight()
{
rightWheel.write(BACKWARDS_R);
leftWheel.write(FORWARD_L);
}
void hardLeft()
{
rightWheel.write(FORWARD_R);
leftWheel.write(LEFTZERO);
}
void hardRight()
{
rightWheel.write(RIGHTZERO);
leftWheel.write(FORWARD_L);
}
void hardBackLeft()
{
rightWheel.write(BACKWARDS_R);
leftWheel.write(LEFTZERO);
}
void hardBackRight()
{
rightWheel.write(RIGHTZERO);
leftWheel.write(BACKWARDS_L);
}
void halfLeft()
{
rightWheel.write(FORWARD_R);
leftWheel.write((4.5*LEFTZERO+1.0*FORWARD_L)/5.5);
}
void halfRight()
{
rightWheel.write((4.5*RIGHTZERO+1.0*FORWARD_R)/5.5);
leftWheel.write(FORWARD_L);
}
void driveCustom(int left, int right)
{
leftWheel.write(left);
rightWheel.write(right);
}
void mapAndDrive(byte left, byte right)
{
//Serial.println("left:" + (String)left + " right:" + (String)right);
int l;
int r;
if(left < 50)
{
l = FORWARD_L;
}
else if(left > 205)
{
l = BACKWARDS_L;
}
else// if(left <
{
l = LEFTZERO;
}
if(right < 50)
{
r = FORWARD_R;
}
else if (right > 205)
{
r = BACKWARDS_R;
}
else
{
r = RIGHTZERO;
}
driveCustom(l, r);
}
void stopDriving()
{
rightWheel.write(RIGHTZERO);
leftWheel.write(LEFTZERO);
}
void enterRCmode()
{
pinMode(LEFTSERVO, INPUT);
pinMode(RIGHTSERVO, INPUT);
pinMode(WINCHSERVO, INPUT);
}
void setupPS2controller()
{
int error;
byte type;
//config_gamepad(clock, command, attention, data, Pressures?, Rumble?)
error = ps2x.config_gamepad(PS2PINS[0],PS2PINS[1],PS2PINS[2],PS2PINS[3], false, false);
if(error == 0){
Serial.println("Found Controller, configured successful");
}
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
//Serial.print(ps2x.Analog(1), HEX);
type = ps2x.readType();
switch(type) {
case 0:
Serial.println("Unknown Controller type");
setupPS2controller();
break;
case 1:
Serial.println("DualShock Controller Found");
break;
}
ps2x.read_gamepad();
if(ps2x.Analog(PSS_LY) == 255)
{
Serial.println("value was 255 (AKA controller not set up correctly, re-running setup");
setupPS2controller();
}
}
void PS2control()
{
int stretcherAnglePos = (STRETCHERBACK + STRETCHEROUT)/2.0;
while(true) //run this forever
{
ps2x.read_gamepad(); //read controller. changes are only recognized here
/*
//INFORMATION FOR WRITING ADDITIONAL FUNCTIONS
//BUTTON NAMES
PSB_L1
PSB_L2
PSB_L3
PSB_R1
PSB_R2
PSB_R3
PSB_PAD_LEFT
PSB_PAD_RIGHT
PSB_PAD_UP
PSB_PAD_DOWN
PSB_START
PSB_SELECT
PSAB_BLUE
PSB_GREEN
PSB_PINK
PSB_RED
//BUTTON FUNCTIONS
ps2x.ButtonPressed(button) //true if just pressed
ps2x.Button(button) //true if currently pressed
ps2x.NewButtonState)(button) //true if state just changed
//ANALOG NAMES
PSS_LY
PSS_LX
PSS_RY
PSS_RX
//ANALOG FUNCTIONS
ps2x.Analog(button/stick) //byte, pressure
*/
//wrench lift
if(ps2x.Button(PSB_R2))
{
Serial.println("Triangle pressed");
winch.write(W_DOWN);
}
else if(ps2x.Button(PSB_R1))
{
Serial.println("Circle pressed");
winch.write((W_STOP-W_UP)/2); //go up half speed
}
else
{
winch.write(W_STOP);
}
//stretcher
if(ps2x.Button(PSB_L2))
{
Serial.println("Left dpad pressed");
stretcherMotor.writeMicroseconds(MOTOROUT);
}
else if(ps2x.Button(PSB_L1))
{
Serial.println("Right dpad pressed");
stretcherMotor.writeMicroseconds(MOTORIN);
}
else
{
stretcherMotor.writeMicroseconds(MOTORSTOP);
}
//stretcher angle
if(ps2x.Button(PSB_PAD_UP))
{
Serial.println("up dpad pressed");
if(stretcherAnglePos > STRETCHEROUT)
{
stretcherAnglePos -= 1;
}
}
else if(ps2x.Button(PSB_PAD_DOWN))
{
Serial.println("down dpad pressed");
if(stretcherAnglePos < STRETCHERBACK)
{
stretcherAnglePos += 1;
}
}
stretcherAngle.write(stretcherAnglePos);
mapAndDrive(ps2x.Analog(PSS_LY), ps2x.Analog(PSS_RY));
delay(10);
}
}