Alexandre Lemay
/
APP1_s5_A17
PARTYYYY
Accelerometre.cpp@4:ab14f0e33f2b, 2017-09-04 (annotated)
- Committer:
- evrast
- Date:
- Mon Sep 04 23:39:44 2017 +0000
- Revision:
- 4:ab14f0e33f2b
- Parent:
- 3:56cd9dff3b5f
- Child:
- 5:70db90b673b9
More dank accelerometer
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 | 0:c2282db9d19e | 4 | |
evrast | 3:56cd9dff3b5f | 5 | Accelerometre::Accelerometre():i2c(p9,p10),pc(USBTX, USBRX),acc(p9, p10, 40000){ |
evrast | 4:ab14f0e33f2b | 6 | |
evrast | 4:ab14f0e33f2b | 7 | acc.setBitDepth(MMA8452::BIT_DEPTH_12); |
evrast | 4:ab14f0e33f2b | 8 | acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); |
evrast | 4:ab14f0e33f2b | 9 | acc.setDataRate(MMA8452::RATE_100); |
evrast | 4:ab14f0e33f2b | 10 | |
evrast | 3:56cd9dff3b5f | 11 | } |
evrast | 3:56cd9dff3b5f | 12 | |
evrast | 4:ab14f0e33f2b | 13 | void Accelerometre::readxyzAngle(double *Angle){ |
evrast | 4:ab14f0e33f2b | 14 | double x=0, y=0, z=0; |
evrast | 4:ab14f0e33f2b | 15 | acc.readXYZGravity(&x,&y,&z); |
evrast | 4:ab14f0e33f2b | 16 | *Angle = AngleCalculation(z); |
evrast | 3:56cd9dff3b5f | 17 | |
evrast | 3:56cd9dff3b5f | 18 | } |
evrast | 3:56cd9dff3b5f | 19 | |
evrast | 4:ab14f0e33f2b | 20 | double Accelerometre::AngleCalculation(double ZValue){ |
evrast | 4:ab14f0e33f2b | 21 | if(ZValue>1){ |
evrast | 4:ab14f0e33f2b | 22 | ZValue = 1; |
evrast | 4:ab14f0e33f2b | 23 | } |
evrast | 4:ab14f0e33f2b | 24 | double angle =std::acos(ZValue) |
evrast | 4:ab14f0e33f2b | 25 | return angle*180* PI; |
evrast | 3:56cd9dff3b5f | 26 | |
evrast | 3:56cd9dff3b5f | 27 | |
evrast | 4:ab14f0e33f2b | 28 | } |
evrast | 4:ab14f0e33f2b | 29 | |
evrast | 3:56cd9dff3b5f | 30 | |
evrast | 3:56cd9dff3b5f | 31 | //int Accelerometre::readSingleByte(int regis){ |
evrast | 3:56cd9dff3b5f | 32 | // int c; |
evrast | 3:56cd9dff3b5f | 33 | // pc.printf("Reading single byte\n"); |
evrast | 3:56cd9dff3b5f | 34 | // i2c.start(); |
evrast | 3:56cd9dff3b5f | 35 | // int a=i2c.write(WRITE_DATA); // A write to device |
evrast | 3:56cd9dff3b5f | 36 | // i2c.write(regis); // Register to read from (acceleration in X) |
evrast | 3:56cd9dff3b5f | 37 | // i2c.start(); // Need to send start condition here |
evrast | 3:56cd9dff3b5f | 38 | // i2c.write(READ_DATA); // tell devide you want to read |
evrast | 3:56cd9dff3b5f | 39 | // c=i2c.read(0); |
evrast | 3:56cd9dff3b5f | 40 | // i2c.stop(); |
evrast | 3:56cd9dff3b5f | 41 | // pc.printf("value is %d\n", c); |
evrast | 3:56cd9dff3b5f | 42 | // pc.printf("end\n"); |
evrast | 3:56cd9dff3b5f | 43 | // return c; |
evrast | 3:56cd9dff3b5f | 44 | // } |
evrast | 3:56cd9dff3b5f | 45 | |
evrast | 0:c2282db9d19e | 46 | |
evrast | 3:56cd9dff3b5f | 47 | void Accelerometre::writeByte(int regis,int data){ |
evrast | 0:c2282db9d19e | 48 | pc.printf("Reading single byte\n"); |
evrast | 0:c2282db9d19e | 49 | i2c.start(); |
evrast | 3:56cd9dff3b5f | 50 | i2c.write(WRITE_DATA); // A write to device |
evrast | 0:c2282db9d19e | 51 | i2c.write(regis); // Register to read from (acceleration in X) |
evrast | 3:56cd9dff3b5f | 52 | i2c.write(data); |
evrast | 0:c2282db9d19e | 53 | i2c.stop(); |
evrast | 3:56cd9dff3b5f | 54 | } |