feat(ffb): integrer le moteur de vibration haptique double-couche et telemetrie terrain

- Moteur de vibration haptique cadencé à 125 Hz sur le Cœur 1 (Slot 0x11 FFB Logitech)
- Superposition haptique validée : grondement / secousses 12 Hz et texture de vitesse 60 Hz
- Simulation d'à-coup mécanique aux palettes de vitesse et vibration ABS au freinage fort
- Réception de la télémétrie CRSF sur GP1 (Serial1 RX) pour moduler les vibrations selon la rugosité du terrain mesurée par la FC
- Ajout de la commande série T=val (0..100%) et sauvegarde en Flash EEPROM
- Ajout du script de calibration et simulateur interactif test_vibration.py
This commit is contained in:
bleackstorm committed 2026-11-12 09:06:03 +01:00
1 parent 6373ed4371
commit aabd72a41f
2 files changed
+521 -7

No files matched your search

+179 -7
View File
@@ -58,6 +58,8 @@ const uint16_t GAZ_AVANT_MAX_PAR_VITESSE[3] = {
#define FORCE_RESSORT_DEFAUT 140 // Force du retour au centre : 0 à 255
#define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 à 7
#define ETAPES_INIT_FFB_TOTAL 2 // Nombre d'étapes d'init FFB (0=Range, 1=Spring)
#define VIBRATION_DEFAUT 100 // Intensité par défaut des vibrations (0 à 100%)
#define MAGIC_NUMBER_CONFIG 0x48 // Identifiant de validité en EEPROM
Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800);
Adafruit_USBH_Host USBHost;
@@ -77,7 +79,8 @@ struct ConfigG29 {
uint8_t forceRessort; // Force du retour au centre : 0 à 255
uint16_t angleSouhaiteTotal; // Plage totale en degrés : 40 à 900
float expoVolant; // Expo volant : 0.0f à 0.8f
uint8_t magicNumber; // Identifiant de validité en EEPROM (0x47)
uint8_t intensiteVibration; // Intensité globale des vibrations : 0 à 100%
uint8_t magicNumber; // Identifiant de validité en EEPROM (0x48)
};
volatile ConfigG29 configSysteme;
@@ -133,6 +136,14 @@ volatile uint32_t momentDernierChangementDurete_ms = 0;
volatile bool logDureteRequis = false;
volatile bool demandeSauvegardeEEPROM = false;
// Variables pour les vibrations haptiques et la télémétrie
volatile uint8_t rugositeTerrainExterne = 0; // 0 à 255 (pilotable via CRSF télémétrie / FC)
volatile float dernierAccelPct = 0.0f; // 0.0 à 100.0%
volatile float dernierFreinPct = 0.0f; // 0.0 à 100.0%
volatile uint32_t momentDernierChocRapport_ms = 0; // Horodatage du passage de vitesse
volatile int8_t directionDernierChocRapport = 0; // -1 gauche, +1 droite
volatile bool forceEstAuNeutre = false;
char serialBuffer[32];
uint8_t serialBufferIdx = 0;
@@ -152,7 +163,8 @@ void reinitialiserConfigurationDefaut() {
configSysteme.forceRessort = FORCE_RESSORT_DEFAUT;
configSysteme.angleSouhaiteTotal = ANGLE_DEFAUT_TOTAL;
configSysteme.expoVolant = EXPO_DEFAUT;
configSysteme.magicNumber = 0x47;
configSysteme.intensiteVibration = VIBRATION_DEFAUT;
configSysteme.magicNumber = MAGIC_NUMBER_CONFIG;
recalculerParametresVolant();
}
@@ -187,7 +199,8 @@ void afficherConfiguration() {
configSysteme.forceRessort, (configSysteme.forceRessort * 100) / 255);
Serial.printf("Plage Volant(P): %d degres [P=40..900]\n", configSysteme.angleSouhaiteTotal);
Serial.printf("Expo Volant(X) : %.2f [X=0.00..0.80]\n", configSysteme.expoVolant);
Serial.println("Commandes : V=ch, A=ch, F=ch, E=ch, D=val, P=val, X=val, S(save), R(reset), ?");
Serial.printf("Vibrations (T) : %d%% [T=0..100] (Mix 12Hz/60Hz + Telemetrie)\n", configSysteme.intensiteVibration);
Serial.println("Commandes : V=ch, A=ch, F=ch, E=ch, D=val, P=val, X=val, T=val, S(save), R(reset), ?");
}
void chargerConfiguration() {
@@ -217,10 +230,12 @@ void chargerConfiguration() {
bool angleValide = (configSysteme.angleSouhaiteTotal >= 40) &&
(configSysteme.angleSouhaiteTotal <= 900);
if (configSysteme.magicNumber != 0x47 || conflitArm || !canauxValides || !canauxDistincts || !expoValide || !angleValide) {
bool vibValide = (configSysteme.intensiteVibration <= 100);
if (configSysteme.magicNumber != MAGIC_NUMBER_CONFIG || conflitArm || !canauxValides || !canauxDistincts || !expoValide || !angleValide || !vibValide) {
if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!");
else if (!canauxValides || !canauxDistincts) Serial.println("!! Conflit ou canal invalide en EEPROM : configuration reinitialisee !!");
else if (!expoValide || !angleValide) Serial.println("!! Valeur d'angle ou d'expo invalide en EEPROM : configuration reinitialisee !!");
else if (!expoValide || !angleValide || !vibValide) Serial.println("!! Valeur invalide en EEPROM : configuration reinitialisee !!");
else Serial.println(">> Initialisation configuration d'usine par defaut...");
reinitialiserConfigurationDefaut();
sauvegarderConfiguration();
@@ -329,6 +344,17 @@ void traiterLigneSerie(char* commande) {
} else {
Serial.println("!! Expo volant (X) doit etre entre 0.0 et 0.8 !!");
}
} else if (periph == 'T' || periph == 't') {
int val = atoi(&commande[2]);
if (val >= 0 && val <= 100) {
rp2040.idleOtherCore();
configSysteme.intensiteVibration = (uint8_t)val;
rp2040.resumeOtherCore();
sauvegarderConfiguration();
afficherConfiguration();
} else {
Serial.println("!! Vibrations (T) doit etre entre 0 et 100% !!");
}
}
}
}
@@ -474,9 +500,81 @@ bool evaluerFailsafe() {
return stale;
}
// -------------------------------------------------------------------------
// RÉCEPTION TÉLÉMÉTRIE CRSF SUR GP1 (Serial1 RX)
// -------------------------------------------------------------------------
void traiterTelemetrieCRSF() {
static uint8_t crsfRxBuffer[64];
static uint8_t crsfRxIdx = 0;
static uint8_t paquetLongueurAttendue = 0;
static int16_t prevPitch = 0;
static int16_t prevRoll = 0;
static uint32_t dernierPaquetTelemetrie_ms = 0;
while (Serial1.available() > 0) {
uint8_t b = Serial1.read();
if (crsfRxIdx == 0) {
// Les trames CRSF débutent par une adresse connue (0xC8, 0xEE, 0xEA, etc.)
if (b == 0xC8 || b == 0xEE || b == 0xEA) {
crsfRxBuffer[0] = b;
crsfRxIdx = 1;
paquetLongueurAttendue = 0;
}
} else if (crsfRxIdx == 1) {
paquetLongueurAttendue = b;
if (paquetLongueurAttendue < 2 || paquetLongueurAttendue > 62) {
crsfRxIdx = 0; // Trame invalide, rejet
} else {
crsfRxBuffer[1] = b;
crsfRxIdx = 2;
}
} else {
crsfRxBuffer[crsfRxIdx++] = b;
// Trame complète reçue : taille totale = 2 octets d'en-tête + longueur indiquée
if (crsfRxIdx >= (paquetLongueurAttendue + 2)) {
dernierPaquetTelemetrie_ms = millis();
uint8_t frameType = crsfRxBuffer[2];
// Trame d'attitude (0x1E) : Pitch (int16), Roll (int16), Yaw (int16)
if (frameType == 0x1E && paquetLongueurAttendue >= 8) {
int16_t pitch = (int16_t)((crsfRxBuffer[3] << 8) | crsfRxBuffer[4]);
int16_t roll = (int16_t)((crsfRxBuffer[5] << 8) | crsfRxBuffer[6]);
int16_t deltaPitch = abs(pitch - prevPitch);
int16_t deltaRoll = abs(roll - prevRoll);
prevPitch = pitch;
prevRoll = roll;
uint16_t secousses = deltaPitch + deltaRoll;
rugositeTerrainExterne = (uint8_t)constrain(map(secousses, 15, 250, 0, 255), 0, 255);
}
// Trame personnalisée (0x7F ou 0x2A pour accéléromètre brut / rugosité)
else if ((frameType == 0x7F || frameType == 0x2A) && paquetLongueurAttendue >= 3) {
rugositeTerrainExterne = crsfRxBuffer[3];
}
crsfRxIdx = 0;
}
if (crsfRxIdx >= sizeof(crsfRxBuffer)) {
crsfRxIdx = 0;
}
}
}
// Décroissance douce de la rugosité si la télémétrie ne transmet plus depuis > 200 ms
if (rugositeTerrainExterne > 0 && (millis() - dernierPaquetTelemetrie_ms > 200)) {
static uint32_t dernierDecul = 0;
if (millis() - dernierDecul >= 50) {
dernierDecul = millis();
rugositeTerrainExterne = (rugositeTerrainExterne > 10) ? (rugositeTerrainExterne - 10) : 0;
}
}
}
void loop() {
rp2040.wdt_reset();
verifierCommandesSerie();
traiterTelemetrieCRSF();
static int8_t dernierRapportLog = 0xFF;
if (majLedRequise) {
@@ -542,8 +640,9 @@ void loop() {
uint32_t compteOK = compteurRapportsOK;
uint32_t rapportsParSec = ((compteOK - dernierCompteOK) * 1000UL) / (dt ? dt : 1);
dernierCompteOK = compteOK;
Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/%d | Durete=%d/255 | Plage=%d deg | Rapport=%d | Arme=%d | Statut_LED=%d | USB=%lu rap/s | Rejetes=%lu | Failsafe=%d\n",
volant_est_pret, g29_pid, etapeInitMoteur, ETAPES_INIT_FFB_TOTAL, configSysteme.forceRessort, configSysteme.angleSouhaiteTotal,
Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/%d | Durete=%d/255 | Vib=%d%% | Rugosite=%d | Plage=%d deg | Rapport=%d | Arme=%d | Statut_LED=%d | USB=%lu rap/s | Rejetes=%lu | Failsafe=%d\n",
volant_est_pret, g29_pid, etapeInitMoteur, ETAPES_INIT_FFB_TOTAL, configSysteme.forceRessort,
configSysteme.intensiteVibration, rugositeTerrainExterne, configSysteme.angleSouhaiteTotal,
rapportEngage, estArme, statutEnvoiLed,
(unsigned long)rapportsParSec, (unsigned long)compteurRapportsRejetes, failsafeActif);
}
@@ -615,6 +714,60 @@ void envoyerLedsG29(uint8_t masque5bits) {
}
}
void gererVibrationHaptique() {
if (!volant_est_pret || etapeInitMoteur < ETAPES_INIT_FFB_TOTAL) return;
if (etapeEnvoiLed != 0 || majRangeRequise || majRessortRequise) return;
if (!tuh_hid_send_ready(g29_dev_addr, g29_instance)) return;
uint8_t intensite = configSysteme.intensiteVibration;
if (intensite == 0 || !estArme) {
if (!forceEstAuNeutre) {
const uint8_t cmdNeutre[7] = {0x11, 0x00, 128, 0x00, 0x00, 0x00, 0x00};
if (envoyerTrameG29_0x30(cmdNeutre)) {
forceEstAuNeutre = true;
}
}
return;
}
forceEstAuNeutre = false;
float gainGlobal = (float)intensite / 100.0f;
float t = (float)millis() * 0.001f;
float forceCalc = 128.0f;
uint32_t now = millis();
uint32_t dtChoc = now - momentDernierChocRapport_ms;
// Cas A : À-coup sec au passage de rapport (durée 70 ms)
if (dtChoc < 70) {
forceCalc = 128.0f + (float)directionDernierChocRapport * 85.0f * gainGlobal;
}
// Cas B : Freinage violent (> 70%) -> vibration ABS à 45 Hz
else if (dernierFreinPct > 70.0f) {
float absAmp = (dernierFreinPct - 70.0f) * 2.5f * gainGlobal;
forceCalc = 128.0f + absAmp * sinf(2.0f * 3.14159265f * 45.0f * t);
}
// Cas C : Double-couche haptique (12 Hz grondement/terrain + 60 Hz roulement/vitesse)
else {
// 12 Hz : Grondement moteur au ralenti / régime + secousses terrain
float freq12 = 12.0f + (dernierAccelPct * 0.15f);
float ampMoteur12 = (14.0f + dernierAccelPct * 0.40f) * gainGlobal;
float ampTerrain12 = ((float)rugositeTerrainExterne * 0.45f) * gainGlobal;
float v12 = (ampMoteur12 + ampTerrain12) * sinf(2.0f * 3.14159265f * freq12 * t);
// 60 Hz : Grain de roulement lié à la vitesse (rapport engagé) et aux gaz
float ampVitesse60 = (dernierAccelPct * 0.30f) * (1.0f + (float)rapportEngage * 0.25f) * gainGlobal;
float ampTerrain60 = ((float)rugositeTerrainExterne * 0.20f) * gainGlobal;
float v60 = (ampVitesse60 + ampTerrain60) * sinf(2.0f * 3.14159265f * 60.0f * t);
forceCalc = 128.0f + v12 + v60;
}
uint8_t forceByte = (uint8_t)constrain((int)forceCalc, 0, 255);
const uint8_t cmdForce[7] = {0x11, 0x00, forceByte, 0x00, 0x00, 0x00, 0x00};
envoyerTrameG29_0x30(cmdForce);
}
void setup1() {
sleep_ms(100);
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
@@ -674,6 +827,13 @@ void loop1() {
}
envoyerLedsG29(masqueCible);
}
// Moteur de vibration haptique (Mix 12 Hz + 60 Hz cadencé à ~125 Hz, toutes les 8 ms)
static uint32_t dernierTickVib = 0;
if (volant_est_pret && etapeInitMoteur >= ETAPES_INIT_FFB_TOTAL && (millis() - dernierTickVib >= 8)) {
dernierTickVib = millis();
gererVibrationHaptique();
}
}
// =========================================================================
@@ -711,6 +871,8 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
rapportEngage = 0;
demandeRetourNeutre = false;
estArme = false;
forceEstAuNeutre = false;
rugositeTerrainExterne = 0;
boutonArmDejaEnfonce = true; // Ignorer R2 s'il est déjà enfoncé au branchement
palettePlusDejaEnfoncee = true; // Exiger relâchement des palettes et molette au boot
paletteMoinsDejaEnfoncee= true;
@@ -736,6 +898,8 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
etapeInitMoteur = 0;
rapportEngage = 0;
estArme = false;
forceEstAuNeutre = false;
rugositeTerrainExterne = 0;
reinitialiserCanauxCRSF();
majLedRequise = true;
}
@@ -774,6 +938,10 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i
uint16_t freinRaw = 65535 - ((report[48] << 8) | report[47]);
uint16_t embrayageRaw = 65535 - ((report[50] << 8) | report[49]);
// Pourcentages des pédales pour la synthèse haptique (0.0 à 100.0%)
dernierAccelPct = (accelRaw > DEADZONE_PEDALE) ? (((float)(accelRaw - DEADZONE_PEDALE) / (float)(65535 - DEADZONE_PEDALE)) * 100.0f) : 0.0f;
dernierFreinPct = (freinRaw > DEADZONE_PEDALE) ? (((float)(freinRaw - DEADZONE_PEDALE) / (float)(65535 - DEADZONE_PEDALE)) * 100.0f) : 0.0f;
uint8_t boutonsEpaule = report[6];
bool pPlusActuel = (boutonsEpaule & PALETTE_PLUS_MASK);
bool pMoinsActuel = (boutonsEpaule & PALETTE_MOINS_MASK);
@@ -801,6 +969,8 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i
if (rapportEngage < 3 && !(rapportEngage < 0 && accelRaw > DEADZONE_PEDALE)) {
rapportEngage++;
majLedRequise = true;
momentDernierChocRapport_ms = millis();
directionDernierChocRapport = 1; // À-coup sec vers la droite
}
palettePlusDejaEnfoncee = true;
} else if (!pPlusActuel && palettePlusDejaEnfoncee) {
@@ -811,6 +981,8 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i
if (rapportEngage > -1 && !(rapportEngage <= 0 && accelRaw > DEADZONE_PEDALE)) {
rapportEngage--;
majLedRequise = true;
momentDernierChocRapport_ms = millis();
directionDernierChocRapport = -1; // À-coup sec vers la gauche
}
paletteMoinsDejaEnfoncee = true;
} else if (!pMoinsActuel && paletteMoinsDejaEnfoncee) {