MAG3110 Virgo robot adaptation

Fork of MAG3110 by JP PANG

Committer:
akashvibhute
Date:
Thu Aug 18 07:44:56 2016 +0000
Revision:
11:31b140f32906
Parent:
10:33eb06fe479c
Child:
12:2680e94139bb
fixed buggy pointer return

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:63a8594a3866 1 #include "MAG3110.h"
SomeRandomBloke 0:63a8594a3866 2
SomeRandomBloke 0:63a8594a3866 3 /******************************************************************************
SomeRandomBloke 0:63a8594a3866 4 * Constructors
SomeRandomBloke 0:63a8594a3866 5 ******************************************************************************/
akashvibhute 6:b2bb387caf3f 6 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl)
SomeRandomBloke 1:5a0e7a58d980 7 {
akashvibhute 11:31b140f32906 8
SomeRandomBloke 0:63a8594a3866 9 }
SomeRandomBloke 0:63a8594a3866 10
SomeRandomBloke 1:5a0e7a58d980 11 void MAG3110::begin()
SomeRandomBloke 0:63a8594a3866 12 {
SomeRandomBloke 1:5a0e7a58d980 13 char cmd[2];
akashvibhute 11:31b140f32906 14
akashvibhute 11:31b140f32906 15 cmd[0] = MAG_CTRL_REG1;
akashvibhute 11:31b140f32906 16 cmd[1] = 0x00; //Puts device in Standby mode
akashvibhute 11:31b140f32906 17 _i2c.write(MAG_ADDR, cmd,2);
SomeRandomBloke 0:63a8594a3866 18
SomeRandomBloke 1:5a0e7a58d980 19 cmd[0] = MAG_CTRL_REG2;
akashvibhute 11:31b140f32906 20 cmd[1] = 0x80; //Set Automatic Magnetic Sensor Reset bit
akashvibhute 6:b2bb387caf3f 21 _i2c.write(MAG_ADDR, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 22
SomeRandomBloke 1:5a0e7a58d980 23 cmd[0] = MAG_CTRL_REG1;
akashvibhute 11:31b140f32906 24 cmd[1] = 0x09; //Set ODR=40Hz and OSR=32 and set device to active mode.
akashvibhute 6:b2bb387caf3f 25 _i2c.write(MAG_ADDR, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 26
SomeRandomBloke 1:5a0e7a58d980 27 // No adjustment initially
SomeRandomBloke 1:5a0e7a58d980 28 _avgX = 0;
SomeRandomBloke 1:5a0e7a58d980 29 _avgY = 0;
SomeRandomBloke 0:63a8594a3866 30 }
SomeRandomBloke 0:63a8594a3866 31
SomeRandomBloke 0:63a8594a3866 32 // Read a single byte form 8 bit register, return as int
SomeRandomBloke 0:63a8594a3866 33 int MAG3110::readReg(char regAddr)
SomeRandomBloke 0:63a8594a3866 34 {
SomeRandomBloke 0:63a8594a3866 35 char cmd[1];
SomeRandomBloke 0:63a8594a3866 36
SomeRandomBloke 0:63a8594a3866 37 cmd[0] = regAddr;
akashvibhute 6:b2bb387caf3f 38 _i2c.write(MAG_ADDR, cmd, 1);
SomeRandomBloke 0:63a8594a3866 39
SomeRandomBloke 0:63a8594a3866 40 cmd[0] = 0x00;
akashvibhute 6:b2bb387caf3f 41 _i2c.read(MAG_ADDR, cmd, 1);
SomeRandomBloke 0:63a8594a3866 42 return (int)( cmd[0]);
SomeRandomBloke 0:63a8594a3866 43 }
SomeRandomBloke 0:63a8594a3866 44
SomeRandomBloke 0:63a8594a3866 45
SomeRandomBloke 0:63a8594a3866 46 // read a register per, pass first reg value, reading 2 bytes increments register
SomeRandomBloke 0:63a8594a3866 47 // Reads MSB first then LSB
SomeRandomBloke 0:63a8594a3866 48 int MAG3110::readVal(char regAddr)
SomeRandomBloke 0:63a8594a3866 49 {
SomeRandomBloke 0:63a8594a3866 50 char cmd[2];
SomeRandomBloke 0:63a8594a3866 51
SomeRandomBloke 0:63a8594a3866 52 cmd[0] = regAddr;
akashvibhute 6:b2bb387caf3f 53 _i2c.write(MAG_ADDR, cmd, 1);
SomeRandomBloke 0:63a8594a3866 54
SomeRandomBloke 0:63a8594a3866 55 cmd[0] = 0x00;
SomeRandomBloke 0:63a8594a3866 56 cmd[1] = 0x00;
akashvibhute 6:b2bb387caf3f 57 _i2c.read(MAG_ADDR, cmd, 2);
SomeRandomBloke 0:63a8594a3866 58 return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
SomeRandomBloke 0:63a8594a3866 59 }
SomeRandomBloke 0:63a8594a3866 60
SomeRandomBloke 0:63a8594a3866 61 void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
SomeRandomBloke 0:63a8594a3866 62 {
SomeRandomBloke 0:63a8594a3866 63 *xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 64 *yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 65 *zVal = readVal(MAG_OUT_Z_MSB);
SomeRandomBloke 0:63a8594a3866 66 }
SomeRandomBloke 0:63a8594a3866 67
SomeRandomBloke 0:63a8594a3866 68
akashvibhute 6:b2bb387caf3f 69 void MAG3110::setCalibration(float minX, float maxX, float minY, float maxY, float minZ, float maxZ)
SomeRandomBloke 0:63a8594a3866 70 {
SomeRandomBloke 0:63a8594a3866 71 _avgX=(maxX+minX)/2;
SomeRandomBloke 0:63a8594a3866 72 _avgY=(maxY+minY)/2;
akashvibhute 6:b2bb387caf3f 73 _avgZ=(maxZ+minZ)/2;
akashvibhute 6:b2bb387caf3f 74 }
akashvibhute 6:b2bb387caf3f 75
akashvibhute 11:31b140f32906 76 void MAG3110::get_uT(float * uT)
akashvibhute 6:b2bb387caf3f 77 {
akashvibhute 6:b2bb387caf3f 78 char data_bytes[7];
akashvibhute 6:b2bb387caf3f 79 char d[1];
akashvibhute 8:203af65371e8 80 d[0]=MAG_DR_STATUS;
akashvibhute 6:b2bb387caf3f 81
akashvibhute 6:b2bb387caf3f 82 _i2c.write(MAG_ADDR,d,1,true);
akashvibhute 6:b2bb387caf3f 83 _i2c.read(MAG_ADDR,data_bytes,7);// Read the 6 data bytes - LSB and MSB for X, Y and Z Axes.
akashvibhute 6:b2bb387caf3f 84
akashvibhute 11:31b140f32906 85 uT[0] = (float)( (int16_t)data_bytes[1] << 8 | (int16_t)data_bytes[2] ) * 0.1;
akashvibhute 11:31b140f32906 86 uT[1] = (float)( (int16_t)data_bytes[3] << 8 | (int16_t)data_bytes[4] ) * 0.1;
akashvibhute 11:31b140f32906 87 uT[2] = (float)( (int16_t)data_bytes[5] << 8 | (int16_t)data_bytes[6] ) * 0.1;
SomeRandomBloke 0:63a8594a3866 88 }
SomeRandomBloke 0:63a8594a3866 89
SomeRandomBloke 0:63a8594a3866 90
SomeRandomBloke 0:63a8594a3866 91