#include #include #include "FS.h" #include "SD.h" #include "SPI.h" #include "RTClib.h" #include // #include "pcnt.h" // https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/include/driver/driver/pcnt.h #include "driver/pcnt.h" //constantes pour la configuration du compteur #define PULSE_COMPTEUR #define PCNT_TEST_UNIT PCNT_UNIT_0 // Il existe 8 compteurs de 0 à 7 //#define PCNT_H_LIM_VAL 10000 //valeur max du compteur_minuteur #define PCNT_L_LIM_VAL 0 //valeur mini du compteur //#define PCNT_THRESH1_VAL 5 //#define PCNT_THRESH0_VAL -5 #define PCNT_INPUT_SIG_IO 4 // Pulse Input GPIO #define PCNT_INPUT_CTRL_IO 5 // Control GPIO HIGH=count up, LOW=count down int numero_capteur = 0; // numéro de série du capteur int bouton_wifi = 7; // pin du bouton wifi bool sem_wifi; //semaphore du minuteur du bouton wifi bool sem_compteur; // sémaphore pour le compteur int16_t pulsations = 0; // variable de compteur int vitesse; // int16_t Pulses = 0; // variable de lecture de compteur 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 unsigned long freq_ecriture = 60000; // durée entre deux écritures sur la carte SD en micro secondes const byte pinMoteur = 4; // broche de contrôle du moteur byte pulsePin = 13; //broche de l'encodeur Servo moteur; //création de l'objet moteur const int broche_CS = 5; // broche de la carte SD char horodatage[25]; //création du tableau pour contenir l'horodatage RTC_DS3231 rtc; //déclaration de la rtc bool etat_bouton_wifi; void start_compteur() { esp_err_t error; Serial.println("Initialisation du compteur"); pcnt_config_t pcnt_config = { pulsePin, // Pulse input gpio_num, if you want to use gpio16, pulse_gpio_num = 16, a negative value will be ignored PCNT_PIN_NOT_USED, // Control signal input gpio_num, a negative value will be ignored PCNT_MODE_KEEP, // PCNT low control mode PCNT_MODE_KEEP, // PCNT high control mode PCNT_COUNT_INC, // PCNT positive edge count mode PCNT_COUNT_DIS, // PCNT negative edge count mode //PCNT_H_LIM_VAL, // Maximum counter value //PCNT_L_LIM_VAL, // Minimum counter value PCNT_TEST_UNIT, // PCNT unit number PCNT_CHANNEL_0, // the PCNT channel }; if(pcnt_unit_config(&pcnt_config) == ESP_OK) //init unit Serial.println("Config Unit_0 = ESP_OK"); /*Configure input filter value*/ pcnt_set_filter_value(PCNT_TEST_UNIT, 100); /*Enable input filter*/ pcnt_filter_enable(PCNT_TEST_UNIT); /*Set value for watch point thresh1*/ //pcnt_set_event_value(PCNT_TEST_UNIT, PCNT_EVT_THRES_1, PCNT_THRESH1_VAL); /*Enable watch point event of thresh1*/ //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_1); //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_1); /*Set value for watch point thresh0*/ //pcnt_set_event_value(PCNT_TEST_UNIT, PCNT_EVT_THRES_0, PCNT_THRESH0_VAL); /*Enable watch point event of thresh0*/ //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_0); //pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_THRES_0); /*Enable watch point event of h_lim*/ //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM); //pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM); /*Enable watch point event of l_lim*/ //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_L_LIM); //pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_L_LIM); /*Enable watch point event of zero*/ //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_ZERO); //pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_ZERO); /*Pause counter*/ //pcnt_counter_pause(PCNT_TEST_UNIT); /*Reset counter value*/ pcnt_counter_clear(PCNT_TEST_UNIT); /*Register ISR handler*/ //pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, NULL); /*Enable interrupt for PCNT unit*/ //pcnt_intr_enable(PCNT_TEST_UNIT); /*Resume counting*/ //pcnt_counter_resume(PCNT_TEST_UNIT); pcnt_get_counter_value(PCNT_TEST_UNIT, &pulsations); } void sd_init(){ if (!SD.begin(broche_CS)) { Serial.println("Carte SD introuvable , reboot"); delay(1000); ESP.restart(); } else{ Serial.println("Carte SD détectée"); // ligne de debug à commenter en prod writeFile(SD, );// ajouter une fonction : "écrire fichier d'identité et créer fichier data sur la carte SD } } void start_moteur() { moteur.attach(pinMoteur); moteur.writeMicroseconds(1500); // envoi d'un 'neutre' au variateur delay(1000); moteur.writeMicroseconds(2000); //valeur de départ pour atteindre 2400tr/min } void rtc_init() { if (! rtc.begin()) { Serial.println("RTC introuvable !"); // ligne de debug à commenter en prod // ajouter un message d'erreur par clignotement de LED // ajouter rebooter l'esp while (1); } if (rtc.lostPower()) { Serial.println("Veuillez regler l'heure sur le module RTC!"); // ligne de debug à commenter en prod // ajouter un message d'erreur par clignotement de LED } else { DateTime now = rtc.now(); // mise à l'heure de l'esp, à partir de la rtc } } void setup() { // serial Serial.begin(115200); // wifi pinMode(bouton_wifi, INPUT); sem_wifi = false; // initialaisation du sémaphore // moteur moteur.attach(pinMoteur); start_moteur(); // compteur de pulsations sem_compteur = false; // initialisation du sémaphore pinMode(pulsePin,INPUT_PULLUP); start_compteur(); // lecteur de carte SD sd_init (); // RTC rtc_init(); } //void stop_compteur { //} //void start_wifiAP { //} void stop_moteur() { moteur.writeMicroseconds(1500); delay(1000); moteur.detach(); } void compte_tour() { if ( sem_compteur == false ) { compteur_minuteur = millis(); sem_compteur == true; } if ( sem_compteur == true && compteur_minuteur - millis() > freq_ecriture ){ vitesse = readPcntCounter_0() / 2; //vitesse en tours par min scribe_sd (); pcnt_counter_clear(0); compteur_minuteur = millis(); sem_compteur = false; } } void scribe_sd (fs::FS &fs, const char * path, const char * horodatage) { Serial.printf("Ecriture dans le fichier: %s\n", path); File file = fs.open(path, FILE_APPEND); if (!file) { Serial.println("Echec d'ouverture du fichier"); return; } if (file.print(horodatage)) { Serial.println("Fichier modifié avec succes."); } else { Serial.println("Echec de la modification du fichier."); } file.close(); } void vigie () { etat_bouton_wifi = digitalRead(bouton_wifi); if ( etat_bouton_wifi == HIGH && sem_wifi == false ) { sem_wifi = true; wifi_minuteur = millis(); } if ( etat_bouton_wifi == HIGH && sem_wifi == true && millis() - wifi_minuteur > 5000 ) { stop_moteur(); stop_compteur(); start_wifiAP(); } } void loop (){ vigie (); pid (); compte_tour (); }