Le programme précèdent estime le SOC au démarrage (setup) après une reconnexion de l’alimentation de la batterie via sa tension.
Mais un enregistrement du SOC en mémoire NVS (Non Volatile Storage) de l’ESP32 surnommé parfois maladroitement EEPROM sur le net de la consommation d’énergie serait mieux.
je n'ai pas réussi à utiliser la library EEPROM avec des floats.
Il y a des exemples pertinents sur ce lien d’utilisation de NVS avec la Library « preferences » qui gère l’enregistrement de façon permanente sur ESP32
D’ailleurs voici un autre exemple de base d’utilisation de cette Library preferences avec des floats, car les exemples ne sont pas terribles.
/ ESP32 EEPROM TTGO
// https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino
#include <Preferences.h> //must installed
#include <TFT_eSPI.h> //
#include <SPI.h>
hw_timer_t * timer = NULL;
TFT_eSPI tft = TFT_eSPI(); //library 1,4" color 135, 240
Preferences preferences;
float counter;
float counter1;
void setup() {
Serial.begin(115200);
Serial.println();
tft.init(); //135 × 240.
tft.setRotation(2); //0 format portrait a l'endroit
preferences.begin("my-app", false);
counter = preferences.getFloat("counter", 0);
counter1 = preferences.getFloat("counter1", 0);
// preferences.end();
tft.fillScreen(TFT_BLACK); //efface l'ecran en noir
tft.setCursor(0, 0, 2); //// (x,y,taille)
tft.setTextColor(TFT_GREEN,TFT_BLACK); //(couleur police, background police)
tft.println("IUT GEII SOISSONS");
}
void loop() {
counter=counter+0.01;
counter1=counter1+0.1;
preferences.begin("my-app", false); //memorise les données
preferences.putFloat("counter", counter);
preferences.putFloat("counter1", counter1);
//preferences.end();
delay(1000);
// Serial.printf("Current counter value: %f\n", counter);
// Serial.printf("Current counter value: %f\n", counter);
tft.setCursor(0, 50, 4); // (x,y,taille) //ecran TTGO
tft.setTextColor(TFT_GREEN,TFT_BLACK); //(couleur police, background police)
tft.print(counter,2); //affiche les données sauvegarde apres un RESET
tft.setCursor(0, 80, 4); // (x,y,taille)
tft.setTextColor(TFT_GREEN,TFT_BLACK); //(couleur police, background police)
tft.print(counter1,2); //affiche
}
Enregistrer les données toutes les secondes, est ce vraiment cruciale pour la durée de vie de la flash ?
Je ne sais pas ! on verra bien…
Voici donc un autre programme d’instrumentation sauvegardant en NVS certaine données tel que la consommation, la distance
Mais après chaque recharge pleine, il faudra réinitialiser la consommation d’énergie, la distance, le temps de roulage….
Avec le bouton droit du TTGO. Il y aura une sauvegarde de la distance totale effectué aisni que de la consommation totale
// ttgo_instrumetation_velo_BT1 NVS
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Preferences.h> //must installed
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
hw_timer_t * timer = NULL;
TFT_eSPI tft = TFT_eSPI(); //library 1,4" color 135, 240
Preferences preferences;
bool flag=0;
float volt=54.6;
float volt1=54.6;
float volt2=54.6;
float volt3=54.6;
float volt4=54.6;
float volt5=54.6;
float voltmini=54.6;
float Mvolt,voltAver;
float ACS758;
float courantAver, courant,courant1,courant2,courant3,courant4,courant5;
float Asconso;
float Ahconso;
float AhTotale;
float regeneration;
float power, wh,wh1, ws=0;
float resistanceBD=1,resistanceBDmax,resistanceBDmin;
float resistanceBC;
float vitesse=0,vitesse2=0; //kph
float distance=1; //m
float distanceTotale; //km
float km;
float temperatur1;
float temperaturAve;
float temperaturAve1;
const float nbrcellule=13; //13*3.7=48V
const float tensionmax=4;
const float tensionmini=3;
float capacityAhN=10; //valeur nominale de la capacity
// float capacityWh=480 ; //nbrcellule*3.7*capacityAhN;
const float perimetre=1590; //perimetre roue (m)
float SOC; //
float SOH; //etat de santé estimation
float efficience=8; //Wh/km
float autonomykm;
float DVpkm=0.5;
float autonomykmV;
unsigned long to;
unsigned long t1;
char BluetoothData;
bool flagnbrc=1;
byte unsigned menu=0;
byte unsigned flagvitesse=0;
byte unsigned flagSright2;
byte unsigned flagSright5;
byte unsigned flagvoltmini;
volatile byte unsigned seconde=0;
byte unsigned minut=40;
byte unsigned heure=16;
byte unsigned address;
float secondeRoulage=1;
float tempsh; //calcul le temps de roulage
float vitessemoyenne=0;
float echelle;
float powerAvera;
#define Sleft 0 //switch left
#define Sright 35 //switch right
void IRAM_ATTR fonction_ISR() {//broche 17 SENSOR hall 3144 odometer, tachymeter
//interruption exterieure pour compter le nombre de tour
distance=distance+1.59;
t1=millis()-to;
if(t1<2000 && t1>=18) {
vitesse2=(perimetre*3.6)/t1; } //precision minimale 1tour*1.590*3.6/2s=2.324 km/h
else {vitesse=0;}
vitesse=(vitesse+vitesse2)/2; //moyenneur
flagvitesse=0;
to=millis();
}
void IRAM_ATTR timer_isr() { //routine interruption 1 seconde
flag=1; // https://github.com/Xinyuan-LilyGO/TTGO-T-Display
flagvitesse++; //https://f6czv.fr/documentation-sur-esp32-et-le-ttgo-t-display
flagSright2++;
flagSright5++;
flagvoltmini++;
seconde++;
}//fin isr
void setup() {
pinMode(17, INPUT_PULLUP); //odometre ky 003 avec 3144
pinMode(35, INPUT_PULLUP); //bouton droit
pinMode(26, INPUT); //voltage battery
pinMode(27, INPUT); //ACS758 cuurent
pinMode(15, OUTPUT);
pinMode(36, INPUT); //LM35
Serial.begin(115200); //Serial.begin(115200);
SerialBT.begin("veloBLU"); //Bluetooth device name pour appairage
preferences.begin("my-app", false);
tft.init(); //135 × 240.
tft.setRotation(2); //0 format portrait a l'endroit
analogReadResolution(12);
uint8_t timer_id = 1;
uint16_t prescaler = 80; // Between 0 and 65 535
int threshold = 1000000; //
timer = timerBegin(timer_id, prescaler, true);
timerAttachInterrupt(timer, &timer_isr, true);
timerAlarmWrite(timer, threshold, true);
timerAlarmEnable(timer);
//odometre
attachInterrupt(17, fonction_ISR, FALLING); //HIGH, FALLING, CHANGE, LOW
//enregistrer NVS
Asconso = preferences.getFloat("Asconso", 0);
secondeRoulage = preferences.getFloat("secondeRoulage", 0);
distance=preferences.getFloat("distance", 0);
ws=preferences.getFloat("ws", 0);
AhTotale=preferences.getFloat("AhTotale", 0);
distanceTotale=preferences.getFloat("distanceTotale", 0);
}//fin setup
void loop() {
if (flag==1) { //toutes les secondes
// digitalWrite(15,1);
ACS758=analogRead(27); // 50A 0.04V/A bidirec peut lire le courant de charge et freinage
courant5=courant4; //4096*0.06/3.3=74.47 unidirectionel
courant4=courant3;
courant3=courant2;
courant2=courant1;
courant1=courant;
courant=(((ACS758-1888)/34)+0);
courantAver=(courant5+courant4+courant3+courant2+courant1+courant)/6;
if (courant>(courant1+3)) {resistanceBD=(volt1-volt)/(courant-courant1);} //resistance batterie decharge
if (resistanceBD<=resistanceBDmin) {resistanceBDmin=resistanceBD;}
if (resistanceBDmin<0.01) {resistanceBDmin=0.01;}
if (resistanceBDmin>0.25) {resistanceBDmin=0.25;}
if (resistanceBD>=resistanceBDmax) {resistanceBDmax=resistanceBD;}
if (courant<0 & (courant<(courant1-3))) {resistanceBC=(volt1-volt)/(courant-courant1);} //resistance batterie charge
Mvolt=analogRead(26); //pont diviseur 33k et 1200ohms
volt5=volt4;
volt4=volt3;
volt3=volt2;
volt2=volt1;
volt1=volt; //volt precedent il y a 1 seconde
volt=(Mvolt*88.2)/3624; //3840
voltAver=(volt5+volt4+volt3+volt2+volt1+volt)/6;
if (voltAver<voltmini && courant>0 ) {voltmini=voltAver;flagvoltmini=0;} //tension mini de decharge
const float Imax=20; //courant du max d'acceleration du controler
if (voltmini>(nbrcellule*tensionmax-(0.1*Imax))) {voltmini=nbrcellule*tensionmax-(0.1*Imax);} //decalage
if (voltmini<(voltAver-3)) {voltmini=(voltAver-3);} //limitation du volt mini
// if (flagvoltmini>120) {voltmini=volt;} //RAZ de voltmini si au bout de 120seconde, il n'y a pas eu de grosse demande de courant.
// if (courant<(-3) ) {voltmini=volt;} //RAZ de voltmini lors de la charge ou freinage electrique
power=volt*courantAver; //Watt permet de connaitre la consommation
ws=power+ws ; //energy wattseconde
preferences.putFloat("ws", ws);
wh=(ws/3600)+wh1; //energy wattheure
Asconso= courant+Asconso ; //ampere.seconde
preferences.putFloat("Asconso", Asconso);
Ahconso=Asconso/3600; //ampere.heure
SOC=(((capacityAhN-Ahconso)*100)/capacityAhN); //% state of charge
SOH=(((voltmini/nbrcellule)-tensionmini)*100)/(tensionmax-tensionmini) ; //estimation de la capacité energetique restante
if (SOH>=100) {SOH=100; }
if (SOH<=0) {SOH=0;}
if (voltmini<=39) {SOH=0; } //13*3V=39V
autonomykm=((capacityAhN*3.7*nbrcellule)*SOC)/(efficience*100); // autonomykm restante
//DVpkm=(tensionmax*nbrcellule-voltmini)/km ; //(tensionmax*nbrcellule-voltmini)/km
DVpkm=(tensionmax*nbrcellule-voltAver)/km ; //(tensionmax*nbrcellule-voltmini)/km
if (DVpkm>=1) {DVpkm=1;}
if (DVpkm<=0.005) {DVpkm=0.005;}
autonomykmV=(voltmini-(tensionmini*nbrcellule))/DVpkm; //(voltmini-(tensionmini*nbrcellule))/DVpkm;
if (autonomykmV>100) {autonomykmV=100;}
temperatur1=analogRead(36); //temperature LM35
temperatur1=temperatur1*0.079+9.4;
if(seconde >= 60){seconde=0; minut++;}
if(minut >= 60){minut=0; heure++;}
if(heure>=24) { heure=0;}
if (flagvitesse>3) {vitesse=0;vitesse2=0;} //si la vitesse est tres faible
if (vitesse>0) {secondeRoulage++;tempsh=secondeRoulage/3600; //calcul le temps de roulage
preferences.putFloat("secondeRoulage", secondeRoulage);
temperaturAve=temperaturAve+temperatur1; //temperature average temps roulage
temperaturAve1=temperaturAve/(tempsh*3600);
} //calcul le temps de roulage
vitessemoyenne=km/tempsh; //0.001/(1s/3600)
km=distance/1000;
if (km>=0.1) {efficience=(wh)/km;}
if (efficience>=25) {efficience=25;}
if (efficience<=2) {efficience=2;}
if (menu==0) {
tft.fillScreen(TFT_BLACK); //efface l'ecran en noir
tft.setCursor(0, 0, 2); //// (x,y,taille)
tft.setTextColor(TFT_GREEN,TFT_BLACK); //(couleur police, background police)
tft.println("IUT GEII SOISSONS");
tft.setCursor(0, 20, 4);// (x,y,taille)
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(volt,1);
tft.print("V ");
tft.print(courantAver,1);
tft.print("A ");
tft.setCursor(0, 50, 4);// (x,y,taille)
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(Ahconso,1);
tft.print("Ah");
tft.print(voltmini,1);
tft.print("V");
tft.setCursor(0, 80, 7);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print(vitesse,1);
tft.setCursor(110,115, 2);
tft.print("kph ");
tft.setCursor(0,135, 4);
tft.setTextColor(TFT_BLUE,TFT_BLACK);
tft.print(km,1);
tft.print("km ");
tft.setCursor(0,165, 4);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(power,0);
tft.print("W ");
tft.setCursor(0,195, 6);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print(heure);
tft.print(".");
tft.print(minut);
tft.print(" ");
}//fin menu=0
if (menu==1) {
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0, 4);// (x,y,taille)
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print("SOC");
tft.print(SOC,0);
tft.print("% ");
tft.setCursor(0, 30, 4);// (x,y,taille)
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print("SOH ");
tft.print(SOH,0);
tft.print("% ");
tft.setCursor(0,60, 4);
tft.setTextColor(TFT_BLUE,TFT_BLACK);
tft.print(efficience,1);
tft.setCursor(90,75, 2);
tft.print("Wh/km ");
tft.setCursor(0,90, 4);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("A:");
tft.print(autonomykm,0);
tft.print("km ");
tft.setCursor(0,120, 4);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(DVpkm,2);
tft.print("V/km ");
tft.setCursor(0,150, 4);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("E:");
tft.print(autonomykmV,0);
tft.print("km ");
tft.setCursor(0,180, 4);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print(wh,0);
tft.print("Wh ");
} //fin de menu1
if (menu==2) {
voltmini=volt; //a faire à l'arret pour recalculer le SOH avec OCV
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0, 4);// (x,y,taille)
tft.print("R:");
tft.print(resistanceBD,3);
tft.print(" ");
tft.setCursor(0, 30, 4);// (x,y,taille)
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print("Rm:");
tft.print(resistanceBDmin,3);
tft.print(" ");
tft.setCursor(0, 60, 4);// (x,y,taille)
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print("RM:");
tft.print(resistanceBDmax,3);
tft.print(" ");
tft.setCursor(0, 90, 4);// (x,y,taille)
tft.setTextColor(TFT_BLUE,TFT_BLACK);
tft.print("RC:");
tft.print(resistanceBC,3);
tft.print(" ");
tft.setCursor(0, 120, 4);// (x,y,taille)
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print(temperatur1,1); //temperaturAve1 , temperatur1
tft.print("°C ");
tft.setCursor(0, 150, 4);// (x,y,taille)
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print(temperaturAve1,0);
tft.print("°Cmoy ");
tft.setCursor(0,180, 4);
tft.setTextColor(TFT_BLUE,TFT_BLACK);
tft.print(distanceTotale,0);
tft.print("kmT ");
tft.setCursor(0,210, 4);
tft.setTextColor(TFT_BLUE,TFT_BLACK);
tft.print(AhTotale,0);
tft.print("AhT ");
} //fin de menu2
/*
Serial.print(volt,1);Serial.print(";");
Serial.print(courantAver,1);Serial.print(";");
Serial.print(vitesse,1);Serial.println(";");
*/
//supervision communication bluetooth avec application
SerialBT.print("*A"+String(voltAver,1)+"*"); // https://www.keuwl.com/electronics/rduino/bluet/08-dht11/
SerialBT.print("*B"+String(courantAver,1)+"*");
SerialBT.print ("*C"+String(Ahconso,2)+"*");
SerialBT.print ("*D"+String(vitesse,1)+"*");
SerialBT.print ("*E"+String(km,1)+"*");
SerialBT.print ("*F"+String(efficience,1)+"*");
SerialBT.print ("*G"+String(power,0)+"*");
SerialBT.print ("*H"+String(wh,0)+"*");
SerialBT.print ("*I"+String(resistanceBD,3)+"*");
SerialBT.print ("*J"+String(SOC,0)+"*");
SerialBT.print ("*K"+String(SOH,0)+"*");
SerialBT.print ("*L"+String(autonomykm,0)+"*");
SerialBT.print ("*M"+String(autonomykmV,0)+"*");
SerialBT.print ("*N"+String(DVpkm,3)+"*");
SerialBT.print ("*O"+String(vitessemoyenne,2)+"*");
SerialBT.print ("*Q"+String(voltmini,1)+"*");
SerialBT.print ("*R"+String(tempsh,2)+"*");
SerialBT.print ("*S"+String(powerAvera,1)+"*");
//graphique 3 courbes, separation par les vigules
/* SerialBT.print("*P");
SerialBT.print(String(SOC,0)+","); //bleu ciel
SerialBT.print(String(SOH,0)+","); //red
SerialBT.print(String(autonomykm,0)); //verte
SerialBT.print("*");
*/
/* SerialBT.print("*P");
SerialBT.print(String(voltAver,1)+","); //voltAver
SerialBT.print(String(courantAver,1)+","); //bleu ciel
SerialBT.print(String(voltmini,1)+",");
SerialBT.print(String(vitesse,0)); //red
SerialBT.print("*");
*/
/*
SerialBT.print("*P");
SerialBT.print(String(SOH,0)+","); //bleu ciel
SerialBT.print(String(SOC,0)+",");
SerialBT.print(String(autonomykmV,0)+",");
echelle=DVpkm*50;
SerialBT.print(String(echelle,1)); //verte
SerialBT.print("*");
*/
SerialBT.print("*P");
SerialBT.print(String(efficience,1)+","); //voltAver
SerialBT.print(String(vitesse,1)+","); //red
echelle=power/30;
SerialBT.print(String(echelle,0)+","); //green
powerAvera=wh/(tempsh*10);
SerialBT.print(String(powerAvera,0)); //violet
SerialBT.print("*");
//digitalWrite(15,0);
flag=0;}//fin flag seconde
if (SerialBT.available()){
BluetoothData=SerialBT.read(); //Get next character from bluetooth
if(BluetoothData=='X') minut++; //reglage minute et heure
if(BluetoothData=='Y') {menu++;if (menu>=3) {menu=0;} }
}
if (digitalRead(Sleft)==0 ) {menu++;
if (menu>=3) {menu=0;} }
if (digitalRead(Sright)==0 ) { //remise à zero
if (flagSright2>=3) {
AhTotale=Ahconso+AhTotale;
preferences.putFloat("AhTotale", AhTotale);
distanceTotale=distanceTotale+km;
preferences.putFloat("distanceTotale", distanceTotale);
Asconso=0;
preferences.putFloat("Asconso", Asconso);
ws=0;
preferences.putFloat("ws", ws);
secondeRoulage=1;
preferences.putFloat("secondeRoulage", secondeRoulage);
distance=1;
preferences.putFloat("distance", distance);
vitessemoyenne=0;
resistanceBDmin=0.25;
resistanceBDmax=0;
resistanceBD=0.01 ;
flagSright2=0; } // end flagSright==2
} //end Sright
}//fin loop
maintenant, il va falloir gerer la poignée d'acceleration avec une limitation de vitesse et de puissance