mbed-os github

Dependencies:   ADS1015 Faulhaber HTU21D_mod MS5837_potless Sensor_Head_RevB_3 USBDevice_dfu Utilsdfu beep

Fork of ARNSRS_testDFU by POTLESS

Revision:
56:61b0f4d6a890
Parent:
55:6536e02d729e
Child:
57:a0827fa46b03
diff -r 6536e02d729e -r 61b0f4d6a890 main.cpp
--- a/main.cpp	Thu May 31 14:38:30 2018 +0000
+++ b/main.cpp	Tue Jun 12 10:42:12 2018 +0000
@@ -10,8 +10,6 @@
 
 
 //Commandes  des servos
-
-DigitalOut E5V (PA_4);
 #define PWM_SERVO_POUMON PB_15
 #define nSleep_SERVO_POUMON PC_6
 #define FWD_SERVO_POUMON PB_14
@@ -33,21 +31,22 @@
 #define PIN_V_PILES_DIGI PC_3
 #define PIN_V_USB_DIGI PA_0
 
-USBSerial serialMonit( 0x0483 , 0x5740 , 0x0001 , false ) ; 
+USBSerial serialMonit(0x0483, 0x5740, 0x0001, false);
+//USBSerial serialMonit;
 
 //#define OUTPUT(...) { NVIC_DisableIRQ(USART3_IRQn); serialMonit.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(USART3_IRQn);}
-#define OUTPUT(...) { NVIC_DisableIRQ(USART2_IRQn); serialMonit.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(USART2_IRQn);}
+//#define OUTPUT(...) { NVIC_DisableIRQ(USART2_IRQn); serialMonit.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(USART2_IRQn);}
+#define OUTPUT(...) { serialMonit.printf(__VA_ARGS__); fflush(stdout);}
 #define IHM(...) { NVIC_DisableIRQ(USART3_IRQn); display.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(USART3_IRQn);}
 
 //PinName pwm, PinName nSleep, PinName fwd, PinName rev, PinName channelA, PinName channelB, int pulsesPerRev, int Rapport, Encoding encoding = X2_ENCODING
-Faulhaber Servo_Poumon("Servo_Poumon", PWM_SERVO_POUMON, nSleep_SERVO_POUMON, FWD_SERVO_POUMON, REV_SERVO_POUMON, Channel_A_SERVO_POUMON, Channel_B_SERVO_POUMON, 16, 207, Faulhaber::X2_ENCODING);
-Faulhaber Servo_Fuite("Servo_Fuite", PWM_SERVO_FUITE, nSleep_SERVO_FUITE, FWD_SERVO_FUITE, REV_SERVO_FUITE, Channel_A_SERVO_FUITE, Channel_B_SERVO_FUITE, 16, 1257, Faulhaber::X2_ENCODING);
+//Angle 0 -> tuyau fermé, Angle 90 -> tuyau ouvert
+Faulhaber Servo_Poumon("Servo_Poumon", PWM_SERVO_POUMON, nSleep_SERVO_POUMON, FWD_SERVO_POUMON, REV_SERVO_POUMON, Channel_A_SERVO_POUMON, Channel_B_SERVO_POUMON, 16, 1257, Faulhaber::X2_ENCODING);
+Faulhaber Servo_Fuite("Servo_Fuite", PWM_SERVO_FUITE, nSleep_SERVO_FUITE, FWD_SERVO_FUITE, REV_SERVO_FUITE, Channel_A_SERVO_FUITE, Channel_B_SERVO_FUITE, 16, 207, Faulhaber::X2_ENCODING);
 
 //Moniteur série, Serial 2
 //Serial serialMonit(PC_10, PC_11, 115200); // UART 3 pour windev nucleo - version définitive sur uart3 pour carte ARNSRS
-//Serial serialMonit(PA_2, PA_3,115200); // UART2 à décommenter pour carte ARNSRS
-
-
+Serial serial(PA_2, PA_3,115200); // UART2 à décommenter pour DEBUG et Deep DEBUG
 
 //Communication avec l'IHM
 Serial display(PC_4, PC_5, 115200);
