1313
1414#include " Cnosos.h"
1515
16+ #define TIEMPO_PARA_LEER_0 2000
17+
1618// Constructores
1719Cnosos::Cnosos (Robot r, byte bits) {
1820 robot = r;
@@ -26,6 +28,60 @@ Cnosos::Cnosos(){
2628
2729
2830// Funciones
31+ /*
32+ //Traducido directamente
33+ byte Cnosos::siguiente(){
34+ robot.alante();
35+ bool bit;
36+ bool sigo = true;
37+ byte izda, centro, dcha;
38+ while (sigo) {
39+ izda = SENSOR_IZDA;
40+ centro = SENSOR_CENTRO;
41+ dcha = SENSOR_DCHA;
42+ if (izda && centro && dcha) {
43+ // cinta atravesada
44+ bit = true;
45+ sigo = false;
46+ while (SENSOR_IZDA && SENSOR_CENTRO && SENSOR_DCHA);
47+ }else if (!(izda || centro || dcha)) {
48+ while(!(SENSOR_IZDA || SENSOR_CENTRO || SENSOR_DCHA)) {}; //Salta el hueco
49+ if (!SENSOR_IZDA && SENSOR_CENTRO && !SENSOR_DCHA){
50+ // hueco
51+ bit = false;
52+ sigo = false;
53+ }
54+ }else if (izda ^ dcha) {
55+ if (SENSOR_IZDA) {
56+ robot.rotaIzda();
57+ while (SENSOR_IZDA && !SENSOR_DCHA) {};
58+ } else if (SENSOR_DCHA) {
59+ robot.rotaDcha();
60+ while (SENSOR_DCHA && !SENSOR_IZDA) {};
61+ }
62+ robot.alante();
63+ }
64+ }
65+
66+ // Bit sobrepasado. Enderezamos si es preciso.
67+ while (SENSOR_IZDA || SENSOR_DCHA) {
68+ if (SENSOR_IZDA) {
69+ robot.rotaIzda();
70+ while (SENSOR_IZDA);
71+ } else if (SENSOR_DCHA) {
72+ robot.rotaDcha();
73+ while (SENSOR_DCHA);
74+ }
75+ }
76+
77+ robot.para();
78+ delay(TIEMPO_PENSAR);
79+
80+ return bit;
81+ }
82+ */
83+ /*
84+ //Interpretacion Tapia
2985byte Cnosos::siguiente() {
3086 robot.alante();
3187 byte bit;
@@ -38,34 +94,36 @@ byte Cnosos::siguiente() {
3894 dcha = SENSOR_DCHA;
3995 if (izda && centro && dcha) {
4096 //Se confirma que es una cinta atravesada
41- delay (TIEMPO_CONFIRMACION );
4297 Serial.println("Todos activados");
4398 // cinta atravesada confirmada
4499 bit = 1;
45100 sigo = false;
101+ //Mientras haya cinta atravesada, sigue alante para evitarla
46102 while(SENSOR_IZDA && SENSOR_CENTRO && SENSOR_DCHA) {
47- // Mientras haya cinta atravesada, sigue alante
48103 robot.alante();
49104 }
105+ //Si al final del cruce se encuentra con un hueco, rota a la dcha para evitarlo
106+ while(!SENSOR_IZDA && !SENSOR_CENTRO && !SENSOR_DCHA) {
107+ robot.rotaDcha();
108+ }
109+ //Una vez el robot centrado, se continua normalmente
50110 } else if (!izda && !centro && !dcha) {
51- // TODO funciona raro, saltos entre sensores entran en esta categoria
111+ //TODO funciona raro, huecos entre sensores entran en esta categoria
52112 Serial.println("Ninguno activado");
53113 while(!SENSOR_IZDA && !SENSOR_CENTRO && !SENSOR_DCHA) {
54114 //Mientras haya hueco, sigue alante
55- robot.alante ();
115+ robot.alante();
56116 }
57117 //Si solo se activa el sensor del centro
58118 if (!SENSOR_IZDA && SENSOR_CENTRO && !SENSOR_DCHA) {
59119 // hueco confirmado
60120 bit = 0;
61121 sigo = false;
62122 }
63- // Si se activa otro sensor , descartalo; ha sido un error
123+ //Si se activa otra combinacion de sensores , descartalo; ha sido un error
64124 }
65125 //Avanza normalmente a partir de ahora.
66- if (centro) {
67- robot.alante ();
68- } else if (izda) {
126+ if (izda) {
69127 robot.rotaIzda();
70128 Serial.println("Girando izda");
71129 //Espera a que se limpie el sensor izquierdo
@@ -80,13 +138,56 @@ byte Cnosos::siguiente() {
80138 //Mientras haya un hueco, ignoralo
81139 while(!SENSOR_IZDA && !SENSOR_CENTRO && !SENSOR_DCHA){ };
82140 }
141+ robot.alante();
83142 Serial.println("Otro bucle");
84143 }
85144 robot.para();
86145 Serial.println("Bit leido");
87146 delay(TIEMPO_PENSAR);
88147 return bit;
89148}
149+ */
150+
151+ // Doble atravesado es el nuevo hueco
152+ byte Cnosos::siguiente () {
153+ byte bit;
154+ bool sigo = true ;
155+ bool izda, centro, dcha;
156+ int i;
157+ // Avanza hasta encontrar una cinta atravesada
158+ while (sigo) {
159+ robot.alante ();
160+ izda = SENSOR_IZDA ;
161+ centro = SENSOR_CENTRO ;
162+ dcha = SENSOR_DCHA ;
163+ if (izda || dcha){
164+ if (SENSOR_IZDA && SENSOR_CENTRO && SENSOR_DCHA ){
165+ bit = 1 ;
166+ sigo = false ;
167+
168+ i = 0 ;
169+ while (SENSOR_IZDA && SENSOR_CENTRO && SENSOR_DCHA ){
170+ i++;
171+ }
172+ // Si se ha pasado el limite, entonces esta cinta atravesada vale 0
173+ if (i>TIEMPO_PARA_LEER_0 ){
174+ bit = 0 ;
175+ }
176+ }else if (SENSOR_IZDA && !SENSOR_DCHA ){
177+ robot.rotaIzda ();
178+ while (!SENSOR_CENTRO );
179+ }else if (!SENSOR_IZDA && SENSOR_DCHA ){
180+ robot.rotaDcha ();
181+ while (!SENSOR_CENTRO );
182+ }
183+ }
184+ }
185+ robot.para ();
186+ Serial.println (" Bit leido" );
187+ delay (TIEMPO_PENSAR );
188+ return bit;
189+ }
190+
90191
91192byte Cnosos::lee_numero () {
92193 byte x = 0 ;
@@ -99,7 +200,7 @@ byte Cnosos::lee_numero() {
99200 if (siguiente () == 1 ){
100201 x++;
101202 }
102- // Si no encuentra un cruce, como ya se introduce un 0, dejalo como esta
203+ // Si no encuentra un cruce, como ya se introduce un 0 al desplazar el numero , dejalo como esta
103204 }
104205 return x;
105206}
@@ -227,11 +328,12 @@ void Cnosos::sal_izq() {
227328// Necesita modulo Morse inicializado para que suene/luzca
228329// Necesita modulo Bluetooth inicializado para mandar mensajes
229330void Cnosos::luce_numero (byte n) {
331+ robot.BLUETOOTH .envia (" Numero: " );
332+ robot.BLUETOOTH .enviaLinea (String (n));
333+
230334 for (byte i = 0 ; i < n; i++) {
231335 robot.MORSE .raya ();
232336 }
233- robot.BLUETOOTH .envia (" Numero: " );
234- robot.BLUETOOTH .enviaLinea (String (n));
235337}
236338
237339// Necesita modulo Morse inicializado para que suene/luzca
0 commit comments