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:
76:9dc83d5cf075
Parent:
75:eec3e587c8b8
Child:
77:0cecc4adbab2
--- a/main.cpp	Thu Jul 19 12:10:08 2018 +0000
+++ b/main.cpp	Thu Jul 19 13:35:04 2018 +0000
@@ -189,7 +189,7 @@
 Thread thread_Head;
 Thread thread_Volets_POUMON;
 Thread thread_Volets_FUITE;
-
+Thread thread_PID;
 
 //Récap des fonctions
 void Save_Pos();//Sauvegarde des positions moteurs
@@ -226,9 +226,9 @@
     int Pos_P = Servo_Poumon.getPulses();
     UTILS::Write(VOLET_POUMON_ADDR, Pos_P);
     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", Pos_F);
@@ -249,16 +249,16 @@
     //A fond et avec une erreur tolérée de +- 5 deg autour du Home. Cohérent avec le future init qui recherche une erreur > 5 deg
     Volets_Speed = 1;
     Volet_DeadBand = 10;
-    
+
     Consigne_poumon = HOME_SERVO_POUMON;
     Consigne_fuite = HOME_SERVO_FUITE;
-    
+
     Servo_Fuite.Go_To(Consigne_fuite, Volets_Speed, Volet_DeadBand);
-        
+
     Servo_Poumon.Go_To(Consigne_poumon, Volets_Speed, Volet_DeadBand);
-    
+
     Save_Pos();
-    
+
     OUTPUT("  Volet poumon en sécu\r\n");
     OUTPUT("  Volet fuite en sécu\r\n");
 
@@ -271,27 +271,27 @@
     //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);
-    
+
     wait(0.5);
-    
+
     HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4|GPIO_PIN_5);
-    
+
     // mise à zero UART IHM pour éviter son redémarrage intempestif (le tx suffirait)
     HAL_PWREx_EnablePullUpPullDownConfig();
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_4);
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_C, PWR_GPIO_BIT_5);
-    
+
 }
 
 void Sleep_HIGHT_WakeUp()
 {
-        
+
     E5V = 0;
- 
+
     UTILS::UnMount_Flash();
- 
+
     DEBUG("  Mise en veille...\r\n");
-      
+
     //Mise en veille
     HAL_PWREx_EnablePullUpPullDownConfig() ;
     HAL_PWREx_EnableGPIOPullDown(PWR_GPIO_A, PWR_GPIO_BIT_0);
@@ -303,22 +303,22 @@
     // 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;
     //n’est pas exécuté à cause du E3V=0 précédent
     HAL_PWREx_EnterSHUTDOWNMode();
-     
+
 }
 
 void Sleep_LOW_WakeUp()
 {
     E5V = 0;
-    
+
     UTILS::UnMount_Flash();
- 
+
     DEBUG("  Mise en veille...\r\n");
-    
+
     UTILS::EffacePage(2, PAGE_FLAG_ADDR, 1);
 
     UTILS::Write(FLAG_ADDR, 0xBABEFACE);
@@ -334,39 +334,39 @@
     // 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;
     //Enter shutdown mode
     HAL_PWREx_EnterSHUTDOWNMode();
 }
- 
-void ALIM_is_unplugged() 
+
+void ALIM_is_unplugged()
 {
     ALIM_UNPLUGGED = true;
-    }
+}
 
 void ALIM_unplugged()
 {
     DEBUG("  L'alimentation a été débranchée.\n");
-    
+
     buzzer.beep(1000,0.1);
-  
+
     Sleep_IHM();
-    
+
     Mode_SECU();
-    
+
     Sleep_HIGHT_WakeUp();
-     
+
 }
 
 //Sequence d'arrêt demandé par commande "sleep"
 void Stop_Sequence()
 {
     OUTPUT("  Mise en veille de l'appareil.\r\n");
-    
+
     Sleep_IHM();
-    
+
     Mode_SECU();
 
     Sleep_LOW_WakeUp();
@@ -399,33 +399,34 @@
     fflush(stdout);
 }
 
