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:
19:cac3761a5d0b
Parent:
18:bfd78c05b589
Child:
20:5f79fb4565a7
diff -r bfd78c05b589 -r cac3761a5d0b main.cpp
--- a/main.cpp	Tue Dec 05 15:20:29 2017 +0000
+++ b/main.cpp	Tue Feb 06 13:18:34 2018 +0000
@@ -27,47 +27,20 @@
 TX          ->      PC_10
 RX          ->      PC_11
 
-- STM32L476RG sur H-Bridge L293 :
+- STM32L476RG sur carte alim AQL :
 
-Reférence L293 : http://www.ti.com/lit/ds/symlink/l293.pdf
+Reférence drv8839 : http://www.ti.com/lit/ds/symlink/drv8839.pdf
 
-Volet Poumon :
+Volet Fuite :
 PWM     ->      PB_5        ->      1
 FWD     ->      PB_4        ->      2
 REV     ->      PB_10       ->      7
 
-Volet Fuite :
+Volet Poumon :
 PWM     ->      PB_13       ->      9
 FWD     ->      PB_14       ->      10
 REV     ->      PB_15       ->      15
 
-Potentiomètres retour Position :
-
-Alim en 3.3v
-
-Feedback Volet Poumon    ->      PC_2
-Feedback Volet Fuite     ->      PC_3
-
-- Câblage complet du H-Bridge L293 :
-
-1       ->      PB_5
-2       ->      PB_4
-3       ->      Borne moteur
-4       ->      non connectée
-5       ->      GND
-6       ->      Borne moteur
-7       ->      PB_10
-8       ->      V+ Alimentation moteurs 4.5v - 36v
-
-9       ->      PB_13
-10      ->      PB_14
-11      ->      Borne moteur
-12      ->      GND
-13      ->      non connectée
-14      ->      borne moteur
-15      ->      PB_15
-16      ->      V+ Alimentation logic  5v (alim moteur si tension compatible....)
-
 */
 
 #include "mbed.h"
@@ -75,29 +48,28 @@
 #include "Sensor_head_revB.h"
 #include "HTU21D.h"
 #include "PID.h"
-#include "Actuonix.h"
+#include "Faulhaber.h"
 
 //Commandes  des servos
-#define PWM_SERVO_POUMON PB_5
-#define FWD_SERVO_POUMON PB_4
-#define REV_SERVO_POUMON PB_10
-#define FEED_BACK_SERVO_POUMON PC_2
+#define PWM_SERVO_POUMON PB_15
+#define nSleep_SERVO_POUMON PC_6
+#define FWD_SERVO_POUMON PB_14
+#define REV_SERVO_POUMON PB_13
+#define Channel_A_SERVO_POUMON PB_1
+#define Channel_B_SERVO_POUMON PB_2
+#define HOME_SERVO_POUMON 0
 
-#define PWM_SERVO_FUITE PB_13
-#define FWD_SERVO_FUITE PB_14
-#define REV_SERVO_FUITE PB_15
-#define FEED_BACK_SERVO_FUITE PC_3
-
-//Mapping des valeur.
-#define MAX_MAP 100//Mapping en pourcentage
-//#define MAX_MAP 90//Mapping en en degrés sur 90 degrés
+#define PWM_SERVO_FUITE PB_10
+#define nSleep_SERVO_FUITE PC_5
+#define FWD_SERVO_FUITE PB_4
+#define REV_SERVO_FUITE PB_5
+#define Channel_A_SERVO_FUITE PB_8
+#define Channel_B_SERVO_FUITE PB_9
+#define HOME_SERVO_FUITE 90
 
 //Ecrit dans le moniteur série de la tablette à 115200 bds si sur 1, penser à mettre NEED_CONSOLE_OUTPUT à 0
 #define NEED_ANDROID_OUTPUT 1
 
-//Sortie en mode VT100, à commenter si on veut une sortie type Arduino
-//#define VT100
-
 //Mode PID, STD à commenter / décommenter
 #define STD_MODE
 //#define PID_MODE
@@ -116,9 +88,9 @@
 #define ANDROID(...)
 #endif
 
