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:
57:a0827fa46b03
Parent:
56:61b0f4d6a890
Child:
58:694d4d00407c
--- a/main.cpp	Tue Jun 12 10:42:12 2018 +0000
+++ b/main.cpp	Tue Jun 26 09:45:56 2018 +0000
@@ -31,11 +31,9 @@
 #define PIN_V_PILES_DIGI PC_3
 #define PIN_V_USB_DIGI PA_0
 
-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(OTG_FS_IRQn);serialMonit.printf(__VA_ARGS__); fflush(stdout);NVIC_EnableIRQ(OTG_FS_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);}
 
@@ -44,15 +42,15 @@
 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 serial(PA_2, PA_3,115200); // UART2 à décommenter pour DEBUG et Deep DEBUG
+//Communication USB, OTG_FS
+USBSerial serialMonit(0x0483, 0x5740, 0x0001, false);
+//USBSerial serialMonit;//Pour pilotes windows Olivier
 
-//Communication avec l'IHM
-Serial display(PC_4, PC_5, 115200);
+//Moniteur série debug, Serial 2
+Serial serial(PA_2, PA_3,115200); // UART2 à décommenter / commenter pour DEBUG et Deep DEBUG
 
-//Moniteur pour le debug avec une nucleo, a commenter avec la carte definitive
-//Serial serial(USBTX, USBRX, 115200);
+//Communication avec l'IHM, Serial 3
+Serial display(PC_4, PC_5, 115200);
 
 //Init de la lib ARNSRS;
 SENSOR_HEAD_REV_B sensors;
@@ -166,21 +164,46 @@
 InterruptIn vpile_off_on(PIN_V_PILES_DIGI);
 InterruptIn vusb_off_on(PIN_V_USB_DIGI);
 
-//Pin du régulateur
-DigitalOut E5V (PA_4);
+//Pin enable du régulateur 5 v
+DigitalOut E5V(PA_4);
+
+//Pin enable du régulateur 3.3 v
+DigitalOut E3V(PB_11);
+
+//Pin du Buzzer
+Beep buzzer(PC_8);
+
+
+    /*
+
+    Pour mémoire, les réglage de priorité des thread
 
-//Buzzer
-Beep buzzer(PC_8);
+          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
+    */
+    
+//Déclaration des Threads    
+Thread thread_Secu(osPriorityLow);
+Thread thread_Head(osPriorityLow);
+Thread thread_Volets_POUMON(osPriorityLow);
+Thread thread_Volets_FUITE(osPriorityLow);
 
 void Au_Dodo()
 {
     buzzer.beep(1000,0.3);
-    wait_ms(700);
+    wait_ms(500);
     buzzer.beep(1000,0.3);
-    wait_ms(700);
-    buzzer.beep(1000,0.5);
     wait(1);
     
+    E5V = 0;
+    E3V = 0;
+    
     //Mise en veille
     HAL_PWREx_EnablePullUpPullDownConfig() ;
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
@@ -203,12 +226,19 @@
     //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;
     
+    DigitalOut Screen(PC_4);
+    Screen = 0;
+    */
+    
+    E3V = 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");
@@ -217,7 +247,26 @@
     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);
+
+/*
+    __disable_irq();
+
+    thread_Secu.terminate();
+
+    wait(300);
+
+    thread_Head.terminate();
+
+    wait(300);
     
+    thread_Volets_POUMON.terminate();
+    
+    wait(300);
+    
+    thread_Volets_FUITE.terminate();
+    
+    wait(300);
+*/    
     //ejection de la flash pour pas crasher le system de fichiers
     UTILS::UnMount_Flash();
     
@@ -254,8 +303,6 @@
     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. */
         DEBUG("  Impossible de changer BFB2.\n");
     } else {
         DEBUG("  BFB2 a été changé.\n");
@@ -304,16 +351,15 @@
     Consigne_poumon = HOME_SERVO_POUMON;
     Consigne_fuite = HOME_SERVO_FUITE;
 
-    Volets_Speed = 0.1;
+    Volets_Speed = 1;
     Volet_DeadBand = 10;
 
-    /*
+    
         while(1) {
             wait_ms(100);
-            if (Servo_Poumon.Pos_OK() == true) break;
-            if (Servo_Fuite.Pos_OK() == true) break;
+            if (Servo_Poumon.Pos_OK() == true && Servo_Fuite.Pos_OK() == true) break;
         }
-    */
+    
 
     OUTPUT("  Appareil en mode SECU\r\n");
 
@@ -347,6 +393,7 @@
     Mode_SECU();
 
     wait(1);
+    
     //ejection de la flash pour pas crasher le system de fichiers
     UTILS::UnMount_Flash();
 
@@ -434,29 +481,36 @@
     }
 }
 
-void GO_TO_thread()
+void GO_TO_thread_POUMON()
 {
     while (true) {
-        //DEEP_DEBUG("  GO_TO_Thread\r\n");
+        //DEEP_DEBUG("  GO_TO_Thread_POUMON\r\n");
 
         //Servo_Poumon.Go_To_Prop(Consigne_poumon);
         //Servo_Poumon.Go_To_PID(Consigne_poumon, Volet_DeadBand);
         Servo_Poumon.Go_To(Consigne_poumon, Volets_Speed, Volet_DeadBand);
+    }
+}
+
+void GO_TO_thread_FUITE()
+{
+    while (true) {
+        //DEEP_DEBUG("  GO_TO_Thread_FUITE\r\n");
 
         //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 );
+        Servo_Fuite.Go_To(Consigne_fuite, Volets_Speed, Volet_DeadBand);
     }
 }
 
 void SECU_thread()
 {
     while (true) {
-
+    //Mettre toutes les vérifs de sécu....
     }
 }
 
