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:
71:23e7435e236f
Parent:
70:0ae5fd4f826c
Child:
72:6d5590dbe13e
diff -r 0ae5fd4f826c -r 23e7435e236f main.cpp
--- a/main.cpp	Fri Jul 13 06:05:47 2018 +0000
+++ b/main.cpp	Mon Jul 16 18:24:08 2018 +0000
@@ -93,6 +93,9 @@
 //Flag si on débranche l'USB
 bool USB_UNPLUGGED = false;
 
+//Flag si on débranche les piles
+bool PILES_UNPLUGGED = false;
+
 //Variables de stockage des infos capteurs
 int co2 = 0;
 float pression = 0;
@@ -195,15 +198,17 @@
 {
 
     //Enregistrement position des servos sans mettre en sécu
-    UTILS::EffacePage(2, 31, 1);
+    UTILS::EffacePage(2, PAGE_POUMON_ADDR, 1);
 
-    uint64_t Pos_P = Servo_Poumon.getPulses();
+    int Pos_P = Servo_Poumon.getPulses();
     UTILS::Write(VOLET_POUMON_ADDR, Pos_P);
-    DEBUG("  position volet poumon sauvegardée = %d pulse(s)\r\n", (int) Pos_P);
-
-    uint64_t Pos_F = Servo_Fuite.getPulses();
+    DEBUG("  position volet poumon sauvegardée = %d pulse(s)\r\n", Pos_P);
+    
+    UTILS::EffacePage(2, PAGE_FUITE_ADDR, 1);
+    
+    int Pos_F = Servo_Fuite.getPulses();
     UTILS::Write(VOLET_FUITE_ADDR, Pos_F);
-    DEBUG("  position volet fuite sauvegardée = %d pulse(s)\r\n", (int) Pos_F);
+    DEBUG("  position volet fuite sauvegardée = %d pulse(s)\r\n", Pos_F);
 
 }
 
@@ -231,7 +236,7 @@
     
     UTILS::EffacePage(2, 29, 1);
 
-    UTILS::Write(DFU_ADDR, 0xBABEFACE);
+    UTILS::Write(FLAG_ADDR, 0xBABEFACE);
 
     //Mise en veille
     HAL_PWREx_EnablePullUpPullDownConfig() ;
@@ -248,85 +253,52 @@
     //Enter shutdown mode
     HAL_PWREx_EnterSHUTDOWNMode();
 }
-
-// gestion du cas où le sleep est demandé volontairement
-// et où l'utilisateur coupe ensuite l'alimentation en débranchant ou interrupteur sur off
-// on refait un shutdown mais avec un wake up sur le branchement du cable + interrupteur sur ON
-void flagSleep()
-{
-
-    if ((int) UTILS::Read(DFU_ADDR) == 0xBABEFACE ) {
-        DEBUG("  Re démarrage sur BABEFACE.\n");
-
-        UTILS::EffacePage(2, 29, 1);
-
-        // mise à zero UART IHM pour éviter son redémarrage intempestif (le tx suffirait)
-        HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_4);
-        HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_5);
-
-        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 );
-
-        DEBUG("  Mise en veille.\n");
-
-        //Enter shutdown mode
-        HAL_PWREx_EnterSHUTDOWNMode();
-
-    }
-
-}
  
 void USB_is_unplugged() 
 {
     USB_UNPLUGGED = true;
     }
-       
+
 void USB_unplugged()
 {
     DEBUG("  La prise USB a été débranchée.\n");
-
+    
+    buzzer.beep(1000,0.1);
+  
     //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;
-
+    
     // mise à zero UART IHM pour éviter son redémarrage intempestif (le tx suffirait)
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_4);
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_5);
     
-    FLAG_PID = 0;
+    //On coupe les thread car on va utiliser une autre fonction
+    thread_Volets_POUMON.terminate();
+    thread_Volets_FUITE.terminate();
 
+    //A fond et avec une erreur tolérée de +- 5 deg autour du Home
+    Volets_Speed = 1;
+    Volet_DeadBand = 10;
+    
     Consigne_poumon = HOME_SERVO_POUMON;
     Consigne_fuite = HOME_SERVO_FUITE;
