Added function to retrieve raw data from sensor
Dependents: KL46_eCompass KL46_eCompass_TiltCompensed_Acel-Mag Ragnarok_2ejes compass_acc ... more
Fork of MMA8451Q by
Revision 6:d3f7851ff32e, committed 2014-05-16
- Comitter:
- JimCarver
- Date:
- Fri May 16 18:21:49 2014 +0000
- Parent:
- 5:b8512e0de86b
- Commit message:
- Implements new virtual MotionSensor class
Changed in this revision
diff -r b8512e0de86b -r d3f7851ff32e MMA8451Q.cpp --- a/MMA8451Q.cpp Mon Apr 07 21:03:37 2014 +0000 +++ b/MMA8451Q.cpp Fri May 16 18:21:49 2014 +0000 @@ -17,66 +17,81 @@ */ #include "MMA8451Q.h" +#include "MotionSensor.h" -#define REG_WHO_AM_I 0x0D +#define REG_WHOAMI 0x0D #define REG_CTRL_REG_1 0x2A +#define REG_DR_STATUS 0x00 #define REG_OUT_X_MSB 0x01 #define REG_OUT_Y_MSB 0x03 #define REG_OUT_Z_MSB 0x05 #define UINT14_MAX 16383 +float countsperG = 4096.0f; // default 2G range + MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { // activate the peripheral - uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; + uint8_t data[2] = {REG_CTRL_REG_1, 0x00}; writeRegs(data, 2); } - MMA8451Q::~MMA8451Q() { } -uint8_t MMA8451Q::getWhoAmI() { - uint8_t who_am_i = 0; - readRegs(REG_WHO_AM_I, &who_am_i, 1); - return who_am_i; +void MMA8451Q::enable(void) { + uint8_t data[2]; + readRegs( REG_CTRL_REG_1, &data[1], 1); + data[1] |= 0x01; + data[0] = REG_CTRL_REG_1; + writeRegs(data, 2); } -float MMA8451Q::getAccX() { - return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); +void MMA8451Q::disable(void) { + uint8_t data[2]; + readRegs( REG_CTRL_REG_1, &data[1], 1); + data[1] &= 0xFE; + data[0] = REG_CTRL_REG_1; + writeRegs(data, 2); } -float MMA8451Q::getAccY() { - return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); -} - -float MMA8451Q::getAccZ() { - return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); +uint32_t MMA8451Q::whoAmI() { + uint8_t who_am_i = 0; + readRegs(REG_WHOAMI, &who_am_i, 1); + return (uint32_t) who_am_i; } +uint32_t MMA8451Q::dataReady(void) { + uint8_t stat = 0; + readRegs(REG_DR_STATUS, &stat, 1); + return (uint32_t) stat; +} -void MMA8451Q::getAccAllAxis(float * res) { - res[0] = getAccX(); - res[1] = getAccY(); - res[2] = getAccZ(); +uint32_t MMA8451Q::sampleRate(uint32_t f) { + return(50); // for now sample rate is fixed at 50Hz +} + +void MMA8451Q::getX(float * x) { + *x = (float(getAccAxis(REG_OUT_X_MSB)) / countsperG); } -void MMA8451Q::getAccXYZraw(int16_t * d) { - int16_t acc; - uint8_t res[6]; - readRegs(REG_OUT_X_MSB, res, 6); +void MMA8451Q::getY(float * y) { + *y = (float(getAccAxis(REG_OUT_Y_MSB)) / countsperG); +} + +void MMA8451Q::getZ(float * z) { + *z = (float(getAccAxis(REG_OUT_Z_MSB)) / countsperG); +} - acc = (res[0] << 6) | (res[1] >> 2); - if (acc > UINT14_MAX/2) - acc -= UINT14_MAX; - d[0] = acc; - acc = (res[2] << 6) | (res[3] >> 2); - if (acc > UINT14_MAX/2) - acc -= UINT14_MAX; - d[1] = acc; - acc = (res[4] << 6) | (res[5] >> 2); - if (acc > UINT14_MAX/2) - acc -= UINT14_MAX; - d[2] = acc; +void MMA8451Q::getX(int16_t * d) { + *d = getAccAxis(REG_OUT_X_MSB); +} + +void MMA8451Q::getY(int16_t * d) { + *d = getAccAxis(REG_OUT_Y_MSB); +} + +void MMA8451Q::getZ(int16_t * d) { + *d = getAccAxis(REG_OUT_Z_MSB); } int16_t MMA8451Q::getAccAxis(uint8_t addr) { @@ -84,11 +99,38 @@ uint8_t res[2]; readRegs(addr, res, 2); - acc = (res[0] << 6) | (res[1] >> 2); - if (acc > UINT14_MAX/2) - acc -= UINT14_MAX; + acc = (res[0] << 8) | res[1]; + acc = acc >> 2; + return acc; +} + + +void MMA8451Q::getAxis(MotionSensorDataUnits &data) { + int16_t t[3]; + uint8_t res[6]; - return acc; + readRegs(REG_OUT_X_MSB, res, 6); + t[0] = (res[0] << 8) | res[1]; + t[1] = (res[2] << 8) | res[3]; + t[2] = (res[4] << 8) | res[5]; + data.x = ((float) (t[0] >> 2)) / countsperG; + data.y = ((float) (t[1] >> 2)) / countsperG; + data.z = ((float) (t[2] >> 2)) / countsperG; +} + + +void MMA8451Q::getAxis(MotionSensorDataCounts &data) { + int16_t acc; + uint8_t res[6]; + readRegs(REG_OUT_X_MSB, res, 6); + + + acc = (res[0] << 8) | res[1]; + data.x = acc >> 2; + acc = (res[2] << 8) | res[3]; + data.y = acc >> 2; + acc = (res[4] << 8) | res[5]; + data.z = acc >> 2; } void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
diff -r b8512e0de86b -r d3f7851ff32e MMA8451Q.h --- a/MMA8451Q.h Mon Apr 07 21:03:37 2014 +0000 +++ b/MMA8451Q.h Fri May 16 18:21:49 2014 +0000 @@ -20,33 +20,10 @@ #define MMA8451Q_H #include "mbed.h" +#include "MotionSensor.h" -/** -* MMA8451Q accelerometer example -* -* @code -* #include "mbed.h" -* #include "MMA8451Q.h" -* -* #define MMA8451_I2C_ADDRESS (0x1d<<1) -* -* int main(void) { -* -* MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); -* PwmOut rled(LED_RED); -* PwmOut gled(LED_GREEN); -* PwmOut bled(LED_BLUE); -* -* while (true) { -* rled = 1.0 - abs(acc.getAccX()); -* gled = 1.0 - abs(acc.getAccY()); -* bled = 1.0 - abs(acc.getAccZ()); -* wait(0.1); -* } -* } -* @endcode -*/ -class MMA8451Q + +class MMA8451Q : public MotionSensor { public: /** @@ -63,47 +40,27 @@ */ ~MMA8451Q(); - /** - * Get the value of the WHO_AM_I register - * - * @returns WHO_AM_I value - */ - uint8_t getWhoAmI(); - - /** - * Get X axis acceleration - * - * @returns X axis acceleration - */ - float getAccX(); - - /** - * Get Y axis acceleration - * - * @returns Y axis acceleration - */ - float getAccY(); - - /** - * Get Z axis acceleration - * - * @returns Z axis acceleration - */ - float getAccZ(); - - /** - * Get XYZ axis acceleration - * - * @param res array where acceleration data will be stored - */ - void getAccAllAxis(float * res); - void getAccXYZraw(int16_t * d); + void enable(void); + void disable(void); + uint32_t sampleRate(uint32_t fequency); + uint32_t whoAmI(void); + uint32_t dataReady(void); + void getX(int16_t * x); + void getY(int16_t * y); + void getZ(int16_t * z); + void getX(float * x); + void getY(float * y); + void getZ(float * z); + void getAxis(MotionSensorDataCounts &data); + void getAxis(MotionSensorDataUnits &data); + void readRegs(int addr, uint8_t * data, int len); + private: I2C m_i2c; - int m_addr; - void readRegs(int addr, uint8_t * data, int len); + char m_addr; + int16_t getAccAxis(uint8_t addr); void writeRegs(uint8_t * data, int len); - int16_t getAccAxis(uint8_t addr); + };
diff -r b8512e0de86b -r d3f7851ff32e MotionSensor.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MotionSensor.lib Fri May 16 18:21:49 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/components/code/MotionSensor/#4d6e28d4a18a