-Actuonix Servo_Poumon(FEED_BACK_SERVO_POUMON, PWM_SERVO_POUMON, FWD_SERVO_POUMON, REV_SERVO_POUMON, 1, MAX_MAP);
-
-Actuonix Servo_Fuite(FEED_BACK_SERVO_FUITE, PWM_SERVO_FUITE, FWD_SERVO_FUITE, REV_SERVO_FUITE, 1, MAX_MAP);
+//PinName pwm, PinName nSleep, PinName fwd, PinName rev, int brakeable, 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, 1, Channel_A_SERVO_POUMON, Channel_B_SERVO_POUMON, 16, 207, Faulhaber::X2_ENCODING);
+//Faulhaber Servo_Poumon("Servo_Fuite", PWM_SERVO_FUITE, nSleep_SERVO_FUITE, FWD_SERVO_FUITE, REV_SERVO_FUITE, 1, Channel_A_SERVO_FUITE, Channel_B_SERVO_FUITE, 16, 207, Faulhaber::X2_ENCODING);
 
 //Moniteur série, Serial 2
 Serial serialMonit(USBTX,USBRX,115200);
@@ -169,19 +141,15 @@
 char to_store[50];
 time_t seconds;
 
-//VT100
-static const char CLS[] = "\x1B[2J";
-static const char HOME[] = "\x1B[H";
-
 //Thread d'intérogation des capteurs et de positionnement des volets
-Thread thread;
+Thread thread_Sensors;
+Thread thread_Volets;
 
 //Contrôle des servos
-float Consigne_poumon = 50;
-float volet_poumon_Position;
-
-float Consigne_fuite = 50;
-float volet_fuite_Position;
+float Consigne_poumon = 0;
+float volet_poumon_Position = 0;
+float Consigne_fuite = 0;
+float volet_fuite_Position = 0;
 
 #ifdef PID_MODE
 //Paramètre du PID
@@ -193,36 +161,60 @@
 float consigne = 210;
 float Max_Input = 1000;
 float Min_Input = 80;
-float Max_Output = MAX_MAP - 5;//Vérifier la valeur pour angle à laisser ouvert...
-float Min_Output = 0;
+float Max_Output = 85;//Vérifier la valeur pour angle à laisser ouvert...
+float Min_Output = 5;
 
 //Init PID
 PID control_Servo(Kc, Ti, Td, RATE_PID);
 #endif
 
-//Boolean du status de l'appareil, en mode secours ou nominal
-bool EN_MODE_SCOURS = false;
+//Boolean du status de l'appareil, en mode SECU ou nominal
+bool EN_MODE_SECU = false;
 
-//Passage en mode secours
-void Mode_Secours()
+//Passage en mode SECU
+void Mode_SECU()
 {
-#ifdef PID_MODE    
+#ifdef PID_MODE
     //Mise du PID en mode manuel (desactivation...)
     control_Servo.setMode(MANUAL_MODE);
 #endif
-    Consigne_poumon = MAX_MAP;
-    Consigne_fuite = MAX_MAP;
-    printf("-------------- Appareil en mode secours ---------------");
-    EN_MODE_SCOURS = true;
+    Consigne_poumon = HOME_SERVO_POUMON;
+    Consigne_fuite = HOME_SERVO_FUITE;
+    while(1) {
+        wait_ms(300);
+        if (Servo_Poumon.Pos_OK() == true) break;
+        //if (Servo_Poumon.Pos_OK() == true && Servo_Fuite.Pos_OK() == true) break;
+    }
+    printf("-------------- Appareil en mode SECU ---------------\r\n");
+    EN_MODE_SECU = true;
+
+    int Pos = Servo_Poumon.getPulses();
+    UTILS::Store_A_Val((float)Pos, "Servo_Poumon");
+    printf("  position volet poumon sauvegardée = %d pulse(s)\r\n", Pos);
+    //Pos = Servo_Fuite.getPulses();
+    //UTILS::Store_A_Val((float)Pos, "Servo_Fuite");
+    //printf("  position volet fuite sauvegardée = %d pulse(s)\r\n", Pos);
+    thread_Volets.terminate();
+}
+
+//Sequence d'arrêt
+void Stop_Sequence()
+{
+    Mode_SECU();
+    printf("ARRET DE L'APPAREIL");
+    wait(2);
+    thread_Sensors.terminate();
+    wait(2);
+    deepsleep();
 }
 
 //Contrôle du status de l'appareil / des constantes
 bool Check()
 {
     if (ppO2 > 100)
-    return true;//Situation OK
+        return true;//Situation OK
     else
-    return false;//Situation dégradée
+        return false;//Situation dégradée
 }
 
 //Calcul des OTU