-void Traceur_Arduino() {
-/*    
+void Traceur_Arduino()
+{
+    /*
+        //Important pour PID
+        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");
+    */
     //Important pour PID
-    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");
-*/    
-        //Important pour PID
     TRACEUR_DEBUG("%d, ", ppO2);
     TRACEUR_DEBUG("%d, ", consigne);
     TRACEUR_DEBUG("%f, "  , volet_poumon_Position);
-    
+
     /*
     //TODO à commenter je pense...
     TRACEUR_DEBUG("%d, "  , co2);
@@ -439,9 +440,9 @@
     TRACEUR_DEBUG("%f, "  , volet_poumon_Position);
     TRACEUR_DEBUG("%f, "  , volet_fuite_Position);
     */
-    
+
     //Toujours finir par \r\n
-    TRACEUR_DEBUG("\r\n");    
+    TRACEUR_DEBUG("\r\n");
 }
 
 
@@ -554,6 +555,20 @@
     }
 }
 
+void PID_thread()
+{
+    while (true) {
+        if (FLAG_PID == 1 && EN_MODE_SECU == false) {
+            control_Servo.setProcessValue(ppO2);
+            //Nouvelle sortie servo poumon si on est pas en mode SECU
+            Consigne_poumon = 90 - control_Servo.compute();
+            // mode volets asservis (simule un seul moteur)
+            Consigne_fuite = 90 - Consigne_poumon;
+            wait(RATE_PID);
+        }
+    }
+}
+
 //Callback de l'intérruption des envois de commandes depuis le terminal / WINDEV
 void callbackParam()
 {
@@ -609,13 +624,13 @@
     } else if (0 == strcmp(com, "save_pos")) {
         Save_Pos();
     } else if (0 == strcmp(com, "trace")) {
-            if (FLAG_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.\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);
@@ -768,6 +783,11 @@
         consigne = atoi(numb);
         control_Servo.setSetPoint(consigne);
         OUTPUT("  MAJ CONSIGNE PID -->  Consigne = %d\r\n", consigne);
+    } else if (0 == strcmp(com, "rate")) {
+        RATE_PID = atof(numb);
+        control_Servo.reset();
+        control_Servo.setInterval(RATE_PID);
+        OUTPUT("  MAJ RATE PID -->  Rate = %d\r\n", consigne);
     } else if (0 == strcmp(com, "dfu")) {
         OUTPUT("  Passage en DFU...\r\n");
         FLAG_REC = false;
@@ -851,10 +871,10 @@
     HAL_Init();
     __HAL_RCC_PWR_CLK_ENABLE();
     HAL_PWR_EnableBkUpAccess();
-    
+
     //Vérifications Flag de démarrage
     UTILS::START_FLAG();
-    
+
     E5V = 1;
     E3V = 1;
 
@@ -974,7 +994,11 @@
     control_Servo.setSetPoint(consigne);
     //Mode auto au démarrage
     control_Servo.setMode(AUTO_MODE);
+    
+    thread_PID.set_priority(osPriorityNormal);
 
+    thread_PID.start(callback(PID_thread));
+    
     DEBUG("  Cliquez sur le bouton help pour voir la liste des \r\n  commandes administrateur disponibles.\r\n");
 
     OUTPUT("$\r\n");
@@ -1052,7 +1076,7 @@
         if (FLAG_AFF) {
             Affichage();
         }
-        
+
         //Vers le traceur série
         if (FLAG_TRACE) {
             Traceur_Arduino();
@@ -1073,16 +1097,16 @@
             }
         }
 
-
-        //Update du PID
-        if (FLAG_PID == 1 && EN_MODE_SECU == false) {
-            control_Servo.setProcessValue(ppO2);
-            //Nouvelle sortie servo poumon si on est pas en mode SECU
-            Consigne_poumon = 90-control_Servo.compute();
-            // mode volets asservis (simule un seul moteur)
-            Consigne_fuite = 90 - Consigne_poumon;
-        }
-
+        /*
+                //Update du PID
+                if (FLAG_PID == 1 && EN_MODE_SECU == false) {
+                    control_Servo.setProcessValue(ppO2);
+                    //Nouvelle sortie servo poumon si on est pas en mode SECU
+                    Consigne_poumon = 90 - control_Servo.compute();
+                    // mode volets asservis (simule un seul moteur)
+                    Consigne_fuite = 90 - Consigne_poumon;
+                }
+        */
         //Arrêt du Timer mesurant le temps d'éxecution du code
         REAL_RATE.stop();
         //Définition de la nouvelle valeur du temps d'échantillonage du PID.