![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Iniversity work
Diff: main.cpp
- Revision:
- 2:8d0e48d38611
- Parent:
- 1:0630912bb400
- Child:
- 3:7db4d7f82176
--- a/main.cpp Fri Sep 01 20:53:36 2017 +0000 +++ b/main.cpp Sat Sep 02 20:02:17 2017 +0000 @@ -1,41 +1,92 @@ +#include "mbed.h" +#include "MMA8452.h" +#include <math.h> + Serial pc(USBTX, USBRX); -SPI spiTest(p5, p6, p7); -DigitalOut cs(p8); +SPI spiTest(p5, p6, p7,p8); +MMA8452 acc(p9, p10, 100000); -//Serial pc(USBTX,USBRX); PwmOut led1(LED1); PwmOut led2(LED2); PwmOut led3(LED3); - -MMA8452 acc(p9, p10, 100000); void calculateAngle(); +void display(int number); int main() { - - while(1) { - calculateAngle(); + int number = 10; + char c[50]; + while(1) { + sprintf(c,"%f", number); + pc.printf("x:%c \r\n",c[0]); + pc.printf("x:%s \r\n",c); + //calculateAngle(); wait(1); + number--; } } -void calculateAngle(){ - double x, y, z; +void calculateAngle(int x,int y, int z){ + //double x, y, z; float val = 180.0 / 3.1416; - acc.readXYZGravity(&x,&y,&z); - + double calcule = (1-2*((x*x+y*y)/(x*x+y*y+z*z))); + //acc.readXYZGravity(&x,&y,&z); + /*x = -0.016; + y = -0.877; + z = 0.499;*/ 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; + //float amplitude =sqrt(x*x+y*y+z*z); + //float crossProductVector = sqrt(x*x+y*y); + //float t = -1*asin(crossProductVector/amplitude)*val; //was z before + //float e = 0.5*acos(1-2*((pow(x,2)+pow(y,2))/((pow(x,2)+pow(y,2)+pow(z,2)))))*val; + float e2 = 0.5*acos(calcule)*val; pc.printf("x:%lf y:%lf z:%lf\r\n",x,y,z); - pc.printf("angle: %lf autre: %lf \r\n",t,e); + pc.printf("angle: %lf\r\n",e2); +} +void display(int number){ + char c[6]; + //Explication de la fonction sprintf https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm + int test = sprintf(c,"%*.4Lf", number); + //sprintf(c,"%ld", number); + spiTest.write(0x76); + pc.printf("c = %u\r\n", c[3] == '\0'); + pc.printf("test = %d\r\n", test); + + switch(test) { + case 1: + spiTest.write(0x7E); + spiTest.write(c[0]); + break; + case 2: + spiTest.write(0x7D|c[0]); + spiTest.write(0x7E|c[1]); + break; + case 3: + break; + } + + int n = sizeof(c)/sizeof(c[0]); + pc.printf("n = %i\r\n", n); + //control pour le décimal + spiTest.write(0x77); // Decimal control command + //valeur possible + //0b00000001 = 1.234 + //0b00000010 = 12.34 + //0b00000100 = 123.4 + spiTest.write(0b00000001); + /*for (int i=4; i>=0; i--){ + cs = 0; + spiTest.write(c[i]); + cs = 1; + }*/ + /*spiTest.write(c); + ; + spiTest.write(c[0]);*/ } /* -void display(int number); + int main() { //https://developer.mbed.org/handbook/SerialPC @@ -60,49 +111,7 @@ } } } -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() {