Framework of classes and program to measure tilt angles using accelerometers
Fork of tilt_angles by
ENGO333_MMA7660.cpp@1:64f1aefe1842, 2016-11-25 (annotated)
- Committer:
- mpetovello
- Date:
- Fri Nov 25 15:18:28 2016 +0000
- Revision:
- 1:64f1aefe1842
- Parent:
- 0:3bffc1862262
Removed MMA7660 library (unnecessary)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mpetovello | 0:3bffc1862262 | 1 | #include "ENGO333_MMA7660.h" |
mpetovello | 0:3bffc1862262 | 2 | |
mpetovello | 0:3bffc1862262 | 3 | const float GRAVITY = 9.8086; |
mpetovello | 0:3bffc1862262 | 4 | |
mpetovello | 0:3bffc1862262 | 5 | ENGO333_MMA7660::ENGO333_MMA7660() : i2c(p28, p27) |
mpetovello | 0:3bffc1862262 | 6 | { |
mpetovello | 0:3bffc1862262 | 7 | SetActiveMode(); |
mpetovello | 0:3bffc1862262 | 8 | SetSamplingRateAM(MMA7660_AMSR8); |
mpetovello | 0:3bffc1862262 | 9 | measAccel[0] = measAccel[1] = measAccel[2] = 0; |
mpetovello | 0:3bffc1862262 | 10 | } |
mpetovello | 0:3bffc1862262 | 11 | |
mpetovello | 0:3bffc1862262 | 12 | bool ENGO333_MMA7660::TestConnection() |
mpetovello | 0:3bffc1862262 | 13 | { |
mpetovello | 0:3bffc1862262 | 14 | char data[3] = {128, 128, 128}; |
mpetovello | 0:3bffc1862262 | 15 | bool alert; |
mpetovello | 0:3bffc1862262 | 16 | |
mpetovello | 0:3bffc1862262 | 17 | // Check data validity |
mpetovello | 0:3bffc1862262 | 18 | alert = false; |
mpetovello | 0:3bffc1862262 | 19 | i2c.readBytes(MMA7660_ADDRESS, MMA7660_XOUT_REG, data, 3); |
mpetovello | 0:3bffc1862262 | 20 | for (int i = 0; i < 3; ++i) { |
mpetovello | 0:3bffc1862262 | 21 | if (data[i] > 63) |
mpetovello | 0:3bffc1862262 | 22 | alert = true; |
mpetovello | 0:3bffc1862262 | 23 | } |
mpetovello | 0:3bffc1862262 | 24 | |
mpetovello | 0:3bffc1862262 | 25 | if (alert == false) { |
mpetovello | 0:3bffc1862262 | 26 | return true; |
mpetovello | 0:3bffc1862262 | 27 | } else { |
mpetovello | 0:3bffc1862262 | 28 | return false; |
mpetovello | 0:3bffc1862262 | 29 | } |
mpetovello | 0:3bffc1862262 | 30 | } |
mpetovello | 0:3bffc1862262 | 31 | |
mpetovello | 0:3bffc1862262 | 32 | void ENGO333_MMA7660::SetActiveMode() |
mpetovello | 0:3bffc1862262 | 33 | { |
mpetovello | 0:3bffc1862262 | 34 | i2c.writeOneByte(MMA7660_ADDRESS, MMA7660_MODE_REG, 0x01); |
mpetovello | 0:3bffc1862262 | 35 | } |
mpetovello | 0:3bffc1862262 | 36 | |
mpetovello | 0:3bffc1862262 | 37 | void ENGO333_MMA7660::SetSamplingRateAM(MMA7660_AMSR_t samplingRate) |
mpetovello | 0:3bffc1862262 | 38 | { |
mpetovello | 0:3bffc1862262 | 39 | char oldValue, newValue; |
mpetovello | 0:3bffc1862262 | 40 | oldValue = i2c.readOneByte(MMA7660_ADDRESS, MMA7660_SR_REG); |
mpetovello | 0:3bffc1862262 | 41 | oldValue = oldValue & 0x07; |
mpetovello | 0:3bffc1862262 | 42 | newValue = oldValue | samplingRate; |
mpetovello | 0:3bffc1862262 | 43 | i2c.writeOneByte(MMA7660_ADDRESS, MMA7660_SR_REG, newValue); |
mpetovello | 0:3bffc1862262 | 44 | } |
mpetovello | 0:3bffc1862262 | 45 | |
mpetovello | 0:3bffc1862262 | 46 | void ENGO333_MMA7660::ReadAccelerometers() |
mpetovello | 0:3bffc1862262 | 47 | { |
mpetovello | 0:3bffc1862262 | 48 | char data[3]; |
mpetovello | 0:3bffc1862262 | 49 | bool alert; |
mpetovello | 0:3bffc1862262 | 50 | |
mpetovello | 0:3bffc1862262 | 51 | // Check data validity |
mpetovello | 0:3bffc1862262 | 52 | do { |
mpetovello | 0:3bffc1862262 | 53 | alert = false; |
mpetovello | 0:3bffc1862262 | 54 | i2c.readBytes(MMA7660_ADDRESS, MMA7660_XOUT_REG, data, 3); |
mpetovello | 0:3bffc1862262 | 55 | for (int i = 0; i < 3; ++i) { |
mpetovello | 0:3bffc1862262 | 56 | if (data[i] > 63) |
mpetovello | 0:3bffc1862262 | 57 | alert = true; |
mpetovello | 0:3bffc1862262 | 58 | if (data[i] > 31) |
mpetovello | 0:3bffc1862262 | 59 | data[i] += 128 + 64; |
mpetovello | 0:3bffc1862262 | 60 | } |
mpetovello | 0:3bffc1862262 | 61 | } while (alert); |
mpetovello | 0:3bffc1862262 | 62 | |
mpetovello | 0:3bffc1862262 | 63 | measAccel[0] = (signed char)(data[0]) / MMA7660_SCALE * GRAVITY; |
mpetovello | 0:3bffc1862262 | 64 | measAccel[1] = (signed char)(data[1]) / MMA7660_SCALE * GRAVITY; |
mpetovello | 0:3bffc1862262 | 65 | measAccel[2] = (signed char)(data[2]) / MMA7660_SCALE * GRAVITY; |
mpetovello | 0:3bffc1862262 | 66 | } |
mpetovello | 0:3bffc1862262 | 67 | |
mpetovello | 0:3bffc1862262 | 68 | float ENGO333_MMA7660::GetAccelX() const |
mpetovello | 0:3bffc1862262 | 69 | { |
mpetovello | 0:3bffc1862262 | 70 | return measAccel[0]; |
mpetovello | 0:3bffc1862262 | 71 | } |
mpetovello | 0:3bffc1862262 | 72 | |
mpetovello | 0:3bffc1862262 | 73 | float ENGO333_MMA7660::GetAccelY() const |
mpetovello | 0:3bffc1862262 | 74 | { |
mpetovello | 0:3bffc1862262 | 75 | return measAccel[1]; |
mpetovello | 0:3bffc1862262 | 76 | } |
mpetovello | 0:3bffc1862262 | 77 | |
mpetovello | 0:3bffc1862262 | 78 | float ENGO333_MMA7660::GetAccelZ() const |
mpetovello | 0:3bffc1862262 | 79 | { |
mpetovello | 0:3bffc1862262 | 80 | return measAccel[2]; |
mpetovello | 0:3bffc1862262 | 81 | } |
mpetovello | 0:3bffc1862262 | 82 |