MAG3110 Virgo robot adaptation
Fork of MAG3110 by
Diff: MAG3110.cpp
- Revision:
- 6:b2bb387caf3f
- Parent:
- 5:9644c7d596a1
- Child:
- 8:203af65371e8
--- a/MAG3110.cpp Thu Oct 10 02:22:13 2013 +0000 +++ b/MAG3110.cpp Mon Aug 01 02:50:19 2016 +0000 @@ -1,18 +1,9 @@ - #include "MAG3110.h" -#include "mbed.h" /****************************************************************************** * Constructors ******************************************************************************/ -MAG3110::MAG3110(PinName sda, PinName scl, int addr): _i2c(sda, scl), - _i2c_address(addr), _pc(NULL), _debug(false) -{ - begin(); -} - -MAG3110::MAG3110(PinName sda, PinName scl, int addr, Serial *pc): _i2c(sda, scl), - _i2c_address(addr), _pc(pc), _debug(true) +MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl) { begin(); } @@ -23,11 +14,11 @@ cmd[0] = MAG_CTRL_REG2; cmd[1] = 0x80; - _i2c.write(_i2c_address, cmd, 2); + _i2c.write(MAG_ADDR, cmd, 2); cmd[0] = MAG_CTRL_REG1; cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; - _i2c.write(_i2c_address, cmd, 2); + _i2c.write(MAG_ADDR, cmd, 2); // No adjustment initially _avgX = 0; @@ -40,10 +31,10 @@ char cmd[1]; cmd[0] = regAddr; - _i2c.write(_i2c_address, cmd, 1); + _i2c.write(MAG_ADDR, cmd, 1); cmd[0] = 0x00; - _i2c.read(_i2c_address, cmd, 1); + _i2c.read(MAG_ADDR, cmd, 1); return (int)( cmd[0]); } @@ -55,22 +46,14 @@ char cmd[2]; cmd[0] = regAddr; - _i2c.write(_i2c_address, cmd, 1); + _i2c.write(MAG_ADDR, cmd, 1); cmd[0] = 0x00; cmd[1] = 0x00; - _i2c.read(_i2c_address, cmd, 2); + _i2c.read(MAG_ADDR, cmd, 2); return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB } - -float MAG3110::getHeading() -{ - int xVal = readVal(MAG_OUT_X_MSB); - int yVal = readVal(MAG_OUT_Y_MSB); - return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI; -} - void MAG3110::getValues(int *xVal, int *yVal, int *zVal) { *xVal = readVal(MAG_OUT_X_MSB); @@ -79,13 +62,26 @@ } -void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY ) +void MAG3110::setCalibration(float minX, float maxX, float minY, float maxY, float minZ, float maxZ) { _avgX=(maxX+minX)/2; _avgY=(maxY+minY)/2; + _avgZ=(maxZ+minZ)/2; +} + +void MAG3110::get_uT(float *uT) +{ + char data_bytes[7]; + char d[1]; + d[0]=MAG3110_DR_STATUS; + + _i2c.write(MAG_ADDR,d,1,true); + _i2c.read(MAG_ADDR,data_bytes,7);// Read the 6 data bytes - LSB and MSB for X, Y and Z Axes. + + *uT[0] = (float)((int16_t)((data_bytes[1]*256) + (data_bytes[2]))) * 0.1; + *uT[1] = (float)((int16_t)((data_bytes[3]*256) + (data_bytes[4]))) * 0.1; + *uT[2] = (float)((int16_t)((data_bytes[5]*256) + (data_bytes[6]))) * 0.1; } - -