mirror of
https://github.com/adrigongv23/G26---Telemetry-Software.git
synced 2026-08-25 11:33:17 +02:00
Todo subido
This commit is contained in:
parent
f0db3a8aeb
commit
27026caf57
87 changed files with 83292 additions and 17 deletions
63
Tiempo_por_vuelta/acel_slave/acel_slave.ino
Normal file
63
Tiempo_por_vuelta/acel_slave/acel_slave.ino
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
const int sensorPin = 13;
|
||||
bool lastState = HIGH;
|
||||
|
||||
// MAC de la Master
|
||||
uint8_t masterAddress[] = {0x28, 0x05, 0xA5, 0xE1, 0xCF, 0x90};
|
||||
|
||||
typedef struct struct_message {
|
||||
bool handshake;
|
||||
bool trigger;
|
||||
} struct_message;
|
||||
|
||||
struct_message msg;
|
||||
|
||||
void onDataReceive(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len){
|
||||
struct_message incomingMsg;
|
||||
memcpy(&incomingMsg, incomingData, sizeof(incomingMsg));
|
||||
|
||||
if(incomingMsg.handshake){
|
||||
Serial.println("🔄 Handshake recibido del Master, enviando confirmación");
|
||||
msg.handshake = true;
|
||||
esp_now_send(masterAddress, (uint8_t*)&msg, sizeof(msg));
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
pinMode(sensorPin, INPUT_PULLUP);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if(esp_now_init() != ESP_OK){
|
||||
Serial.println("Error al inicializar ESP-NOW");
|
||||
return;
|
||||
}
|
||||
|
||||
// Añadir Master como peer
|
||||
esp_now_peer_info_t peerInfo = {};
|
||||
memcpy(peerInfo.peer_addr, masterAddress, 6);
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
esp_now_add_peer(&peerInfo);
|
||||
|
||||
esp_now_register_recv_cb(onDataReceive);
|
||||
|
||||
Serial.println("Slave listo. Esperando handshake del Master...");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool sensorState = digitalRead(sensorPin);
|
||||
|
||||
// Detectar sensor final
|
||||
if(lastState == HIGH && sensorState == LOW){
|
||||
delay(100); // debounce / margen para paso de coche
|
||||
msg.trigger = true;
|
||||
esp_now_send(masterAddress, (uint8_t*)&msg, sizeof(msg));
|
||||
Serial.println("🏁 Sensor final activado, mensaje enviado al Master");
|
||||
msg.trigger = false; // reset para siguiente vuelta
|
||||
}
|
||||
|
||||
lastState = sensorState;
|
||||
}
|
||||
83
Tiempo_por_vuelta/emisor_pruebas/acceleration.cpp
Normal file
83
Tiempo_por_vuelta/emisor_pruebas/acceleration.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include <Arduino.h>
|
||||
#include <esp_now.h>
|
||||
|
||||
#include "acceleration.hpp"
|
||||
#include "servicios_pruebas.hpp"
|
||||
|
||||
static unsigned long startTime = 0;
|
||||
static bool counting = false;
|
||||
static volatile bool handshakeDone = false;
|
||||
static volatile bool handshakePending = false;
|
||||
static volatile bool triggerPending = false;
|
||||
static unsigned long lastHandshake = 0;
|
||||
|
||||
static void enviar_handshake() {
|
||||
paquete_final_acceleration mensaje = {};
|
||||
mensaje.handshake = true;
|
||||
mensaje.trigger = false;
|
||||
|
||||
esp_err_t resultado = esp_now_send(ESPNOW_MAC_FINAL_ACCELERATION, (uint8_t*)&mensaje, sizeof(mensaje));
|
||||
if (resultado != ESP_OK) Serial.println("Error enviando handshake al sensor final de Acceleration");
|
||||
}
|
||||
|
||||
void reiniciar_acceleration() {
|
||||
startTime = 0;
|
||||
counting = false;
|
||||
handshakeDone = false;
|
||||
handshakePending = false;
|
||||
triggerPending = false;
|
||||
lastHandshake = 0;
|
||||
}
|
||||
|
||||
void recibir_paquete_acceleration(const paquete_final_acceleration& mensaje) {
|
||||
if (mensaje.handshake && !handshakeDone) {
|
||||
handshakeDone = true;
|
||||
handshakePending = true;
|
||||
}
|
||||
|
||||
if (mensaje.trigger) triggerPending = true;
|
||||
}
|
||||
|
||||
void prueba_acceleration() {
|
||||
if (!handshakeDone && millis() - lastHandshake >= 500) {
|
||||
lastHandshake = millis();
|
||||
enviar_handshake();
|
||||
Serial.println("Enviando handshake al sensor final de Acceleration...");
|
||||
}
|
||||
|
||||
if (handshakePending) {
|
||||
handshakePending = false;
|
||||
Serial.println("Sensor final de Acceleration conectado");
|
||||
enviar_datos(1);
|
||||
}
|
||||
|
||||
if (triggerPending) {
|
||||
triggerPending = false;
|
||||
|
||||
if (counting) {
|
||||
unsigned long tiempo = millis() - startTime;
|
||||
counting = false;
|
||||
|
||||
Serial.print("Acceleration terminada: ");
|
||||
Serial.print(tiempo / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
|
||||
enviar_datos(3, 0, tiempo);
|
||||
}
|
||||
}
|
||||
|
||||
if (!detectar_activacion_sensor(SENSOR_ACCELERATION, 1500)) return;
|
||||
|
||||
if (!handshakeDone) {
|
||||
Serial.println("No se inicia: el sensor final de Acceleration no esta conectado");
|
||||
enviar_datos(4);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!counting) {
|
||||
startTime = millis();
|
||||
counting = true;
|
||||
Serial.println("Sensor de salida activado: Acceleration iniciada");
|
||||
enviar_datos(2);
|
||||
}
|
||||
}
|
||||
7
Tiempo_por_vuelta/emisor_pruebas/acceleration.hpp
Normal file
7
Tiempo_por_vuelta/emisor_pruebas/acceleration.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "configuracion.hpp"
|
||||
|
||||
void prueba_acceleration();
|
||||
void reiniciar_acceleration();
|
||||
void recibir_paquete_acceleration(const paquete_final_acceleration& mensaje);
|
||||
58
Tiempo_por_vuelta/emisor_pruebas/autox_endurance.cpp
Normal file
58
Tiempo_por_vuelta/emisor_pruebas/autox_endurance.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "autox_endurance.hpp"
|
||||
#include "configuracion.hpp"
|
||||
#include "servicios_pruebas.hpp"
|
||||
|
||||
static unsigned long startTime = 0;
|
||||
static int activationCount = 0;
|
||||
static int lapCount = 0;
|
||||
static const int WARMUP_TOTAL = 2;
|
||||
|
||||
void reiniciar_autox_endurance() {
|
||||
startTime = 0;
|
||||
activationCount = 0;
|
||||
lapCount = 0;
|
||||
}
|
||||
|
||||
void prueba_autox_endurance() {
|
||||
if (!detectar_activacion_sensor(SENSOR_AUTOX_ENDURANCE, 2000)) return;
|
||||
|
||||
unsigned long now = millis();
|
||||
activationCount++;
|
||||
|
||||
if (activationCount <= WARMUP_TOTAL) {
|
||||
if (activationCount == 1) {
|
||||
Serial.println("Calentamiento: vuelta 1 iniciada");
|
||||
} else {
|
||||
Serial.print("Calentamiento: vuelta ");
|
||||
Serial.print(activationCount - 1);
|
||||
Serial.print(" terminada, vuelta ");
|
||||
Serial.print(activationCount);
|
||||
Serial.println(" iniciada");
|
||||
}
|
||||
|
||||
enviar_datos(1, activationCount);
|
||||
return;
|
||||
}
|
||||
|
||||
if (activationCount == WARMUP_TOTAL + 1) {
|
||||
startTime = now;
|
||||
lapCount = 0;
|
||||
Serial.println("Calentamiento terminado: primera vuelta oficial iniciada");
|
||||
enviar_datos(2, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned long lapTime = now - startTime;
|
||||
lapCount++;
|
||||
startTime = now;
|
||||
|
||||
Serial.print("Vuelta ");
|
||||
Serial.print(lapCount);
|
||||
Serial.print(": ");
|
||||
Serial.print(lapTime / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
|
||||
enviar_datos(3, lapCount, lapTime);
|
||||
}
|
||||
4
Tiempo_por_vuelta/emisor_pruebas/autox_endurance.hpp
Normal file
4
Tiempo_por_vuelta/emisor_pruebas/autox_endurance.hpp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#pragma once
|
||||
|
||||
void prueba_autox_endurance();
|
||||
void reiniciar_autox_endurance();
|
||||
53
Tiempo_por_vuelta/emisor_pruebas/configuracion.hpp
Normal file
53
Tiempo_por_vuelta/emisor_pruebas/configuracion.hpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
/* MAC del receptor del box */
|
||||
static const uint8_t ESPNOW_MAC_RECEPTOR_BOX[6] = {
|
||||
0x28, 0x05, 0xA5, 0xE1, 0xCA, 0x10
|
||||
};
|
||||
|
||||
/* MAC del ESP32 situado al final de Acceleration */
|
||||
static const uint8_t ESPNOW_MAC_FINAL_ACCELERATION[6] = {
|
||||
0x28, 0x05, 0xA5, 0x0B, 0x44, 0xA8
|
||||
};
|
||||
|
||||
|
||||
/* Pines de los sensores */
|
||||
static const int SENSOR_ACCELERATION = 26;
|
||||
static const int SENSOR_SKIDPAD = 26;
|
||||
static const int SENSOR_AUTOX_ENDURANCE = 26;
|
||||
|
||||
enum tipo_prueba : uint8_t {
|
||||
PRUEBA_NINGUNA = 0,
|
||||
PRUEBA_ACCELERATION = 1,
|
||||
PRUEBA_SKIDPAD = 2,
|
||||
PRUEBA_AUTOX_ENDURANCE = 3
|
||||
};
|
||||
|
||||
enum tipo_comando : uint8_t {
|
||||
COMANDO_NINGUNO = 0,
|
||||
COMANDO_SELECCIONAR_PRUEBA = 1,
|
||||
COMANDO_REINICIAR_PRUEBA = 2
|
||||
};
|
||||
|
||||
struct paquete_control {
|
||||
uint8_t comando;
|
||||
uint8_t prueba;
|
||||
};
|
||||
|
||||
struct paquete_datos {
|
||||
uint8_t prueba;
|
||||
uint8_t estado;
|
||||
uint16_t vuelta;
|
||||
unsigned long tiempo;
|
||||
unsigned long laptime_derecha;
|
||||
unsigned long laptime_izquierda;
|
||||
unsigned long average;
|
||||
};
|
||||
|
||||
/* Compatible con el acel_slave original */
|
||||
struct paquete_final_acceleration {
|
||||
bool handshake;
|
||||
bool trigger;
|
||||
};
|
||||
179
Tiempo_por_vuelta/emisor_pruebas/emisor_pruebas.ino
Normal file
179
Tiempo_por_vuelta/emisor_pruebas/emisor_pruebas.ino
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
#include <WiFi.h>
|
||||
#include <esp_now.h>
|
||||
|
||||
#include "configuracion.hpp"
|
||||
#include "servicios_pruebas.hpp"
|
||||
#include "acceleration.hpp"
|
||||
#include "skidpad.hpp"
|
||||
#include "autox_endurance.hpp"
|
||||
|
||||
paquete_datos datos;
|
||||
|
||||
tipo_prueba prueba_activa = PRUEBA_NINGUNA;
|
||||
volatile tipo_prueba prueba_pendiente = PRUEBA_NINGUNA;
|
||||
volatile bool cambio_prueba_pendiente = false;
|
||||
volatile bool reinicio_pendiente = false;
|
||||
|
||||
static bool sensorState = HIGH;
|
||||
static bool lastSensorState = HIGH;
|
||||
static unsigned long lastActivationTime = 0;
|
||||
static const unsigned long DEBOUNCE_DELAY = 50;
|
||||
|
||||
void enviar_datos(uint8_t estado, uint16_t vuelta, unsigned long tiempo, unsigned long derecha, unsigned long izquierda, unsigned long media) {
|
||||
datos.prueba = prueba_activa;
|
||||
datos.estado = estado;
|
||||
datos.vuelta = vuelta;
|
||||
datos.tiempo = tiempo;
|
||||
datos.laptime_derecha = derecha;
|
||||
datos.laptime_izquierda = izquierda;
|
||||
datos.average = media;
|
||||
|
||||
esp_err_t resultado = esp_now_send(ESPNOW_MAC_RECEPTOR_BOX, (uint8_t*)&datos, sizeof(datos));
|
||||
if (resultado != ESP_OK) Serial.println("Error enviando datos al receptor del box");
|
||||
}
|
||||
|
||||
static int pin_sensor_prueba(tipo_prueba prueba) {
|
||||
switch (prueba) {
|
||||
case PRUEBA_ACCELERATION: return SENSOR_ACCELERATION;
|
||||
case PRUEBA_SKIDPAD: return SENSOR_SKIDPAD;
|
||||
case PRUEBA_AUTOX_ENDURANCE: return SENSOR_AUTOX_ENDURANCE;
|
||||
default: return SENSOR_SKIDPAD;
|
||||
}
|
||||
}
|
||||
|
||||
bool detectar_activacion_sensor(int sensorPin, unsigned long minInterval) {
|
||||
sensorState = digitalRead(sensorPin);
|
||||
bool activado = false;
|
||||
|
||||
if (lastSensorState == HIGH && sensorState == LOW) {
|
||||
unsigned long now = millis();
|
||||
|
||||
if (now - lastActivationTime >= minInterval) {
|
||||
lastActivationTime = now;
|
||||
activado = true;
|
||||
}
|
||||
|
||||
delay(DEBOUNCE_DELAY);
|
||||
}
|
||||
|
||||
lastSensorState = sensorState;
|
||||
return activado;
|
||||
}
|
||||
|
||||
static void reiniciar_variables_comunes() {
|
||||
lastActivationTime = 0;
|
||||
sensorState = digitalRead(pin_sensor_prueba(prueba_activa));
|
||||
lastSensorState = sensorState;
|
||||
}
|
||||
|
||||
static void reiniciar_todas_las_pruebas() {
|
||||
reiniciar_acceleration();
|
||||
reiniciar_skidpad();
|
||||
reiniciar_autox_endurance();
|
||||
}
|
||||
|
||||
static void reiniciar_prueba_actual() {
|
||||
switch (prueba_activa) {
|
||||
case PRUEBA_ACCELERATION: reiniciar_acceleration(); break;
|
||||
case PRUEBA_SKIDPAD: reiniciar_skidpad(); break;
|
||||
case PRUEBA_AUTOX_ENDURANCE: reiniciar_autox_endurance(); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
memset(&datos, 0, sizeof(datos));
|
||||
reiniciar_variables_comunes();
|
||||
|
||||
Serial.println("Prueba reiniciada desde el receptor del box");
|
||||
enviar_datos(101);
|
||||
}
|
||||
|
||||
static void seleccionar_prueba(tipo_prueba nueva_prueba) {
|
||||
prueba_activa = nueva_prueba;
|
||||
|
||||
reiniciar_todas_las_pruebas();
|
||||
memset(&datos, 0, sizeof(datos));
|
||||
reiniciar_variables_comunes();
|
||||
|
||||
Serial.println();
|
||||
|
||||
switch (prueba_activa) {
|
||||
case PRUEBA_ACCELERATION: Serial.println("PRUEBA SELECCIONADA: ACCELERATION"); break;
|
||||
case PRUEBA_SKIDPAD: Serial.println("PRUEBA SELECCIONADA: SKIDPAD"); break;
|
||||
case PRUEBA_AUTOX_ENDURANCE: Serial.println("PRUEBA SELECCIONADA: AUTOX / ENDURANCE"); break;
|
||||
default: Serial.println("NINGUNA PRUEBA SELECCIONADA"); break;
|
||||
}
|
||||
|
||||
enviar_datos(100);
|
||||
}
|
||||
|
||||
static void recibir_espnow(const esp_now_recv_info_t *info, const uint8_t *datos_recibidos, int longitud) {
|
||||
if (memcmp(info->src_addr, ESPNOW_MAC_RECEPTOR_BOX, 6) == 0 && longitud == sizeof(paquete_control)) {
|
||||
paquete_control control;
|
||||
memcpy(&control, datos_recibidos, sizeof(control));
|
||||
|
||||
if (control.comando == COMANDO_SELECCIONAR_PRUEBA && control.prueba >= PRUEBA_ACCELERATION && control.prueba <= PRUEBA_AUTOX_ENDURANCE) {
|
||||
prueba_pendiente = (tipo_prueba)control.prueba;
|
||||
cambio_prueba_pendiente = true;
|
||||
} else if (control.comando == COMANDO_REINICIAR_PRUEBA) {
|
||||
reinicio_pendiente = true;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (memcmp(info->src_addr, ESPNOW_MAC_FINAL_ACCELERATION, 6) == 0 && longitud == sizeof(paquete_final_acceleration)) {
|
||||
paquete_final_acceleration mensaje;
|
||||
memcpy(&mensaje, datos_recibidos, sizeof(mensaje));
|
||||
recibir_paquete_acceleration(mensaje);
|
||||
}
|
||||
}
|
||||
|
||||
static bool anadir_peer(const uint8_t *mac) {
|
||||
esp_now_peer_info_t peer = {};
|
||||
memcpy(peer.peer_addr, mac, 6);
|
||||
peer.channel = 0;
|
||||
peer.encrypt = false;
|
||||
return esp_now_add_peer(&peer) == ESP_OK;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(SENSOR_ACCELERATION, INPUT_PULLUP);
|
||||
pinMode(SENSOR_SKIDPAD, INPUT_PULLUP);
|
||||
pinMode(SENSOR_AUTOX_ENDURANCE, INPUT_PULLUP);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error iniciando ESP-NOW");
|
||||
return;
|
||||
}
|
||||
|
||||
esp_now_register_recv_cb(recibir_espnow);
|
||||
|
||||
if (!anadir_peer(ESPNOW_MAC_RECEPTOR_BOX)) Serial.println("Error anadiendo el receptor del box");
|
||||
if (!anadir_peer(ESPNOW_MAC_FINAL_ACCELERATION)) Serial.println("Error anadiendo el sensor final de Acceleration");
|
||||
|
||||
Serial.println("Emisor de pruebas listo");
|
||||
Serial.println("Esperando que el receptor del box seleccione una prueba");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (cambio_prueba_pendiente) {
|
||||
cambio_prueba_pendiente = false;
|
||||
seleccionar_prueba(prueba_pendiente);
|
||||
}
|
||||
|
||||
if (reinicio_pendiente) {
|
||||
reinicio_pendiente = false;
|
||||
reiniciar_prueba_actual();
|
||||
}
|
||||
|
||||
switch (prueba_activa) {
|
||||
case PRUEBA_ACCELERATION: prueba_acceleration(); break;
|
||||
case PRUEBA_SKIDPAD: prueba_skidpad(); break;
|
||||
case PRUEBA_AUTOX_ENDURANCE: prueba_autox_endurance(); break;
|
||||
default: delay(10); break;
|
||||
}
|
||||
}
|
||||
6
Tiempo_por_vuelta/emisor_pruebas/servicios_pruebas.hpp
Normal file
6
Tiempo_por_vuelta/emisor_pruebas/servicios_pruebas.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
void enviar_datos(uint8_t estado, uint16_t vuelta = 0, unsigned long tiempo = 0, unsigned long derecha = 0, unsigned long izquierda = 0, unsigned long media = 0);
|
||||
bool detectar_activacion_sensor(int sensorPin, unsigned long minInterval);
|
||||
77
Tiempo_por_vuelta/emisor_pruebas/skidpad.cpp
Normal file
77
Tiempo_por_vuelta/emisor_pruebas/skidpad.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "configuracion.hpp"
|
||||
#include "servicios_pruebas.hpp"
|
||||
#include "skidpad.hpp"
|
||||
|
||||
static unsigned long startTime = 0;
|
||||
static unsigned long lapTimeRight = 0;
|
||||
static unsigned long lapTimeLeft = 0;
|
||||
static int activationCount = 0;
|
||||
static int cycleCount = 0;
|
||||
|
||||
void reiniciar_skidpad() {
|
||||
startTime = 0;
|
||||
lapTimeRight = 0;
|
||||
lapTimeLeft = 0;
|
||||
activationCount = 0;
|
||||
cycleCount = 0;
|
||||
}
|
||||
|
||||
void prueba_skidpad() {
|
||||
if (!detectar_activacion_sensor(SENSOR_SKIDPAD, 1500)) return;
|
||||
|
||||
unsigned long now = millis();
|
||||
activationCount++;
|
||||
|
||||
switch (activationCount) {
|
||||
case 1:
|
||||
cycleCount++;
|
||||
Serial.print("\n===== Ciclo ");
|
||||
Serial.print(cycleCount);
|
||||
Serial.println(" =====");
|
||||
Serial.println("Primera activacion: nada");
|
||||
enviar_datos(1, cycleCount);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
startTime = now;
|
||||
Serial.println("Segunda activacion: inicia vuelta DERECHA");
|
||||
enviar_datos(2, cycleCount);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
lapTimeRight = now - startTime;
|
||||
Serial.print("Vuelta DERECHA terminada: ");
|
||||
Serial.print(lapTimeRight / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
enviar_datos(3, cycleCount, 0, lapTimeRight);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
startTime = now;
|
||||
Serial.println("Cuarta activacion: inicia vuelta IZQUIERDA");
|
||||
enviar_datos(4, cycleCount, 0, lapTimeRight);
|
||||
break;
|
||||
|
||||
case 5: {
|
||||
lapTimeLeft = now - startTime;
|
||||
unsigned long media = (lapTimeRight + lapTimeLeft) / 2;
|
||||
|
||||
Serial.print("Vuelta IZQUIERDA terminada: ");
|
||||
Serial.print(lapTimeLeft / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
Serial.print("Media de ambas vueltas: ");
|
||||
Serial.print(media / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
|
||||
enviar_datos(5, cycleCount, 0, lapTimeRight, lapTimeLeft, media);
|
||||
activationCount = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
activationCount = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
4
Tiempo_por_vuelta/emisor_pruebas/skidpad.hpp
Normal file
4
Tiempo_por_vuelta/emisor_pruebas/skidpad.hpp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#pragma once
|
||||
|
||||
void prueba_skidpad();
|
||||
void reiniciar_skidpad();
|
||||
33
Tiempo_por_vuelta/receptor_pruebas/acceleration.cpp
Normal file
33
Tiempo_por_vuelta/receptor_pruebas/acceleration.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "acceleration.hpp"
|
||||
|
||||
void reiniciar_receptor_acceleration() {
|
||||
// Acceleration no necesita variables persistentes en el receptor.
|
||||
}
|
||||
|
||||
void procesar_acceleration(const paquete_datos& datos) {
|
||||
switch (datos.estado) {
|
||||
case 1:
|
||||
Serial.println("Sensor final de Acceleration conectado. Sistema listo");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
Serial.println("Acceleration iniciada");
|
||||
break;
|
||||
|
||||
case 3:
|
||||
Serial.print("Acceleration terminada: ");
|
||||
Serial.print(datos.tiempo / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
break;
|
||||
|
||||
case 4:
|
||||
Serial.println("ERROR: el sensor final de Acceleration no esta conectado");
|
||||
break;
|
||||
|
||||
default:
|
||||
Serial.println("Estado de Acceleration desconocido");
|
||||
break;
|
||||
}
|
||||
}
|
||||
6
Tiempo_por_vuelta/receptor_pruebas/acceleration.hpp
Normal file
6
Tiempo_por_vuelta/receptor_pruebas/acceleration.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "configuracion.hpp"
|
||||
|
||||
void procesar_acceleration(const paquete_datos& datos);
|
||||
void reiniciar_receptor_acceleration();
|
||||
61
Tiempo_por_vuelta/receptor_pruebas/autox_endurance.cpp
Normal file
61
Tiempo_por_vuelta/receptor_pruebas/autox_endurance.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "autox_endurance.hpp"
|
||||
|
||||
unsigned long tiempo_acumulado=0;
|
||||
int vueltas_total=0;
|
||||
|
||||
void reiniciar_receptor_autox_endurance() {
|
||||
|
||||
if(tiempo_acumulado!=0 || vueltas_total!=0){
|
||||
|
||||
Serial.print("Tiempo total: ");
|
||||
Serial.print(tiempo_acumulado/1000.0, 3);
|
||||
Serial.println(" s");
|
||||
|
||||
unsigned long media_vueltas=tiempo_acumulado/vueltas_total;
|
||||
|
||||
Serial.print("Media de vueltas: ");
|
||||
Serial.print(media_vueltas/1000.0, 3);
|
||||
Serial.println(" s");
|
||||
|
||||
tiempo_acumulado=0;
|
||||
vueltas_total=0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void procesar_autox_endurance(const paquete_datos& datos) {
|
||||
switch (datos.estado) {
|
||||
case 1:
|
||||
if (datos.vuelta == 1) {
|
||||
Serial.println("Calentamiento: vuelta 1 iniciada");
|
||||
} else {
|
||||
Serial.print("Calentamiento: vuelta ");
|
||||
Serial.print(datos.vuelta - 1);
|
||||
Serial.print(" terminada, vuelta ");
|
||||
Serial.print(datos.vuelta);
|
||||
Serial.println(" iniciada");
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
Serial.println("Calentamiento terminado: primera vuelta oficial iniciada");
|
||||
break;
|
||||
|
||||
case 3:
|
||||
Serial.print("Vuelta ");
|
||||
Serial.print(datos.vuelta);
|
||||
Serial.print(": ");
|
||||
Serial.print(datos.tiempo / 1000.0, 3);
|
||||
tiempo_acumulado+=datos.tiempo;
|
||||
vueltas_total=datos.vuelta;
|
||||
Serial.println(" s");
|
||||
break;
|
||||
|
||||
default:
|
||||
Serial.println("Estado de Autocross / Endurance desconocido");
|
||||
break;
|
||||
}
|
||||
}
|
||||
6
Tiempo_por_vuelta/receptor_pruebas/autox_endurance.hpp
Normal file
6
Tiempo_por_vuelta/receptor_pruebas/autox_endurance.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "configuracion.hpp"
|
||||
|
||||
void procesar_autox_endurance(const paquete_datos& datos);
|
||||
void reiniciar_receptor_autox_endurance();
|
||||
36
Tiempo_por_vuelta/receptor_pruebas/configuracion.hpp
Normal file
36
Tiempo_por_vuelta/receptor_pruebas/configuracion.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
/* MAC del emisor de pruebas */
|
||||
static const uint8_t ESPNOW_MAC_EMISOR_PRUEBAS[6] = {
|
||||
0x28, 0x05, 0xA5, 0xE1, 0xCF, 0x90
|
||||
};
|
||||
|
||||
enum tipo_prueba : uint8_t {
|
||||
PRUEBA_NINGUNA = 0,
|
||||
PRUEBA_ACCELERATION = 1,
|
||||
PRUEBA_SKIDPAD = 2,
|
||||
PRUEBA_AUTOX_ENDURANCE = 3
|
||||
};
|
||||
|
||||
enum tipo_comando : uint8_t {
|
||||
COMANDO_NINGUNO = 0,
|
||||
COMANDO_SELECCIONAR_PRUEBA = 1,
|
||||
COMANDO_REINICIAR_PRUEBA = 2
|
||||
};
|
||||
|
||||
struct paquete_control {
|
||||
uint8_t comando;
|
||||
uint8_t prueba;
|
||||
};
|
||||
|
||||
struct paquete_datos {
|
||||
uint8_t prueba;
|
||||
uint8_t estado;
|
||||
uint16_t vuelta;
|
||||
unsigned long tiempo;
|
||||
unsigned long laptime_derecha;
|
||||
unsigned long laptime_izquierda;
|
||||
unsigned long average;
|
||||
};
|
||||
153
Tiempo_por_vuelta/receptor_pruebas/receptor_pruebas.ino
Normal file
153
Tiempo_por_vuelta/receptor_pruebas/receptor_pruebas.ino
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
#include <WiFi.h>
|
||||
#include <esp_now.h>
|
||||
|
||||
#include "configuracion.hpp"
|
||||
#include "acceleration.hpp"
|
||||
#include "skidpad.hpp"
|
||||
#include "autox_endurance.hpp"
|
||||
|
||||
paquete_datos datos;
|
||||
volatile bool datos_nuevos = false;
|
||||
tipo_prueba prueba_activa = PRUEBA_NINGUNA;
|
||||
|
||||
static void mostrar_menu() {
|
||||
Serial.println();
|
||||
Serial.println("========================================");
|
||||
Serial.println(" SELECCION DE PRUEBA - BOX");
|
||||
Serial.println("========================================");
|
||||
Serial.println("1 -> Acceleration");
|
||||
Serial.println("2 -> Skidpad");
|
||||
Serial.println("3 -> Autocross / Endurance");
|
||||
Serial.println("0 -> Reiniciar prueba actual");
|
||||
Serial.println("m -> Mostrar este menu");
|
||||
Serial.println("========================================");
|
||||
}
|
||||
|
||||
static void enviar_control(uint8_t comando, tipo_prueba prueba) {
|
||||
paquete_control control;
|
||||
control.comando = comando;
|
||||
control.prueba = prueba;
|
||||
|
||||
esp_err_t resultado = esp_now_send(ESPNOW_MAC_EMISOR_PRUEBAS, (uint8_t*)&control, sizeof(control));
|
||||
if (resultado != ESP_OK) Serial.println("Error enviando el comando al emisor");
|
||||
}
|
||||
|
||||
static void reiniciar_procesadores() {
|
||||
reiniciar_receptor_acceleration();
|
||||
reiniciar_receptor_skidpad();
|
||||
|
||||
if(prueba_activa==PRUEBA_AUTOX_ENDURANCE) reiniciar_receptor_autox_endurance();
|
||||
|
||||
}
|
||||
|
||||
static void seleccionar_prueba(tipo_prueba nueva_prueba) {
|
||||
prueba_activa = nueva_prueba;
|
||||
|
||||
memset(&datos, 0, sizeof(datos));
|
||||
datos_nuevos = false;
|
||||
reiniciar_procesadores();
|
||||
|
||||
Serial.println();
|
||||
|
||||
switch (prueba_activa) {
|
||||
case PRUEBA_ACCELERATION: Serial.println("Seleccionando ACCELERATION..."); break;
|
||||
case PRUEBA_SKIDPAD: Serial.println("Seleccionando SKIDPAD..."); break;
|
||||
case PRUEBA_AUTOX_ENDURANCE: Serial.println("Seleccionando AUTOX / ENDURANCE..."); break;
|
||||
default: return;
|
||||
}
|
||||
|
||||
enviar_control(COMANDO_SELECCIONAR_PRUEBA, prueba_activa);
|
||||
}
|
||||
|
||||
static void reiniciar_prueba() {
|
||||
if (prueba_activa == PRUEBA_NINGUNA) {
|
||||
Serial.println("Primero selecciona una prueba");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&datos, 0, sizeof(datos));
|
||||
datos_nuevos = false;
|
||||
reiniciar_procesadores();
|
||||
|
||||
enviar_control(COMANDO_REINICIAR_PRUEBA, prueba_activa);
|
||||
Serial.println("Orden de reinicio enviada al emisor");
|
||||
}
|
||||
|
||||
static void procesar_datos() {
|
||||
if (datos.estado == 100) {
|
||||
Serial.println("Prueba confirmada por el emisor");
|
||||
return;
|
||||
}
|
||||
|
||||
if (datos.estado == 101) {
|
||||
Serial.println("El emisor ha reiniciado la prueba");
|
||||
return;
|
||||
}
|
||||
|
||||
switch ((tipo_prueba)datos.prueba) {
|
||||
case PRUEBA_ACCELERATION: procesar_acceleration(datos); break;
|
||||
case PRUEBA_SKIDPAD: procesar_skidpad(datos); break;
|
||||
case PRUEBA_AUTOX_ENDURANCE: procesar_autox_endurance(datos); break;
|
||||
default: Serial.println("Paquete recibido sin una prueba valida"); break;
|
||||
}
|
||||
}
|
||||
|
||||
static void recibir_datos(const esp_now_recv_info_t *info, const uint8_t *datos_emitidos, int longitud) {
|
||||
if (memcmp(info->src_addr, ESPNOW_MAC_EMISOR_PRUEBAS, 6) != 0) return;
|
||||
if (longitud != sizeof(paquete_datos)) return;
|
||||
|
||||
memcpy(&datos, datos_emitidos, sizeof(datos));
|
||||
datos_nuevos = true;
|
||||
}
|
||||
|
||||
static void leer_monitor_serie() {
|
||||
while (Serial.available() > 0) {
|
||||
char comando = Serial.read();
|
||||
|
||||
switch (comando) {
|
||||
case '1': seleccionar_prueba(PRUEBA_ACCELERATION); break;
|
||||
case '2': seleccionar_prueba(PRUEBA_SKIDPAD); break;
|
||||
case '3': seleccionar_prueba(PRUEBA_AUTOX_ENDURANCE); break;
|
||||
case '0': reiniciar_prueba(); break;
|
||||
case 'm':
|
||||
case 'M': mostrar_menu(); break;
|
||||
case '\n':
|
||||
case '\r': break;
|
||||
default: Serial.println("Comando no valido. Escribe m para ver el menu"); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("Error iniciando ESP-NOW");
|
||||
return;
|
||||
}
|
||||
|
||||
esp_now_peer_info_t emisor = {};
|
||||
memcpy(emisor.peer_addr, ESPNOW_MAC_EMISOR_PRUEBAS, 6);
|
||||
emisor.channel = 0;
|
||||
emisor.encrypt = false;
|
||||
|
||||
esp_now_register_recv_cb(recibir_datos);
|
||||
|
||||
if (esp_now_add_peer(&emisor) != ESP_OK) {
|
||||
Serial.println("Error anadiendo el emisor de pruebas");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Receptor de pruebas del box listo");
|
||||
mostrar_menu();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
leer_monitor_serie();
|
||||
|
||||
if (datos_nuevos) {
|
||||
datos_nuevos = false;
|
||||
procesar_datos();
|
||||
}
|
||||
}
|
||||
48
Tiempo_por_vuelta/receptor_pruebas/skidpad.cpp
Normal file
48
Tiempo_por_vuelta/receptor_pruebas/skidpad.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "skidpad.hpp"
|
||||
|
||||
static int ciclo = 0;
|
||||
|
||||
void reiniciar_receptor_skidpad() {
|
||||
ciclo = 0;
|
||||
}
|
||||
|
||||
void procesar_skidpad(const paquete_datos& datos) {
|
||||
switch (datos.estado) {
|
||||
case 1:
|
||||
ciclo = datos.vuelta;
|
||||
Serial.print("\n===== Ciclo ");
|
||||
Serial.print(ciclo);
|
||||
Serial.println(" =====");
|
||||
Serial.println("Primera activacion: nada");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
Serial.println("Segunda activacion: inicia vuelta DERECHA");
|
||||
break;
|
||||
|
||||
case 3:
|
||||
Serial.print("Vuelta DERECHA terminada: ");
|
||||
Serial.print(datos.laptime_derecha / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
break;
|
||||
|
||||
case 4:
|
||||
Serial.println("Cuarta activacion: inicia vuelta IZQUIERDA");
|
||||
break;
|
||||
|
||||
case 5:
|
||||
Serial.print("Vuelta IZQUIERDA terminada: ");
|
||||
Serial.print(datos.laptime_izquierda / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
Serial.print("Media de ambas vueltas: ");
|
||||
Serial.print(datos.average / 1000.0, 3);
|
||||
Serial.println(" s");
|
||||
break;
|
||||
|
||||
default:
|
||||
Serial.println("Estado de Skidpad desconocido");
|
||||
break;
|
||||
}
|
||||
}
|
||||
6
Tiempo_por_vuelta/receptor_pruebas/skidpad.hpp
Normal file
6
Tiempo_por_vuelta/receptor_pruebas/skidpad.hpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "configuracion.hpp"
|
||||
|
||||
void procesar_skidpad(const paquete_datos& datos);
|
||||
void reiniciar_receptor_skidpad();
|
||||
Loading…
Add table
Add a link
Reference in a new issue