-
-    while(1) {
-        wait_ms(100);
-        if (Servo_Poumon.Pos_OK() == true && Servo_Fuite.Pos_OK() == true) break;
-    }
-
-    OUTPUT("  Appareil en mode SECU\r\n");
+    
+    Servo_Fuite.Go_To(Consigne_fuite, Volets_Speed, Volet_DeadBand);
+        
+    Servo_Poumon.Go_To(Consigne_poumon, Volets_Speed, Volet_DeadBand);
+        
+    DEBUG("  Appareil en mode SECU\r\n");
+    
+    E5V = 0;
     
     save_position();
-       
-    wait(1);
-    
-    //Arrêt des enregistrements
-    FLAG_REC = false;
-
+ 
     UTILS::UnMount_Flash();
-
+ 
     //Mise en veille version cable arraché
     DEBUG("  Mise en veille...\r\n");
-       
+      
     //Mise en veille
     HAL_PWREx_EnablePullUpPullDownConfig() ;
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
@@ -338,40 +310,75 @@
     // 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(); 
-      
+    E3V=0;
+    HAL_PWREx_EnterSHUTDOWNMode(); // n’est pas exécuté à cause du E3V=0 précédent
+     
 }
 
+void Piles_are_unplugged() 
+{
+    PILES_UNPLUGGED = true;
+    }
+            
 void PILES_unplugged()
 {
-    DEBUG("  Les piles ont été débranchées.\n");
-
-    buzzer.beep(1000,0.3);
-    wait_ms(500);
-    buzzer.beep(1000,0.3);
-    wait(1);
-
-    //Arrêt des enregistrements
-    FLAG_REC = false;
-
-    //Ejection flash
-    UTILS::UnMount_Flash();
-   
-    //Enregistrement position des servos sans mettre en sécu
-    save_position();
-
+    DEBUG("  La prise USB a été débranchée.\n");
+    
+    buzzer.beep(1000,0.1);
+  
     //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;
+    
+    // mise à zero UART IHM pour éviter son redémarrage intempestif (le tx suffirait)
+    HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_4);
+    HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_5);
+    
+    //On coupe les thread car on va utiliser une autre fonction
+    thread_Volets_POUMON.terminate();
+    thread_Volets_FUITE.terminate();
 
+    //A fond et avec une erreur tolérée de +- 5 deg autour du Home
+    Volets_Speed = 1;
+    Volet_DeadBand = 5;
+    
+    Consigne_poumon = HOME_SERVO_POUMON;
+    Consigne_fuite = HOME_SERVO_FUITE;
+    
+    //Cette méthode n'oscile pas car le pwm reste alimenté...donc pas de multiples pics de démarrage
+    Servo_Fuite.Go_To(Consigne_fuite, Volets_Speed, Volet_DeadBand);
+    //Pas besoin du wait car comme on execute pas les goto dans des thread séparés, ils s'éxécuteront l'un après l'autre...je commence par la fuite
+    Servo_Poumon.Go_To(Consigne_poumon, Volets_Speed, Volet_DeadBand);
+     
+    DEBUG("  Appareil en mode SECU\r\n");
+    
+    E5V = 0;
+    
+    save_position();
+ 
+    UTILS::UnMount_Flash();
+ 
+    //Mise en veille version cable arraché
+    DEBUG("  Mise en veille...\r\n");
+      
     //Mise en veille
-    DEBUG("  Mise en veille...\r\n");
-    //wait(1);
-    sleepDemande();
+    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
+    E3V=0;
+    HAL_PWREx_EnterSHUTDOWNMode(); // n’est pas exécuté à cause du E3V=0 précédent
+     
     
 }
 
@@ -610,6 +617,7 @@
     while (true) {
         //Mettre toutes les vérifs de sécu....
         if (USB_UNPLUGGED) USB_unplugged();
+        if (PILES_UNPLUGGED) PILES_unplugged();
     }
 }
 
