Merge pull request #5 from bleackstorm/feat/g29-dial-ffb-eeprom

feat(g29): regler la durete FFB a la molette et etendre l'EEPROM
This commit is contained in:
bleackstorm authored and GitHub committed 2026-11-11 22:35:31 +01:00
commit db2571ec6c
1 file changed
+243 -66
+243 -66
View File
@@ -18,11 +18,18 @@
#define G29_PID_PS4 0xC260 // G29 en mode PS4 (le mode PS3 = 0xC24F a un autre format de rapport)
#define G29_REPORT_ID 0x01
// Masques des palettes dans report[6]
// Masques des palettes et boutons dans report[6]
#define PALETTE_MOINS_MASK 0x01 // L1 (Palette gauche : Rétrograder)
#define PALETTE_PLUS_MASK 0x02 // R1 (Palette droite : Monter un rapport)
#define BOUTON_ARM_MASK 0x08 // R2 (Bascule Armé / Désarmé)
// Masques additionnels dans report[7] (Molette et boutons + / - / Enter)
#define BOUTON_PLUS_MASK 0x04 // Bouton [+] (Dureté +)
#define BOUTON_MOINS_MASK 0x08 // Bouton [-] (Dureté -)
#define DIAL_CW_MASK 0x10 // Molette sens horaire (Dureté +)
#define DIAL_CCW_MASK 0x20 // Molette sens anti-horaire (Dureté -)
#define DIAL_ENTER_MASK 0x40 // Bouton central de la molette (Sauvegarde Flash)
// Armement ELRS : AUX1 = canal 5 (réservé, ne peut pas être mappé ailleurs)
#define CANAL_ARM 5
@@ -43,17 +50,14 @@ const uint16_t GAZ_AVANT_MAX_PAR_VITESSE[3] = {
#define GAZ_ARRIERE_MAX 850
#define DEADZONE_PEDALE 800
// Configuration de la plage de braquage et du retour de force (Auto-Center) du G29
// Configuration par défaut du volant G29
#define CENTRE_VOLANT_BRUT 32528
#define ANGLE_MAX_VOLANT_G29 900.0f
#define ANGLE_SOUHAITE_TOTAL 180.0f
#define EXPO_VOLANT 0.25f // 0.0f = Linéaire, 0.25f = Doux au centre (FPU RP2350)
#define FORCE_RESSORT_G29 140 // Force du retour au centre : 0 à 255
#define ANGLE_DEFAUT_TOTAL 180.0f
#define EXPO_DEFAUT 0.25f // 0.0f = Linéaire, 0.25f = Doux au centre (FPU RP2350)
#define FORCE_RESSORT_DEFAUT 140 // Force du retour au centre : 0 à 255
#define PENTE_RESSORT_G29 7 // Raideur autour du centre : 1 à 7
const int32_t MAX_DELTA_VOLANT = (int32_t)((ANGLE_SOUHAITE_TOTAL / 2.0f) * (65535.0f / ANGLE_MAX_VOLANT_G29));
Adafruit_NeoPixel rgb_led(NUM_PIXELS, PIN_LED_RGB, NEO_GRB + NEO_KHZ800);
Adafruit_USBH_Host USBHost;
@@ -64,15 +68,23 @@ volatile uint16_t crsf_channels[16] = {
CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID, CRSF_VAL_MID
};
struct MapCanaux {
uint8_t volant;
uint8_t accel;
uint8_t frein;
uint8_t embrayage;
uint8_t magicNumber;
struct ConfigG29 {
uint8_t volant;
uint8_t accel;
uint8_t frein;
uint8_t embrayage;
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)
};
MapCanaux configCanaux;
ConfigG29 configSysteme;
volatile int32_t maxDeltaVolant = 0;
void recalculerParametresVolant() {
maxDeltaVolant = (int32_t)(((float)configSysteme.angleSouhaiteTotal / 2.0f) * (65535.0f / ANGLE_MAX_VOLANT_G29));
}
// Boîte séquentielle : -1 = R, 0 = N (Point Mort), 1 = 1ère, 2 = 2ème, 3 = 3ème
volatile int8_t rapportEngage = 0;
@@ -110,6 +122,16 @@ volatile uint8_t dernierMasqueLedG29 = 0xFF;
volatile uint8_t etapeEnvoiLed = 0;
volatile uint8_t etapeInitMoteur = 0;
// Variables pour le réglage FFB à la volée (Molette / Boutons +/- et Série)
volatile bool majRessortRequise = false;
volatile bool majRangeRequise = false;
volatile bool dialPlusDejaEnfonce = false;
volatile bool dialMoinsDejaEnfonce = false;
volatile bool dialEnterDejaEnfonce = false;
volatile uint32_t momentDernierChangementDurete_ms = 0;
volatile bool logDureteRequis = false;
volatile bool demandeSauvegardeEEPROM = false;
char serialBuffer[32];
uint8_t serialBufferIdx = 0;
@@ -120,35 +142,52 @@ void reinitialiserCanauxCRSF() {
crsf_channels[CANAL_ARM - 1] = CRSF_VAL_MIN; // Toujours désarmé par défaut
}
void reinitialiserConfigurationDefaut() {
configSysteme.volant = 1;
configSysteme.accel = 2;
configSysteme.frein = 3;
configSysteme.embrayage = 4;
configSysteme.forceRessort = FORCE_RESSORT_DEFAUT;
configSysteme.angleSouhaiteTotal = ANGLE_DEFAUT_TOTAL;
configSysteme.expoVolant = EXPO_DEFAUT;
configSysteme.magicNumber = 0x47;
recalculerParametresVolant();
}
void sauvegarderConfiguration() {
rp2040.idleOtherCore();
EEPROM.put(0, configCanaux);
EEPROM.put(0, configSysteme);
EEPROM.commit();
rp2040.resumeOtherCore();
Serial.println(">> Configuration sauvegardée en mémoire Flash !");
}
void afficherConfiguration() {
Serial.println("--- Mapping Canaux (Waveshare RP2350-USB-A) ---");
Serial.printf("Volant (V) : Canal %d\n", configCanaux.volant);
Serial.printf("Accel (A) : Canal %d\n", configCanaux.accel);
Serial.printf("Frein (F) : Canal %d\n", configCanaux.frein);
Serial.printf("Embrayage (E) : Canal %d\n", configCanaux.embrayage);
Serial.printf("ARM (R2) : Canal %d (reserve)\n", CANAL_ARM);
Serial.println("\n--- Configuration G29-CRSF (Waveshare RP2350-USB-A) ---");
Serial.printf("Volant (V) : Canal %d\n", configSysteme.volant);
Serial.printf("Accel (A) : Canal %d\n", configSysteme.accel);
Serial.printf("Frein (F) : Canal %d\n", configSysteme.frein);
Serial.printf("Embrayage (E) : Canal %d\n", configSysteme.embrayage);
Serial.printf("ARM (R2) : Canal %d (reserve)\n", CANAL_ARM);
Serial.printf("Durete FFB (D) : %d / 255 (%d%%) [Molette / Boutons +/- ou D=xxx]\n",
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), ?");
}
void chargerConfiguration() {
EEPROM.get(0, configCanaux);
bool conflitArm = configCanaux.volant == CANAL_ARM || configCanaux.accel == CANAL_ARM ||
configCanaux.frein == CANAL_ARM || configCanaux.embrayage == CANAL_ARM;
if (configCanaux.magicNumber != 42 || conflitArm) {
EEPROM.get(0, configSysteme);
bool conflitArm = configSysteme.volant == CANAL_ARM || configSysteme.accel == CANAL_ARM ||
configSysteme.frein == CANAL_ARM || configSysteme.embrayage == CANAL_ARM;
if (configSysteme.magicNumber != 0x47 || conflitArm ||
configSysteme.angleSouhaiteTotal < 40 || configSysteme.angleSouhaiteTotal > 900) {
if (conflitArm) Serial.println("!! Un axe etait mappe sur le canal ARM : configuration reinitialisee !!");
configCanaux.volant = 1;
configCanaux.accel = 2;
configCanaux.frein = 3;
configCanaux.embrayage = 4;
configCanaux.magicNumber = 42;
else Serial.println(">> Initialisation configuration d'usine par defaut...");
reinitialiserConfigurationDefaut();
sauvegarderConfiguration();
} else {
recalculerParametresVolant();
}
}
@@ -160,27 +199,79 @@ void traiterLigneSerie(char* commande) {
afficherConfiguration();
return;
}
if (commande[0] == 'S' || commande[0] == 's') {
sauvegarderConfiguration();
return;
}
if (commande[0] == 'R' || commande[0] == 'r') {
rp2040.idleOtherCore();
reinitialiserConfigurationDefaut();
reinitialiserCanauxCRSF();
rp2040.resumeOtherCore();
majRangeRequise = true;
majRessortRequise = true;
sauvegarderConfiguration();
afficherConfiguration();
return;
}
if (strlen(commande) >= 3 && commande[1] == '=') {
char periph = commande[0];
int nouveauCanal = atoi(&commande[2]);
if (nouveauCanal == CANAL_ARM) {
Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM);
return;
}
if (nouveauCanal >= 1 && nouveauCanal <= 16) {
bool modifOk = true;
switch (periph) {
case 'V': case 'v': configCanaux.volant = nouveauCanal; break;
case 'A': case 'a': configCanaux.accel = nouveauCanal; break;
case 'F': case 'f': configCanaux.frein = nouveauCanal; break;
case 'E': case 'e': configCanaux.embrayage = nouveauCanal; break;
default: modifOk = false; break;
if (periph == 'V' || periph == 'v' || periph == 'A' || periph == 'a' ||
periph == 'F' || periph == 'f' || periph == 'E' || periph == 'e') {
int nouveauCanal = atoi(&commande[2]);
if (nouveauCanal == CANAL_ARM) {
Serial.printf("!! Canal %d reserve a l'armement (R2) !!\n", CANAL_ARM);
return;
}
if (modifOk) {
if (nouveauCanal >= 1 && nouveauCanal <= 16) {
rp2040.idleOtherCore();
switch (periph) {
case 'V': case 'v': configSysteme.volant = nouveauCanal; break;
case 'A': case 'a': configSysteme.accel = nouveauCanal; break;
case 'F': case 'f': configSysteme.frein = nouveauCanal; break;
case 'E': case 'e': configSysteme.embrayage = nouveauCanal; break;
}
reinitialiserCanauxCRSF();
rp2040.resumeOtherCore();
sauvegarderConfiguration();
afficherConfiguration();
}
} else if (periph == 'D' || periph == 'd') {
int val = atoi(&commande[2]);
if (val >= 0 && val <= 255) {
rp2040.idleOtherCore();
configSysteme.forceRessort = (uint8_t)val;
rp2040.resumeOtherCore();
majRessortRequise = true;
sauvegarderConfiguration();
afficherConfiguration();
} else {
Serial.println("!! Durete (D) doit etre entre 0 et 255 !!");
}
} else if (periph == 'P' || periph == 'p') {
int val = atoi(&commande[2]);
if (val >= 40 && val <= 900) {
rp2040.idleOtherCore();
configSysteme.angleSouhaiteTotal = (uint16_t)val;
recalculerParametresVolant();
rp2040.resumeOtherCore();
majRangeRequise = true;
sauvegarderConfiguration();
afficherConfiguration();
} else {
Serial.println("!! Plage d'angle (P) doit etre entre 40 et 900 degres !!");
}
} else if (periph == 'X' || periph == 'x') {
float val = atof(&commande[2]);
if (val >= 0.0f && val <= 0.8f) {
rp2040.idleOtherCore();
configSysteme.expoVolant = val;
rp2040.resumeOtherCore();
sauvegarderConfiguration();
afficherConfiguration();
} else {
Serial.println("!! Expo volant (X) doit etre entre 0.0 et 0.8 !!");
}
}
}
}
@@ -356,6 +447,25 @@ void loop() {
else Serial.println("!! [ARM] Refuse : passer au point mort (N) et relacher l'accelerateur !!");
}
if (logDureteRequis) {
logDureteRequis = false;
Serial.printf(">> [DURETE G29] Force ressort : %d / 255 (%d%%)\n",
configSysteme.forceRessort, (configSysteme.forceRessort * 100) / 255);
}
// Sauvegarde automatique 2s après la fin du réglage à la molette
if (momentDernierChangementDurete_ms > 0 && (millis() - momentDernierChangementDurete_ms >= 2000)) {
momentDernierChangementDurete_ms = 0;
sauvegarderConfiguration();
}
// Sauvegarde immédiate lors d'un appui sur le bouton central Enter de la molette
if (demandeSauvegardeEEPROM) {
demandeSauvegardeEEPROM = false;
momentDernierChangementDurete_ms = 0;
sauvegarderConfiguration();
}
static uint32_t dernierHeartbeat = 0;
static uint32_t dernierCompteOK = 0;
if (millis() - dernierHeartbeat >= 2000) {
@@ -364,14 +474,16 @@ 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/6 | Rapport=%d | Arme=%d | Statut_LED=%d | USB=%lu rap/s | Rejetes=%lu | Failsafe=%d\n",
volant_est_pret, g29_pid, etapeInitMoteur, rapportEngage, estArme, statutEnvoiLed,
Serial.printf("[ETAT] G29_Pret=%d (PID=%04X) | InitFFB=%d/6 | 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, configSysteme.forceRessort, configSysteme.angleSouhaiteTotal,
rapportEngage, estArme, statutEnvoiLed,
(unsigned long)rapportsParSec, (unsigned long)compteurRapportsRejetes, failsafeActif);
}
static uint32_t dernier_envoi = 0;
if (millis() - dernier_envoi >= 6) {
dernier_envoi = millis();
// Transmission CRSF précise à 166.67 Hz (toutes les 6000 µs sans dérive)
static uint32_t dernier_envoi_us = 0;
if (micros() - dernier_envoi_us >= 6000) {
dernier_envoi_us += 6000;
envoyer_paquet_crsf(evaluerFailsafe());
}
}
@@ -391,9 +503,9 @@ void gererInitMoteurG29() {
if (!volant_est_pret || etapeInitMoteur >= 6) return;
if (millis() - momentConnexionUSB_ms < 600) return;
uint16_t angle = (uint16_t)ANGLE_SOUHAITE_TOTAL;
uint16_t angle = configSysteme.angleSouhaiteTotal;
const uint8_t cmdRange[7] = {0xF8, 0x81, (uint8_t)(angle & 0xFF), (uint8_t)(angle >> 8), 0x00, 0x00, 0x00};
const uint8_t cmdSpring[7] = {0xFE, 0x0D, (uint8_t)(PENTE_RESSORT_G29 & 0x07), (uint8_t)(PENTE_RESSORT_G29 & 0x07), (uint8_t)FORCE_RESSORT_G29, 0x00, 0x00};
const uint8_t cmdSpring[7] = {0xFE, 0x0D, (uint8_t)(PENTE_RESSORT_G29 & 0x07), (uint8_t)(PENTE_RESSORT_G29 & 0x07), configSysteme.forceRessort, 0x00, 0x00};
switch (etapeInitMoteur) {
case 0: if (envoyerTrameG29_0x30(cmdRange)) etapeInitMoteur++; break;
@@ -447,13 +559,41 @@ void loop1() {
return;
}
if (majRangeRequise) {
uint16_t angle = configSysteme.angleSouhaiteTotal;
const uint8_t cmdRange[7] = {0xF8, 0x81, (uint8_t)(angle & 0xFF), (uint8_t)(angle >> 8), 0x00, 0x00, 0x00};
if (envoyerTrameG29_0x30(cmdRange)) {
majRangeRequise = false;
}
return;
}
if (majRessortRequise) {
const uint8_t cmdSpring[7] = {0xFE, 0x0D, (uint8_t)(PENTE_RESSORT_G29 & 0x07), (uint8_t)(PENTE_RESSORT_G29 & 0x07), configSysteme.forceRessort, 0x00, 0x00};
if (envoyerTrameG29_0x30(cmdSpring)) {
majRessortRequise = false;
}
return;
}
uint8_t masqueCible = 0x00;
switch (rapportEngage) {
case -1: masqueCible = ((millis() / 250) % 2 == 0) ? 0x1F : 0x00; break;
case 0: masqueCible = 0x00; break;
case 1: masqueCible = 0x01; break;
case 2: masqueCible = 0x03; break;
case 3: masqueCible = 0x07; break;
// Si la dureté a été modifiée il y a moins de 1.5s, afficher la jauge de dureté sur les LEDs
if (momentDernierChangementDurete_ms > 0 && (millis() - momentDernierChangementDurete_ms < 1500)) {
uint8_t f = configSysteme.forceRessort;
if (f >= 240) masqueCible = 0x1F; // 5 LEDs (Rouge)
else if (f >= 180) masqueCible = 0x0F; // 4 LEDs (Jaune haut)
else if (f >= 120) masqueCible = 0x07; // 3 LEDs (Jaune bas)
else if (f >= 60) masqueCible = 0x03; // 2 LEDs (Vert haut)
else if (f > 0) masqueCible = 0x01; // 1 LED (Vert bas)
else masqueCible = 0x00; // 0 LED (libre)
} else {
switch (rapportEngage) {
case -1: masqueCible = ((millis() / 250) % 2 == 0) ? 0x1F : 0x00; break;
case 0: masqueCible = 0x00; break;
case 1: masqueCible = 0x01; break;
case 2: masqueCible = 0x03; break;
case 3: masqueCible = 0x07; break;
}
}
envoyerLedsG29(masqueCible);
}
@@ -595,11 +735,48 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i
paletteMoinsDejaEnfoncee = false;
}
int32_t volantCentre = (int32_t)volantRaw - CENTRE_VOLANT_BRUT;
int32_t volantBorne = constrain(volantCentre, -MAX_DELTA_VOLANT, MAX_DELTA_VOLANT);
// Réglage de la dureté du volant à la volée via la molette ou boutons [+] / [-]
uint8_t b7 = (len > 7) ? report[7] : 0;
bool dialPlusActuel = (b7 & DIAL_CW_MASK) || (b7 & BOUTON_PLUS_MASK);
bool dialMoinsActuel = (b7 & DIAL_CCW_MASK) || (b7 & BOUTON_MOINS_MASK);
bool dialEnterActuel = (b7 & DIAL_ENTER_MASK);
float x = (float)volantBorne / (float)MAX_DELTA_VOLANT;
float y = x * (1.0f - EXPO_VOLANT + EXPO_VOLANT * (x * x));
if (dialPlusActuel && !dialPlusDejaEnfonce) {
if (configSysteme.forceRessort <= 245) configSysteme.forceRessort += 10;
else configSysteme.forceRessort = 255;
majRessortRequise = true;
logDureteRequis = true;
momentDernierChangementDurete_ms = millis();
dialPlusDejaEnfonce = true;
} else if (!dialPlusActuel && dialPlusDejaEnfonce) {
dialPlusDejaEnfonce = false;
}
if (dialMoinsActuel && !dialMoinsDejaEnfonce) {
if (configSysteme.forceRessort >= 10) configSysteme.forceRessort -= 10;
else configSysteme.forceRessort = 0;
majRessortRequise = true;
logDureteRequis = true;
momentDernierChangementDurete_ms = millis();
dialMoinsDejaEnfonce = true;
} else if (!dialMoinsActuel && dialMoinsDejaEnfonce) {
dialMoinsDejaEnfonce = false;
}
if (dialEnterActuel && !dialEnterDejaEnfonce) {
demandeSauvegardeEEPROM = true;
dialEnterDejaEnfonce = true;
} else if (!dialEnterActuel && dialEnterDejaEnfonce) {
dialEnterDejaEnfonce = false;
}
int32_t volantCentre = (int32_t)volantRaw - CENTRE_VOLANT_BRUT;
int32_t deltaMax = (maxDeltaVolant > 0) ? maxDeltaVolant : 6553;
int32_t volantBorne = constrain(volantCentre, -deltaMax, deltaMax);
float x = (float)volantBorne / (float)deltaMax;
float expo = configSysteme.expoVolant;
float y = x * (1.0f - expo + expo * (x * x));
uint16_t vVolant = constrain((int32_t)(CRSF_VAL_MID - y * (CRSF_VAL_MAX - CRSF_VAL_MID)), CRSF_VAL_MIN, CRSF_VAL_MAX);
uint16_t vAccel = CRSF_VAL_MID;
@@ -620,10 +797,10 @@ void __not_in_flash_func(tuh_hid_report_received_cb)(uint8_t dev_addr, uint8_t i
? constrain(map(embrayageRaw, DEADZONE_PEDALE, 65535, CRSF_VAL_MID, CRSF_VAL_MAX), CRSF_VAL_MIN, CRSF_VAL_MAX)
: CRSF_VAL_MID;
assignerCanalCRSF(configCanaux.volant, vVolant);
assignerCanalCRSF(configCanaux.accel, vAccel);
assignerCanalCRSF(configCanaux.frein, vFrein);
assignerCanalCRSF(configCanaux.embrayage, vEmbrayage);
assignerCanalCRSF(configSysteme.volant, vVolant);
assignerCanalCRSF(configSysteme.accel, vAccel);
assignerCanalCRSF(configSysteme.frein, vFrein);
assignerCanalCRSF(configSysteme.embrayage, vEmbrayage);
crsf_channels[CANAL_ARM - 1] = estArme ? CRSF_VAL_MAX : CRSF_VAL_MIN;
// Horodatage APRÈS l'écriture des canaux : à la sortie d'un failsafe, le cœur 0