-//Callback de l'intérruption des envois de commandes depuis le terminal
+//Callback de l'intérruption des envois de commandes depuis le terminal / WINDEV
 void callbackParam()
 {
 
@@ -515,8 +569,9 @@
         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;
+        FLAG_DISPLAY = false;
         Commande_IHM = 0;
+        wait(300);
         Au_Dodo();
     } else if (0 == strcmp(com, "ARNSRS_ID")) {
         OUTPUT("  Changement de l'ID de l'appareil pour le N°: %s?\r\n", numb);
@@ -638,6 +693,7 @@
         DEBUG("  Servo Fuite     =  %f\r\n", Consigne_fuite);
     } else if (0 == strcmp(com, "reset")) {
         FLAG_REC = false;
+        Mode_SECU();
         UTILS::UnMount_Flash();
         OUTPUT("  Reset de l'appareil.\r\n");
         wait(1);
@@ -739,56 +795,33 @@
     HAL_PWR_EnableBkUpAccess();
 
     E5V = 1;
+    E3V = 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();
 
-    //Liste des fichiers sur la Flash
-    //UTILS::Dir_Flash(&serialMonit);
-
     if (UTILS::File_Exist("ARNSRS_ID.sys") == false) {
         UTILS::Store_A_Val(000, "ARNSRS_ID.sys");
         DEBUG("ARNSRS ID forcée à 000\r\n");
     }
-    /*
-    while(1){
-    OUTPUT("\r\n\r\n  Démarrage de l'appareil, veuillez patienter...\r\n\r\n");}
-    */
+    
+    DEBUG("\r\n\r\n  Démarrage de l'appareil, veuillez patienter...\r\n\r\n");
+    
 
     //Vérification RTC, si on est le 01/01/70, c'est qu'il y a un problème...
     seconds = time(NULL);
     char YEAR[10];
     strftime(YEAR, 10, "%D", localtime(&seconds));
-    if (0 == strcmp(YEAR, "01/01/70")); //OUTPUT("  Vous devez régler la RTC...\r\n\r\n");
+    if (0 == strcmp(YEAR, "01/01/70")); DEBUG("  Vous devez régler la RTC...\r\n\r\n");
 
     bool calib_O2 = false;
     bool calib_CO2 = false;
 
-    /*
-    Par défaut les valeur en cas de calibration sur true sont les suivant
-
-       nbCalibO2 = 5
-       Mode = SPOOLING
-       Filtre = DIGI_FILTER32
-       CalibrationCO2 = "CALIB_AIR"
-
-       Parfois la calibration du Cozir coince...faire reset et relancer...
-
-       Pour calibrer avec ces paramètres :
-
-       sensors.Sensors_INIT(true, true);
-
-       Pour changer utiliser la syntaxe suivante :
-
-       sensors.Sensors_INIT(true, true, true, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR);
-
-    */
-
     sensors.Sensors_INIT(calib_O2, calib_CO2);
 
     wait(1);
@@ -803,43 +836,27 @@
     Servo_Poumon.Init("Servo_Poumon.sys");
     Servo_Fuite.Init("Servo_Fuite.sys");
 
-    //OUTPUT("  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
-    */
+    DEBUG("  Demarrage des threads...\r\n\r\n");
 
     wait(1);
 
-    Thread thread_Secu(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 thread_Head(osPriorityNormal);
-
     thread_Head.start(callback(SENSORS_thread));
 
     DEBUG("  Tête capteurs démarrée\r\n\r\n");
 
     wait(1);
 
-    Thread thread_Volets(osPriorityNormal);
+    thread_Volets_POUMON.start(callback(GO_TO_thread_POUMON));
+    
+    wait(1);
 
-    thread_Volets.start(callback(GO_TO_thread));
+    thread_Volets_FUITE.start(callback(GO_TO_thread_FUITE));
 
     DEBUG("  Contrôle des volets démarré\r\n\r\n");
 
@@ -848,14 +865,15 @@
     control_Servo.setInputLimits(Min_Input, Max_Input);
     //Sortie servo entre 0 et 100 %
     control_Servo.setOutputLimits(Min_Output, Max_Output);
-    //Mode auto au démarrage
-    control_Servo.setMode(MANUAL_MODE);
+    //Reglage de l'interval du PID
+    control_Servo.setInterval(RATE_PID);
     //Consigne à x mb
     control_Servo.setSetPoint(consigne);
+    //Mode auto au démarrage
+    control_Servo.setMode(MANUAL_MODE);
 
     DEBUG("  Cliquez sur le bouton help pour voir la liste des \r\n  commandes administrateur disponibles.\r\n");
 
-
     OUTPUT("$\r\n");
     wait(0.5);
     UTILS::Dir_Flash(&serialMonit);
@@ -910,7 +928,7 @@
                 Td,
                 consigne,
                 Commande_IHM
-               );
+                );
 
 
         //Pour windev
@@ -946,12 +964,13 @@
         if (FLAG_DFU) {
             FLAG_DFU = false;
             FLAG_REC = false;
-            //__disable_irq();
             thread_Secu.terminate();
             wait_ms(300);
             thread_Head.terminate();
             wait_ms(300);
-            thread_Volets.terminate();
+            thread_Volets_POUMON.terminate();
+            wait_ms(300);
+            thread_Volets_FUITE.terminate();
             wait_ms(300);
             UTILS::UnMount_Flash();
             FLASH_set_boot_bank(2);
@@ -977,8 +996,6 @@
         } else {
             RATE_TRUE = 0;
 
-            //control_Servo.setInterval(RATE);
-
             DEEP_DEBUG("Pour ralentir le code, Ref_Time doit être supérieur à %f seconde(s)\r\n\n", RATE);
         }