Simple library for MAG3110 magenetometer as built into Avnet Wi-Go module

Dependencies:   MotionSensor

Dependents:   Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more

Committer:
SomeRandomBloke
Date:
Fri May 09 18:01:36 2014 +0000
Revision:
7:0f45239e157a
Parent:
6:f510561f6107
Parent:
5:f3abe901c33a
merged pull request fro calXY

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:63a8594a3866 1
SomeRandomBloke 0:63a8594a3866 2 #include "MAG3110.h"
SomeRandomBloke 0:63a8594a3866 3 #include "mbed.h"
SomeRandomBloke 0:63a8594a3866 4
SomeRandomBloke 0:63a8594a3866 5 /******************************************************************************
SomeRandomBloke 0:63a8594a3866 6 * Constructors
SomeRandomBloke 0:63a8594a3866 7 ******************************************************************************/
SomeRandomBloke 1:5a0e7a58d980 8 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
JimCarver 5:f3abe901c33a 9 _i2c_address(0x1d), _pc(NULL), _debug(false)
SomeRandomBloke 7:0f45239e157a 10
SomeRandomBloke 0:63a8594a3866 11 {
SomeRandomBloke 1:5a0e7a58d980 12 begin();
SomeRandomBloke 1:5a0e7a58d980 13 }
SomeRandomBloke 0:63a8594a3866 14
SomeRandomBloke 1:5a0e7a58d980 15 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
JimCarver 5:f3abe901c33a 16 _i2c_address(0x1d), _pc(pc), _debug(true)
SomeRandomBloke 7:0f45239e157a 17
SomeRandomBloke 1:5a0e7a58d980 18 {
SomeRandomBloke 1:5a0e7a58d980 19 begin();
SomeRandomBloke 0:63a8594a3866 20 }
SomeRandomBloke 0:63a8594a3866 21
SomeRandomBloke 1:5a0e7a58d980 22 void MAG3110::begin()
SomeRandomBloke 0:63a8594a3866 23 {
SomeRandomBloke 1:5a0e7a58d980 24 char cmd[2];
SomeRandomBloke 0:63a8594a3866 25
SomeRandomBloke 1:5a0e7a58d980 26 cmd[0] = MAG_CTRL_REG2;
SomeRandomBloke 1:5a0e7a58d980 27 cmd[1] = 0x80;
SomeRandomBloke 1:5a0e7a58d980 28 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 29
SomeRandomBloke 1:5a0e7a58d980 30 cmd[0] = MAG_CTRL_REG1;
JimCarver 5:f3abe901c33a 31 cmd[1] = MAG_3110_ACTIVE;
SomeRandomBloke 1:5a0e7a58d980 32 _i2c.write(_i2c_address, cmd, 2);
mmaas 6:f510561f6107 33
SomeRandomBloke 1:5a0e7a58d980 34 // No adjustment initially
SomeRandomBloke 1:5a0e7a58d980 35 _avgX = 0;
SomeRandomBloke 1:5a0e7a58d980 36 _avgY = 0;
SomeRandomBloke 0:63a8594a3866 37 }
SomeRandomBloke 0:63a8594a3866 38
SomeRandomBloke 0:63a8594a3866 39 // Read a single byte form 8 bit register, return as int
SomeRandomBloke 0:63a8594a3866 40 int MAG3110::readReg(char regAddr)
SomeRandomBloke 0:63a8594a3866 41 {
SomeRandomBloke 0:63a8594a3866 42 char cmd[1];
SomeRandomBloke 0:63a8594a3866 43
SomeRandomBloke 0:63a8594a3866 44 cmd[0] = regAddr;
JimCarver 5:f3abe901c33a 45 if(_i2c.write(_i2c_address, cmd, 1)) {
JimCarver 5:f3abe901c33a 46 printf("MAG3110 write error\r\n");
JimCarver 5:f3abe901c33a 47 _i2c.stop();
JimCarver 5:f3abe901c33a 48 _i2c.start();
JimCarver 5:f3abe901c33a 49 }
SomeRandomBloke 0:63a8594a3866 50 cmd[0] = 0x00;
SomeRandomBloke 1:5a0e7a58d980 51 _i2c.read(_i2c_address, cmd, 1);
SomeRandomBloke 0:63a8594a3866 52 return (int)( cmd[0]);
SomeRandomBloke 0:63a8594a3866 53 }
SomeRandomBloke 0:63a8594a3866 54
SomeRandomBloke 0:63a8594a3866 55 // read a register per, pass first reg value, reading 2 bytes increments register
SomeRandomBloke 0:63a8594a3866 56 // Reads MSB first then LSB
SomeRandomBloke 0:63a8594a3866 57 int MAG3110::readVal(char regAddr)
SomeRandomBloke 0:63a8594a3866 58 {
SomeRandomBloke 0:63a8594a3866 59 char cmd[2];
JimCarver 5:f3abe901c33a 60 int16_t t;
SomeRandomBloke 0:63a8594a3866 61 cmd[0] = regAddr;
JimCarver 5:f3abe901c33a 62 if(_i2c.write(_i2c_address, cmd, 1)) {
JimCarver 5:f3abe901c33a 63 printf("MAG3110 write error\r\n");
JimCarver 5:f3abe901c33a 64 _i2c.stop();
JimCarver 5:f3abe901c33a 65 _i2c.start();
JimCarver 5:f3abe901c33a 66 }
SomeRandomBloke 0:63a8594a3866 67
SomeRandomBloke 0:63a8594a3866 68 cmd[0] = 0x00;
SomeRandomBloke 0:63a8594a3866 69 cmd[1] = 0x00;
SomeRandomBloke 1:5a0e7a58d980 70 _i2c.read(_i2c_address, cmd, 2);
JimCarver 5:f3abe901c33a 71 t = (cmd[0] * 256) + (unsigned short) cmd[1];
JimCarver 5:f3abe901c33a 72 return ((int) t); //concatenate the MSB and LSB
SomeRandomBloke 0:63a8594a3866 73 }
SomeRandomBloke 0:63a8594a3866 74
SomeRandomBloke 0:63a8594a3866 75
SomeRandomBloke 0:63a8594a3866 76 float MAG3110::getHeading()
SomeRandomBloke 0:63a8594a3866 77 {
SomeRandomBloke 0:63a8594a3866 78 int xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 79 int yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 80 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
SomeRandomBloke 0:63a8594a3866 81 }
SomeRandomBloke 0:63a8594a3866 82
SomeRandomBloke 0:63a8594a3866 83 void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
SomeRandomBloke 0:63a8594a3866 84 {
SomeRandomBloke 0:63a8594a3866 85 *xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 86 *yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 87 *zVal = readVal(MAG_OUT_Z_MSB);
SomeRandomBloke 0:63a8594a3866 88 }
SomeRandomBloke 0:63a8594a3866 89
JimCarver 5:f3abe901c33a 90 void MAG3110::ReadXYZ(float * mag)
JimCarver 5:f3abe901c33a 91 {
JimCarver 5:f3abe901c33a 92 int x, y, z;
JimCarver 5:f3abe901c33a 93 x = readVal(MAG_OUT_X_MSB);
JimCarver 5:f3abe901c33a 94 y = readVal(MAG_OUT_Y_MSB);
JimCarver 5:f3abe901c33a 95 z = readVal(MAG_OUT_Z_MSB);
JimCarver 5:f3abe901c33a 96 mag[0] = (float) x / 10.0;
JimCarver 5:f3abe901c33a 97 mag[1] = (float) y / 10.0;
JimCarver 5:f3abe901c33a 98 mag[2] = (float) z / 10.0;
JimCarver 5:f3abe901c33a 99
JimCarver 5:f3abe901c33a 100 }
JimCarver 5:f3abe901c33a 101
JimCarver 5:f3abe901c33a 102 void MAG3110::ReadXYZraw(int16_t * mag_raw)
JimCarver 5:f3abe901c33a 103 {
JimCarver 5:f3abe901c33a 104 mag_raw[0] = readVal(MAG_OUT_X_MSB);
JimCarver 5:f3abe901c33a 105 mag_raw[1] = readVal(MAG_OUT_Y_MSB);
JimCarver 5:f3abe901c33a 106 mag_raw[2] = readVal(MAG_OUT_Z_MSB);
JimCarver 5:f3abe901c33a 107 }
SomeRandomBloke 0:63a8594a3866 108
SomeRandomBloke 0:63a8594a3866 109 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
SomeRandomBloke 0:63a8594a3866 110 {
SomeRandomBloke 0:63a8594a3866 111 _avgX=(maxX+minX)/2;
SomeRandomBloke 0:63a8594a3866 112 _avgY=(maxY+minY)/2;
SomeRandomBloke 0:63a8594a3866 113 }
SomeRandomBloke 0:63a8594a3866 114
SomeRandomBloke 0:63a8594a3866 115
SomeRandomBloke 0:63a8594a3866 116
mmaas 6:f510561f6107 117 void MAG3110::calXY(PinName pin, int activeValue )
mmaas 6:f510561f6107 118 {
mmaas 6:f510561f6107 119 DigitalIn calPin(pin);
mmaas 6:f510561f6107 120 int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
mmaas 6:f510561f6107 121
mmaas 6:f510561f6107 122
mmaas 6:f510561f6107 123 // Wait for Button Press and Release before beginning calibration
mmaas 6:f510561f6107 124 while(calPin != activeValue) {}
mmaas 6:f510561f6107 125 while(calPin == activeValue) {}
SomeRandomBloke 0:63a8594a3866 126
SomeRandomBloke 0:63a8594a3866 127
mmaas 6:f510561f6107 128 // Read initial values of magnetomoter - read it here to create a slight delay for calPin to settle
mmaas 6:f510561f6107 129 tempXmax = tempXmin = readVal(MAG_OUT_X_MSB);
mmaas 6:f510561f6107 130 tempYmax = tempYmin = readVal(MAG_OUT_Y_MSB);
mmaas 6:f510561f6107 131
mmaas 6:f510561f6107 132 // Update min and max values until calPin asserted again
mmaas 6:f510561f6107 133 while(calPin != activeValue) {
mmaas 6:f510561f6107 134 newX = readVal(MAG_OUT_X_MSB);
mmaas 6:f510561f6107 135 newY = readVal(MAG_OUT_Y_MSB);
mmaas 6:f510561f6107 136 if (newX > tempXmax) tempXmax = newX;
mmaas 6:f510561f6107 137 if (newX < tempXmin) tempXmin = newX;
mmaas 6:f510561f6107 138 if (newY > tempYmax) tempYmax = newY;
mmaas 6:f510561f6107 139 if (newY < tempYmin) tempYmin = newY;
mmaas 6:f510561f6107 140 }
mmaas 6:f510561f6107 141
mmaas 6:f510561f6107 142 setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
mmaas 6:f510561f6107 143
mmaas 6:f510561f6107 144 }
mmaas 6:f510561f6107 145