MAG3110 Virgo robot adaptation
Fork of MAG3110 by
MAG3110.cpp
- Committer:
- akashvibhute
- Date:
- 2016-08-17
- Revision:
- 10:33eb06fe479c
- Parent:
- 9:f86b9453bb46
- Child:
- 11:31b140f32906
File content as of revision 10:33eb06fe479c:
#include "MAG3110.h" /****************************************************************************** * Constructors ******************************************************************************/ MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl) { //begin(); } void MAG3110::begin() { char cmd[2]; cmd[0] = MAG_CTRL_REG2; cmd[1] = 0x80; _i2c.write(MAG_ADDR, cmd, 2); cmd[0] = MAG_CTRL_REG1; cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; _i2c.write(MAG_ADDR, cmd, 2); // No adjustment initially _avgX = 0; _avgY = 0; } // Read a single byte form 8 bit register, return as int int MAG3110::readReg(char regAddr) { char cmd[1]; cmd[0] = regAddr; _i2c.write(MAG_ADDR, cmd, 1); cmd[0] = 0x00; _i2c.read(MAG_ADDR, cmd, 1); return (int)( cmd[0]); } // read a register per, pass first reg value, reading 2 bytes increments register // Reads MSB first then LSB int MAG3110::readVal(char regAddr) { char cmd[2]; cmd[0] = regAddr; _i2c.write(MAG_ADDR, cmd, 1); cmd[0] = 0x00; cmd[1] = 0x00; _i2c.read(MAG_ADDR, cmd, 2); return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB } void MAG3110::getValues(int *xVal, int *yVal, int *zVal) { *xVal = readVal(MAG_OUT_X_MSB); *yVal = readVal(MAG_OUT_Y_MSB); *zVal = readVal(MAG_OUT_Z_MSB); } 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_x, float *uT_y, float *uT_z) { char data_bytes[7]; char d[1]; d[0]=MAG_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_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; }