Iniversity work

Dependencies:   mbed MMA8452

main.cpp

Committer:
vinceP
Date:
2017-09-01
Revision:
1:0630912bb400
Parent:
0:ca687da7d225
Child:
2:8d0e48d38611

File content as of revision 1:0630912bb400:

Serial pc(USBTX, USBRX);
SPI spiTest(p5, p6, p7);
DigitalOut cs(p8);

//Serial pc(USBTX,USBRX);
PwmOut led1(LED1);
PwmOut led2(LED2);
PwmOut led3(LED3);

 
MMA8452 acc(p9, p10, 100000);
void calculateAngle();

int main() {
   
  while(1) {
      calculateAngle();
      wait(1);
   }
}

void calculateAngle(){
    double x, y, z;
    float val = 180.0 / 3.1416;
    acc.readXYZGravity(&x,&y,&z);
    
    led1 = abs(x);
    led2 = abs(y);
    led3 = abs(z);
    float amplitude =sqrt(pow(x,2)+pow(y,2)+pow(z,2));
    float crossProductVector = y-x;
    float t = asin(crossProductVector/amplitude)*val; //was z before
    float e = 0.5*acos(1-((pow(x,2)+pow(y,2))/((pow(x,2)+pow(y,2)+pow(z,2)))))*val;
    pc.printf("x:%lf   y:%lf z:%lf\r\n",x,y,z);
    pc.printf("angle: %lf autre: %lf \r\n",t,e);
}
/*
void display(int number);

int main() {
    //https://developer.mbed.org/handbook/SerialPC
    //https://developer.mbed.org/handbook/mbed-NXP-LPC1768-Getting-Started
    //pc.printf("Echoes back to the screen anything you type\n");
    int nombre = 0;
    cs = 1;
    spiTest.frequency(250000);
    cs = 0;
    spiTest.write(0x81);
    //int t =  spiTest.write(nombre);
    //pc.printf("Nombre = %X\r\n", t);
    cs = 1;
    while(1) {
        //uart.printf("%c",pc.getc());
        wait(0.7);
        nombre++;
        display(nombre);
        pc.printf("Nombre = %X\r\n", nombre);
        if(nombre == 15){
            return(0);
        }
    }
}
void display(int number){
    char c[4];
    //Explication de la fonction sprintf https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
    int test = sprintf(c,"%u", number);
    //sprintf(c,"%ld", number);
    cs = 0;
    spiTest.write(0x76);
    cs = 1;
    pc.printf("c = %u\r\n", c[3] == '\0');
    pc.printf("test = %d\r\n", test);
    
    switch(test) {
       case 1:
          cs = 0;
          spiTest.write(0x7E);
          cs = 1;
          cs = 0;
          spiTest.write(c[0]);
          cs = 1;
          break;
       case 2:
          cs = 0;
          spiTest.write(0x7D|c[0]);
          cs = 1;
          cs = 0;
          spiTest.write(0x7E|c[1]);
          cs = 1;
          break;
        case 3:
          break;
    }
    
    int n = sizeof(c)/sizeof(c[0]);
    pc.printf("n = %i\r\n", n);
    /*for (int i=4; i>=0; i--){
        cs = 0;
        spiTest.write(c[i]);
        cs = 1;
    }*/
    /*spiTest.write(c);
    ;
    spiTest.write(c[0]);
}*/

/* Ignore cette partie c'Est le lab
int main() {
    //https://developer.mbed.org/handbook/SerialPC
    //https://developer.mbed.org/handbook/mbed-NXP-LPC1768-Getting-Started
    //pc.printf("Echoes back to the screen anything you type\n");
    int nombre = 2;
    cs = 1;
    //uart.printf("%d",nombre);
    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    // spiTest.format(8,3);
    // spiTest.frequency(1000000);
    cs = 0;
 
    // Send 0x8f, the command to read the WHOAMI register
    int t =  spiTest.write(0x8F);
    pc.printf("WHOAMI register = %X\r\n", t);
 
    // Send a dummy byte to receive the contents of the WHOAMI register
    //int whoami = spiTest.write(0x00);
    //printf("WHOAMI register = 0x%X\n", whoami);
    cs = 1;
    while(1) {
        //uart.printf("%c",pc.getc());
        cs = 0;
 
        // Send 0x8f, the command to read the WHOAMI register
        pc.scanf("%d",&nombre);
        t = spiTest.write(nombre);
        pc.printf("WHOAMI register = %X\r\n", t);
        cs = 0;
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
        //pc.putc(pc.getc());
    }
}
*/