Alexandre Lemay
/
APP1_s5_A17
PARTYYYY
Accelerometre.cpp@6:909e7877d915, 2017-09-05 (annotated)
- Committer:
- evrast
- Date:
- Tue Sep 05 05:51:17 2017 +0000
- Revision:
- 6:909e7877d915
- Parent:
- 5:70db90b673b9
- Child:
- 10:2836530d9a5e
je dormais;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evrast | 0:c2282db9d19e | 1 | #include "Accelerometre.h" |
evrast | 0:c2282db9d19e | 2 | |
evrast | 0:c2282db9d19e | 3 | |
evrast | 6:909e7877d915 | 4 | Accelerometre::Accelerometre():i2c(p28,p27),pc(USBTX, USBRX),acc(p28, p27, 40000){ |
evrast | 4:ab14f0e33f2b | 5 | |
evrast | 4:ab14f0e33f2b | 6 | acc.setBitDepth(MMA8452::BIT_DEPTH_12); |
evrast | 6:909e7877d915 | 7 | acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_2G); |
evrast | 4:ab14f0e33f2b | 8 | acc.setDataRate(MMA8452::RATE_100); |
evrast | 4:ab14f0e33f2b | 9 | |
evrast | 3:56cd9dff3b5f | 10 | } |
evrast | 3:56cd9dff3b5f | 11 | |
evrast | 4:ab14f0e33f2b | 12 | void Accelerometre::readxyzAngle(double *Angle){ |
evrast | 4:ab14f0e33f2b | 13 | double x=0, y=0, z=0; |
evrast | 4:ab14f0e33f2b | 14 | acc.readXYZGravity(&x,&y,&z); |
evrast | 4:ab14f0e33f2b | 15 | *Angle = AngleCalculation(z); |
evrast | 3:56cd9dff3b5f | 16 | |
evrast | 3:56cd9dff3b5f | 17 | } |
evrast | 3:56cd9dff3b5f | 18 | |
evrast | 4:ab14f0e33f2b | 19 | double Accelerometre::AngleCalculation(double ZValue){ |
evrast | 4:ab14f0e33f2b | 20 | if(ZValue>1){ |
evrast | 4:ab14f0e33f2b | 21 | ZValue = 1; |
evrast | 4:ab14f0e33f2b | 22 | } |
evrast | 6:909e7877d915 | 23 | if(ZValue <-1){ |
evrast | 6:909e7877d915 | 24 | ZValue = -1; |
evrast | 6:909e7877d915 | 25 | } |
evrast | 5:70db90b673b9 | 26 | double angle =std::acos(ZValue); |
evrast | 6:909e7877d915 | 27 | return (angle*180/ PI); |
evrast | 3:56cd9dff3b5f | 28 | |
evrast | 3:56cd9dff3b5f | 29 | |
evrast | 4:ab14f0e33f2b | 30 | } |
evrast | 4:ab14f0e33f2b | 31 | |
evrast | 3:56cd9dff3b5f | 32 | |
evrast | 3:56cd9dff3b5f | 33 | //int Accelerometre::readSingleByte(int regis){ |
evrast | 3:56cd9dff3b5f | 34 | // int c; |
evrast | 3:56cd9dff3b5f | 35 | // pc.printf("Reading single byte\n"); |
evrast | 3:56cd9dff3b5f | 36 | // i2c.start(); |
evrast | 3:56cd9dff3b5f | 37 | // int a=i2c.write(WRITE_DATA); // A write to device |
evrast | 3:56cd9dff3b5f | 38 | // i2c.write(regis); // Register to read from (acceleration in X) |
evrast | 3:56cd9dff3b5f | 39 | // i2c.start(); // Need to send start condition here |
evrast | 3:56cd9dff3b5f | 40 | // i2c.write(READ_DATA); // tell devide you want to read |
evrast | 3:56cd9dff3b5f | 41 | // c=i2c.read(0); |
evrast | 3:56cd9dff3b5f | 42 | // i2c.stop(); |
evrast | 3:56cd9dff3b5f | 43 | // pc.printf("value is %d\n", c); |
evrast | 3:56cd9dff3b5f | 44 | // pc.printf("end\n"); |
evrast | 3:56cd9dff3b5f | 45 | // return c; |
evrast | 3:56cd9dff3b5f | 46 | // } |
evrast | 3:56cd9dff3b5f | 47 | |
evrast | 0:c2282db9d19e | 48 | |
evrast | 3:56cd9dff3b5f | 49 | void Accelerometre::writeByte(int regis,int data){ |
evrast | 0:c2282db9d19e | 50 | pc.printf("Reading single byte\n"); |
evrast | 0:c2282db9d19e | 51 | i2c.start(); |
evrast | 3:56cd9dff3b5f | 52 | i2c.write(WRITE_DATA); // A write to device |
evrast | 0:c2282db9d19e | 53 | i2c.write(regis); // Register to read from (acceleration in X) |
evrast | 3:56cd9dff3b5f | 54 | i2c.write(data); |
evrast | 0:c2282db9d19e | 55 | i2c.stop(); |
evrast | 0:c2282db9d19e | 56 | pc.printf("end\n"); |
evrast | 5:70db90b673b9 | 57 | } |
evrast | 5:70db90b673b9 | 58 |