mbed-os github

Dependencies:   ADS1015 Faulhaber HTU21D_mod MS5837_potless Sensor_Head_RevB_3 USBDevice_dfu Utilsdfu beep

Fork of ARNSRS_testDFU by POTLESS

Files at this revision

API Documentation at this revision

Comitter:
POTLESS_2
Date:
Thu Jul 12 10:01:34 2018 +0000
Parent:
66:fa183b065efb
Child:
69:9afe54d53bb0
Commit message:
commande save_os et option traceur arduino, thread au lieu d'interruption sur le d?branchage USB

Changed in this revision

Faulhaber.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Faulhaber.lib	Wed Jul 11 12:38:36 2018 +0000
+++ b/Faulhaber.lib	Thu Jul 12 10:01:34 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/POTLESS/code/Faulhaber/#7984300c2441
+https://os.mbed.com/teams/POTLESS/code/Faulhaber/#d4b6e4ae329e
--- a/main.cpp	Wed Jul 11 12:38:36 2018 +0000
+++ b/main.cpp	Thu Jul 12 10:01:34 2018 +0000
@@ -34,6 +34,7 @@
 //#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(OTG_FS_IRQn);serialMonit.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(OTG_FS_IRQn);}
+#define TRACEUR(...) { serialMonit.printf(__VA_ARGS__); fflush(stdout);}
 #define OUTPUT(...) { serialMonit.printf(__VA_ARGS__); fflush(stdout);}
 #define IHM(...) { NVIC_DisableIRQ(USART3_IRQn); display.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(USART3_IRQn);}
 
@@ -42,7 +43,7 @@
 //USBSerial serialMonit;//Pour pilotes windows Olivier
 
 //Moniteur série debug, Serial 2
-Serial serial(PA_2, PA_3,115200); // UART2 à décommenter / commenter pour DEBUG et Deep DEBUG
+Serial serial(PA_2, PA_3,115200); // UART2 à décommenter / commenter pour DEBUG et DEEP_DEBUG
 
 //Communication avec l'IHM, Serial 3
 Serial display(PC_4, PC_5, 115200);
@@ -73,6 +74,9 @@
 //Flag pour interrompre l'affichage si on veut...
 bool FLAG_AFF = false;
 
+//Flag pour interrompre l'affichage si on veut...
+bool FLAG_TRACE = false;
+
 //Flag pour envoyer à l'app windev...
 bool FLAG_WINDEV = false;
 
@@ -85,6 +89,7 @@
 //Flag pour interrompre les demandes O2 en cours ed calibration...
 bool FLAG_O2 = true;
 
+
 //Variables de stockage des infos capteurs
 int co2 = 0;
 float pression = 0;
@@ -162,7 +167,10 @@
 
 //Interruption pin
 InterruptIn vpiles_off_on(PIN_V_PILES_DIGI);
-InterruptIn vusb_off_on(PIN_V_USB_DIGI);
+//InterruptIn vusb_off_on(PIN_V_USB_DIGI);
+
+//Pin connexion USB
+DigitalIn vusb(PIN_V_USB_DIGI);
 
 //Pin enable du régulateur 5 v
 DigitalOut E5V(PA_4);
@@ -179,6 +187,23 @@
 Thread thread_Volets_POUMON;
 Thread thread_Volets_FUITE;
 
+void save_position()
+{
+
+    //Enregistrement position des servos sans mettre en sécu
+    UTILS::EffacePage(2, 31, 1);
+
+    uint64_t 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();
+    UTILS::Write(VOLET_FUITE_ADDR, Pos_F);
+    DEBUG("  position volet fuite sauvegardée = %d pulse(s)\r\n", (int) Pos_F);
+
+}
+
+
 void sleepDemande()
 {
     buzzer.beep(1000,0.1);
@@ -189,22 +214,22 @@
     wait_ms(100);
     buzzer.beep(1000,0.1);
     wait(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;
-    
+
     // 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);
-    
+
     E5V = 0;
-    
+
     UTILS::EffacePage(2, 29, 1);
-    
+
     UTILS::Write(DFU_ADDR, 0xBABEFACE);
-    
+
     //Mise en veille
     HAL_PWREx_EnablePullUpPullDownConfig() ;
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
@@ -216,25 +241,26 @@
     // 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();
 }
 
-// gestion du cas où le sleep est demandé volontairement 
+// 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(){
-    
+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
@@ -245,20 +271,20 @@
         // 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_unplugged()
 {
     DEBUG("  La prise USB a été débranchée.\n");
-    
+
     buzzer.beep(1000,0.1);
     wait_ms(100);
     buzzer.beep(1000,0.1);
@@ -273,29 +299,36 @@
     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);
-        
-    E5V = 0;
-     
+    
+    FLAG_PID = 0;
+
+    Consigne_poumon = HOME_SERVO_POUMON;
+    Consigne_fuite = HOME_SERVO_FUITE;
+
+    Volets_Speed = 1;
+    Volet_DeadBand = 10;
+
+    while(1) {
+        wait_ms(100);
+        if (Servo_Poumon.Pos_OK() == true && Servo_Fuite.Pos_OK() == true) break;
+    }
+
+
+    OUTPUT("  Appareil en mode SECU\r\n");
+    
+    save_position();
+       
+    wait(1);
+    
     //Arrêt des enregistrements
     FLAG_REC = false;
 
     UTILS::UnMount_Flash();
-    
-    //Enregistrement position des servos sans mettre en sécu
-    UTILS::EffacePage(2, 31, 1);
-    
-    uint64_t 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();
-    UTILS::Write(VOLET_FUITE_ADDR, Pos_F);
-    DEBUG("  position volet fuite sauvegardée = %d pulse(s)\r\n", (int) Pos_F);
-      
     //Mise en veille version cable arraché
     DEBUG("  Mise en veille...\r\n");
     //wait(1);
@@ -310,43 +343,37 @@
     // 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 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
-    UTILS::EffacePage(2, 31, 1);
-    
-    uint64_t 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);
+    save_position();
 
