-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vision_2013_little_robot.ino
302 lines (276 loc) · 7.56 KB
/
Vision_2013_little_robot.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
#include <elapsedMillis.h>
#include <Stepper.h>
#include <LiquidCrystal.h>
#include <TimerThree.h>
#include "VisionStepper.h"
#include "VisionSensorsDevices.h"
#include "pins_little_robot.h"
#include "little_robot_constants.h"
#define STATE_STOP -1
#define STATE_WAIT -2
#define STATE_WAIT_MOTORS_STOP -3
#define BLACK 100
#define FRONT 1
#define BACK 2
#define LEFT 3
#define RIGHT 4
elapsedMillis wait_time;
int time_to_wait, state_to_set_after_wait;
VisionStepper motorLeft;
VisionStepper motorRight;
sensors_and_devices SnD;
boolean obstructionDetected = false;
boolean motorsPaused = false;
boolean ignoreSensors = false;
int state = 0;
int shotBalls = 0;
int directionMovement = 0;
void setup()
{
SnD.init();
//Serial.begin(9600);
motorLeft.init();
motorLeft.initPins(enablePinLeft, directionPinLeft, stepPinLeft);
motorLeft.initDelays(startSpeedDelay, highPhaseDelay, maxSpeedDelay);
motorLeft.initSizes(wheelDiameter, wheelRevolutionSteps, distanceBetweenWheels);
motorRight.init();
motorRight.initPins(enablePinRight, directionPinRight, stepPinRight);
motorRight.initDelays(startSpeedDelay, highPhaseDelay, maxSpeedDelay);
motorRight.initSizes(wheelDiameter, wheelRevolutionSteps, distanceBetweenWheels);
//pinMode(buttonTestPin, INPUT_PULLUP);
obstructionDetected = false;
motorsPaused = false;
ignoreSensors = false;
delay(1000);
state = 0;
}
void loop()
{
switch (state)
{
case 0: //move forward
MoveForward(45,mediumSpeedDelay);
waitForMotorsStop(state + 2);
break;
case 1:
//Serial.println(SnD.detectColor());
/*if(SnD.detectColor() == BLACK)
{
motorLeft.pause();
motorRight.pause();
state++;
} */
//SnD.startShooting();
//delay(1000);
//SnD.startSpinningBallTray();
//MoveForward(28,ultraSlowSpeedDelay);
//waitForMotorsStop(STATE_STOP);
break;
case 2: //wait to complete and rotate left
TurnLeft(90);
waitForMotorsStop(state + 1);
break;
case 3: //wait to complete and move forward
MoveForward(35,mediumSpeedDelay);
waitForMotorsStop(state + 1);
break;
case 4: //shoot balls
SnD.startShooting();
delay(1000);
SnD.startSpinningBallTray();
MoveForward(83,ultraSlowSpeedDelay);
waitForMotorsStop(state + 1);
/*
if(shotBalls < 6)
{
if(shotBalls == 0){
SnD.startShooting();
delay(500);
}
else{
SnD.stopShooting();
delay(100);
SnD.startShooting();
}
//SnD.startSpinningBallTray();
//delay(250);
SnD.shootBall();
MoveForward(5,slowSpeedDelay);
shotBalls++;
waitForMotorsStop(state);
//SnD.startSpinningBallTray();
}
else
{
SnD.stopShooting();
SnD.stopSpinningBallTray();
MoveForward(55,slowSpeedDelay);
waitForMotorsStop(state + 1);
}
*/
break;
case 5: //wait to complete and rotate left
SnD.stopShooting();
SnD.stopSpinningBallTray();
TurnLeft(90);
waitForMotorsStop(state + 1);
break;
case 6: //wait to complete and move forward
MoveForward(40, mediumSpeedDelay);
waitForMotorsStop(state + 1);
break;
case 7: //wait to complete and move forward
ignoreSensors = true;
MoveForward(20, slowSpeedDelay);
waitForMotorsStop(state + 1);
break;
case 8: //wait to complete and move backward
ignoreSensors = false;
MoveBackward(55, mediumSpeedDelay);
waitForMotorsStop(state + 1);
break;
case 9:
TurnRight(90);
waitForMotorsStop(state + 1);
break;
case 10:
MoveBackward(65,mediumSpeedDelay);
waitForMotorsStop(state + 1);
break;
case 11:
SnD.ThrowNet();
state = STATE_STOP;
break;
case STATE_STOP: //stop
break;
case STATE_WAIT:
if (wait_time > time_to_wait)
{
state = state_to_set_after_wait;
}
break;
case STATE_WAIT_MOTORS_STOP:
if (motorLeft.isOff() && motorRight.isOff())
{
state = state_to_set_after_wait;
}
if(state_to_set_after_wait == 5)
SnD.shootBall();
break;
}
obstructionDetected = false;
if (SnD.detectFront() && directionMovement == FRONT)
{
//Serial.println("Front detected!");
obstructionDetected = true;
}
if (SnD.detectBack() && directionMovement == BACK)
{
//Serial.println("Back detected!");
obstructionDetected = true;
}
if (SnD.detectLeft() && directionMovement == LEFT)
{
//Serial.println("Left detected!");
obstructionDetected = true;
}
if (SnD.detectRight() && directionMovement == RIGHT)
{
//Serial.println("Right detected!");
obstructionDetected = true;
}
if(obstructionDetected == true && ignoreSensors == false)
{
motorLeft.pause();
motorRight.pause();
motorsPaused = true;
}
else
{
if(motorsPaused == true){
motorLeft.unpause();
motorRight.unpause();
motorsPaused = false;
}
}
motorRight.doLoop();
motorLeft.doLoop();
}
void wait(int time_in_ms, int state_after)
{
state = STATE_WAIT;
wait_time = 0;
time_to_wait = time_in_ms;
state_to_set_after_wait = state_after;
}
void waitForMotorsStop(int state_after)
{
state = STATE_WAIT_MOTORS_STOP;
state_to_set_after_wait = state_after;
}
void MoveForward(float distance, int step_delay)
{
directionMovement = FRONT;
motorLeft.setTargetDelay(step_delay);
motorRight.setTargetDelay(step_delay);
motorLeft.setDirectionForward();
motorRight.setDirectionForward();
motorRight.toggleDirection();
motorLeft.doDistanceInCm(distance);
motorRight.doDistanceInCm(distance);
}
void MoveBackward(float distance, int step_delay)
{
directionMovement = BACK;
motorLeft.setTargetDelay(step_delay);
motorRight.setTargetDelay(step_delay);
motorLeft.setDirectionForward();
motorRight.setDirectionForward();
motorLeft.toggleDirection();
motorLeft.doDistanceInCm(distance);
motorRight.doDistanceInCm(distance);
}
void TurnLeft(int angle)
{
directionMovement = LEFT;
motorLeft.setDirectionForward();
motorRight.setDirectionForward();
motorRight.toggleDirection();
motorLeft.toggleDirection();
motorLeft.doRotationInAngle(angle);
motorRight.doRotationInAngle(angle);
}
void TurnRight(int angle)
{
directionMovement = RIGHT;
motorLeft.setDirectionForward();
motorRight.setDirectionForward();
motorLeft.doRotationInAngle(angle);
motorRight.doRotationInAngle(angle);
}
void ArcToLeft(int radius, int step_delay, boolean forward)
{
motorLeft.setTargetDelay(step_delay * 2);
motorRight.setTargetDelay(step_delay);
motorLeft.setDirectionForward();
motorRight.setDirectionForward();
if(forward)
motorRight.toggleDirection();
else
motorLeft.toggleDirection();
motorLeft.doDistanceInCm(radius / 4);
motorRight.doDistanceInCm(radius / 2);
}
void ArcToRight(int radius, int step_delay, boolean forward)
{
motorLeft.setTargetDelay(step_delay);
motorRight.setTargetDelay(step_delay * 2);
motorLeft.setDirectionForward();
motorRight.setDirectionForward();
if(forward)
motorRight.toggleDirection();
else
motorLeft.toggleDirection();
motorLeft.doDistanceInCm(radius / 2);
motorRight.doDistanceInCm(radius / 4);
}