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
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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue