Chandra Tjhai
/
ENGO333_Lab_tiltmeter
Simple tiltmeter using accelerometer
ENGO333_MMA7660.h@0:7d6134e052e0, 2019-11-23 (annotated)
- Committer:
- chtjhai
- Date:
- Sat Nov 23 04:58:09 2019 +0000
- Revision:
- 0:7d6134e052e0
Simple tiltmeter using accelerometer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chtjhai | 0:7d6134e052e0 | 1 | /** |
chtjhai | 0:7d6134e052e0 | 2 | * File : ENGO333_MMA7660.h |
chtjhai | 0:7d6134e052e0 | 3 | * Created by : Chandra Tjhai |
chtjhai | 0:7d6134e052e0 | 4 | * Created on : September 10, 2016 |
chtjhai | 0:7d6134e052e0 | 5 | * |
chtjhai | 0:7d6134e052e0 | 6 | * Description : |
chtjhai | 0:7d6134e052e0 | 7 | * This library is created for ENGO 333 class. The accelerometer is mounted |
chtjhai | 0:7d6134e052e0 | 8 | * on the mbed Application Board. The I2C connection is permanently set to p28 |
chtjhai | 0:7d6134e052e0 | 9 | * (SDA) and p27 (SCL). |
chtjhai | 0:7d6134e052e0 | 10 | */ |
chtjhai | 0:7d6134e052e0 | 11 | |
chtjhai | 0:7d6134e052e0 | 12 | #ifndef ENGO333_MMA7660_H |
chtjhai | 0:7d6134e052e0 | 13 | #define ENGO333_MMA7660_H |
chtjhai | 0:7d6134e052e0 | 14 | |
chtjhai | 0:7d6134e052e0 | 15 | #include "ENGO333_I2C.h" |
chtjhai | 0:7d6134e052e0 | 16 | #include "Tiltmeter.h" |
chtjhai | 0:7d6134e052e0 | 17 | |
chtjhai | 0:7d6134e052e0 | 18 | // Sensor I2C address |
chtjhai | 0:7d6134e052e0 | 19 | #define MMA7660_ADDRESS 0x98 |
chtjhai | 0:7d6134e052e0 | 20 | |
chtjhai | 0:7d6134e052e0 | 21 | // Define sensor registers |
chtjhai | 0:7d6134e052e0 | 22 | #define MMA7660_XOUT_REG 0x00 |
chtjhai | 0:7d6134e052e0 | 23 | #define MMA7660_YOUT_REG 0x01 |
chtjhai | 0:7d6134e052e0 | 24 | #define MMA7660_ZOUT_REG 0x02 |
chtjhai | 0:7d6134e052e0 | 25 | #define MMA7660_TILT_REG 0x03 |
chtjhai | 0:7d6134e052e0 | 26 | #define MMA7660_SRST_REG 0x04 |
chtjhai | 0:7d6134e052e0 | 27 | #define MMA7660_SPCNT_REG 0x05 |
chtjhai | 0:7d6134e052e0 | 28 | #define MMA7660_INTSU_REG 0x06 |
chtjhai | 0:7d6134e052e0 | 29 | #define MMA7660_MODE_REG 0x07 |
chtjhai | 0:7d6134e052e0 | 30 | #define MMA7660_SR_REG 0x08 |
chtjhai | 0:7d6134e052e0 | 31 | #define MMA7660_PDET_REF 0x09 |
chtjhai | 0:7d6134e052e0 | 32 | #define MMA7660_PD_REG 0x0A |
chtjhai | 0:7d6134e052e0 | 33 | |
chtjhai | 0:7d6134e052e0 | 34 | // Define acceleration scale |
chtjhai | 0:7d6134e052e0 | 35 | #define MMA7660_SCALE (21.33) |
chtjhai | 0:7d6134e052e0 | 36 | |
chtjhai | 0:7d6134e052e0 | 37 | // Define AMSR values |
chtjhai | 0:7d6134e052e0 | 38 | typedef enum |
chtjhai | 0:7d6134e052e0 | 39 | { |
chtjhai | 0:7d6134e052e0 | 40 | MMA7660_AMSR120 = 0, |
chtjhai | 0:7d6134e052e0 | 41 | MMA7660_AMSR64 = 1, |
chtjhai | 0:7d6134e052e0 | 42 | MMA7660_AMSR32 = 2, |
chtjhai | 0:7d6134e052e0 | 43 | MMA7660_AMSR16 = 3, |
chtjhai | 0:7d6134e052e0 | 44 | MMA7660_AMSR8 = 4, |
chtjhai | 0:7d6134e052e0 | 45 | MMA7660_AMSR4 = 5, |
chtjhai | 0:7d6134e052e0 | 46 | MMA7660_AMSR2 = 6, |
chtjhai | 0:7d6134e052e0 | 47 | MMA7660_AMSR1 = 7 |
chtjhai | 0:7d6134e052e0 | 48 | }MMA7660_AMSR_t; |
chtjhai | 0:7d6134e052e0 | 49 | |
chtjhai | 0:7d6134e052e0 | 50 | /** |
chtjhai | 0:7d6134e052e0 | 51 | * Class |
chtjhai | 0:7d6134e052e0 | 52 | * A class to handle MMA7660 3-DOF accelerometer |
chtjhai | 0:7d6134e052e0 | 53 | */ |
chtjhai | 0:7d6134e052e0 | 54 | class ENGO333_MMA7660 |
chtjhai | 0:7d6134e052e0 | 55 | { |
chtjhai | 0:7d6134e052e0 | 56 | private: |
chtjhai | 0:7d6134e052e0 | 57 | ENGO333_I2C i2c; // I2C communication connection |
chtjhai | 0:7d6134e052e0 | 58 | float measAccel[3]; // Measured acceleration values in units of m/s/s |
chtjhai | 0:7d6134e052e0 | 59 | |
chtjhai | 0:7d6134e052e0 | 60 | public: |
chtjhai | 0:7d6134e052e0 | 61 | /** |
chtjhai | 0:7d6134e052e0 | 62 | * Default Constructor |
chtjhai | 0:7d6134e052e0 | 63 | * Once called, trigger active mode and set MMA7660_AMSR8 |
chtjhai | 0:7d6134e052e0 | 64 | */ |
chtjhai | 0:7d6134e052e0 | 65 | ENGO333_MMA7660(); |
chtjhai | 0:7d6134e052e0 | 66 | |
chtjhai | 0:7d6134e052e0 | 67 | /** |
chtjhai | 0:7d6134e052e0 | 68 | * Function : |
chtjhai | 0:7d6134e052e0 | 69 | * Test device's accelerometer connection. MMA7660 does not have identifier |
chtjhai | 0:7d6134e052e0 | 70 | * registers. Thus, this function will simply take measurements and check |
chtjhai | 0:7d6134e052e0 | 71 | * their validity. |
chtjhai | 0:7d6134e052e0 | 72 | * |
chtjhai | 0:7d6134e052e0 | 73 | * Argument : |
chtjhai | 0:7d6134e052e0 | 74 | * NONE |
chtjhai | 0:7d6134e052e0 | 75 | * |
chtjhai | 0:7d6134e052e0 | 76 | * Return : |
chtjhai | 0:7d6134e052e0 | 77 | * Return TRUE if connection is good, otherwise FALSSE |
chtjhai | 0:7d6134e052e0 | 78 | */ |
chtjhai | 0:7d6134e052e0 | 79 | virtual bool TestConnection(); |
chtjhai | 0:7d6134e052e0 | 80 | |
chtjhai | 0:7d6134e052e0 | 81 | |
chtjhai | 0:7d6134e052e0 | 82 | private: |
chtjhai | 0:7d6134e052e0 | 83 | |
chtjhai | 0:7d6134e052e0 | 84 | // Read the accelerometer data and store the values for later use. You can access the values by |
chtjhai | 0:7d6134e052e0 | 85 | // calling the GetAccelX(), GetAccelY() and/or GetAccelZ(); |
chtjhai | 0:7d6134e052e0 | 86 | // |
chtjhai | 0:7d6134e052e0 | 87 | // Arguments: |
chtjhai | 0:7d6134e052e0 | 88 | // None |
chtjhai | 0:7d6134e052e0 | 89 | // |
chtjhai | 0:7d6134e052e0 | 90 | // Returns: |
chtjhai | 0:7d6134e052e0 | 91 | // Nothing |
chtjhai | 0:7d6134e052e0 | 92 | // |
chtjhai | 0:7d6134e052e0 | 93 | // Remarks: |
chtjhai | 0:7d6134e052e0 | 94 | // The data will be stored in the 'MeasuredAccel' member variable in units of m/s/s |
chtjhai | 0:7d6134e052e0 | 95 | virtual void ReadAccelerometers(); |
chtjhai | 0:7d6134e052e0 | 96 | |
chtjhai | 0:7d6134e052e0 | 97 | |
chtjhai | 0:7d6134e052e0 | 98 | // Get the most recently measured X-axis acceleration as stored during the last call to |
chtjhai | 0:7d6134e052e0 | 99 | // ReadAccelerometer() |
chtjhai | 0:7d6134e052e0 | 100 | // |
chtjhai | 0:7d6134e052e0 | 101 | // Arguments: |
chtjhai | 0:7d6134e052e0 | 102 | // None |
chtjhai | 0:7d6134e052e0 | 103 | // |
chtjhai | 0:7d6134e052e0 | 104 | // Returns: |
chtjhai | 0:7d6134e052e0 | 105 | // The function returns the most recently measured X-axis acceleration in units of m/s/s |
chtjhai | 0:7d6134e052e0 | 106 | virtual float GetAccelX() const; |
chtjhai | 0:7d6134e052e0 | 107 | |
chtjhai | 0:7d6134e052e0 | 108 | |
chtjhai | 0:7d6134e052e0 | 109 | // Get the most recently measured Y-axis acceleration as stored during the last call to |
chtjhai | 0:7d6134e052e0 | 110 | // ReadAccelerometer() |
chtjhai | 0:7d6134e052e0 | 111 | // |
chtjhai | 0:7d6134e052e0 | 112 | // Arguments: |
chtjhai | 0:7d6134e052e0 | 113 | // None |
chtjhai | 0:7d6134e052e0 | 114 | // |
chtjhai | 0:7d6134e052e0 | 115 | // Returns: |
chtjhai | 0:7d6134e052e0 | 116 | // The function returns the most recently measured Y-axis acceleration in units of m/s/s |
chtjhai | 0:7d6134e052e0 | 117 | virtual float GetAccelY() const; |
chtjhai | 0:7d6134e052e0 | 118 | |
chtjhai | 0:7d6134e052e0 | 119 | |
chtjhai | 0:7d6134e052e0 | 120 | // Get the most recently measured Z-axis acceleration as stored during the last call to |
chtjhai | 0:7d6134e052e0 | 121 | // ReadAccelerometer() |
chtjhai | 0:7d6134e052e0 | 122 | // |
chtjhai | 0:7d6134e052e0 | 123 | // Arguments: |
chtjhai | 0:7d6134e052e0 | 124 | // None |
chtjhai | 0:7d6134e052e0 | 125 | // |
chtjhai | 0:7d6134e052e0 | 126 | // Returns: |
chtjhai | 0:7d6134e052e0 | 127 | // The function returns the most recently measured Z-axis acceleration in units of m/s/s |
chtjhai | 0:7d6134e052e0 | 128 | virtual float GetAccelZ() const; |
chtjhai | 0:7d6134e052e0 | 129 | |
chtjhai | 0:7d6134e052e0 | 130 | |
chtjhai | 0:7d6134e052e0 | 131 | // Set the device to 'active' mode |
chtjhai | 0:7d6134e052e0 | 132 | // |
chtjhai | 0:7d6134e052e0 | 133 | // Arguments: |
chtjhai | 0:7d6134e052e0 | 134 | // None |
chtjhai | 0:7d6134e052e0 | 135 | // |
chtjhai | 0:7d6134e052e0 | 136 | // Returns: |
chtjhai | 0:7d6134e052e0 | 137 | // Nothing |
chtjhai | 0:7d6134e052e0 | 138 | void SetActiveMode(); |
chtjhai | 0:7d6134e052e0 | 139 | |
chtjhai | 0:7d6134e052e0 | 140 | |
chtjhai | 0:7d6134e052e0 | 141 | // Set the device sampling rate through AM bits, see MMA7660_SR_REG |
chtjhai | 0:7d6134e052e0 | 142 | // |
chtjhai | 0:7d6134e052e0 | 143 | // Arguments: |
chtjhai | 0:7d6134e052e0 | 144 | // samplingRate = setting of AM bits in MMA7660_SR_REG |
chtjhai | 0:7d6134e052e0 | 145 | // |
chtjhai | 0:7d6134e052e0 | 146 | // Returns: |
chtjhai | 0:7d6134e052e0 | 147 | // Nothing |
chtjhai | 0:7d6134e052e0 | 148 | void SetSamplingRateAM(MMA7660_AMSR_t samplingRate); |
chtjhai | 0:7d6134e052e0 | 149 | |
chtjhai | 0:7d6134e052e0 | 150 | }; |
chtjhai | 0:7d6134e052e0 | 151 | |
chtjhai | 0:7d6134e052e0 | 152 | #endif |
chtjhai | 0:7d6134e052e0 | 153 | |
chtjhai | 0:7d6134e052e0 | 154 |