@@ -64,6 +63,12 @@
 volatile int indexParam = 0;
 bool newParamFlag = false;
 
+//pour Param Venant de l'IHM
+const int sizeIHM = 50;
+char  IHM[sizeIHM];
+volatile int indexIHM = 0;
+bool newIHMFlag = false;
+
 //Flag PID ON / OFF
 int FLAG_PID = 0;
 
@@ -116,10 +121,10 @@
 int Max_Log_Size = 1800;//1 heure, 1800 lignes donc à multiplier par la fréquence d'enregistrement, Ref_Time, pour voir combien de temps représente un Log...
 
 //Contrôle des servos
-float Consigne_poumon = 0;
-float volet_poumon_Position = 0;
-float Consigne_fuite = 0;
-float volet_fuite_Position = 0;
+float Consigne_poumon = HOME_SERVO_POUMON;
+float volet_poumon_Position;
+float Consigne_fuite = HOME_SERVO_FUITE;
+float volet_fuite_Position;
 float Volets_Speed = 1;
 float Volet_DeadBand = 5;
 
@@ -151,14 +156,75 @@
 int VPiles = 1;
 float VPiles_val = 1;
 
+//Commande à envoyer à l'IHM
+//0 -> Marche
+//1 -> Au dodo
+//3 -> Big problème
+int Commande_IHM = 0;
 
 //Interruption pin
 InterruptIn vpile_off_on(PIN_V_PILES_DIGI);
 InterruptIn vusb_off_on(PIN_V_USB_DIGI);
 
+//Pin du régulateur
+DigitalOut E5V (PA_4);
+
 //Buzzer
 Beep buzzer(PC_8);
 
+void Au_Dodo()
+{
+    buzzer.beep(1000,0.3);
+    wait_ms(700);
+    buzzer.beep(1000,0.3);
+    wait_ms(700);
+    buzzer.beep(1000,0.5);
+    wait(1);
+    
+    //Mise en veille
+    HAL_PWREx_EnablePullUpPullDownConfig() ;
+    HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
+    HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); // pour PA_0
+    // Clear wake up Flag
+    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF1);
+    // Enable wakeup pin WKUP2
+    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); // high est la valeur par défaut
+    // Set RTC back-up register RTC_BKP31R to indicate
+    //later on that system has entered shutdown mode
+    WRITE_REG( RTC->BKP31R, 0x1 );
+    //Enter shutdown mode
+    HAL_PWREx_EnterSHUTDOWNMode();
+}
+    
+void USB_unpluged()
+{
+    DEBUG("  La prise USB a été débranchée.\n");
+    
+    //Arrêt des enregistrements
+    FLAG_REC = false;
+    
+    //Mise en veille de l'IHM
+    Commande_IHM = 1;
+    IHM("<0 0 ;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;%d>\r\n", Commande_IHM);
+    FLAG_DISPLAY = false;
+    Commande_IHM = 0;
+    
+    //Enregistrement position des servos sans mettre en sécu
+    int Pos_P = Servo_Poumon.getPulses();
+    UTILS::Store_A_Val((float)Pos_P, "Servo_Poumon.sys");
+    DEBUG("  position volet poumon sauvegardée = %d pulse(s)\r\n", Pos_P);
+
+    int Pos_F = Servo_Fuite.getPulses();
+    UTILS::Store_A_Val((float)Pos_F, "Servo_Fuite.sys");
+    DEBUG("  position volet fuite sauvegardée = %d pulse(s)\r\n", Pos_F);
+    
+    //ejection de la flash pour pas crasher le system de fichiers
+    UTILS::UnMount_Flash();
+    
+    //Mise en veille
+    Au_Dodo();
+}
+        
 void FLASH_set_boot_bank(int bank)
 {
     //HAL_DeInit();
@@ -173,7 +239,6 @@
     /* Get the Dual boot configuration status. */
     HAL_FLASHEx_OBGetConfig(&OBInit);
     wait(1);
-
     /* Enable/Disable dual boot feature */
     OBInit.OptionType = OPTIONBYTE_USER;
     OBInit.USERType   = OB_USER_BFB2;
@@ -186,18 +251,19 @@
             break;
 
     }