-    uint64_t 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);
-    
     //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 en veille
     DEBUG("  Mise en veille...\r\n");
     //wait(1);
@@ -380,6 +407,31 @@
     fflush(stdout);
 }
 
+void Traceur_Arduino() {
+    
+    //Important pour PIG
+    TRACEUR("%d, ", ppO2);
+    TRACEUR("%d, ", consigne);
+    
+    /*
+    //TODO à commenter je pense...
+    TRACEUR("%d, "  , co2);
+    TRACEUR("%d, ", (int)OTU);
+    TRACEUR("%f, ", pression);
+    TRACEUR("%f, ", Temp1);
+    TRACEUR("%f, ", Temp2);
+    TRACEUR("%d, ", Humid);
+    TRACEUR("%d, "  , CellO2_1);
+    TRACEUR("%d, "  , CellO2_2);
+    TRACEUR("%f, "  , volet_poumon_Position);
+    TRACEUR("%f, "  , volet_fuite_Position);
+    */
+    
+    //Toujours finir par \r\n
+    TRACEUR("\r\n");
+    
+    
+    }
 
 //Passage en mode SECU
 void Mode_SECU()
@@ -399,8 +451,8 @@
         wait_ms(100);
         if (Servo_Poumon.Pos_OK() == true && Servo_Fuite.Pos_OK() == true) break;
     }
-    
-    
+
+
     OUTPUT("  Appareil en mode SECU\r\n");
 
     wait_ms(100);
@@ -409,16 +461,9 @@
 
     wait_ms(100);
 
-    UTILS::EffacePage(2, 31, 1);
-    //Enregistrement position des servos en sécu pour plus de précision
-    uint64_t 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);
+    save_position();
+    
     OUTPUT("  Volet poumon en sécu\r\n");
-
-    uint64_t 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);
     OUTPUT("  Volet fuite en sécu\r\n");
 
     Servo_Poumon.Sleep();
@@ -452,7 +497,7 @@
     pin.mode(PullDown);
 
     if(!pin) {
-        Stop_Sequence();
+        //Stop_Sequence();
         return 0;
     } else {
         return 1;
@@ -527,11 +572,11 @@
 {
     while (true) {
         //DEEP_DEBUG("  GO_TO_Thread_POUMON\r\n");
-       
+
         Servo_Poumon.Go_To_Prop(Consigne_poumon);
         //Servo_Poumon.Go_To_PID(Consigne_poumon, Volet_DeadBand);//Nécessite de déclarer un dernier terme true dans l'init du moteur
         //Servo_Poumon.Go_To(Consigne_poumon, Volets_Speed, Volet_DeadBand);
-        
+
     }
 }
 
@@ -543,7 +588,7 @@
         Servo_Fuite.Go_To_Prop(Consigne_fuite);
         //Servo_Fuite.Go_To_PID(Consigne_fuite, Volet_DeadBand);
         //Servo_Fuite.Go_To(Consigne_fuite, Volets_Speed, Volet_DeadBand);
-       
+
     }
 }
 
@@ -551,6 +596,7 @@
 {
     while (true) {
         //Mettre toutes les vérifs de sécu....
+        if (Power_Test(vusb) == 0) USB_unplugged();
     }
 }
 
@@ -606,6 +652,16 @@
 
     if (0 == strcmp(com, "secu")) {
         Mode_SECU();
+    } else if (0 == strcmp(com, "save_pos")) {
+        save_position();
+    } else if (0 == strcmp(com, "trace")) {
+            if (FLAG_TRACE) {
+            FLAG_TRACE = false;
+            OUTPUT("  Arrêt du tracé Arduino.\r\n");
+        } else {
+            FLAG_TRACE = true;
+            OUTPUT("  Démarrage du tracé Arduino. Fremez le moniteur et ouvrez le traceur.\r\n");
+        }       
     } else if (0 == strcmp(com, "PING")) {
         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);
