-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurtleBot.ino
More file actions
450 lines (392 loc) · 9.95 KB
/
TurtleBot.ino
File metadata and controls
450 lines (392 loc) · 9.95 KB
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
/*
TurtleBot Main Program
El siguiente programa fue creado para el proyecto
TurtleBot, un robot educativo de bajo costo para ser
implementado en estudiantes de jardin y primer grado.
Created 23-02-2022
By Erickson Gallardo
Modified 10-05-2022
By Erickson Gallardo
*/
//#define DEBUG
#ifdef DEBUG
#define DEBUGPRINTLN(X) Serial.println(X);
#define DEBUGPRINT(X) Serial.print(X);
#else
#define DEBUGPRINTLN(X) // nothing
#define DEBUGPRINT(X) // nothing
#endif
#define AIA 9 // (pwm) pin 9 connected to pin A-IA
#define AIB 5 // (pwm) pin 5 connected to pin A-IB
#define BIA 10 // (pwm) pin 10 connected to pin B-IA
#define BIB 6 // (pwm) pin 6 connected to pin B-IB
// buttons
#define FORWARD_BTN A0
#define RIGHT_BTN A1
#define BACK_BTN A2
#define LEFT_BTN A3
#define WAIT_BTN A4
#define DELETE_BTN A5
#define START_BTN 4
#define BUZZER 8
#define MOTION_TIME 750
#define ROTATION_TIME 450
#define WAIT_TIME 1000
#define FORWARD_NOTE 1000
#define RIGHT_NOTE 1150
#define BACKWARD_NOTE 1300
#define LEFT_NOTE 1450
#define WAIT_NOTE 1600
byte SPEED = 150; // change this (0-255) to control the SPEED of the motors
byte movements[50]; // list of moves
byte movements_count = 0;
byte STATE = 0;
/*
STATE CODE
-1 = ERROR
0 = WHAIT TO INSTRUCTIONS
1 = EXECUTING INSTRUCTIONS
2 = DELETE INSTRUCTION
*/
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
DEBUGPRINTLN("Inicializando pines Puente H");
pinMode(AIA, OUTPUT);
pinMode(AIB, OUTPUT);
pinMode(BIA, OUTPUT);
pinMode(BIB, OUTPUT);
DEBUGPRINTLN("Iniciando Botones");
pinMode(FORWARD_BTN, INPUT_PULLUP);
pinMode(RIGHT_BTN, INPUT_PULLUP);
pinMode(BACK_BTN, INPUT_PULLUP);
pinMode(LEFT_BTN, INPUT_PULLUP);
pinMode(WAIT_BTN, INPUT_PULLUP);
pinMode(DELETE_BTN, INPUT_PULLUP);
pinMode(START_BTN, INPUT_PULLUP);
DEBUGPRINTLN("Sonido inicio");
resumeAlert();
resumeAlert();
DEBUGPRINTLN("Iniciando programa principal\n");
}
void loop() {
/*
BUTTON READ STATE
BUTTONS NUMBER CODE
FORWARD 1
RIGHT 2
BACK 3
LEFT 4
WAIT 5
DELETE 6
START 7
*/
DEBUGPRINTLN("Iniciando Ciclo");
if (STATE == 0) {
DEBUGPRINTLN("Estado S = 0\n Esperando accion botones");
byte btn_pressed = readButtons();
delay(300);
if (btn_pressed == 6) {
DEBUGPRINTLN("Historial de botones borrado \n Cambiando a estado S = 2\n");
STATE = 2;
} else if (btn_pressed == 7) {
DEBUGPRINTLN("Iniciar movimientos \n Cambiando a estado S = 1\n");
STATE = 1;
} else {
addMovement(btn_pressed);
}
}
/*
EXECUTION STATE
*/
if (STATE == 1) {
DEBUGPRINTLN("Iniciando Estado S = 1\n Ejecutar ");
if (movements_count == 0) {
DEBUGPRINTLN("Error - No hay movimientos registrados");
errorAlert();
} else {
DEBUGPRINTLN("Iniciando ejecución de movimientos\n");
okAlert();
delay(1000);
executeMovements();
}
STATE = 0;
}
/*
DELETE STATE
*/
if (STATE == 2) {
DEBUGPRINTLN("Iniciando estado S = 2\n Borrar movimientos");
if (movements_count == 0) {
Serial.println("There aren't registeres movements");
errorAlert();
} else {
deleteAlert();
deleteMovements();
}
STATE = 0;
}
}
void errorAlert() {
tone(BUZZER, 550, 150);
delay(175);
tone(BUZZER, 550, 150);
delay(175);
tone(BUZZER, 550, 150);
delay(175);
}
void okAlert() {
tone(BUZZER, 500, 150);
delay(175);
tone(BUZZER, 750, 150);
delay(175);
tone(BUZZER, 1000, 150);
delay(175);
}
void deleteAlert() {
tone(BUZZER, 1000, 150);
delay(160);
tone(BUZZER, 750, 150);
delay(160);
tone(BUZZER, 500, 150);
delay(160);
tone(BUZZER, 250, 150);
delay(160);
}
void stopAlert() {
tone(BUZZER, 500, 400);
}
void pauseAlert() {
tone(BUZZER, 2000, 150);
delay(160);
tone(BUZZER, 1500, 150);
delay(160);
}
void resumeAlert() {
tone(BUZZER, 1500, 150);
delay(160);
tone(BUZZER, 2000, 150);
delay(160);
}
void endAlert() {
tone(BUZZER, 1500, 150);
delay(160);
tone(BUZZER, 1750, 150);
delay(160);
tone(BUZZER, 2000, 150);
delay(160);
tone(BUZZER, 2000, 150);
delay(160);
}
void deleteMovements() {
for ( int i = 0; i < movements_count + 1; i++) {
movements[i] = 0;
}
movements_count = 0;
Serial.print("All moves have been deleted, count: "); Serial.println(movements_count);
}
void waitingFor(int total_time) {
unsigned long checkpoint = millis();
while (millis() - checkpoint < total_time) {
if (readButton(DELETE_BTN) || readButton(START_BTN)) {
break;
}
}
}
void getAction(byte action) {
if (action == 1) {
forward(MOTION_TIME);
stopMotion(WAIT_TIME);
} else if (action == 2) {
right(ROTATION_TIME);
stopMotion(WAIT_TIME);
} else if (action == 3) {
backward(MOTION_TIME);
stopMotion(WAIT_TIME);
} else if (action == 4) {
left(ROTATION_TIME);
stopMotion(WAIT_TIME);
} else if (action == 5) {
stopMotion(1000);
}
}
void executeMovements() {
DEBUGPRINTLN("Proceso: executeMovements");
bool inPause = false;
bool stopped = false;
for ( int i = 0; i < movements_count; i++) {
/*Serial.println(movements[i]);
delay(250);
*/
DEBUGPRINTLN("Ejecutando movimiento");
getAction(movements[i]);
if (!digitalRead(START_BTN)) {
Serial.println("execution paused");
pauseAlert();
inPause = true;
while (inPause) {
inPause = !digitalRead(START_BTN) == 1 ? false : true;
}
Serial.println("resumed execution ");
resumeAlert();
delay(500);
}
if (!digitalRead(DELETE_BTN)) {
stopAlert();
Serial.println("execution stopped");
stopped = true;
break;
}
}
if (!stopped) {
endAlert();
}
delay(200);
}
/*
Añade un movimiento a la lista de ejecución, el
arreglo cuenta con un limite de 50 posiciones, al
llegar al limite se sobrescribe la ultima accion.
@param movement, numero del 1 - 7 correspondiente a
un movimiento en espesifico.
*/
void addMovement(byte movement) {
movements[movements_count] = movement;
movements_count = movements_count < 50 ? movements_count + 1 : movements_count;
DEBUGPRINT("\n Movimiento ");
DEBUGPRINT(movement);
DEBUGPRINT(" añadido, Movimientos totales:");
DEBUGPRINTLN(movements_count);
}
/*
Activa un bucle infinito hasta la activación de
un boton, cada boton es evaluado y codificado con
un valor numerico correspondiente a una accion de
movimiento.
@return valor numerico de 1 - 7 correspondiente a
un movimiento en particular.
*/
byte readButtons() {
byte btn = 0;
while (true) {
if (!digitalRead(FORWARD_BTN)) {
DEBUGPRINTLN("Btn: Adelante - Activado");
btn = 1;
tone(BUZZER, FORWARD_NOTE, 200);
break;
} else if (!digitalRead(RIGHT_BTN)) {
DEBUGPRINTLN("Btn: Derecha - Activado");
btn = 2;
tone(BUZZER, RIGHT_NOTE, 200);
break;
} else if (!digitalRead(BACK_BTN)) {
DEBUGPRINTLN("Btn: Atras - Activado");
btn = 3;
tone(BUZZER, BACKWARD_NOTE, 200);
break;
} else if (!digitalRead(LEFT_BTN)) {
DEBUGPRINTLN("Btn: Izquierda - Activado");
btn = 4;
tone(BUZZER, LEFT_NOTE, 200);
break;
} else if (!digitalRead(WAIT_BTN)) {
DEBUGPRINTLN("Btn: Esperar - Activado");
btn = 5;
tone(BUZZER, WAIT_NOTE, 200);
break;
} else if (!digitalRead(DELETE_BTN)) {
btn = 6;
DEBUGPRINTLN("Btn: Stop - Activado");
break;
} else if (!digitalRead(START_BTN)) {
DEBUGPRINTLN("Btn: Ejecutar - Activado");
btn = 7;
break;
}
}
return btn;
}
/*
Evalua el estado de activación de un boton determinado
@param btn, pin de coneccion del botón a evaluar
@return pressed, valor buleano correspondiente a la
activación del botón
*/
bool readButton(int btn) {
bool pressed = !digitalRead(btn) == 1 ? true : false;
return pressed;
}
/*
Activa el movimiento de los motores para realizar
un movimiento frontal, manteniendo un tiempo de
inactividad antes de ejecutar otro movimiento
@param t, valor entero que corresponde al tiempo de
inactividad posterior de ejecutar el movimiento actual.
*/
void forward(unsigned int t) {
analogWrite(AIA, SPEED);
analogWrite(AIB, 0);
analogWrite(BIA, SPEED);
analogWrite(BIB, 0);
waitingFor(t);
}
/*
Activa el movimiento de los motores para realizar
un movimiento en retroceso, manteniendo
un tiempo de inactividad antes de ejecutar otro
movimiento
@param t, valor entero que corresponde al tiempo de
inactividad posterior de ejecutar el movimiento actual.
*/
void backward(unsigned int t) {
analogWrite(AIA, 0);
analogWrite(AIB, SPEED);
analogWrite(BIA, 0);
analogWrite(BIB, SPEED);
waitingFor(t);
}
/*
Activa el movimiento de los motores para realizar
un giro de 90 grados en direccion izquierda, manteniendo
un tiempo de inactividad antes de ejecutar otro
movimiento
@param t, valor entero que corresponde al tiempo de
inactividad posterior de ejecutar el movimiento actual.
*/
void left(unsigned int t) {
analogWrite(AIA, SPEED);
analogWrite(AIB, 0);
analogWrite(BIA, 0);
analogWrite(BIB, SPEED);
waitingFor(t);
}
/*
Activa el movimiento de los motores para realizar
un giro de 90 grados en direccion derecha, manteniendo
un tiempo de inactividad antes de ejecutar otro
movimiento
@param t, valor entero que corresponde al tiempo de
inactividad posterior de ejecutar el movimiento actual.
*/
void right(unsigned int t) {
analogWrite(AIA, 0);
analogWrite(AIB, SPEED);
analogWrite(BIA, SPEED);
analogWrite(BIB, 0);
waitingFor(t);
}
/*
Detiene el movimiento de los motores manteniendo
un tiempo de inactividad antes de ejecutar
otro movimiento
@param t, valor entero que corresponde al tiempo de
inactividad posterior de ejecutar el movimiento actual.
*/
void stopMotion(unsigned int t) {
analogWrite(AIA, 0);
analogWrite(AIB, 0);
analogWrite(BIA, 0);
analogWrite(BIB, 0);
waitingFor(t);
}