-HAL_FLASHEx_OBProgram (&OBInit);
-wait(1);
-
+    HAL_FLASHEx_OBProgram (&OBInit);
+    wait(1);
     if(HAL_FLASHEx_OBProgram (&OBInit) != HAL_OK) {
         /* Failed setting the option bytes configuration.
          * Call 'HAL_FLASH_GetError()' for details. */
-        serialMonit.printf("Impossible de changer BFB2.\n");
-    } else {serialMonit.printf("BFB2.\n");}
-    
-    HAL_FLASH_OB_Launch();  
-    HAL_FLASH_OB_Lock(); 
-    serialMonit.printf("Fin BFB2.\n");
+        DEBUG("  Impossible de changer BFB2.\n");
+    } else {
+        DEBUG("  BFB2 a été changé.\n");
+    }
+
+    HAL_FLASH_OB_Launch();
+    HAL_FLASH_OB_Lock();
+    DEBUG("  Fin routine BFB2.\n");
 }
 
 void Affichage()
@@ -256,6 +322,7 @@
     EN_MODE_SECU = true;
 
     wait_ms(100);
+    
     int Pos_P = Servo_Poumon.getPulses();
     UTILS::Store_A_Val((float)Pos_P, "Servo_Poumon.sys");
     DEBUG("  position volet poumon sauvegardée = %d pulse(s)\r\n", Pos_P);
@@ -284,52 +351,10 @@
     UTILS::UnMount_Flash();
 
     OUTPUT("  Ejection de la Flash\r\n");
-    wait(1);
-    buzzer.beep(1000,0.5);
+
     wait(1);
-    // préparation deepsleep
-
-    /*
-    //première méthode mais je ne sais pas ce que ça consomme et il faut normalement reduire le frequence d'horloge avant
-    //il est possible que ça génère un plantage et la rtc ne fonctionnera pas?
-    HAL_PWREx_EnableLowPowerRunMode();
-    DEBUG("----------------LOW POWER RUN MODE--------------------\r\n");
-
-    bool isDSallowed;
-    isDSallowed =  sleep_manager_can_deep_sleep();
-    DEBUG("  Deep sleep autorisé ? %i\r\n", isDSallowed);
-    wait(1.0);
-
-    DEBUG("\n\r Deepsleep tentative normale !(bouton bleu pour ressortir)\n");
-    wait(0.1);
-
-    sleep_manager_sleep_auto(); // ne marche pas, je ne sais pas pk
-    */
-
-    /*
-    HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN2); // pour PC_13
-    // Clear wake up Flag
-    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF2);
-    // Enable wakeup pin WKUP2
-    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN2_LOW); // low parce qu'on veut que ça redémarre avec le user button TODO à passer sur High pour que ça redémarrage avec l'arrivée du hus sur l'USB
-    */
-
-    HAL_PWREx_EnablePullUpPullDownConfig() ;
-    HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
-
-    HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); // pour PA_0
-    // Clear wake up Flag
-    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WUF1);
-    // Enable wakeup pin WKUP2
-    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); // high est la valeur par défaut
-
-    // Set RTC back-up register RTC_BKP31R to indicate
-    //later on that system has entered shutdown mode
-    WRITE_REG( RTC->BKP31R, 0x1 );
-    //Enter shutdown mode
-    DEBUG("\n\r Attention c'est du brutal !  bouton bleu pour ressort.... ZZZZZZZ ZZZZZ !!!\n");
-
-    HAL_PWREx_EnterSHUTDOWNMode();
+    
+    Au_Dodo();
 }
 
 //Fonction test de valeur d'entrée digitale