@@ -236,10 +228,10 @@
     PpO2 = pression partielle d’ oxygène en bars
     */
 
-    if (ppO2 > 500){
+    if (ppO2 > 500) {
         float val = (2 * (float)ppO2/1000 - 1);//je divise par 1000 car la PP est en mb...
         OTU += Ref_Time * pow(val, COEF_OTU);
-        } 
+    }
 }
 
 //Thread d'intérogation des capteurs, positions servo
@@ -264,19 +256,25 @@
 
         //Retour position des servos
         volet_poumon_Position = Servo_Poumon.Get_Position();
-        volet_fuite_Position = Servo_Fuite.Get_Position();
+
+        if(!Check()) Mode_SECU();
+    }
+}
 
-        //Position des servo mise à jour en permanence dans le thread
-        Servo_Poumon.Go_To_Prop(Consigne_poumon);
-        Servo_Fuite.Go_To_Prop(Consigne_fuite);
-        
-        if(!Check()) Mode_Secours();
+void GO_TO_thread()
+{
+    while (true) {
+        //Servo_Poumon.Go_To_Prop(Consigne_poumon);
+        //Servo_Poumon.Go_To_PID(Consigne_poumon);
+        Servo_Poumon.Go_To(Consigne_poumon);
+        //Servo_Fuite.Go_To_Prop(Consigne_fuite);
+        //Servo_Fuite.Go_To_PID(Consigne_fuite);
     }
 }
 
 void Affichage_moniteur()
 {
-#ifndef VT100
+
     printf("\r\n");
     printf("  CO2             = %d ppm\r\n"  , co2);
     printf("  PPO2            = %d mb\r\n", ppO2);
@@ -297,34 +295,6 @@
     printf("\r\n");
     printf("A enregistrer = %s\n", to_store);
     printf("\r\n");
-#endif
-
-#ifdef VT100
-    printf(HOME);
-    printf("\x1b[30m");
-    printf("\x1b[0m\r  CO2           = \x1b[1m\x1b[K%d ppm\n", co2);
-    printf("\x1b[0m\r  PPO2          = \x1b[1m\x1b[K%d mb\n", ppO2);
-    printf("\n");
-    printf("\x1b[0m\r  OTU           = \x1b[1m\x1b[K%d mb\n", OTU);
-    printf("\n");
-    printf("\x1b[0m\r  Pression      = \x1b[1m\x1b[K%.2f msw\n", pression);
-    printf("\n");
-    printf("\x1b[0m\r  Temp MS5837   = \x1b[1m\x1b[K%.2f C\n", Temp1);
-    printf("\x1b[0m\r  Temp HTU21D   = \x1b[1m\x1b[K%.2f C\n", Temp2);
-    printf("\n");
-    printf("\x1b[0m\r  Humidity      = \x1b[1m\x1b[K%d %\n", Humid);
-    printf("\n");
-    printf("\x1b[0m\r  Cell O2 n 1  = \x1b[1m\x1b[K%d\n", CellO2_1);
-    printf("\x1b[0m\r  Cell O2 n 2  = \x1b[1m\x1b[K%d\n", CellO2_2);
-    printf("\n");
-    printf("\x1b[0m\r  Volet Poumon = \x1b[1m\x1b[K%.2f\n", volet_poumon_Position);
-    printf("\x1b[0m\r  Volet Fuite  = \x1b[1m\x1b[K%.2f\n", volet_fuite_Position);
-    printf("\n");
-    printf("\x1b[0m\r  Temps d execution de la boucle = \x1b[1m\x1b[K%f seconde(s)\n", (RATE + RATE_TRUE / 1000));
-    printf("\r\n");
-    printf("\x1b[0m\r  A enregistrer = \x1b[1m\x1b[K%s\n", to_store);
-    printf("\r\n");
-#endif
 }
 
 //Callback de l'intérruption des envois de commandes depuis le terminal
