read acceleration and angler ratio from mpu6050 and estimate pitch and roll angle
Dependencies: mbed
Diff: HMC5883L.cpp
- Revision:
- 3:40559ebef0f1
diff -r 4a6b46653abf -r 40559ebef0f1 HMC5883L.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HMC5883L.cpp Wed May 13 04:02:27 2015 +0000 @@ -0,0 +1,38 @@ +#include "mbed.h" +#include "myConstants.h" +#include "HMC5883L.h" + +HMC5883L::HMC5883L(I2C* i2c) { + this->i2c = i2c; +} + +HMC5883L::~HMC5883L() { + i2c = NULL; +} + +int HMC5883L::init() { + return 1; +} + +int HMC5883L::read() { + char cmd[2] = {0x02, 0x01}; + int ret = i2c->write(hmc_addr, cmd, 2); + if(ret != 0) return 0; + + cmd[0] = 0x03; + i2c->write(hmc_addr, cmd, 1); + i2c->read(hmc_addr | 0x01, data.reg, 6, true); + + for(int i=0; i<3; i++) { + char temp = 0; + temp = data.reg[i*2]; + data.reg[i*2] = data.reg[i*2+1]; + data.reg[i*2+1] = temp; + } + + int16_t temp = data.value[1]; + data.value[1] = data.value[2]; + data.value[2] = temp; + + return 1; +} \ No newline at end of file