@@ -437,17 +462,34 @@
 
     while(serialMonit.readable()) {
         if ((indexParam  == sizeParam) || newParamFlag  == true) { //éviter la saturation du buffer
-            NVIC_DisableIRQ(USART2_IRQn);  // USART 2 pour carte ARNSRS
             char char_flush = serialMonit.getc();
-            NVIC_EnableIRQ(USART2_IRQn); // USART 2 pour carte ARNSRS
         } else {
-            NVIC_DisableIRQ(USART2_IRQn); // USART 2 pour carte ARNSRS
             param [indexParam ++] = serialMonit.getc();//chargement du buffer dans le message
             if ((indexParam == sizeParam) || (param[indexParam - 1] == '\n')) {//le message est complet ou nouvelle ligne ou autre si on veut...
                 param[indexParam] = 0;
                 newParamFlag  = true;
             }
-            NVIC_EnableIRQ(USART2_IRQn); // USART 2 pour carte ARNSRS
+        }
+    }
+}
+
+//Callback de l'intérruption des envois de commandes depuis l'IHM
+void callbackIHM()
+{
+
+    while(display.readable()) {
+        if ((indexIHM  == sizeIHM) || newIHMFlag  == true) { //éviter la saturation du buffer
+            NVIC_DisableIRQ(USART3_IRQn); 
+            char char_flush = display.getc();
+            NVIC_EnableIRQ(USART3_IRQn);
+        } else {
+            NVIC_DisableIRQ(USART3_IRQn);
+            IHM [indexIHM ++] = display.getc();//chargement du buffer dans le message
+            if ((indexIHM == sizeIHM) || (IHM[indexIHM - 1] == '\n')) {//le message est complet ou nouvelle ligne ou autre si on veut...
+                IHM[indexIHM] = 0;
+                newIHMFlag  = true;
+            }
+            NVIC_EnableIRQ(USART3_IRQn);
         }
     }
 }
@@ -460,20 +502,24 @@
 
     sscanf(message,"%s %s",&com , &numb);
 
