Quick test of the Wi-Go Magnetometer

Dependencies:   TSI mbed MAG3110

Committer:
SomeRandomBloke
Date:
Mon May 27 18:58:26 2013 +0000
Revision:
6:dafb20e438bf
Parent:
4:e7831feff821
minor update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 2:c19e63728e2e 1 /**
SomeRandomBloke 2:c19e63728e2e 2 * Simple MAG3110 test/demo program, based on various bits of code found.
SomeRandomBloke 2:c19e63728e2e 3 * Updated for Mbed FRDM-KL25Z and Avnet Wi-Go module.
SomeRandomBloke 2:c19e63728e2e 4 *
SomeRandomBloke 2:c19e63728e2e 5 * Operation:
SomeRandomBloke 2:c19e63728e2e 6 * 1. On startup Red LED flashes indicating calibration mode entered
SomeRandomBloke 2:c19e63728e2e 7 * 2. Slide finger along capacitive sensor and release
SomeRandomBloke 2:c19e63728e2e 8 * 3. Green LED flashes indicating calibration mode.
SomeRandomBloke 2:c19e63728e2e 9 * 4. Rotate board once in horizontal plane
SomeRandomBloke 2:c19e63728e2e 10 * 5. Tap and release capacitive sensor. Board now calibrated with min/max values
SomeRandomBloke 2:c19e63728e2e 11 * 6. LEDs now off. Rotate board. When Blue LED lights the bottom of the board is
SomeRandomBloke 2:c19e63728e2e 12 * pointing to approximately North (+/- 22.5')
SomeRandomBloke 2:c19e63728e2e 13 *
SomeRandomBloke 2:c19e63728e2e 14 * If USB cable connected then you would see debug messages and other headings displayed, e.g. N, NE, E, SE, S, SW, W and NW.
SomeRandomBloke 2:c19e63728e2e 15 *
SomeRandomBloke 2:c19e63728e2e 16 * Also displays register values when starting.
SomeRandomBloke 2:c19e63728e2e 17 *
SomeRandomBloke 2:c19e63728e2e 18 * Ideally the calibration settings would be stored in flash/eeprom and retrieves each time.
SomeRandomBloke 2:c19e63728e2e 19 *
SomeRandomBloke 2:c19e63728e2e 20 * By Andrew D. Lindsay, @AndrewDLindsay
SomeRandomBloke 2:c19e63728e2e 21 *
SomeRandomBloke 3:945e32be0448 22 *
SomeRandomBloke 2:c19e63728e2e 23 *
SomeRandomBloke 2:c19e63728e2e 24 */
SomeRandomBloke 2:c19e63728e2e 25
SomeRandomBloke 0:26bfff579f01 26 #include "mbed.h"
SomeRandomBloke 1:d45fc466a46e 27 #include "TSISensor.h"
SomeRandomBloke 0:26bfff579f01 28 #include "math.h"
SomeRandomBloke 3:945e32be0448 29 #include "MAG3110.h"
SomeRandomBloke 0:26bfff579f01 30
SomeRandomBloke 0:26bfff579f01 31
SomeRandomBloke 2:c19e63728e2e 32 #define ON 0
SomeRandomBloke 2:c19e63728e2e 33 #define OFF 1
SomeRandomBloke 0:26bfff579f01 34
SomeRandomBloke 2:c19e63728e2e 35 // Some LEDs for showing status
SomeRandomBloke 0:26bfff579f01 36 DigitalOut redLed(LED_RED);
SomeRandomBloke 0:26bfff579f01 37 DigitalOut greenLed(LED_GREEN);
SomeRandomBloke 0:26bfff579f01 38 DigitalOut blueLed(LED_BLUE);
SomeRandomBloke 0:26bfff579f01 39
SomeRandomBloke 2:c19e63728e2e 40 // Slide sensor acts as a button
SomeRandomBloke 1:d45fc466a46e 41 TSISensor tsi;
SomeRandomBloke 2:c19e63728e2e 42
SomeRandomBloke 6:dafb20e438bf 43 //MAG3100 mag(PTE0, PTE1, &pc); //DEBUG verion
SomeRandomBloke 4:e7831feff821 44 MAG3110 mag(PTE0, PTE1);
SomeRandomBloke 3:945e32be0448 45
SomeRandomBloke 0:26bfff579f01 46
SomeRandomBloke 0:26bfff579f01 47
SomeRandomBloke 0:26bfff579f01 48 void calXY() //magnetometer calibration: finding max and min of X, Y axis
SomeRandomBloke 0:26bfff579f01 49 {
SomeRandomBloke 0:26bfff579f01 50 int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
SomeRandomBloke 3:945e32be0448 51
SomeRandomBloke 1:d45fc466a46e 52 redLed = ON;
SomeRandomBloke 1:d45fc466a46e 53
SomeRandomBloke 2:c19e63728e2e 54 printf("Waiting for initial press\n");
SomeRandomBloke 2:c19e63728e2e 55 // Wait for slider to be pressed
SomeRandomBloke 2:c19e63728e2e 56 while( tsi.readDistance() == 0 ) {
SomeRandomBloke 2:c19e63728e2e 57 redLed = ON;
SomeRandomBloke 2:c19e63728e2e 58 wait(0.2);
SomeRandomBloke 1:d45fc466a46e 59 redLed = OFF;
SomeRandomBloke 1:d45fc466a46e 60 wait(0.2);
SomeRandomBloke 1:d45fc466a46e 61 }
SomeRandomBloke 3:945e32be0448 62
SomeRandomBloke 2:c19e63728e2e 63 printf("Waiting for release\n");
SomeRandomBloke 1:d45fc466a46e 64
SomeRandomBloke 1:d45fc466a46e 65 // Wait for release
SomeRandomBloke 1:d45fc466a46e 66 while( tsi.readDistance() != 0 ) {
SomeRandomBloke 1:d45fc466a46e 67 redLed = OFF;
SomeRandomBloke 1:d45fc466a46e 68 wait(0.2);
SomeRandomBloke 1:d45fc466a46e 69 redLed = ON;
SomeRandomBloke 1:d45fc466a46e 70 wait(0.2);
SomeRandomBloke 1:d45fc466a46e 71 }
SomeRandomBloke 1:d45fc466a46e 72 redLed = OFF;
SomeRandomBloke 2:c19e63728e2e 73 wait(0.5);
SomeRandomBloke 3:945e32be0448 74
SomeRandomBloke 2:c19e63728e2e 75 printf("Rotate\n");
SomeRandomBloke 3:945e32be0448 76
SomeRandomBloke 3:945e32be0448 77 tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB);
SomeRandomBloke 3:945e32be0448 78 tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 2:c19e63728e2e 79
SomeRandomBloke 2:c19e63728e2e 80 while(tsi.readDistance() == 0) {
SomeRandomBloke 2:c19e63728e2e 81 greenLed = ON;
SomeRandomBloke 2:c19e63728e2e 82 wait(0.1);
SomeRandomBloke 2:c19e63728e2e 83 greenLed = OFF;
SomeRandomBloke 2:c19e63728e2e 84 wait(0.1);
SomeRandomBloke 3:945e32be0448 85 newX = mag.readVal(MAG_OUT_X_MSB);
SomeRandomBloke 3:945e32be0448 86 newY = mag.readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 2:c19e63728e2e 87 if (newX > tempXmax) tempXmax = newX;
SomeRandomBloke 2:c19e63728e2e 88 if (newX < tempXmin) tempXmin = newX;
SomeRandomBloke 2:c19e63728e2e 89 if (newY > tempYmax) tempYmax = newY;
SomeRandomBloke 2:c19e63728e2e 90 if (newY < tempYmin) tempYmin = newY;
SomeRandomBloke 0:26bfff579f01 91 }
SomeRandomBloke 3:945e32be0448 92
SomeRandomBloke 3:945e32be0448 93 mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
SomeRandomBloke 0:26bfff579f01 94
SomeRandomBloke 1:d45fc466a46e 95 // Wait for release
SomeRandomBloke 1:d45fc466a46e 96 while( tsi.readDistance() != 0 ) {
SomeRandomBloke 1:d45fc466a46e 97 greenLed = OFF;
SomeRandomBloke 1:d45fc466a46e 98 wait(0.2);
SomeRandomBloke 1:d45fc466a46e 99 greenLed = ON;
SomeRandomBloke 1:d45fc466a46e 100 wait(0.2);
SomeRandomBloke 1:d45fc466a46e 101 }
SomeRandomBloke 1:d45fc466a46e 102 greenLed = OFF;
SomeRandomBloke 1:d45fc466a46e 103 wait(1.0);
SomeRandomBloke 0:26bfff579f01 104 }
SomeRandomBloke 0:26bfff579f01 105
SomeRandomBloke 0:26bfff579f01 106 int main()
SomeRandomBloke 0:26bfff579f01 107 {
SomeRandomBloke 0:26bfff579f01 108 printf("MAG3110 Test\n");
SomeRandomBloke 0:26bfff579f01 109
SomeRandomBloke 1:d45fc466a46e 110 redLed = OFF;
SomeRandomBloke 1:d45fc466a46e 111 greenLed = OFF;
SomeRandomBloke 1:d45fc466a46e 112 blueLed = OFF;
SomeRandomBloke 3:945e32be0448 113
SomeRandomBloke 0:26bfff579f01 114 // Get some values
SomeRandomBloke 3:945e32be0448 115 printf("DR_STATUS %X\n", mag.readReg( MAG_DR_STATUS ));
SomeRandomBloke 3:945e32be0448 116 printf("WHO_AM_I %X\n", mag.readReg( MAG_WHO_AM_I ));
SomeRandomBloke 3:945e32be0448 117 printf("SYSMOD %X\n", mag.readReg( MAG_SYSMOD ));
SomeRandomBloke 3:945e32be0448 118 printf("DIE_TEMP %d\n", mag.readReg( MAG_DIE_TEMP ));
SomeRandomBloke 0:26bfff579f01 119
SomeRandomBloke 3:945e32be0448 120 printf("OFF_X %d\n", mag.readVal( MAG_OFF_X_MSB ));
SomeRandomBloke 3:945e32be0448 121 printf("OFF_Y %d\n", mag.readVal( MAG_OFF_Y_MSB ));
SomeRandomBloke 3:945e32be0448 122 printf("OFF_Z %d\n", mag.readVal( MAG_OFF_Z_MSB ));
SomeRandomBloke 0:26bfff579f01 123
SomeRandomBloke 3:945e32be0448 124 printf("CTRL_REG1 %X\n", mag.readReg( MAG_CTRL_REG1 ));
SomeRandomBloke 3:945e32be0448 125 printf("CTRL_REG2 %X\n", mag.readReg( MAG_CTRL_REG2 ));
SomeRandomBloke 0:26bfff579f01 126
SomeRandomBloke 0:26bfff579f01 127 printf("calibrate\n");
SomeRandomBloke 0:26bfff579f01 128 calXY();
SomeRandomBloke 0:26bfff579f01 129 printf("....Finished\n");
SomeRandomBloke 0:26bfff579f01 130
SomeRandomBloke 1:d45fc466a46e 131 redLed = OFF;
SomeRandomBloke 1:d45fc466a46e 132 greenLed = OFF;
SomeRandomBloke 1:d45fc466a46e 133 blueLed = OFF;
SomeRandomBloke 3:945e32be0448 134
SomeRandomBloke 0:26bfff579f01 135 while (1) {
SomeRandomBloke 0:26bfff579f01 136 wait(0.5);
SomeRandomBloke 3:945e32be0448 137 int xVal = mag.readVal(MAG_OUT_X_MSB);
SomeRandomBloke 3:945e32be0448 138 int yVal = mag.readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 4:e7831feff821 139 float heading = mag.getHeading();
SomeRandomBloke 3:945e32be0448 140
SomeRandomBloke 2:c19e63728e2e 141 // Do something with heading - display direction and turn on blue LED if heading approx north
SomeRandomBloke 3:945e32be0448 142 if (abs(heading) <= 22.5) {
SomeRandomBloke 3:945e32be0448 143 printf("N\n");
SomeRandomBloke 3:945e32be0448 144 blueLed = ON;
SomeRandomBloke 3:945e32be0448 145 } else blueLed = OFF;
SomeRandomBloke 0:26bfff579f01 146 if (abs(heading) >= 157.5) printf("S\n");
SomeRandomBloke 0:26bfff579f01 147 if (heading >= 67.5 && heading <= 112.5) printf("E \n");
SomeRandomBloke 0:26bfff579f01 148 if (heading <= -67.5 && heading >= -112.5) printf("W \n");
SomeRandomBloke 0:26bfff579f01 149 if (heading > 22.5 && heading < 67.5) printf("NE\n");
SomeRandomBloke 0:26bfff579f01 150 if (heading < -22.5 && heading > -67.5) printf("NW\n");
SomeRandomBloke 0:26bfff579f01 151 if (heading > 112.5 && heading < 157.5) printf("SE\n");
SomeRandomBloke 0:26bfff579f01 152 if (heading < -112.5 && heading > -157.5) printf("SW\n");
SomeRandomBloke 0:26bfff579f01 153
SomeRandomBloke 0:26bfff579f01 154 if (heading < 0) heading += 360.0;
SomeRandomBloke 0:26bfff579f01 155 printf("X = %d, Y = %d, Heading %f\n", xVal, yVal, heading);
SomeRandomBloke 0:26bfff579f01 156
SomeRandomBloke 0:26bfff579f01 157 }
SomeRandomBloke 0:26bfff579f01 158 }