@@ -801,8 +857,12 @@
 
     OUTPUT("?\r\n");
 
-    if (0 == strcmp(com, "1")) {
-
+    if (0 == strcmp(com, "USB")) {
+        OUTPUT("  Passage en Mode Transfert Flash To USB...\r\n");
+        FLAG_REC = false;
+        Mode_SECU();
+        UTILS::UnMount_Flash();
+        UTILS::FLASH_set_boot_bank(2);
     } else if (0 == strcmp(com, "2")) {
 
     } else if (0 == strcmp(com, "3")) {
@@ -837,36 +897,36 @@
 int main()
 {
     flagSleep(); // 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();
     HAL_PWR_EnableBkUpAccess();
 
     UTILS::DFU();
-    
+
     E5V = 1;
     E3V = 1;
 
     if (PIN_V_USB_DIGI) flag_USB = true;
     if (PIN_V_PILES_DIGI) flag_Piles = true;
 
-    UTILS::PVD_Config();
+    //UTILS::PVD_Config();
 
     buzzer.beep(1000,0.5);
 
     //Ci-dessous commande pour formater une nouvelle carte
-    UTILS::Format_Flash();
+    //UTILS::Format_Flash();
 
     //Montage Flash
     UTILS::Mount_Flash();
-    
+
     /*
         if (UTILS::File_Exist("ARNSRS_ID.sys") == false) {
             UTILS::Store_A_Val(000, "ARNSRS_ID.sys");
             DEBUG("ARNSRS ID forcée à 000\r\n");
         }
     */
-    
+
     //pour le premier lancement quand l'appareil n'a pas d'ID.
     //int ID = 0;
     //UTILS::Write(ADRESSE_ID, ID);
@@ -904,51 +964,51 @@
     */
 
     DEBUG("  Demarrage des threads...\r\n\r\n");
-    
-        /*
- 
+
+    /*
+
     Pour mémoire, les réglage de priorité des thread
- 
-          osPriorityIdle          = -3,          ///< priority: idle (lowest)
-          osPriorityLow           = -2,          ///< priority: low
-          osPriorityBelowNormal   = -1,          ///< priority: below normal
-          osPriorityNormal        =  0,          ///< priority: normal (default)
-          osPriorityAboveNormal   = +1,          ///< priority: above normal
-          osPriorityHigh          = +2,          ///< priority: high
-          osPriorityRealtime      = +3,          ///< priority: realtime (highest)
-          osPriorityError         =  0x84        ///< system cannot determine priority or thread has illegal priority
+
+      osPriorityIdle          = -3,          ///< priority: idle (lowest)
+      osPriorityLow           = -2,          ///< priority: low
+      osPriorityBelowNormal   = -1,          ///< priority: below normal
+      osPriorityNormal        =  0,          ///< priority: normal (default)
+      osPriorityAboveNormal   = +1,          ///< priority: above normal
+      osPriorityHigh          = +2,          ///< priority: high
+      osPriorityRealtime      = +3,          ///< priority: realtime (highest)
+      osPriorityError         =  0x84        ///< system cannot determine priority or thread has illegal priority
     */
- 
+
     thread_Volets_POUMON.set_priority(osPriorityHigh);
- 
+
     thread_Volets_POUMON.start(callback(GO_TO_thread_POUMON));
- 
+
     DEBUG("  Contrôle volet Poumon démarré\r\n\r\n");
- 
+
     wait(1);
-    
+
     thread_Volets_FUITE.set_priority(osPriorityHigh);
- 
+
     thread_Volets_FUITE.start(callback(GO_TO_thread_FUITE));
- 
+
     DEBUG("  Contrôle volet Fuite démarré\r\n\r\n");
- 
+
     wait(1);
- 
+
     thread_Secu.set_priority(osPriorityNormal);
- 
+
     thread_Secu.start(callback(SECU_thread));
- 
+
     DEBUG("  Contrôle des paramètres de sécu démarré\r\n\r\n");
- 
+
     wait(1);
- 
+
     thread_Head.set_priority(osPriorityNormal);
- 
+
     thread_Head.start(callback(SENSORS_thread));
- 
+
     DEBUG("  Tête capteurs démarrée\r\n\r\n");
- 
+
     wait(1);
 
 
@@ -973,11 +1033,11 @@
     serialMonit.attach(&callbackParam);
 
     display.attach(&callbackIHM);
-    
+
     OTU_Ticker.attach(&Calcul_OTU, 60);
-    
-    vusb_off_on.fall(&USB_unplugged);
-    
+
+    //vusb_off_on.fall(&USB_unplugged);
+
     vpiles_off_on.fall(&PILES_unplugged);
 
     buzzer.beep(1000,0.3);
@@ -1041,6 +1101,12 @@
         if (FLAG_AFF) {
             Affichage();
         }
+        
+        //Vers le traceur série
+        if (FLAG_TRACE) {
+            Traceur_Arduino();
+        }
+
 
         //Enregistrement de la chaine
         if (FLAG_REC) {