-    DEEP_DEBUG("\r\n  Commande = %s       Valeur = %s \r\n\r\n", com, numb);
-    //serialMonit.printf("\r\n  Commande = %s       Valeur = %s \r\n\r\n", com, numb);
+    DEBUG("\r\n  Commande = %s       Valeur = %s \r\n\r\n", com, numb);
 
     OUTPUT("?\r\n");
 
     if (0 == strcmp(com, "secu")) {
         Mode_SECU();
     } else if (0 == strcmp(com, "PING")) {
-        FLAG_PID = 0;
+        /*FLAG_PID = 0;
         FLAG_AFF = false;
         FLAG_WINDEV = false;
-        control_Servo.setMode(MANUAL_MODE);
+        control_Servo.setMode(MANUAL_MODE);*/
+        Commande_IHM = 1;
+        IHM("<0 0 ;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;%d>\r\n", Commande_IHM);
+        FLAG_DISPLAY == false;
+        Commande_IHM = 0;
+        Au_Dodo();
     } else if (0 == strcmp(com, "ARNSRS_ID")) {
-        OUTPUT("Changement de l'ID de l'appareil pour le N°: %s?\r\n", numb);
+        OUTPUT("  Changement de l'ID de l'appareil pour le N°: %s?\r\n", numb);
         UTILS::Store_A_Val(atoi(numb), "ARNSRS_ID");
     } else if (0 == strcmp(com, "monit")) {
         FLAG_AFF = false;
@@ -543,6 +589,7 @@
         UTILS::Clean_Flash_All();
     } else if (0 == strcmp(com, "dir")) {
         FLAG_WINDEV = false;
+        wait(0.5);
         OUTPUT("$\r\n");
         wait(0.5);
         UTILS::Dir_Flash(&serialMonit);
@@ -599,32 +646,32 @@
         Kc = atof(numb);
         control_Servo.reset();
         control_Servo.setTunings(Kc, Ti, Td);
-        OUTPUT("  MAJ PID -->  Kc = %f   Ti = %f   Td = %f?\r\n", Kc, Ti, Td);
+        OUTPUT("  MAJ PID -->  Kc = %f   Ti = %f   Td = %f\r\n", Kc, Ti, Td);
     } else if (0 == strcmp(com, "Ti")) {
         Ti = atof(numb);
         control_Servo.reset();
         control_Servo.setTunings(Kc, Ti, Td);
-        OUTPUT("  MAJ PID -->  Kc = %f   Ti = %f   Td = %f?\r\n", Kc, Ti, Td);
+        OUTPUT("  MAJ PID -->  Kc = %f   Ti = %f   Td = %f\r\n", Kc, Ti, Td);
     } else if (0 == strcmp(com, "Td")) {
         Td = atof(numb);
         control_Servo.reset();
         control_Servo.setTunings(Kc, Ti, Td);
-        OUTPUT("  MAJ PID -->  Kc = %f   Ti = %f   Td = %f?\r\n", Kc, Ti, Td);
+        OUTPUT("  MAJ PID -->  Kc = %f   Ti = %f   Td = %f\r\n", Kc, Ti, Td);
     } else if (0 == strcmp(com, "Cons")) {
         consigne = atoi(numb);
         control_Servo.setSetPoint(consigne);
-        OUTPUT("  MAJ CONSIGNE PID -->  Consigne = %d?\r\n", consigne);
+        OUTPUT("  MAJ CONSIGNE PID -->  Consigne = %d\r\n", consigne);
     } else if (0 == strcmp(com, "dfu")) {
         OUTPUT("  Passage en DFU...\r\n");
         FLAG_DFU = true;
     } else if (0 == strcmp(com, "PID")) {
         if (FLAG_PID == 1) {
             control_Servo.setMode(MANUAL_MODE);
-            OUTPUT("  PID OFF?\r\n");
+            OUTPUT("  PID OFF\r\n");
             FLAG_PID = 0;
         } else if (FLAG_PID == 0) {
             control_Servo.setMode(AUTO_MODE);
-            OUTPUT("  PID ON?\r\n");
+            OUTPUT("  PID ON\r\n");
             FLAG_PID = 1;
         }
     } else {
@@ -637,6 +684,33 @@
     newParamFlag = false;
 }
 
