diff --git a/src/main.cpp b/src/main.cpp index 43bb95b..58332b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ bool sem_compteur; // sémaphore pour le compteur int16_t count = 0; // variable de compteur int vRotReel; // vitesse réelle mesurée du moteur +unsigned long compteur_minuteur = 0; // minuteur pour le déclenchement du compteur +int refresh_compte_tour = 500; // durée entre deux mesures du compte-tour en ms ///////////////////////////////// /*NUMÉRO DE SÉRIE DE L'APPAREIL*/ @@ -30,6 +32,7 @@ int numero_capteur = 0001; /* DÉFINITIONS DE LA RTC */ /////////////////////////// RTC_DS3231 rtc; //déclaration de la rtc +DateTime now{rtc.now()}; /////////////////////////// /* DÉFINITIONS DE LA LED */ @@ -41,7 +44,7 @@ RTC_DS3231 rtc; //déclaration de la rtc //////////////////////////////// unsigned long freq_ecriture = 60000; // durée entre deux écritures sur la carte SD en micro secondes char horodatage[12]; //création du tableau pour contenir l'horodatage -char fileName[5]; //tableau pour le nom de fichier +char fichier[] = "/"; //tableau pour le nom de fichier ////////////////////////////////// /* DÉFINITIONS DU DRIVER MOTEUR */ @@ -60,7 +63,6 @@ ESP32PWM pwm; // Activation du pwm int bouton_wifi = 2; // pin du bouton wifi bool sem_wifi; //sémaphore du minuteur du bouton wifi unsigned long wifi_minuteur = 0; // minuteur pour l'appui long sur le bouton wifi -unsigned long compteur_minuteur = 0; // minuteur pour l'appui long sur le bouton wifi bool etat_bouton_wifi; int tempo_bouton_wifi = 5000; //temps d'appui en millisecondes pour arrêter le capteur et passer en mode wifi @@ -89,6 +91,38 @@ void errorCode(int codeNumber) { } } +///////////////////////// +/* FONCTIONS DU MOTEUR */ +///////////////////////// + +void start_moteur() { + ESP32PWM::allocateTimer(2); + delay(500); + moteur.setPeriodHertz(50); + moteur.attach(moteurPin, minUs, maxUs); + delay(15); + moteur.write(0); // envoi du 0 à l'ESC pour initialisation + delay(1500); // délai permettant au variateur de détecter le zero + moteur.write(pos); // vitesse initiale +} + +void gaz_moteur() { + if (vRotVis > vRotReel) { + pos ++; + } + if (vRotReel > vRotVis) { + pos --; + } + moteur.write(pos); +} + +void stop_moteur() { + moteur.write(0); + delay(1000); + moteur.detach(); +} + + /////////////////////////// /* FONCTIONS DU COMPTEUR */ @@ -131,47 +165,16 @@ void stop_compteur() { } void compte_tour() { - if ( sem_compteur == false ) { - compteur_minuteur = millis(); - sem_compteur = true; - } - if ( sem_compteur == true && compteur_minuteur - millis() >= freq_ecriture ){ - vRotReel = pcnt_get_counter_value(PCNT_TEST_UNIT, &count) * 60; //vitesse en tours par min - pcnt_counter_clear(PCNT_TEST_UNIT); - compteur_minuteur = millis(); - sem_compteur = false; + int offset = (compteur_minuteur - millis()); + + if (offset >= refresh_compte_tour ){ + vRotReel = (pcnt_get_counter_value(PCNT_TEST_UNIT, &count)/offset * 60000); //vitesse en tours par min + gaz_moteur(); // ajustement des gaz en fonction de la vitesse mesurée + Serial.println("vRotReel = "); + Serial.println(vRotReel); } } -///////////////////////// -/* FONCTIONS DU MOTEUR */ -///////////////////////// - -void start_moteur() { - ESP32PWM::allocateTimer(0); - moteur.setPeriodHertz(50); - moteur.attach(moteurPin, minUs, maxUs); - delay(15); - moteur.write(0); // envoi du 0 à l'ESC pour initialisation - delay(1500); // délai permettant au variateur de détecter le zero - moteur.write(pos); // vitesse initiale -} - -void gaz_moteur() { - if (vRotVis > vRotReel) { - pos ++; - } - if (vRotReel > vRotVis) { - pos --; - } - moteur.write(pos); -} - -void stop_moteur() { - moteur.write(0); - delay(1000); - moteur.detach(); -} ///////////////////////// /// FONCTIONS SD CARD /// @@ -277,7 +280,6 @@ void testFileIO(fs::FS &fs, const char * path){ file.close(); } - void sd_init() { if(!SD.begin()){ Serial.println("Card Mount Failed"); @@ -291,35 +293,31 @@ void sd_init() { errorCode(3); return; } - Serial.println("balise 01"); - DateTime now = rtc.now(); - fileName[0] = '/'; - fileName[1] = (now.month(),DEC); - fileName[2] = (now.day(),DEC); - fileName[3] = (now.hour(),DEC); - fileName[4] = (now.minute(),DEC); - char entete[64] = "fichier de data"; - writeFile(SD, fileName, entete); + + //DateTime now{rtc.now()};//DateTime + now = rtc.now(); + char date_format[] = "MM-DD-hh-mm"; + char *date = now.toString(date_format); + strcat(fichier,date); + char entete[] = "# fichier de data \n"; + writeFile(SD, fichier, entete); } void scribe_sd (){ - DateTime now = rtc.now(); + if ( sem_compteur == false ) { + compteur_minuteur = millis(); + sem_compteur = true; + } + if ( sem_compteur == true && compteur_minuteur - millis() >= freq_ecriture ){ + char timestamp[] = "YY-MM-DD-hh:mm:ss"; + char *horodatage = now.toString(timestamp); char buffer[64]; snprintf(buffer, sizeof buffer, "%d", vRotReel); - horodatage[0] = now.year(); - horodatage[1] = '-'; - horodatage[2] = now.month(); - horodatage[3] = '-'; - horodatage[4] = now.day(); - horodatage[5] = '-'; - horodatage[6] = now.hour(); - horodatage[7] = ':'; - horodatage[8] = now.minute(); - horodatage[9] = ':'; - horodatage[10] = now.second(); - horodatage[11] = ' '; - appendFile(SD, fileName, horodatage); - appendFile(SD, fileName, buffer); + appendFile(SD, fichier, horodatage); + appendFile(SD, fichier, " " ); + appendFile(SD, fichier, buffer); + appendFile(SD, fichier, "\n" ); + } } @@ -335,14 +333,14 @@ void rtc_init() { // ESP.restart(); //while (1); } - if (rtc.lostPower()) { + /* if (rtc.lostPower()) { Serial.println("Veuillez régler l'heure et vérifier la pile du module RTC!"); // ligne de debug à commenter en prod errorCode(2); - } + }*/ else { - DateTime now = rtc.now(); // - Serial.println("mise à l'heure de l'esp, à partir de la rtc"); + //DateTime now = rtc.now(); // + Serial.println("rtc OK"); } @@ -447,23 +445,22 @@ void vigie_Wifi () { void setup() { Serial.begin(9600); // serial just for feedback delay(3000); + pinMode(LED,OUTPUT); // LED bouton wifi et erreurs + pinMode(bouton_wifi, INPUT); // bouton wifi Serial.println("balise 0"); rtc_init(); // RTC sd_init (); // initialisation de la carte SD - pinMode(bouton_wifi, INPUT); // bouton wifi sem_wifi = false; // initialisation du sémaphore - pinMode(LED,OUTPUT); // LED bouton wifi et erreurs - start_moteur(); // moteur - /* compteur de pulsations */ sem_compteur = false; // initialisation du sémaphore //pinMode(pulsePin,INPUT_PULLUP); start_compteur(); + start_moteur(); // moteur + } void loop (){ vigie_Wifi(); compte_tour(); - gaz_moteur(); scribe_sd(); }