clement roche / Mbed 2 deprecated main_robot_gant_v22

Dependencies:   PCA9685 mbed

Fork of main_robot_gant_v2 by clement roche

Revision:
0:3b1099549fdf
diff -r 000000000000 -r 3b1099549fdf main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 23 07:03:29 2017 +0000
@@ -0,0 +1,358 @@
+#include "mbed.h"
+#include "math.h"
+
+int term = 0;
+int present = 0;
+int acc_x[2];
+int acc_y[2];
+int acc_z[2];
+int i = 0;
+int j = 0;
+char data[21];
+float angleX, angleY, angleZ, total, angleX_old;
+
+DigitalOut myled(LED1);//  creqtion d'une led
+SPI capteur(PTD2, PTD3, PTD1); // mosi, miso, sclk
+SPI capteur_2(PTD6, PTD7, PTD5); // mosi, miso, sclk  
+Ticker action; //  creation d'un timer
+
+Serial xbee(PTE0,PTE1);
+Serial hc06(PTE22,PTE23);
+
+//Phalanges doigt 0
+DigitalOut D0C1(PTA16);// Pouce bout
+DigitalOut D0C2(PTA17);// Pouce millieu
+DigitalOut D0C3(PTE31);// Pouce base
+
+//Phalanges doigt 1
+DigitalOut D1C1(PTC13);// Index bout
+DigitalOut D1C2(PTC16);// Index millieu
+DigitalOut D1C3(PTC17);// Index base
+
+//Phalanges doigt 2
+DigitalOut D2C1(PTC10);// Majeur bout
+DigitalOut D2C2(PTC11);// Majeur millieu
+DigitalOut D2C3(PTC12);// Majeur base
+
+//Phalanges doigt 3
+DigitalOut D3C1(PTC4);// Annulaire bout
+DigitalOut D3C2(PTC5);// Annulaire millieu
+DigitalOut D3C3(PTC6);// Annulaire base
+
+//Phalanges doigt 4
+DigitalOut D4C1(PTC7);// Orriculaire bout
+DigitalOut D4C2(PTC0);// Orriculaire millieu
+DigitalOut D4C3(PTC3);// Orriculaire base
+
+struct S_accel{
+    int x;
+    int y;
+    int z;
+    DigitalOut *CS;
+}a[15], b[15];
+//
+
+uint8_t text_xbee[21];
+uint8_t idx_carac_xbee_in = 0;
+
+uint8_t text[21];
+uint8_t idx_carac_hc06_in = 0;
+
+void irq_xbee(void){
+    text_xbee[idx_carac_xbee_in] = xbee.getc();
+    idx_carac_xbee_in = (idx_carac_xbee_in+1) % 20;
+}
+//
+void irq_hc06(void){
+    text[idx_carac_hc06_in] = hc06.getc();
+    idx_carac_hc06_in = (idx_carac_hc06_in+1) % 20;
+}
+//
+void Init_Cs(void){
+    //Phalanges doigt 0
+    a[0].CS = &D0C1;
+    a[1].CS = &D0C2;
+    a[2].CS = &D0C3;
+
+    //Phalanges doigt 1
+    a[3].CS = &D1C1;
+    a[4].CS = &D1C2;
+    a[5].CS = &D1C3;
+
+    //Phalanges doigt 2
+    a[6].CS = &D2C1;
+    a[7].CS = &D2C2;
+    a[8].CS = &D2C3;
+
+    //Phalanges doigt 3
+    a[9].CS = &D3C1;
+    a[10].CS = &D3C2;
+    a[11].CS = &D3C3;
+
+    //Phalanges doigt 4
+    a[12].CS = &D4C1;
+    a[13].CS = &D4C2;
+    a[14].CS = &D4C3;
+    
+    for (i=0;i <= 14;i++){// Tous les CS a 1
+        *(a[i].CS) = 1;
+    }
+ }
+//
+void Write_Register(int cap, int addr, int valeur){
+    //Ecriture dans un registre
+    if(cap <=5){
+        *(a[cap].CS) = 0;
+        valeur = (1<<8)+ valeur;
+        capteur_2.write(addr);
+        capteur_2.write(valeur);
+        *(a[cap].CS) = 1;
+    }else{
+        //Ecriture dans un registre
+        *(a[cap].CS) = 0;
+        valeur = (1<<8)+ valeur;
+        capteur.write(addr);
+        capteur.write(valeur);
+        *(a[cap].CS) = 1;
+    }
+}
+//
+int Read_Register(int cap, int addr){
+    int addr_2;
+    // Lecture d'un registre et affichage sur port comm
+    if(cap <=5){
+        addr_2 = (8<<4) + addr;
+        *(a[cap].CS) = 0;
+        capteur_2.write(addr_2);
+        term = capteur_2.write(0x00);
+        printf("Registre = 0x%X\r\n", term);
+        *(a[cap].CS) = 1;
+        return term;
+    }else{
+        addr_2 = (8<<4) + addr;
+        *(a[cap].CS) = 0;
+        capteur.write(addr_2);
+        term = capteur.write(0x00);
+        printf("Registre = 0x%X\r\n", term);
+        *(a[cap].CS) = 1;
+        return term;
+    }
+}
+//
+void Read_x(int cap){
+    if(cap <=5){    
+        *(a[cap].CS) = 0;
+        capteur_2.write(0xA8);
+        acc_x[0]= capteur_2.write(0x00);
+        //printf("X_L = 0x%X\r\n", acc_x[0]);  //Affichacge LSB axe X
+        *(a[cap].CS) = 1;
+
+        *(a[cap].CS) = 0;
+        capteur_2.write(0xA9);
+        acc_x[1] = capteur_2.write(0x00);
+        //printf("X_H = 0x%X\r\n", acc_x[1]);  // Affichage MSB axe X
+        *(a[cap].CS) = 1;
+
+        a[cap].x = ((acc_x[1] <<8) | acc_x[0]);  //  Mise en forme valeur
+        //printf("X = 0x%d\r\n", a[cap].x);  //  Affichage valeur axe x
+    }else{
+        *(a[cap].CS) = 0;
+        capteur.write(0xA8);
+        acc_x[0]= capteur.write(0x00);
+        //printf("X_L = 0x%X\r\n", acc_x[0]);  //Affichacge LSB axe X
+        *(a[cap].CS) = 1;
+    
+        *(a[cap].CS) = 0;
+        capteur.write(0xA9);
+        acc_x[1] = capteur.write(0x00);
+        //printf("X_H = 0x%X\r\n", acc_x[1]);  // Affichage MSB axe X
+        *(a[cap].CS) = 1;
+        
+        a[cap].x = (acc_x[1] <<8)+ acc_x[0];  //  Mise en forme valeur
+        //printf("X = %d\r\n", a[cap].x);  //  Affichage valeur axe x
+    }
+}
+//
+void Read_y(int cap){
+        if(cap <=5){    
+            *(a[cap].CS) = 0;
+            capteur_2.write(0xAA);
+            acc_y[0]= capteur_2.write(0x00);
+            //printf("Y_L = 0x%X\r\n", acc_y[0]);  //Affichacge LSB axe Y
+            *(a[cap].CS) = 1;
+
+            *(a[cap].CS) = 0;
+            capteur_2.write(0xAB);
+            acc_y[1] = capteur_2.write(0x00);
+            //printf("Y_H = 0x%X\r\n", acc_y[1]);  // Affichage MSB axe Y
+            *(a[cap].CS) = 1;
+
+            a[cap].y = (acc_y[1] <<8)+ acc_y[cap];  //  Mise en forme valeur
+            //printf("Y = 0x%X\r\n", a[0].y);  //  Affichage valeur axe Y
+    }else{
+        *(a[cap].CS) = 0;
+        capteur.write(0xAA);
+        acc_y[0]= capteur.write(0x00);
+        //printf("Y_L = 0x%X\r\n", acc_y[0]);  //Affichacge LSB axe Y
+        *(a[cap].CS) = 1;
+
+        *(a[cap].CS) = 0;
+        capteur.write(0xAB);
+        acc_y[1] = capteur.write(0x00);
+        //printf("Y_H = 0x%X\r\n", acc_y[1]);  // Affichage MSB axe Y
+        *(a[cap].CS) = 1;
+
+        a[cap].y = (acc_y[1] <<8)+ acc_y[cap];  //  Mise en forme valeur
+        //printf("Y = 0x%X\r\n", a[0].y);  //  Affichage valeur axe Y
+    }
+}
+//
+void Read_z(int cap){
+    if(cap <=5){    
+        *(a[cap].CS) = 0;
+        capteur_2.write(0xAC);
+        acc_z[0]= capteur_2.write(0x00);
+        //printf("Z_L = 0x%X\r\n", acc_z[0]);  //Affichacge LSB axe Z
+        *(a[cap].CS) = 1;
+
+        *(a[cap].CS) = 0;
+        capteur_2.write(0xAD);
+        acc_z[1] = capteur_2.write(0x00);
+        //printf("Z_H = 0x%X\r\n", acc_z[1]);  // Affichage MSB axe Z
+        *(a[cap].CS) = 1;
+
+        a[cap].z = (acc_z[1] <<8)+ acc_z[0];  //  Mise en forme valeur
+        //printf("Z = 0x%X\r\n", a[cap].z);  //  Affichage valeur axe Z
+    }else{
+        *(a[cap].CS) = 0;
+        capteur.write(0xAC);
+        acc_z[0]= capteur.write(0x00);
+        //printf("Z_L = 0x%X\r\n", acc_z[0]);  //Affichacge LSB axe Z
+        *(a[cap].CS) = 1;
+
+        *(a[cap].CS) = 0;
+        capteur.write(0xAD);
+        acc_z[1] = capteur.write(0x00);
+        //printf("Z_H = 0x%X\r\n", acc_z[1]);  // Affichage MSB axe Z
+        *(a[cap].CS) = 1;
+
+        a[cap].z = (acc_z[1] <<8)+ acc_z[0];  //  Mise en forme valeur
+        //printf("Z = 0x%X\r\n", a[cap].z);  //  Affichage valeur axe Z
+    }
+}
+//
+void Capteur_present(void){// verification presence capteurs
+    i = 0;
+    for (i=0;i <= 5;i++){
+        present = Read_Register(i, 0x0F);
+        if( present == 0b01001001){
+            printf("capteur_1 %X = OK\r\n", i);
+        } else{
+            printf("capteur_1 %X = Error\r\n", i);
+        }
+    }
+    for (i=6;i <= 14;i++){
+        present = Read_Register(i, 0x0F);
+        if( present == 0b01001001){
+            printf("capteur_2 %X = OK\r\n", i);
+        } else{
+            printf("capteur_2 %X = Error\r\n", i);
+        }
+    }
+}
+//
+void lecture(void){
+        for(i=0;i <= 14;i++){
+            //  Lecture des axes
+            Read_x(i);
+            Read_y(i);
+            Read_z(i);
+            
+            a[i].x = (a[i].x + 32768)/2;
+            a[i].y = (a[i].y + 32768)/2;
+            a[i].z = (a[i].z + 32768)/2;
+            
+            a[i].x = a[i].x /100;
+            
+            if(a[i].x >= 240)
+            {
+                a[i].x = 240;
+            }
+            if(a[i].x <= 180)
+            {
+                a[i].x = 180;
+            }
+            
+            a[i].x = 180 - (a[i].x);
+            a[i].x = -(a[i].x * 2);
+            
+            //printf("%d\r\n", a[3].x);
+            
+           /* hc06.printf("Doigt 1 %d\r\n", a[3].x);
+            
+            hc06.printf("Doigt 2 %d\r\n", a[6].x);*/
+            
+            //hc06.printf("Doigt 1 %d\r\n", a[0].x);
+            
+            //hc06.printf("Doigt 2 %d\r\n", a[3].x);
+            
+/*          angleX = (0,0167 * a[i].x) - 300;
+            
+            angleX = (angleX_old + angleX) /2;
+            angleX_old = angleX;
+*/
+
+            
+            // Ajout dans le tableau
+            data[i] = a[i].x;
+            
+            
+            hc06.printf("Doigt 1 : %d\r\n", data[3]);
+            hc06.printf("Doigt 2 : %d\r\n", data[6]);
+        }
+        xbee.printf("%c", data[0]);
+        xbee.printf("%c", data[3]);
+        xbee.printf("%c", data[6]);
+        xbee.printf("%c", data[9]);
+        xbee.printf("%c", data[12]);
+}
+//
+
+
+
+
+int main() {
+    
+    //  Configuration xbee
+    xbee.baud(115200);// Vitesse du xbee
+    xbee.attach(&irq_xbee);// Assignation des interruptions
+    
+    hc06.baud(115200);// Vitesse du xbee
+    hc06.attach(&irq_hc06);// Assignation des interruptions
+    
+    //  Configuration bus spi
+    capteur.format(8,3);// Selection parametre du bus SPI
+    capteur.frequency(5000000);// Vitesse du bus SPI
+    
+    capteur_2.format(8,3);// Selection parametre du bus SPI
+    capteur_2.frequency(5000000);// Vitesse du bus SPI
+    
+    Init_Cs();//  Assignation des chip select
+    
+    Capteur_present();//  Verification presence capteurs
+    
+    for (i=0;i <= 5;i++){//  Mise en marche capteurs
+        Write_Register(i, 0x20, 0x67);// registre CTRL_REG1_A AODR = 10Hz
+    }
+    //
+    for (i=6;i <= 14;i++){//  Mise en marche capteurs
+        Write_Register(i, 0x20, 0x67);// registre CTRL_REG1_A AODR = 10Hz
+    }
+    //
+    action.attach(&lecture, 0.1);
+
+    
+    while(1) {
+              //hc06.printf("%c", data[0]);      
+    }
+}