+void Decoding_IHM(char message [])
+{
+
+    char com[20] = "";
+    char numb[30] = "";
+
+    sscanf(message,"%s %s",&com , &numb);
+
+    DEEP_DEBUG("\r\n  Commande = %s       Valeur = %s \r\n\r\n", com, numb);
+
+    OUTPUT("?\r\n");
+
+    if (0 == strcmp(com, "1")) {
+        
+    } else if (0 == strcmp(com, "2")) {
+        
+    } else if (0 == strcmp(com, "3")) {
+
+    } else {
+        
+    }
+
+    strcpy(IHM," ");
+    indexIHM = 0;
+    newIHMFlag = false;
+}
+
 void Create_File_Name_Date()
 {
     //Du nom du fichier Date / heure
@@ -656,24 +730,20 @@
 
 
 
-
-
-
-
 int main()
 {
 
     HAL_Init();
 
-
     __HAL_RCC_PWR_CLK_ENABLE();
     HAL_PWR_EnableBkUpAccess();
 
-
-
+    E5V = 1;
 
+    buzzer.beep(1000,0.5);
+    
     //Ci-dessous commande pour formater une nouvelle carte
-    UTILS::Format_Flash();
+    //UTILS::Format_Flash();
 
     //Montage Flash
     UTILS::Mount_Flash();
@@ -751,19 +821,11 @@
 
     wait(1);
 
-    Thread thread_Volets(osPriorityNormal);
-
-    thread_Volets.start(callback(GO_TO_thread));
-
-    //OUTPUT("  Contrôle des volets démarré\r\n\r\n");
-
-    wait(1);
-
     Thread thread_Secu(osPriorityNormal);
 
     thread_Secu.start(callback(SECU_thread));
 
-    //OUTPUT("  Contrôle des paramètres de sécu démarré\r\n\r\n");
+    DEBUG("  Contrôle des paramètres de sécu démarré\r\n\r\n");
 
     wait(1);
 
@@ -771,10 +833,16 @@
 
     thread_Head.start(callback(SENSORS_thread));
 
-    //OUTPUT("  Tête capteurs démarrée\r\n\r\n");
+    DEBUG("  Tête capteurs démarrée\r\n\r\n");
 
     wait(1);
 
+    Thread thread_Volets(osPriorityNormal);
+
+    thread_Volets.start(callback(GO_TO_thread));
+
+    DEBUG("  Contrôle des volets démarré\r\n\r\n");
+
     //Init PID
     //Entrée PPO2 entre 100 et 1000 mb
     control_Servo.setInputLimits(Min_Input, Max_Input);
@@ -785,7 +853,7 @@
     //Consigne à x mb
     control_Servo.setSetPoint(consigne);
 
-    //OUTPUT("  Cliquez sur le bouton help pour voir la liste des \r\n  commandes administrateur disponibles.\r\n");
+    DEBUG("  Cliquez sur le bouton help pour voir la liste des \r\n  commandes administrateur disponibles.\r\n");
 
 
     OUTPUT("$\r\n");
@@ -793,13 +861,15 @@
     UTILS::Dir_Flash(&serialMonit);
 
     serialMonit.attach(&callbackParam);
+    
+    display.attach(&callbackIHM);
 
-    vusb_off_on.fall(&Stop_Sequence);
+    vusb_off_on.fall(&USB_unpluged);
 
-    E5V = 1;
-
-    buzzer.beep(1000,0.5);
-
+    buzzer.beep(1000,0.3);
+    wait_ms(500);
+    buzzer.beep(1000,0.3);
+    
     while (true) {
 
         //Démarrage du Timer mesurant le temps d'éxecution du code
@@ -809,14 +879,19 @@
             DEEP_DEBUG("  From PC = %s\r\n", param);
             Decoding_Message(param);
         }
+        
+        if (newIHMFlag) {
+            DEEP_DEBUG("  From IHM = %s\r\n", IHM);
+            Decoding_IHM(IHM);
+        }
 
         //Fabrication de la chaine Date / heure
         seconds = time(NULL);
         char Time_buf[32];
         strftime(Time_buf, 32, "%D %I-%M-%S ", localtime(&seconds));
 
-        //Fabrication de la chaine à enregistrer
-        sprintf(to_store,"<%s;%d;%d;%.2f;%d;%.2f;%.2f;%d;%d;%d;%.2f;%.2f;%d;%.3f;%.3f;%.3f;%d>",
+        //Fabrication de la chaine de com
+        sprintf(to_store,"<%s;%d;%d;%.2f;%d;%.2f;%.2f;%d;%d;%d;%.2f;%.2f;%d;%.3f;%.3f;%.3f;%d;%d>",
                 Time_buf,
                 co2,
                 ppO2,
@@ -833,7 +908,8 @@
                 Kc,
                 Ti,
                 Td,
-                consigne
+                consigne,
+                Commande_IHM
                );
 
 
@@ -849,9 +925,7 @@
 
         //Vers le moniteur série
         if (FLAG_AFF) {
-            NVIC_DisableIRQ(USART3_IRQn); // USART 2 si ARNSRS mais plus utilisé
             Affichage();
-            NVIC_EnableIRQ(USART3_IRQn); // USART 2 si ARNSRS mais plus utilisé
         }
 
         //Enregistrement de la chaine
@@ -884,7 +958,7 @@
             OUTPUT("  Reboot...\r\n");
             HAL_NVIC_SystemReset;
         }
-        
+
         //Update du PID
         control_Servo.setProcessValue(ppO2);
         //Nouvelle sortie servo fuite si on est pas en mode SECU