/
Robotic_Ball_Retrieval_Code.ino
204 lines (202 loc) · 4.7 KB
/
Robotic_Ball_Retrieval_Code.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
#include <Servo.h>
#include <Wire.h>
#include "Pixy.h"
#include <Adafruit_MotorShield.h>
Pixy pixy;
Servo cage;
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Create the motor shield object
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1); // Declare our motor ports
Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
int yVars[100];
int x=pixy.blocks[1].x;
int y=pixy.blocks[1].y;
uint16_t blocks;
int determinantA=2;
int determinantB=2;
void setup()
{
Serial.begin(250000); // 9600 baud for the serial *console* (not for the UART connected to Pixy)
AFMS.begin();
pixy.init();
cage.attach(10);
cage.write(130);
}
void backwards (int roit, int loit){
myMotor1->run(RELEASE);
myMotor4->run(RELEASE);
myMotor3->run(RELEASE);
myMotor4->setSpeed(roit/2);
myMotor4->run(BACKWARD);
myMotor3->setSpeed(loit/2);
myMotor3->run(FORWARD);
delay(250);
myMotor4->setSpeed(roit);
myMotor4->run(BACKWARD);
myMotor3->setSpeed(loit);
myMotor3->run(FORWARD);
}
void straight (int roit, int loit){
myMotor1->run(RELEASE);
myMotor4->run(RELEASE);
myMotor3->run(RELEASE);
myMotor4->setSpeed(roit/2);
myMotor4->run(FORWARD);
myMotor3->setSpeed(loit/2);
myMotor3->run(BACKWARD);
delay(50);
myMotor4->setSpeed(roit);
myMotor4->run(FORWARD);
myMotor3->setSpeed(loit);
myMotor3->run(BACKWARD);
}
void yeet (){
myMotor1->run(RELEASE);
myMotor4->run(RELEASE);
myMotor3->run(RELEASE);
myMotor1->setSpeed(0);
myMotor1->run(BACKWARD);
myMotor4->setSpeed(0);
myMotor4->run(FORWARD);
myMotor3->setSpeed(0);
myMotor3->run(FORWARD);
//delay(10);
}
void SpinRight (int pew){
myMotor1->setSpeed(pew);
myMotor1->run(BACKWARD);
myMotor4->setSpeed(pew);
myMotor4->run(BACKWARD);
myMotor3->setSpeed(pew);
myMotor3->run(BACKWARD);
}
void SpinLeft (int pewpew){
myMotor1->setSpeed(pewpew);
myMotor1->run(FORWARD);
myMotor4->setSpeed(pewpew);
myMotor4->run(FORWARD);
myMotor3->setSpeed(pewpew);
myMotor3->run(FORWARD);
}
void track (int ident){
if (pixy.blocks[ident].height*pixy.blocks[ident].width>19){
if (pixy.blocks[ident].x>0 && pixy.blocks[ident].x<=75) { straight(240,105); }
if (pixy.blocks[ident].x>75 && pixy.blocks[ident].x<=150) { straight(240,135); }
if (pixy.blocks[ident].x<170 && pixy.blocks[ident].x>150) { straight(185,185); }
if (pixy.blocks[ident].x<250 && pixy.blocks[ident].x>=170) { straight(135,185); }
if (pixy.blocks[ident].x<320 && pixy.blocks[ident].x>=250) { straight(105,240); }
}
}
void seenBall (){
if (blocks){
track(1);
}
else{/*
yeet();
delay(200);
if (blocks){}
else{*/
if (pixy.blocks[0].height*pixy.blocks[0].width>49 && pixy.blocks[1].height*pixy.blocks[1].width>49){
SpinLeft(40);
delay(175);
yeet();
cage.write(70);
SpinRight(40);
delay(2500);
determinantA=1;
determinantB=2;
}//}
}
}
void seenBallz (){
if (blocks){
track(0);
}
else{/*
yeet();
delay(200);
if (blocks){}
else{*/
if (pixy.blocks[0].height*pixy.blocks[0].width>49 && pixy.blocks[1].height*pixy.blocks[1].width>49){
SpinLeft(40);
delay(175);
yeet();
cage.write(70);
SpinRight(40);
delay(2500);
determinantA=1;
determinantB=2;
}//}
}
}
void seenHomez (){
if (blocks){
track(0);
}
else{
yeet();
delay(200);
if (blocks){}
else{
if (pixy.blocks[0].height*pixy.blocks[0].width>49 && pixy.blocks[1].height*pixy.blocks[1].width>49){
SpinLeft(40);
delay(200);
yeet();
cage.write(130);
backwards(100,100);
delay(500);
SpinRight(80);
delay(1000);
yeet();
determinantB=1;
}}
}
}
void seenHome (){
if (blocks){
track(1);
}
else{//if (pixy.blocks[1].signature!=1 && pixy.blocks[0].signature!=1){
yeet();
delay(200);
if (blocks){ }
else{
if (pixy.blocks[0].height*pixy.blocks[0].width>49 && pixy.blocks[1].height*pixy.blocks[1].width>49){
SpinLeft(40);
delay(200);
yeet();
cage.write(130);
backwards(100,100);
delay(500);
SpinRight(80);
delay(1000);
yeet();
}}
}
determinantB=1;
}
void loop(){
blocks = pixy.getBlocks();
if (determinantA==2){
SpinRight(40);
if (pixy.blocks[0].signature==1 || pixy.blocks[1].signature==1){
if (pixy.blocks[0].signature==1){
seenBallz();
}
if (pixy.blocks[1].signature==1){
seenBall();
}}}
if (determinantA==1){
if (determinantB==2){
SpinRight(40);
if (pixy.blocks[0].signature==2 || pixy.blocks[1].signature==2){
if (pixy.blocks[0].signature==2){
seenHomez();
}
if (pixy.blocks[1].signature==2){
seenHome();
}
}
}
}
}