Quick test of the Wi-Go Magnetometer
Dependencies: TSI mbed MAG3110
main.cpp@4:e7831feff821, 2013-05-24 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Fri May 24 20:05:54 2013 +0000
- Revision:
- 4:e7831feff821
- Parent:
- 3:945e32be0448
- Child:
- 6:dafb20e438bf
updates
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:e7831feff821 | 32 | //#define PI 3.14159265359 |
SomeRandomBloke | 2:c19e63728e2e | 33 | #define ON 0 |
SomeRandomBloke | 2:c19e63728e2e | 34 | #define OFF 1 |
SomeRandomBloke | 0:26bfff579f01 | 35 | |
SomeRandomBloke | 2:c19e63728e2e | 36 | // Some LEDs for showing status |
SomeRandomBloke | 0:26bfff579f01 | 37 | DigitalOut redLed(LED_RED); |
SomeRandomBloke | 0:26bfff579f01 | 38 | DigitalOut greenLed(LED_GREEN); |
SomeRandomBloke | 0:26bfff579f01 | 39 | DigitalOut blueLed(LED_BLUE); |
SomeRandomBloke | 0:26bfff579f01 | 40 | |
SomeRandomBloke | 2:c19e63728e2e | 41 | // Slide sensor acts as a button |
SomeRandomBloke | 1:d45fc466a46e | 42 | TSISensor tsi; |
SomeRandomBloke | 2:c19e63728e2e | 43 | |
SomeRandomBloke | 3:945e32be0448 | 44 | //MAG3100 mag(&i2c, &pc); //DEBUG verion |
SomeRandomBloke | 4:e7831feff821 | 45 | MAG3110 mag(PTE0, PTE1); |
SomeRandomBloke | 3:945e32be0448 | 46 | |
SomeRandomBloke | 0:26bfff579f01 | 47 | |
SomeRandomBloke | 0:26bfff579f01 | 48 | |
SomeRandomBloke | 0:26bfff579f01 | 49 | void calXY() //magnetometer calibration: finding max and min of X, Y axis |
SomeRandomBloke | 0:26bfff579f01 | 50 | { |
SomeRandomBloke | 0:26bfff579f01 | 51 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
SomeRandomBloke | 3:945e32be0448 | 52 | |
SomeRandomBloke | 1:d45fc466a46e | 53 | redLed = ON; |
SomeRandomBloke | 1:d45fc466a46e | 54 | |
SomeRandomBloke | 2:c19e63728e2e | 55 | printf("Waiting for initial press\n"); |
SomeRandomBloke | 2:c19e63728e2e | 56 | // Wait for slider to be pressed |
SomeRandomBloke | 2:c19e63728e2e | 57 | while( tsi.readDistance() == 0 ) { |
SomeRandomBloke | 2:c19e63728e2e | 58 | redLed = ON; |
SomeRandomBloke | 2:c19e63728e2e | 59 | wait(0.2); |
SomeRandomBloke | 1:d45fc466a46e | 60 | redLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 61 | wait(0.2); |
SomeRandomBloke | 1:d45fc466a46e | 62 | } |
SomeRandomBloke | 3:945e32be0448 | 63 | |
SomeRandomBloke | 2:c19e63728e2e | 64 | printf("Waiting for release\n"); |
SomeRandomBloke | 1:d45fc466a46e | 65 | |
SomeRandomBloke | 1:d45fc466a46e | 66 | // Wait for release |
SomeRandomBloke | 1:d45fc466a46e | 67 | while( tsi.readDistance() != 0 ) { |
SomeRandomBloke | 1:d45fc466a46e | 68 | redLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 69 | wait(0.2); |
SomeRandomBloke | 1:d45fc466a46e | 70 | redLed = ON; |
SomeRandomBloke | 1:d45fc466a46e | 71 | wait(0.2); |
SomeRandomBloke | 1:d45fc466a46e | 72 | } |
SomeRandomBloke | 1:d45fc466a46e | 73 | redLed = OFF; |
SomeRandomBloke | 2:c19e63728e2e | 74 | wait(0.5); |
SomeRandomBloke | 3:945e32be0448 | 75 | |
SomeRandomBloke | 2:c19e63728e2e | 76 | printf("Rotate\n"); |
SomeRandomBloke | 3:945e32be0448 | 77 | |
SomeRandomBloke | 3:945e32be0448 | 78 | tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 3:945e32be0448 | 79 | tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 2:c19e63728e2e | 80 | |
SomeRandomBloke | 2:c19e63728e2e | 81 | while(tsi.readDistance() == 0) { |
SomeRandomBloke | 2:c19e63728e2e | 82 | greenLed = ON; |
SomeRandomBloke | 2:c19e63728e2e | 83 | wait(0.1); |
SomeRandomBloke | 2:c19e63728e2e | 84 | greenLed = OFF; |
SomeRandomBloke | 2:c19e63728e2e | 85 | wait(0.1); |
SomeRandomBloke | 3:945e32be0448 | 86 | newX = mag.readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 3:945e32be0448 | 87 | newY = mag.readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 2:c19e63728e2e | 88 | if (newX > tempXmax) tempXmax = newX; |
SomeRandomBloke | 2:c19e63728e2e | 89 | if (newX < tempXmin) tempXmin = newX; |
SomeRandomBloke | 2:c19e63728e2e | 90 | if (newY > tempYmax) tempYmax = newY; |
SomeRandomBloke | 2:c19e63728e2e | 91 | if (newY < tempYmin) tempYmin = newY; |
SomeRandomBloke | 0:26bfff579f01 | 92 | } |
SomeRandomBloke | 3:945e32be0448 | 93 | |
SomeRandomBloke | 3:945e32be0448 | 94 | mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
SomeRandomBloke | 0:26bfff579f01 | 95 | |
SomeRandomBloke | 1:d45fc466a46e | 96 | // Wait for release |
SomeRandomBloke | 1:d45fc466a46e | 97 | while( tsi.readDistance() != 0 ) { |
SomeRandomBloke | 1:d45fc466a46e | 98 | greenLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 99 | wait(0.2); |
SomeRandomBloke | 1:d45fc466a46e | 100 | greenLed = ON; |
SomeRandomBloke | 1:d45fc466a46e | 101 | wait(0.2); |
SomeRandomBloke | 1:d45fc466a46e | 102 | } |
SomeRandomBloke | 1:d45fc466a46e | 103 | greenLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 104 | wait(1.0); |
SomeRandomBloke | 0:26bfff579f01 | 105 | } |
SomeRandomBloke | 0:26bfff579f01 | 106 | |
SomeRandomBloke | 0:26bfff579f01 | 107 | int main() |
SomeRandomBloke | 0:26bfff579f01 | 108 | { |
SomeRandomBloke | 0:26bfff579f01 | 109 | printf("MAG3110 Test\n"); |
SomeRandomBloke | 0:26bfff579f01 | 110 | |
SomeRandomBloke | 1:d45fc466a46e | 111 | redLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 112 | greenLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 113 | blueLed = OFF; |
SomeRandomBloke | 3:945e32be0448 | 114 | |
SomeRandomBloke | 0:26bfff579f01 | 115 | // Get some values |
SomeRandomBloke | 3:945e32be0448 | 116 | printf("DR_STATUS %X\n", mag.readReg( MAG_DR_STATUS )); |
SomeRandomBloke | 3:945e32be0448 | 117 | printf("WHO_AM_I %X\n", mag.readReg( MAG_WHO_AM_I )); |
SomeRandomBloke | 3:945e32be0448 | 118 | printf("SYSMOD %X\n", mag.readReg( MAG_SYSMOD )); |
SomeRandomBloke | 3:945e32be0448 | 119 | printf("DIE_TEMP %d\n", mag.readReg( MAG_DIE_TEMP )); |
SomeRandomBloke | 0:26bfff579f01 | 120 | |
SomeRandomBloke | 3:945e32be0448 | 121 | printf("OFF_X %d\n", mag.readVal( MAG_OFF_X_MSB )); |
SomeRandomBloke | 3:945e32be0448 | 122 | printf("OFF_Y %d\n", mag.readVal( MAG_OFF_Y_MSB )); |
SomeRandomBloke | 3:945e32be0448 | 123 | printf("OFF_Z %d\n", mag.readVal( MAG_OFF_Z_MSB )); |
SomeRandomBloke | 0:26bfff579f01 | 124 | |
SomeRandomBloke | 3:945e32be0448 | 125 | printf("CTRL_REG1 %X\n", mag.readReg( MAG_CTRL_REG1 )); |
SomeRandomBloke | 3:945e32be0448 | 126 | printf("CTRL_REG2 %X\n", mag.readReg( MAG_CTRL_REG2 )); |
SomeRandomBloke | 0:26bfff579f01 | 127 | |
SomeRandomBloke | 0:26bfff579f01 | 128 | printf("calibrate\n"); |
SomeRandomBloke | 0:26bfff579f01 | 129 | calXY(); |
SomeRandomBloke | 0:26bfff579f01 | 130 | printf("....Finished\n"); |
SomeRandomBloke | 0:26bfff579f01 | 131 | |
SomeRandomBloke | 1:d45fc466a46e | 132 | redLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 133 | greenLed = OFF; |
SomeRandomBloke | 1:d45fc466a46e | 134 | blueLed = OFF; |
SomeRandomBloke | 3:945e32be0448 | 135 | |
SomeRandomBloke | 0:26bfff579f01 | 136 | while (1) { |
SomeRandomBloke | 0:26bfff579f01 | 137 | wait(0.5); |
SomeRandomBloke | 3:945e32be0448 | 138 | int xVal = mag.readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 3:945e32be0448 | 139 | int yVal = mag.readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 4:e7831feff821 | 140 | float heading = mag.getHeading(); |
SomeRandomBloke | 3:945e32be0448 | 141 | |
SomeRandomBloke | 2:c19e63728e2e | 142 | // Do something with heading - display direction and turn on blue LED if heading approx north |
SomeRandomBloke | 3:945e32be0448 | 143 | if (abs(heading) <= 22.5) { |
SomeRandomBloke | 3:945e32be0448 | 144 | printf("N\n"); |
SomeRandomBloke | 3:945e32be0448 | 145 | blueLed = ON; |
SomeRandomBloke | 3:945e32be0448 | 146 | } else blueLed = OFF; |
SomeRandomBloke | 0:26bfff579f01 | 147 | if (abs(heading) >= 157.5) printf("S\n"); |
SomeRandomBloke | 0:26bfff579f01 | 148 | if (heading >= 67.5 && heading <= 112.5) printf("E \n"); |
SomeRandomBloke | 0:26bfff579f01 | 149 | if (heading <= -67.5 && heading >= -112.5) printf("W \n"); |
SomeRandomBloke | 0:26bfff579f01 | 150 | if (heading > 22.5 && heading < 67.5) printf("NE\n"); |
SomeRandomBloke | 0:26bfff579f01 | 151 | if (heading < -22.5 && heading > -67.5) printf("NW\n"); |
SomeRandomBloke | 0:26bfff579f01 | 152 | if (heading > 112.5 && heading < 157.5) printf("SE\n"); |
SomeRandomBloke | 0:26bfff579f01 | 153 | if (heading < -112.5 && heading > -157.5) printf("SW\n"); |
SomeRandomBloke | 0:26bfff579f01 | 154 | |
SomeRandomBloke | 0:26bfff579f01 | 155 | if (heading < 0) heading += 360.0; |
SomeRandomBloke | 0:26bfff579f01 | 156 | printf("X = %d, Y = %d, Heading %f\n", xVal, yVal, heading); |
SomeRandomBloke | 0:26bfff579f01 | 157 | |
SomeRandomBloke | 0:26bfff579f01 | 158 | } |
SomeRandomBloke | 0:26bfff579f01 | 159 | } |