Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ADS1015 ARNSRS_SENSORS DmTftLibrary MS5803_14BA SDFileSystem mbed
Fork of ARNSRS_SERVOS_USB_TFT by
Revision 19:31ac20c07bb2, committed 2017-05-14
- Comitter:
- POTLESS_2
- Date:
- Sun May 14 16:52:18 2017 +0000
- Parent:
- 18:e3827d5dbdd9
- Child:
- 20:bb8d2cea2076
- Commit message:
- Marche plus.....
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu May 11 15:50:35 2017 +0000
+++ b/main.cpp Sun May 14 16:52:18 2017 +0000
@@ -1,16 +1,11 @@
-/* procédure lecture capteur COZIR ou MINIR - CO2 seul
-sur broches F401RE SERIAL PA_11 et PA_12
-renvoie sur moniteur (IDE arduino par exemple) de la valeur lue
+/*
+Stm32L476RG
-procédures possibles sont :
-request (char commande) - renvoie la valeur lue si la commande ne représente qu'un seul caractère (entre '')
-cozirSend (string commande) - permet d'envoyer des commandes multicaractères (entre "") - exemple A 32 - donne la réponse obtenue
+2 servos
+BLE => Android
-
-la fonction cozirSend est davantage "sécurisée"
*/
-
#include "mbed.h"
#include "SDFileSystem.h"
#include "ARNSRS_SENSORS.h"
@@ -26,6 +21,8 @@
#define NEED_GRAPH_OUTPUT 0
//Datalog carte SD si sur 1
#define NEED_SD_DATA_LOG 0
+//BLE avec app android
+#define NEED_BLE_ANDROID 1
#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { serialMonit.printf(__VA_ARGS__); }
@@ -39,11 +36,23 @@
#define TRACE(...)
#endif
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+
//Contrôle des servos
PwmOut servo1(PB_5);
-//PwmOut servo2(PB_4);
-float servo1_move = 0.0005;
-//float servo2_move = 0.0005;
+AnalogIn FeedBack1(PB_0);
+float Servo1_POS;
+float Limit_min_S1, Limit_max_S1;
+float Delta_FB_1;
+
+PwmOut servo2(PB_4);
+AnalogIn FeedBack2(PA_4);
+float Servo2_POS;
+float Limit_min_S2, Limit_max_S2;
+float Delta_FB_2;
//Init BLE
BLEDevice ble;
@@ -52,9 +61,6 @@
//Init de la lib ARNSRS;
ARNSRS arnsrs;
-//Quelque FLAG...
-bool FLAG_SIMPLE_TIME = false;
-
//COM Série vers l'ordi, Serial 2 par défaut
Serial serialMonit (USBTX,USBRX);
@@ -75,10 +81,25 @@
char fileName[32];
int points = 1;
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
-//Fonction pour créer et envoyer la chaine d'infos en BLE
-//On balance tout en float, même pour des int...Qui peut le plus peut le moins...
-//deux par deux, methode pour de int et methode pour des float
+
+
+////////////////////////////////////////////////////////////////////////////////
+///// Remap d'une valeur /////
+////////////////////////////////////////////////////////////////////////////////
+
+float remap(float x, float in_min, float in_max, float out_min, float out_max)
+{
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//// Fonction pour créer et envoyer la chaine d'infos en BLE ////
+////////////////////////////////////////////////////////////////////////////////
+
+//On balance deux float
void build_send_Message_float(string com, float A, float B)
{
char buf[50];
@@ -87,6 +108,7 @@
wait_ms(100);
}
+//On balance deux int
void build_send_Message_int(string com, int A, int B)
{
char buf[50];
@@ -95,6 +117,10 @@
wait_ms(100);
}
+////////////////////////////////////////////////////////////////////////////////
+//// BLE ////
+////////////////////////////////////////////////////////////////////////////////
+
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
//DEBUG("Disconnected!\n\r");
@@ -102,34 +128,48 @@
ble.startAdvertising();
}
+////////////////////////////////////////////////////////////////////////////////
+//// Fonction commande servos par BLE ////
+////////////////////////////////////////////////////////////////////////////////
+
+void Servo_BLE(char message_Android [])
+{
+
+ //Variables pour décomposition du message
+ char *commande, *valeur;
+
+ //La commande
+ commande = strtok(message_Android, ",;");
+ DEBUG("Commande = %s\n", commande);
+
+ //La valeur associée
+ valeur = strtok(NULL, ",;");
+ DEBUG("Valeur = %s\n", valeur);
+
+ //On compare la commande pour faire les actions
+
+ //Commande servo1
+ if(strcmp(commande, "[S1]") == 0) {
+ servo1 = atoi(valeur) / 100.f;
+ DEBUG(" Servo 1 = %s\n", valeur);
+ }
+
+ //Commande servo2
+ if(strcmp(commande, "[S2]") == 0) {
+ servo2 = atoi(valeur) / 100.f;
+ DEBUG("Servo 2 = %f\r\n", valeur);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//// Echanges UART ////
+////////////////////////////////////////////////////////////////////////////////
+
void onDataWritten(const GattWriteCallbackParams *params)
{
if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
uint16_t bytesRead = params->len;
- /*
- // les chaines de réponse
- uint8_t press_mb_ToSend[14];
- uint8_t co2_ppm_ToSend[14];
- uint8_t o2_mb_ToSend[14];
- // fabrication des chaines de réponse
- // on suppose que chaque donnee peut être représentée par 5 digits
- char s_press_mb [50];
- sprintf(s_press_mb,"PRESS %i mb ",press_mb); // mef ! laisser les espaces de la fin
-
- char s_co2_ppm [50];
- sprintf(s_co2_ppm,"CO2 %i ppm ",co2_ppm);
-
- char s_o2_mb [50];
- sprintf(s_o2_mb,"O2 %i mb ",o2_mb);
-
- // convertir les chaines en uint8_t
- for(int j=0;j<sizeof(s_press_mb);j++){ // hypothèse : les chaines press, co2, o2 ont la même longeur
- press_mb_ToSend[j]=s_press_mb[j];
- co2_ppm_ToSend[j]=s_co2_ppm[j];
- o2_mb_ToSend[j]=s_o2_mb[j];
- }
-*/
// chaine reçue de l'app android (à dimensionner selon besoin)
char message [bytesRead] ;
@@ -137,75 +177,15 @@
for (int j=0; j<bytesRead; j++) {
message [j] = (*((params->data)+j));
}
-
- //Variables pour décomposition du message
- char *commande, *valeur;
-
- //La commande
- commande = strtok(message, ",;");
- DEBUG("Commande = %s\n", commande);
- //La valeur associée
- valeur = strtok(NULL, ",;");
- DEBUG("Valeur = %s\n", valeur);
-
- //On compare la commande pour faire les actions
-
- //Commande servo1
- if(strcmp(commande, "[S1]") == 0) {
- servo1_move = atoi(valeur) * 0.00001;
- DEBUG("Servo 1 = %f\r\n", servo1_move);
- servo1.pulsewidth(servo1_move);
- }
- /*
- //Commande servo2
- if(strcmp(commande, "[S2]") == 0) {
- servo2_move = atoi(valeur) * 0.00001;
- DEBUG("Servo 2 = %f\r\n", servo2_move);
- servo2.pulsewidth(servo2_move);
- }
- */
-/*
- DEBUG("received %u bytes => ", bytesRead);
- DEBUG("commande recue -%s-\r\n",commande);
- DEBUG ( "comparaison a commande 'p' vaut %d\r\n",strcmp(commande, "p"));
- DEBUG ( "comparaison a commande 'o' vaut %d\r\n",strcmp(commande, "o"));
- DEBUG ( "comparaison a commande 'c' vaut %d\r\n",strcmp(commande, "c"));
- DEBUG ( "comparaison a commande 't' vaut %d\r\n",strcmp(commande, "t"));
- DEBUG ( "comparaison a commande 'u' vaut %d\r\n",strcmp(commande, "t"));
+
+ Servo_BLE(message);
+ }
+}
- // envoi des réponses en fonction de la commande recue
- if (strcmp(commande, "p") == 0) {
- DEBUG("s_press_mb %s +++> data to send %s \r\n",s_press_mb,press_mb_ToSend);
- ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), press_mb_ToSend, sizeof(press_mb_ToSend));
- }
- if (strcmp(commande, "c") == 0) {
- DEBUG("s_co2_ppm %s +++> data to send %s \r\n",s_co2_ppm,co2_ppm_ToSend);
- ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), co2_ppm_ToSend, sizeof(co2_ppm_ToSend));
- }
- if (strcmp(commande, "o") == 0) {
- DEBUG("s_o2_mb %s +++> data to send %s \r\n",s_o2_mb,o2_mb_ToSend);
- ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), o2_mb_ToSend, sizeof(o2_mb_ToSend));
- }
- else
-*/
+////////////////////////////////////////////////////////////////////////////////
+//// Fonction qui change le nom du fichier ouvert pour le LOG ////
+////////////////////////////////////////////////////////////////////////////////
- //En accord avec app Android
- /*if (strcmp(commande, "t") == 0) {
- build_send_Message_int("t", co2, ppO2);
- }else
- if (strcmp(commande, "u") == 0) {
- build_send_Message_float("u", Temp1, Temp2);
- }else
- if (strcmp(commande, "v") == 0) {
- build_send_Message_float("v", Humi, pression);
- }else
- if (strchr(commande, "s") == 0) {
- replace(commande, "s","");*/
- }
- }
-
-
-//Fonction qui change le nom du fichier ouvert pour le LOG
FILE *nextLogFile(void)
{
static unsigned int fileNumber = 0;
@@ -219,12 +199,16 @@
return fopen( fileName,"w");
}
+////////////////////////////////////////////////////////////////////////////////
+//// Réglage de l'heure par le moniteur série ////
+////////////////////////////////////////////////////////////////////////////////
+
void SetTime()
{
//Reglage date / heure depuis le terminal
struct tm t;
- serialMonit.printf("Entrer la date et l'heure :\n");
- serialMonit.printf("YYYY MM DD HH MM SS [enter]\n");
+ printf("Entrer la date et l'heure :\n");
+ printf("YYYY MM DD HH MM SS [enter]\n");
scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
, &t.tm_hour, &t.tm_min, &t.tm_sec);
t.tm_year = t.tm_year - 1900;
@@ -232,12 +216,17 @@
// set the time
set_time(mktime(&t));
- if(NEED_CONSOLE_OUTPUT == 0) serialMonit.printf("OK. Vous pouvez fermer le moniteur serie");
+
+ if(NEED_CONSOLE_OUTPUT == 0) printf("OK. Vous pouvez fermer le moniteur serie");
}
+////////////////////////////////////////////////////////////////////////////////
+//// Initialisation DATA LOG ////
+////////////////////////////////////////////////////////////////////////////////
+
void init_DATALOG()
{
- //Initialisation DATA LOG
+
DEBUG("Initialisation SD card\r\n");
DEBUG("\r\n", "");
fp = nextLogFile();
@@ -251,50 +240,12 @@
}
}
-void DATA_LOG()
-{
- time_t seconds = time(NULL);
- if (fp) {
- fprintf(fp, "%s,%d,%d,%f,%f,%f,%f\r\n", ctime(&seconds), co2 , ppO2, pression, Temp1, Temp2, Humi);
- DEBUG(" Enregistrement d'un point sur la carte SD\r\n");
- DEBUG(" Nombre de points = %d\r\n", points);
- DEBUG("\r\n", "");
- points++;
- } else {
- DEBUG(" Probleme carte SD\r\n");
- }
-}
-///////////////////////////////////
-// fonction initialisation /////
-///////////////////////////////////
-void setup()
+////////////////////////////////////////////////////////////////////////////////
+//// Initialisation du BLE ////
+////////////////////////////////////////////////////////////////////////////////
+
+void init_BLE()
{
- wait(2);
- //Réglage de l'heure
- SetTime();
-
- //Servos
- servo1.period(0.001);
- //servo2.period(0.001);
- servo1.pulsewidth(servo1_move);//position initiale 1/2
- //servo2.pulsewidth(servo2_move);
-
- //Initialisation du Data log
- if (NEED_SD_DATA_LOG == 1) init_DATALOG();
-
- //Initialisation capteurs
- arnsrs.Sensors_INIT(false, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR);
-}
-
-/////////////////////////////////////
-/// procédure principale /////////
-/////////////////////////////////////
-
-int main()
-{
- setup();
-
- //DEBUG("Initialising....\n\r");
ble.init();
ble.onDisconnection(disconnectionCallback);
ble.onDataWritten(onDataWritten);
@@ -312,18 +263,146 @@
UARTService uartService(ble);
uartServicePtr = &uartService;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//// Fonction d'enregistrement des données sur carte SD ////
+////////////////////////////////////////////////////////////////////////////////
+
+void DATA_LOG()
+{
+ time_t seconds = time(NULL);
+
+ if (fp) {
+ fprintf(fp, "%s,%d,%d,%f,%f,%f,%f\r\n", ctime(&seconds), co2 , ppO2, pression, Temp1, Temp2, Humi);
+ DEBUG(" Enregistrement d'un point sur la carte SD\r\n");
+ DEBUG(" Nombre de points = %d\r\n", points);
+ DEBUG("\r\n", "");
+ points++;
+ } else {
+ DEBUG(" Probleme carte SD\r\n");
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//// Calibration des limites de feedback des servos ////
+////////////////////////////////////////////////////////////////////////////////
+
+void Calibration_servo()
+{
+ //Servos, calibration du feedback
+ servo1.period(0.001);
+ servo2.period(0.001);
+
+ DEBUG("Check Servos :\r\n")
+ servo1 = 0;
+ servo2 = 0;
+ wait(4);
+ DEBUG(" FeedBack 1 MIN = %f\n", FeedBack1.read());
+ DEBUG(" FeedBack 2 MIN = %f\n", FeedBack2.read());
+ Limit_max_S1 = FeedBack1;
+ Limit_max_S2 = FeedBack2;
+ servo1 = 1;
+ servo2 = 1;
+ wait(4);
+ DEBUG(" FeedBack 1 MAX = %f\n", FeedBack1.read());
+ DEBUG(" FeedBack 2 MAX = %f\n", FeedBack2.read());
+ Limit_min_S1 = FeedBack1;
+ Limit_min_S2 = FeedBack2;
+ servo1 = 0.5;
+ servo2 = 0.5;
+ wait(4);
+
+ //Mesure du delta consigne / feedback
+ Delta_FB_1 = abs(remap(FeedBack1, Limit_max_S1, Limit_min_S1, 0, 100) - 50);
+ Delta_FB_2 = abs(remap(FeedBack2, Limit_max_S1, Limit_min_S1, 0, 100) - 50);
+
+ DEBUG(" Delta Servo 1 = %f\n", Delta_FB_1);
+ DEBUG(" Delta Servo 2 = %f\n", Delta_FB_2);
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+//// Fonction d'affichage de tout dans le moniteur série ou le traceur ////
+////////////////////////////////////////////////////////////////////////////////
+
+void Debug_Complet()
+{
+
+ //RTC
+ time_t seconds = time(NULL);
+
+ DEBUG(" Date / Heure = %s\r\n", ctime(&seconds));
+
+ //Retour capteurs
+ DEBUG(" CO2 = %d\r\n" , co2);
+ DEBUG(" Humidite = %f\r\n" , Humi);
+ DEBUG(" Temperature = %f\r\n" ,Temp1);
+ //P / T sur MS5803
+ DEBUG(" Pression = %f\r\n", pression);
+ DEBUG(" Temperature = %f\r\n", Temp2);
+ //PPO2 sur ADS1015
+ DEBUG(" PPO2 = %d\r\n", ppO2);
+ DEBUG("\r\n", "");
+
+ //Retour position des servos
+ DEBUG(" FeedBack 1 = %3.2f%%\n", Servo1_POS);
+ DEBUG(" FeedBack 2 = %3.2f%%\n", Servo2_POS);
+
+ //Traceur
+ TRACE("%d" , co2);
+ TRACE(", ");
+ TRACE("%d" , ppO2);
+ TRACE(", ");
+ TRACE("%f" , Humi);
+ TRACE(", ");
+ TRACE("%f" , Temp1);
+ TRACE(", ");
+ TRACE("%f" , Temp2);
+ TRACE(", ");
+ TRACE("%f" , pression);
+ TRACE("\r\n");
+}
+
+////////////////////////////////////////////////////////////////////////////////
+/// fonction initialisation //
+////////////////////////////////////////////////////////////////////////////////
+
+void setup()
+{
+ wait(2);
+
+ //Réglage de l'heure
+ SetTime();
+
+ //Initialisation BLE
+ init_BLE();
+
+ //Calibration des limites des servos pour remapper les feedback
+ Calibration_servo();
+
+ //Initialisation du Data log
+ if (NEED_SD_DATA_LOG == 1) init_DATALOG();
+
+ //Initialisation capteurs
+ arnsrs.Sensors_INIT(false, 5, SPOOLING, DIGI_FILTER32, CALIB_AIR);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////////////
+/// procédure principale ///
+////////////////////////////////////////////////////////////////////////////////
+
+int main()
+{
+ setup();
while (1) {
ble.waitForEvent();
- //wait_ms(500);
-
- //RTC
- time_t seconds = time(NULL);
-
- DEBUG(" Date / Heure = %s\r\n", ctime(&seconds));
-
//CO2 / H / T sur Cozir
co2 = arnsrs.requestCO2();
Humi = arnsrs.requestHUMI();
@@ -336,38 +415,23 @@
//PPO2 sur ADS1015
ppO2 = arnsrs.requestPpO2(false);
- //Affichage sur moniteur série
- DEBUG(" CO2 = %d\r\n" , co2);
- DEBUG(" Humidite = %f\r\n" , Humi);
- DEBUG(" Temperature = %f\r\n" ,Temp1);
- //P / T sur MS5803
- DEBUG(" Pression = %f\r\n", pression);
- DEBUG(" Temperature = %f\r\n", Temp2);
- //PPO2 sur ADS1015
- DEBUG(" PPO2 = %d\r\n", ppO2);
- DEBUG("\r\n", "");
+ //Retour position des servos
+ Servo1_POS = remap(FeedBack1, Limit_max_S1, Limit_min_S1, 0, 100);
+ Servo2_POS = remap(FeedBack2, Limit_max_S2, Limit_min_S2, 0, 100);
- //Traceur
- TRACE("%d" , co2);
- TRACE(", ");
- TRACE("%d" , ppO2);
- TRACE(", ");
- TRACE("%f" , Humi);
- TRACE(", ");
- TRACE("%f" , Temp1);
- TRACE(", ");
- TRACE("%f" , Temp2);
- TRACE(", ");
- TRACE("%f" , pression);
- TRACE("\r\n");
-
+ //Debug
+ Debug_Complet();
+
//Data LOG
if (NEED_SD_DATA_LOG == 1) DATA_LOG();
-
+
//En accord avec app Android
- build_send_Message_int("t", co2, ppO2);
- build_send_Message_float("u", Temp1, Temp2);
- build_send_Message_float("v", Humi, pression);
-
+ build_send_Message_int("t", co2, ppO2);
+ build_send_Message_float("u", Temp1, Temp2);
+ build_send_Message_float("v", Humi, pression);
+ //build_send_Message_float("f", Servo1_POS, Servo2_POS);
}
}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