@@ -676,14 +684,15 @@
             OUTPUT("  Démarrage du tracé Arduino.\r\n");
         }       
     } else if (0 == strcmp(com, "PING")) {
-        Commande_IHM = 1;
+        /*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;
+        Commande_IHM = 0;*/
+        E3V = 0;
     } else if (0 == strcmp(com, "ARNSRS_ID")) {
         OUTPUT("  Changement de l'ID de l'appareil pour le N°: %s\r\n", numb);
         //UTILS::Store_A_Val(atoi(numb), "ARNSRS_ID");
-        UTILS::EffacePage(2, 30, 1);
+        UTILS::EffacePage(2, PAGE_ID_ADDR, 1);
         UTILS::Write(ID_ARNSRS_ADDR, (uint64_t) atoi(numb));
     } else if (0 == strcmp(com, "monit")) {
         FLAG_AFF = false;
@@ -807,7 +816,7 @@
         DEBUG("  Servo Fuite     =  %f\r\n", Consigne_fuite);
     } else if (0 == strcmp(com, "reset")) {
         FLAG_REC = false;
-        Mode_SECU();
+        //Mode_SECU();
         UTILS::UnMount_Flash();
         OUTPUT("  Reset de l'appareil.\r\n");
         wait(1);
@@ -836,8 +845,8 @@
         FLAG_REC = false;
         Mode_SECU();
         UTILS::UnMount_Flash();
-        UTILS::EffacePage(2, 29, 1);
-        UTILS::Write(DFU_ADDR, 0xDEADBEEF);
+        UTILS::EffacePage(2, PAGE_FLAG_ADDR, 1);
+        UTILS::Write(FLAG_ADDR, 0xDEADBEEF);
         wait(1);
         NVIC_SystemReset();
     } else if (0 == strcmp(com, "PID")) {
@@ -873,9 +882,10 @@
     if (0 == strcmp(com, "USB")) {
         OUTPUT("  Passage en Mode Transfert Flash To USB...\r\n");
         FLAG_REC = false;
-        Mode_SECU();
+        //Mode_SECU();
         UTILS::UnMount_Flash();
         UTILS::FLASH_set_boot_bank(2);
+        NVIC_SystemReset();
     } else if (0 == strcmp(com, "2")) {
 
     } else if (0 == strcmp(com, "3")) {
@@ -909,7 +919,7 @@
 
 int main()
 {
-    flagSleep(); // on vérifie si la demande de sleep n'est pas active (permet de redémarrer sur un front haut)
+    UTILS::Flag_Sleep(); // on vérifie si la demande de sleep n'est pas active (permet de redémarrer sur un front haut)
 
     HAL_Init();
     __HAL_RCC_PWR_CLK_ENABLE();
@@ -1051,7 +1061,7 @@
 
     vusb_off_on.fall(&USB_is_unplugged);
 
-    vpiles_off_on.fall(&PILES_unplugged);
+    vpiles_off_on.fall(&Piles_are_unplugged);
 
     buzzer.beep(1000,0.3);
     wait_ms(500);
@@ -1078,7 +1088,7 @@
         strftime(Time_buf, 32, "%D %I-%M-%S ", localtime(&seconds));
 
         //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>",
+        sprintf(to_store,"<%s;%d;%d;%.1f;%d;%.2f;%.2f;%d;%d;%d;%.2f;%.2f;%d;%.3f;%.3f;%.3f;%d;%d>",
                 Time_buf,
                 co2,
                 ppO2,
@@ -1139,10 +1149,8 @@
         //Update du PID
         if (FLAG_PID == 1 && EN_MODE_SECU == false) {
             control_Servo.setProcessValue(ppO2);
-            //pour visu sur graph IDE arduino
-            //DEBUG("ppo2 : %d , consigne : %d \r\n",ppO2, consigne);
             //Nouvelle sortie servo poumon si on est pas en mode SECU
-            Consigne_poumon = 90 - control_Servo.compute();
+            Consigne_poumon = control_Servo.compute();
             // mode volets asservis (simule un seul moteur)
             Consigne_fuite = Consigne_poumon - 90;
         }