@@ -364,7 +334,13 @@
 
     sscanf(message,"%s %f",&commande , &valeur);
 
-    if ((char)commande == 'T') {
+    if ((char)commande == 'S') {
+        Mode_SECU();
+    } else if ((char)commande == 'R') {
+        Servo_Poumon.reset();
+    } else if ((char)commande == 'Q') {
+        Stop_Sequence();
+    } else if ((char)commande == 'T') {
         set_time(valeur);
     } else if ((char)commande == 'I') {
         Consigne_poumon = (float)valeur;
@@ -443,35 +419,34 @@
 
 int main()
 {
+    serialMonit.attach(&callbackParam, Serial::RxIrq);
 
-    Servo_Poumon.Calibrate();
-    Servo_Fuite.Calibrate();
+    android.attach(&callbackAndroid, Serial::RxIrq);
 
     /*
     Par défaut les valeur en cas de calibration sur true sont les suivant
 
-        nbCalibO2 = 5
-        Mode = SPOOLING
-        Filtre = DIGI_FILTER32
-        CalibrationCO2 = "CALIB_AIR"
+       nbCalibO2 = 5
+       Mode = SPOOLING
+       Filtre = DIGI_FILTER32
+       CalibrationCO2 = "CALIB_AIR"
 
-        Parfois la calibration du Cozir coince...faire reset et relancer...
+       Parfois la calibration du Cozir coince...faire reset et relancer...
 
-        Pour calibrer avec ces paramètres :
+       Pour calibrer avec ces paramètres :
 
-        sensors.Sensors_INIT(true, true);
+       sensors.Sensors_INIT(true, true);
 
-        Pour changer utiliser la syntaxe suivante :
+       Pour changer utiliser la syntaxe suivante :
 
-        sensors.Sensors_INIT(true, true, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR);
-
-     */
+       sensors.Sensors_INIT(true, true, true, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR);
 
-    sensors.Sensors_INIT(false, true);
+    */
+    sensors.Sensors_INIT(true, false);
 
-    serialMonit.attach(&callbackParam, Serial::RxIrq);
+    wait(1);
 
-    android.attach(&callbackAndroid, Serial::RxIrq);
+    Servo_Poumon.Faulhaber_Init();
 
     serialMonit.printf("  Demarrage...\r\n\r\n  Entrez les comandes COZIR si besoin :\r\n");
 
@@ -489,9 +464,13 @@
           osPriorityError         =  0x84        ///< system cannot determine priority or thread has illegal priority
     */
 
-    thread.start(Get_Info_thread);
+    thread_Sensors.start(Get_Info_thread);
+
+    thread_Sensors.set_priority(osPriorityNormal);
 
-    thread.set_priority(osPriorityNormal);
+    thread_Volets.start(GO_TO_thread);
+
+    thread_Volets.set_priority(osPriorityNormal);
 
 #ifdef PID_MODE
     //Init PID
@@ -505,10 +484,6 @@
     control_Servo.setSetPoint(consigne);
 #endif
 
-#ifdef VT100
-    printf(CLS);
-#endif
-
     while (true) {
 
         //Démarrage du Timer mesurant le temps d'éxecution du code
@@ -573,7 +548,7 @@
 #endif
 
         //Enregistrement de la chaine
-        sensors.Write_SD((string)to_store);
+        UTILS::Write_SD_File((string)to_store);
 
         //Pour Android on ajoute < et > pour décoder l'arrivée du message
         if (NEED_ANDROID_OUTPUT == 1) {
@@ -583,7 +558,7 @@
 
         //Calcul des OTU
         Calcul_OTU();
-        
+
         //Vers le moniteur dérie
         Affichage_moniteur();
 
@@ -591,8 +566,8 @@
 #ifdef PID_MODE
         //Update du PID
         control_Servo.setProcessValue(ppO2);
-        //Nouvelle sortie servo fuite si on est pas en mode secours
-        if(!EN_MODE_SCOURS) Consigne_fuite = control_Servo.compute();
+        //Nouvelle sortie servo fuite si on est pas en mode SECU
+        if(!EN_MODE_SECU) Consigne_fuite = control_Servo.compute();
 #endif
 
         //Arrêt du Timer mesurant le temps d'éxecution du code