-
Notifications
You must be signed in to change notification settings - Fork 0
/
pug.ino
281 lines (234 loc) · 5.08 KB
/
pug.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
#include <Servo.h>
// debug mode
#define DEBUG_ULTRASON false
#define DEBUG false
// Pins
#define RIGHT_MOTOR_PIN_1 8
#define RIGHT_MOTOR_PIN_2 9
#define LEFT_MOTOR_PIN_1 11
#define LEFT_MOTOR_PIN_2 10
#define COLOR_ANALOG_PIN 1
#define POTARD_ANALOG_PIN 2
#define JUMPER_PIN 3
#define ENEMY_LED_PIN 13
#define GREEN_LED_PIN 7
#define YELLOW_LED_PIN 6
#define STARTER_SERVO_PIN 19
// Start timer in ms
#define START_TIMER 15000
// Ultrason
#define MIN_DISTANCE_IN_CM 20
#define ULTRASOUND true // Disable ultrason if false
// Goodies mode
#define TOUPIE false
#define TOUPIE_2 false
#define JUMP false
IntervalTimer ultraTimer;
IntervalTimer motorTimer;
volatile unsigned int ultraBuffer[3] = {0, 0, 0};
volatile unsigned int ultraCount = 1;
volatile unsigned int ultraSum = 0;
volatile boolean hasEnnemy = false;
volatile int loopCounter = 0;
volatile int leftSpeed = 100;
volatile int rightSpeed = 100;
Servo starterServo;
/**
* Returns true if robot is on green side.
**/
boolean isGreen() {
return analogRead(COLOR_ANALOG_PIN) > 500;
}
int getRotateDelay() {
return analogRead(POTARD_ANALOG_PIN);
}
/**
* Set left motor speed
* @param int speed - between -100 and 100
**/
void setLeftSpeed(int speed) {
leftSpeed = speed + 100;
}
/**
* Set right motor speed
* @param int speed - between -100 and 100
*
**/
void setRightSpeed(int speed) {
rightSpeed = speed + 100;
}
/**
* Stop motors (freewheel)
**/
void stopMotors() {
rightSpeed = 100;
leftSpeed = 100;
}
/**
* Rotate of +/- 90° to right
**/
void rotateRight(int rotateTimeRight) {
setLeftSpeed( 60 );
setRightSpeed( -60 );
delay(rotateTimeRight);
stopMotors();
}
/**
* Rotate of +/- 90° to left
**/
void rotateLeft(int rotateTimeLeft)
{
setLeftSpeed( -60 );
setRightSpeed( 60 );
delay(rotateTimeLeft);
stopMotors();
}
/**
* Go backward
* @param int speed - between 0 and 100
**/
void goBackward(int speed) {
setLeftSpeed(-speed);
setRightSpeed(-speed);
}
/**
* Go forward (with ennemy check)
* @param int speed - between 0 and 100
* @param int duration - duration in Ms
**/
void goForward(int speed, int duration) {
for ( int i = 0; i < duration / 1; i++) {
if (ULTRASOUND && hasEnnemy) {
setLeftSpeed(-1); // Break!
setRightSpeed(-1); // Break!
i--;
} else {
setLeftSpeed(speed);
setRightSpeed(speed);
}
delay(1);
}
}
// Servo control methods
/**
* Do a smooth sploch! (vertical to horizontal)
**/
void smoothSploch () {
for (int i = 130; i > 90; i--) {
starterServo.write(i);
delay(50);
}
for (int i = 90; i > 30; i--) {
starterServo.write(i);
delay(10);
}
starterServo.detach();
}
/**
* Put the robot in vertical mode
**/
void prepareToStart () {
starterServo.attach(STARTER_SERVO_PIN);
starterServo.write(130);
}
void setup() {
// Motor Right
pinMode(LEFT_MOTOR_PIN_1, OUTPUT);
pinMode(LEFT_MOTOR_PIN_2, OUTPUT);
// Motor Left
pinMode(RIGHT_MOTOR_PIN_1, OUTPUT);
pinMode(RIGHT_MOTOR_PIN_2, OUTPUT);
motorTimer.begin(_motorsLivecycle, 10);
ultraTimer.begin(_ultrasonLivecycle, 25000);
// Debug led
pinMode(ENEMY_LED_PIN, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
// Jumper
pinMode(JUMPER_PIN, INPUT);
// Serial
Serial.begin(9600);
// Highlight leds in function of robot color
digitalWrite(GREEN_LED_PIN, isGreen());
digitalWrite(YELLOW_LED_PIN, !isGreen());
}
boolean hasCalibrate = false;
void loop() {
if (DEBUG) return;
if(JUMP){
while (!digitalRead(JUMPER_PIN));
while (digitalRead(JUMPER_PIN));
setLeftSpeed( 100 );
setRightSpeed(100);
delay(5000);
stopMotors();
return;
}
if (TOUPIE) {
while (!digitalRead(JUMPER_PIN));
while (digitalRead(JUMPER_PIN));
setLeftSpeed( 100 );
setRightSpeed( -100 );
delay(5000);
stopMotors();
return;
}
if(TOUPIE_2) {
while (!digitalRead(JUMPER_PIN));
while (digitalRead(JUMPER_PIN));
setLeftSpeed(100);
setRightSpeed(100);
delay(500);
setLeftSpeed(-100);
setRightSpeed(-100);
delay(200);
setRightSpeed(100);
delay(1000);
stopMotors();
return;
}
if (!hasCalibrate) {
// Turn for calibrate with potard rotation before the match
Serial.println(getRotateDelay());
if (isGreen()) {
rotateLeft(getRotateDelay());
} else {
rotateRight(getRotateDelay());
}
hasCalibrate = true;
}
// Waiting for a jumper
while (!digitalRead(JUMPER_PIN));
prepareToStart();
// Waiting for remove jumper to start
while (digitalRead(JUMPER_PIN));
smoothSploch();
// Calibration on table border
// goBackward(20);
// delay(3000);
// stopMotors();
// Tempo before start.
delay(START_TIMER);
// Forward
goForward(30, 950); // 900 before morning calibration ;)
// Break!
goBackward(1);
delay(100);
stopMotors();
delay(1000);
// Rotate
if (isGreen()) {
rotateLeft(getRotateDelay());
} else {
rotateRight(getRotateDelay());
}
delay(1000);
// Go upstair!
goBackward(60);
delay(850);
goBackward(1);
delay(100);
stopMotors();
}