Created
September 21, 2021 17:08
-
-
Save RQF7/42f2af532bad0b3a6f6a9e90228f277d to your computer and use it in GitHub Desktop.
Logic gates with Cubelets. https://www.youtube.com/watch?v=O2XSK33VjTU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Cabezal de lectura de la máquina. | |
* | |
* Programa para el bloque rotador. Es desde este bloque desde donde se controla | |
* la operación de toda la máquina: sube o baja el sensor de luz para leer | |
* o escribir en la cinta; con su valor de bloque controla la dirección de las | |
* los motores. | |
*/ | |
#include "cubelet.h" | |
/** Definición de identificadores externos. */ | |
#define SENSOR_CABEZAL 69357 | |
/** Realiza una lectura (o escritura) con el cabezal. */ | |
uint8_t movimiento_de_cabezal(); | |
/** Avanza o retrocede los motores. */ | |
void movimiento_de_motores(int direccion, int numero_bloques); | |
void setup() | |
{ | |
block_value = 0; | |
} | |
/** | |
* Acción de acuerdo a la siguiente tabla de verdad: | |
* L1 L2 E1 | |
* 0 0 0 | |
* 0 1 1 | |
* 1 0 1 | |
* 1 1 1 | |
*/ | |
#define COTA_DE_LUZ 230 | |
void loop() | |
{ | |
wait(2000); | |
movimiento_de_motores(1, 1); | |
uint8_t primerOperando = movimiento_de_cabezal() > COTA_DE_LUZ ? 1 : 0; | |
movimiento_de_motores(1, 3); | |
uint8_t segundoOperando = movimiento_de_cabezal() > COTA_DE_LUZ ? 1 : 0; | |
movimiento_de_motores(1, 3); | |
uint8_t resultadoPrevio = movimiento_de_cabezal() > COTA_DE_LUZ ? 1 : 0; | |
uint8_t resultadoReal = 1; | |
if (primerOperando == 1 && segundoOperando == 1) | |
resultadoReal = 0; | |
if (resultadoPrevio != resultadoReal) { | |
movimiento_de_motores(1, 1); | |
movimiento_de_cabezal(); | |
} | |
wait(2000); | |
} | |
/* Movimiento de motores. *****************************************************/ | |
/** Controla el tiempo que los motores se mueven. */ | |
#define TIEMPO_DE_MOVIMIENTO 320 | |
/** | |
* Mueve los motores según la dirección dada. Cada motor se mueve | |
* durante el tiempo necesario para vanzar un bloque de la cinta. | |
* La dirección se controla mediante: | |
* 1 --> adelante. | |
* 2 --> atrás. | |
*/ | |
void movimiento_de_motores( | |
int direccion, /**< Dirección del movimiento. */ | |
int numero_bloques /**< Número de bloques a avanzar o retroceder. */ | |
) | |
{ | |
block_value = direccion; | |
for (int i = 0; i < numero_bloques; i++) | |
wait(TIEMPO_DE_MOVIMIENTO); | |
block_value = 0; | |
wait(200); | |
} | |
/* Movimiento de cabezal. *****************************************************/ | |
/** Controla el tiempo de giro. | |
* Por alguna extraña razón estos dos valores son los que se quedan estables | |
* por más tiempo. */ | |
#define TIEMPO_DE_ROTACION_UNO 370 | |
#define TIEMPO_DE_ROTACION_DOS 375 | |
/** Controla el tiempo de lectura del cabezal. | |
* Un tiempo demasiado corto puede afectar la rotación: su aún no se queda | |
* totalmente quieto cuando se le pide que regrese, entonces la fuerza | |
* necesaria para regresar no será la misma. */ | |
#define TIEMPO_DE_LECTURA 500 | |
/** | |
* Rota el cabezal de la máquina sobre la cinta. | |
* Regresa el valor leído por el sensor de brillo. | |
*/ | |
uint8_t movimiento_de_cabezal() | |
{ | |
/* Rotación hacia adelante. */ | |
set_rotate_direction(FORWARD); | |
set_rotate(128); | |
wait(TIEMPO_DE_ROTACION_UNO); | |
/* Lectura de sensor. */ | |
set_rotate(0); | |
wait(TIEMPO_DE_LECTURA); | |
uint8_t resultado = get_block_value(SENSOR_CABEZAL); | |
/* Rotación hacia atrás. */ | |
set_rotate_direction(BACKWARD); | |
set_rotate(128); | |
wait(TIEMPO_DE_ROTACION_DOS); | |
set_rotate(0); | |
wait(200); | |
return resultado; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Programa de celdas de lectura. | |
* Led que representa el valor de una celda de la cinta: uno prendido, | |
* dos apagado. | |
*/ | |
#include "cubelet.h" | |
#define COTA_DE_CERCANIA 15 | |
#define TIEMPO_DE_LECTURA 500 | |
#define INTERVALO_CONTRA_FALSOS 250 | |
uint8_t valor_de_luz = 0; | |
void setup() | |
{ | |
block_value = 0; | |
} | |
void loop() | |
{ | |
uint8_t lectura_de_sensor = maximum(); | |
if (lectura_de_sensor > COTA_DE_CERCANIA) | |
{ | |
wait(INTERVALO_CONTRA_FALSOS); | |
lectura_de_sensor = maximum(); | |
if (lectura_de_sensor > COTA_DE_CERCANIA) | |
{ | |
valor_de_luz = inverse(valor_de_luz); | |
set_flashlight(valor_de_luz); | |
wait(TIEMPO_DE_LECTURA); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Ruedas de la máquina de Turing. | |
* | |
* Se comportan según el valor de bloque del bloque rotador (controlador | |
* central), según el siguiente diccionario: | |
* 0 --> No moverse. | |
* 1 --> Adelante. | |
* 2 --> Atrás. | |
*/ | |
#include "cubelet.h" | |
/** Identificadores externos. */ | |
#define CABEZAL 85562 | |
/** Define el valor para avance y retroceso; entre más alto el valor, | |
** mayor la rápidez, y mayor la pérdida de precisión. */ | |
#define POTENCIA_DE_MOTOR 128 | |
void setup() | |
{ | |
block_value = 0; | |
} | |
void loop() | |
{ | |
uint8_t instrucciones = get_block_value(CABEZAL); | |
switch (instrucciones) | |
{ | |
case 0: | |
set_drive(0); | |
break; | |
case 1: | |
set_drive_direction(FORWARD); | |
set_drive(POTENCIA_DE_MOTOR); | |
break; | |
case 2: | |
set_drive_direction(BACKWARD); | |
set_drive(POTENCIA_DE_MOTOR); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment