MAG3110 Virgo robot adaptation
Fork of MAG3110 by
Diff: MAG3110.cpp
- Revision:
- 11:31b140f32906
- Parent:
- 10:33eb06fe479c
- Child:
- 12:2680e94139bb
--- a/MAG3110.cpp Wed Aug 17 05:55:13 2016 +0000 +++ b/MAG3110.cpp Thu Aug 18 07:44:56 2016 +0000 @@ -5,19 +5,23 @@ ******************************************************************************/ MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl) { - //begin(); + } void MAG3110::begin() { char cmd[2]; + + cmd[0] = MAG_CTRL_REG1; + cmd[1] = 0x00; //Puts device in Standby mode + _i2c.write(MAG_ADDR, cmd,2); cmd[0] = MAG_CTRL_REG2; - cmd[1] = 0x80; + cmd[1] = 0x80; //Set Automatic Magnetic Sensor Reset bit _i2c.write(MAG_ADDR, cmd, 2); cmd[0] = MAG_CTRL_REG1; - cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; + cmd[1] = 0x09; //Set ODR=40Hz and OSR=32 and set device to active mode. _i2c.write(MAG_ADDR, cmd, 2); // No adjustment initially @@ -69,7 +73,7 @@ _avgZ=(maxZ+minZ)/2; } -void MAG3110::get_uT(float *uT_x, float *uT_y, float *uT_z) +void MAG3110::get_uT(float * uT) { char data_bytes[7]; char d[1]; @@ -78,9 +82,9 @@ _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_x = (float)((int16_t)((data_bytes[1]*256) + (data_bytes[2]))) * 0.1; - *uT_y = (float)((int16_t)((data_bytes[3]*256) + (data_bytes[4]))) * 0.1; - *uT_y = (float)((int16_t)((data_bytes[5]*256) + (data_bytes[6]))) * 0.1; + uT[0] = (float)( (int16_t)data_bytes[1] << 8 | (int16_t)data_bytes[2] ) * 0.1; + uT[1] = (float)( (int16_t)data_bytes[3] << 8 | (int16_t)data_bytes[4] ) * 0.1; + uT[2] = (float)( (int16_t)data_bytes[5] << 8 | (int16_t)data_bytes[6] ) * 0.1; }