Helmotz

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Cointepas
Date:
Thu May 18 10:25:12 2017 +0000
Commit message:
Programme principal

Changed in this revision

Envoi.cpp Show annotated file Show diff for this revision Revisions of this file
Fonction.h Show annotated file Show diff for this revision Revisions of this file
HMC5883L.cpp Show annotated file Show diff for this revision Revisions of this file
Lecture.cpp 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Envoi.cpp	Thu May 18 10:25:12 2017 +0000
@@ -0,0 +1,27 @@
+#include <mbed.h>
+#include <Fonction.h>
+
+using namespace std;
+
+Serial Etrame(p13, p14, 9600); //(tx, rx, baud)
+int start3 = 0xFF;
+int start4 = 0xFF;
+int echecksum;
+int tabtest[9] = {0xFF, 0x74, 0x05, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
+DigitalOut led9(LED3);
+
+void envoi(){
+        led9 = 0;
+        Etrame.putc(start3);
+        Etrame.putc(start4);
+        echecksum += start3;
+        echecksum += start4;
+        for (int i = 0; i<9; i++) {
+            echecksum += tabtest[i];
+            Etrame.putc(tabtest[i]);
+        }
+        Etrame.putc(echecksum);
+        
+        led9 = 1;
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Fonction.h	Thu May 18 10:25:12 2017 +0000
@@ -0,0 +1,5 @@
+int tabParametre[7];
+
+void lecturetrame();
+void HMC5883L();
+void envoi();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HMC5883L.cpp	Thu May 18 10:25:12 2017 +0000
@@ -0,0 +1,50 @@
+#include "mbed.h"
+#include <Fonction.h>
+
+Serial pc(USBTX, USBRX);
+I2C hmc(p9, p10); //je déclare mon maitre connécté à des pins spécifique SCL et SDA
+const int addr = 0x3C; // adresse d'écriture de ma sonde
+char config_hmc[2];
+char readhmc[6];
+float Bx, By, Bz;
+
+using namespace std;
+
+
+void HMC5883L() 
+{
+   int i = 1;
+   int a = 0; 
+    config_hmc[0]=0x00;
+    config_hmc[1]=0x70;
+    hmc.write(addr, config_hmc,2);
+    
+    config_hmc[0]=0x01;
+    config_hmc[1]=0xA0;
+    hmc.write(addr, config_hmc,2);
+    
+    config_hmc[0]=0x02;
+    config_hmc[1]=0x01;
+    hmc.write(addr, config_hmc,2);
+    
+    wait(0.006);
+    do{
+       // while(ack!=0){
+        hmc.read(addr, readhmc, 6);
+        
+        /*readhmc[0]=0x03;
+        hmc.write(addr, readhmc, 1);*/
+      
+            Bx=((readhmc[0] << 8)|readhmc[1]);
+            By=((readhmc[2] << 8)|readhmc[3]);
+            Bz=((readhmc[4] << 8)|readhmc[5]);
+            
+            pc.printf("\nBx=%f, By=%f, Bz=%f\n",Bx,By,Bz);
+            hmc.stop();
+            wait(0.7);
+            a++;
+            i = 0;
+            wait(5);}while(a < 1);
+            
+           // }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lecture.cpp	Thu May 18 10:25:12 2017 +0000
@@ -0,0 +1,91 @@
+#include <mbed.h>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <Fonction.h>
+
+using namespace std;
+
+DigitalOut led(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+string trame;  //Trame complète.
+Serial Dtrame(p13, p14, 9600); //(tx, rx, baud)
+int i = 6;  //Iterateur.
+stringstream sstr;   //stringstream permet de faire la liaison entre le string de départ et le int sortant.
+int vchecksum;  //Octet permetant le controle de 'checksumi'.
+string start1; //Octet de start.
+int start1i;   //'start1' converti en int.
+string start2; //Octet de start.
+int start2i;   //'start2' converti en int.
+string Lenght;  //Octet du nombre de parametre.
+int Lenghti;    //'Lenght' converti en int.
+string checksum;    //Octet du checksum.
+int checksumi;      //'checksum' converti en int.
+string parametre;   //Octet de parametre.
+int tabParametrei[7];  //'parametre‘ converti en int et stocké dans le tableau.
+char tabctrame[7];
+
+int* lecturetrame()
+{
+    led2 = 1;
+    Dtrame.gets(tabctrame, 7);
+    trame = tabctrame;
+    wait (2.0);
+    led2 = 0;
+    led3 = 0;
+    //cout << "Entrer la trame :" << endl;
+    //cin >> trame;
+    start1 = trame.substr(0,2); //Enregistrement de deux Nombres à partir de 0.
+    sstr << start1; //Stock le contenue du string 'start1' dans le flux 'sstr'.
+    sstr >> hex >> start1i; //Enregistre le contenue du flux 'sstr' en hexa dans l'int 'start1i'.
+    sstr.clear();   //Vide le contenue de 'sstr'.
+    
+    start2 = trame.substr(2,2); //Enregistrement de deux Nombres à partir de 2.
+    sstr << start2; //Stock le contenue du string 'start2' dans le flux 'sstr'.
+    sstr >> hex >> start2i; //Enregistre le contenue du flux 'sstr' en hexa dans l'int 'start2i'.
+    sstr.clear();   //Vide le contenue de 'sstr'.
+    
+    Lenght = trame.substr(4,2); //Enregistrement de deux Nombres à partir de 4.
+    sstr << Lenght; //Stock le contenue du string 'Lenght' dans le flux 'sstr'.
+    sstr >> hex >> Lenghti; //Enregistre le contenue du flux 'sstr' en hexa dans l'int 'Lenghti'.
+    sstr.clear();   //Vide le contenue de 'sstr'.
+    
+    for (int a = 0; a<Lenghti; a++) {    //Tant que 'a' est inferieur à 'lenght'.
+        parametre = trame.substr(i, 2);//Enregistrement de deux Nombres à partir de i.
+        i = i+2;    //Incrémentation de 'i' afin de continuer à naviguer sur la chaine de caractère.
+        sstr << parametre;  //Stock le contenue du string 'parametre' dans le flux 'sstr'.
+        sstr >> hex >> tabParametrei[a]; //Enregistre le contenue du flux 'sstr' en hexa dans le tableau d'int 'tabParametrei[a]'.
+        sstr.clear();   //Vide le contenue de 'sstr'.
+    }
+    
+    checksum = trame.substr(i,2);   //Enregistrement de deux Nombres à partir de 'i'.
+    sstr << checksum;   //Stock le contenue du string 'checksum' dans le flux 'sstr'.
+    sstr >> hex >> checksumi;   //Enregistre le contenue du flux 'sstr' en hexa dans l'int 'checksumi'.
+    
+    vchecksum = start1i + start2i + Lenghti;    //Additionne les valeurs et les enregistres dans 'vchecksum'.
+    
+    wait (0.5);
+    led3 = 1;
+    
+    for (int b = 0; b<Lenghti; b++) {
+        vchecksum += tabParametrei[b];  //Ajoute chaque valeurs de 'tabParametrei[b]' dans 'vchecksum'.
+    }
+    if((vchecksum & 0xFF) == checksumi){    //Si le calcul du 'vchecksum' est égale a "checksumi"
+        //cout << "Trame valide" << endl;     //Trame validé.
+         for(int d = 0; d<10; d++){
+            led = 1;
+            wait (4.0);
+            }
+    }
+    else{
+        //cout << "Error 01-Checksum attendu '" << hex << uppercase << (vchecksum & 0xFF) << "'" << endl;  //Trame fausse.
+        for(int d = 0; d<10; d++){
+            led = 1;
+            wait (2.0);
+            led = 0;
+            wait (2.0);
+            }
+    }
+    return *tabParametrei[7];
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 18 10:25:12 2017 +0000
@@ -0,0 +1,11 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+
+int main() {
+    while(1) {
+        lecturetrame();
+        HMC5883L();
+        envoi();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu May 18 10:25:12 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/97feb9bacc10
\ No newline at end of file