Vince et Yann / Mbed 2 deprecated APP1_customProtocole

Dependencies:   mbed

Fork of APP1_customProtocole by Yann Lemay-Sévigny

Committer:
yannolecool
Date:
Tue Jan 12 20:28:24 2016 +0000
Revision:
7:7424c51ee942
Parent:
6:5c8e02d5ebcc
Code final comment? avec UART custom

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yannolecool 6:5c8e02d5ebcc 1 /*
yannolecool 6:5c8e02d5ebcc 2 APP1 S5 info
yannolecool 6:5c8e02d5ebcc 3 Author : lemy2301 and gagv2103
yannolecool 6:5c8e02d5ebcc 4 */
yannolecool 6:5c8e02d5ebcc 5
yannolecool 3:b3574c385012 6 #include "acceleroMMA8452Q.h"
yannolecool 3:b3574c385012 7 #include "math.h"
yannolecool 3:b3574c385012 8
yannolecool 7:7424c51ee942 9 #define CONVERT_FACTOR 5729.57795131 // 360/(2*PI) * 100
yannolecool 3:b3574c385012 10
yannolecool 3:b3574c385012 11 Accelero::Accelero(int frequency): i2cPort(p28, p27)
yannolecool 3:b3574c385012 12 {
yannolecool 3:b3574c385012 13 init(frequency);
yannolecool 3:b3574c385012 14 }
yannolecool 3:b3574c385012 15
yannolecool 3:b3574c385012 16 void Accelero::init(int frequency)
yannolecool 3:b3574c385012 17 {
yannolecool 6:5c8e02d5ebcc 18 //Set the frequency of the I2C port
yannolecool 4:e6df056992c1 19 i2cPort.frequency(frequency);
yannolecool 4:e6df056992c1 20
yannolecool 6:5c8e02d5ebcc 21 //Reset command
yannolecool 3:b3574c385012 22 char command[2];
yannolecool 3:b3574c385012 23 command[0] = 0x2B;
yannolecool 3:b3574c385012 24 command[1] = 0x40;
yannolecool 4:e6df056992c1 25 i2cPort.write(ACCELERO_WRITE_ADRESS, command, 2, false);
yannolecool 3:b3574c385012 26 wait(0.1);
yannolecool 3:b3574c385012 27
yannolecool 6:5c8e02d5ebcc 28 //Request WHO_AM_I register
yannolecool 3:b3574c385012 29 char response = 0x00;
yannolecool 3:b3574c385012 30 i2cPort.write(ACCELERO_WRITE_ADRESS, &ACCELERO_REGISTER_WHO_AM_I, 1, true);
yannolecool 3:b3574c385012 31 i2cPort.read(ACCELERO_READ_ADRESS, &response, 1, false);
yannolecool 3:b3574c385012 32
yannolecool 6:5c8e02d5ebcc 33 //Turn on the led 4 is the device is properly connected
yannolecool 3:b3574c385012 34 if(response == ACCELERO_RESPONSE_WHO_AM_I)
yannolecool 3:b3574c385012 35 {
yannolecool 3:b3574c385012 36 DigitalOut led4(LED4);
yannolecool 3:b3574c385012 37 led4 = 1;
yannolecool 3:b3574c385012 38 }
yannolecool 3:b3574c385012 39
yannolecool 6:5c8e02d5ebcc 40 //Change the power mode
yannolecool 6:5c8e02d5ebcc 41 command[0] = ACCELERO_REGISTER_CTRL_REG2;
yannolecool 7:7424c51ee942 42 command[1] = 0b10;
yannolecool 6:5c8e02d5ebcc 43 i2cPort.write(ACCELERO_WRITE_ADRESS, command, 2, false);
yannolecool 6:5c8e02d5ebcc 44
yannolecool 6:5c8e02d5ebcc 45 //Put the accelerometer in active mode
yannolecool 7:7424c51ee942 46 //Change the data rate selection to 6.25Hz
yannolecool 3:b3574c385012 47 command[0] = ACCELERO_REGISTER_CTRL_REG1;
yannolecool 7:7424c51ee942 48 command[1] = (0b110 << 3) | 0b1;
yannolecool 4:e6df056992c1 49 i2cPort.write(ACCELERO_WRITE_ADRESS, command, 2, false);
yannolecool 3:b3574c385012 50 }
yannolecool 3:b3574c385012 51
yannolecool 3:b3574c385012 52 vector Accelero::getAccelVector()
yannolecool 3:b3574c385012 53 {
yannolecool 3:b3574c385012 54 char data[6];
yannolecool 3:b3574c385012 55 i2cPort.write(ACCELERO_WRITE_ADRESS, &ACCELERO_REGISTER_OUT_X_MSB, 1, true);
yannolecool 3:b3574c385012 56 i2cPort.read(ACCELERO_READ_ADRESS, data, 6, false);
yannolecool 3:b3574c385012 57
yannolecool 3:b3574c385012 58 vector accelVector;
yannolecool 3:b3574c385012 59
yannolecool 3:b3574c385012 60 //Transform data to be really signed
yannolecool 3:b3574c385012 61 int tempData;
yannolecool 3:b3574c385012 62 tempData = (data[0] << 4) + (data[1] >> 4);
yannolecool 3:b3574c385012 63 if(tempData >= 2048)
yannolecool 3:b3574c385012 64 {
yannolecool 3:b3574c385012 65 tempData |= 0xFFFFF000;
yannolecool 3:b3574c385012 66 }
yannolecool 3:b3574c385012 67 accelVector.x = tempData;
yannolecool 3:b3574c385012 68
yannolecool 3:b3574c385012 69 tempData = (data[2] << 4) + (data[3] >> 4);
yannolecool 3:b3574c385012 70 if(tempData >= 2048)
yannolecool 3:b3574c385012 71 {
yannolecool 3:b3574c385012 72 tempData |= 0xFFFFF000;
yannolecool 3:b3574c385012 73 }
yannolecool 3:b3574c385012 74 accelVector.y = tempData;
yannolecool 3:b3574c385012 75
yannolecool 3:b3574c385012 76 tempData = (data[4] << 4) + (data[5] >> 4);
yannolecool 3:b3574c385012 77 if(tempData >= 2048)
yannolecool 3:b3574c385012 78 {
yannolecool 3:b3574c385012 79 tempData |= 0xFFFFF000;
yannolecool 3:b3574c385012 80 }
yannolecool 3:b3574c385012 81 accelVector.z = tempData;
yannolecool 3:b3574c385012 82
yannolecool 3:b3574c385012 83 return accelVector;
yannolecool 3:b3574c385012 84 }
yannolecool 3:b3574c385012 85
yannolecool 3:b3574c385012 86 int Accelero::getAngle()
yannolecool 3:b3574c385012 87 {
yannolecool 3:b3574c385012 88 vector accelVector = getAccelVector();
yannolecool 3:b3574c385012 89
yannolecool 3:b3574c385012 90 int denominateur = accelVector.x*accelVector.x + accelVector.y*accelVector.y + accelVector.z*accelVector.z;
yannolecool 3:b3574c385012 91
yannolecool 6:5c8e02d5ebcc 92 float result = acos(abs(accelVector.z)/sqrtf(denominateur)) * CONVERT_FACTOR;
yannolecool 3:b3574c385012 93
yannolecool 3:b3574c385012 94 return int(result);
yannolecool 3:b3574c385012 95 }
yannolecool 3:b3574c385012 96
yannolecool 3:b3574c385012 97
yannolecool 3:b3574c385012 98
yannolecool 3:b3574c385012 99
yannolecool 3:b3574c385012 100