MAG3110 Virgo robot adaptation
Fork of MAG3110 by
MAG3110.h@13:b293e595ea95, 2016-08-23 (annotated)
- Committer:
- akashvibhute
- Date:
- Tue Aug 23 03:54:19 2016 +0000
- Revision:
- 13:b293e595ea95
- Parent:
- 12:2680e94139bb
not using generalFunctions library, so commented it out...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akashvibhute | 7:14570d7e3335 | 1 | /* |
akashvibhute | 7:14570d7e3335 | 2 | * Copyright (C) 2016 Akash Vibhute <akash.roboticist@gmail.com> |
akashvibhute | 7:14570d7e3335 | 3 | * |
akashvibhute | 7:14570d7e3335 | 4 | * Library to handle MAG3110 magnetometer IMU on Virgo 3 robot. |
akashvibhute | 7:14570d7e3335 | 5 | * Based off various user libraries |
akashvibhute | 7:14570d7e3335 | 6 | * |
akashvibhute | 7:14570d7e3335 | 7 | * |
akashvibhute | 7:14570d7e3335 | 8 | * Initial Release: Aug/01/2016 |
akashvibhute | 7:14570d7e3335 | 9 | * |
SomeRandomBloke | 0:63a8594a3866 | 10 | */ |
SomeRandomBloke | 0:63a8594a3866 | 11 | |
SomeRandomBloke | 0:63a8594a3866 | 12 | #ifndef MAG3110_H |
SomeRandomBloke | 0:63a8594a3866 | 13 | #define MAG3110_H |
SomeRandomBloke | 0:63a8594a3866 | 14 | |
SomeRandomBloke | 0:63a8594a3866 | 15 | #include "mbed.h" |
akashvibhute | 13:b293e595ea95 | 16 | //#include "generalFunctions.h" |
SomeRandomBloke | 0:63a8594a3866 | 17 | |
akashvibhute | 6:b2bb387caf3f | 18 | #define MAG_ADDR (0x0E << 1) |
SomeRandomBloke | 0:63a8594a3866 | 19 | |
SomeRandomBloke | 0:63a8594a3866 | 20 | // define registers |
SomeRandomBloke | 0:63a8594a3866 | 21 | #define MAG_DR_STATUS 0x00 |
SomeRandomBloke | 0:63a8594a3866 | 22 | #define MAG_OUT_X_MSB 0x01 |
SomeRandomBloke | 0:63a8594a3866 | 23 | #define MAG_OUT_X_LSB 0x02 |
SomeRandomBloke | 0:63a8594a3866 | 24 | #define MAG_OUT_Y_MSB 0x03 |
SomeRandomBloke | 0:63a8594a3866 | 25 | #define MAG_OUT_Y_LSB 0x04 |
SomeRandomBloke | 0:63a8594a3866 | 26 | #define MAG_OUT_Z_MSB 0x05 |
SomeRandomBloke | 0:63a8594a3866 | 27 | #define MAG_OUT_Z_LSB 0x06 |
SomeRandomBloke | 0:63a8594a3866 | 28 | #define MAG_WHO_AM_I 0x07 |
SomeRandomBloke | 0:63a8594a3866 | 29 | #define MAG_SYSMOD 0x08 |
SomeRandomBloke | 0:63a8594a3866 | 30 | #define MAG_OFF_X_MSB 0x09 |
SomeRandomBloke | 0:63a8594a3866 | 31 | #define MAG_OFF_X_LSB 0x0A |
SomeRandomBloke | 0:63a8594a3866 | 32 | #define MAG_OFF_Y_MSB 0x0B |
SomeRandomBloke | 0:63a8594a3866 | 33 | #define MAG_OFF_Y_LSB 0x0C |
SomeRandomBloke | 0:63a8594a3866 | 34 | #define MAG_OFF_Z_MSB 0x0D |
SomeRandomBloke | 0:63a8594a3866 | 35 | #define MAG_OFF_Z_LSB 0x0E |
SomeRandomBloke | 0:63a8594a3866 | 36 | #define MAG_DIE_TEMP 0x0F |
SomeRandomBloke | 0:63a8594a3866 | 37 | #define MAG_CTRL_REG1 0x10 |
SomeRandomBloke | 0:63a8594a3866 | 38 | #define MAG_CTRL_REG2 0x11 |
SomeRandomBloke | 0:63a8594a3866 | 39 | |
SomeRandomBloke | 0:63a8594a3866 | 40 | // what should WHO_AM_I return? |
SomeRandomBloke | 0:63a8594a3866 | 41 | #define MAG_3110_WHO_AM_I_VALUE 0xC4 |
SomeRandomBloke | 0:63a8594a3866 | 42 | |
SomeRandomBloke | 0:63a8594a3866 | 43 | |
SomeRandomBloke | 0:63a8594a3866 | 44 | // Fields in registers |
SomeRandomBloke | 0:63a8594a3866 | 45 | // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac |
SomeRandomBloke | 0:63a8594a3866 | 46 | |
SomeRandomBloke | 0:63a8594a3866 | 47 | // Sampling rate from 80Hz down to 0.625Hz |
SomeRandomBloke | 0:63a8594a3866 | 48 | #define MAG_3110_SAMPLE80 0 |
SomeRandomBloke | 0:63a8594a3866 | 49 | #define MAG_3110_SAMPLE40 0x20 |
SomeRandomBloke | 0:63a8594a3866 | 50 | #define MAG_3110_SAMPLE20 0x40 |
SomeRandomBloke | 0:63a8594a3866 | 51 | #define MAG_3110_SAMPLE10 0x60 |
SomeRandomBloke | 0:63a8594a3866 | 52 | #define MAG_3110_SAMPLE5 0x80 |
SomeRandomBloke | 0:63a8594a3866 | 53 | #define MAG_3110_SAMPLE2_5 0xA0 |
SomeRandomBloke | 0:63a8594a3866 | 54 | #define MAG_3110_SAMPLE1_25 0xC0 |
SomeRandomBloke | 0:63a8594a3866 | 55 | #define MAG_3110_SAMPLE0_625 0xE0 |
SomeRandomBloke | 0:63a8594a3866 | 56 | |
SomeRandomBloke | 0:63a8594a3866 | 57 | // How many samples to average (lowers data rate) |
SomeRandomBloke | 0:63a8594a3866 | 58 | #define MAG_3110_OVERSAMPLE1 0 |
SomeRandomBloke | 0:63a8594a3866 | 59 | #define MAG_3110_OVERSAMPLE2 0x08 |
SomeRandomBloke | 0:63a8594a3866 | 60 | #define MAG_3110_OVERSAMPLE3 0x10 |
SomeRandomBloke | 0:63a8594a3866 | 61 | #define MAG_3110_OVERSAMPLE4 0x18 |
SomeRandomBloke | 0:63a8594a3866 | 62 | |
SomeRandomBloke | 0:63a8594a3866 | 63 | // read only 1 byte per axis |
SomeRandomBloke | 0:63a8594a3866 | 64 | #define MAG_3110_FASTREAD 0x04 |
SomeRandomBloke | 0:63a8594a3866 | 65 | // do one measurement (even if in standby mode) |
SomeRandomBloke | 0:63a8594a3866 | 66 | #define MAG_3110_TRIGGER 0x02 |
SomeRandomBloke | 0:63a8594a3866 | 67 | // put in active mode |
SomeRandomBloke | 0:63a8594a3866 | 68 | #define MAG_3110_ACTIVE 0x01 |
SomeRandomBloke | 0:63a8594a3866 | 69 | |
SomeRandomBloke | 0:63a8594a3866 | 70 | // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _ |
SomeRandomBloke | 0:63a8594a3866 | 71 | // reset sensor after each reading |
SomeRandomBloke | 0:63a8594a3866 | 72 | #define MAG_3110_AUTO_MRST_EN 0x80 |
SomeRandomBloke | 0:63a8594a3866 | 73 | // don't subtract user offsets |
SomeRandomBloke | 0:63a8594a3866 | 74 | #define MAG_3110_RAW 0x20 |
SomeRandomBloke | 0:63a8594a3866 | 75 | // reset magnetic sensor after too-large field |
SomeRandomBloke | 0:63a8594a3866 | 76 | #define MAG_3110_MAG_RST 0x10 |
SomeRandomBloke | 0:63a8594a3866 | 77 | |
SomeRandomBloke | 0:63a8594a3866 | 78 | // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR |
SomeRandomBloke | 0:63a8594a3866 | 79 | #define MAG_3110_ZYXDR 0x08 |
SomeRandomBloke | 0:63a8594a3866 | 80 | |
SomeRandomBloke | 3:bc784c24f3b2 | 81 | /** |
SomeRandomBloke | 3:bc784c24f3b2 | 82 | * MAG3110 Class to read X/Y/Z data from the magentometer |
SomeRandomBloke | 3:bc784c24f3b2 | 83 | * |
SomeRandomBloke | 3:bc784c24f3b2 | 84 | */ |
SomeRandomBloke | 0:63a8594a3866 | 85 | class MAG3110 |
SomeRandomBloke | 0:63a8594a3866 | 86 | { |
SomeRandomBloke | 1:5a0e7a58d980 | 87 | public: |
SomeRandomBloke | 1:5a0e7a58d980 | 88 | /** |
SomeRandomBloke | 4:cf40601402b7 | 89 | * Main constructor |
SomeRandomBloke | 1:5a0e7a58d980 | 90 | * @param sda SDA pin |
SomeRandomBloke | 1:5a0e7a58d980 | 91 | * @param sdl SCL pin |
SomeRandomBloke | 1:5a0e7a58d980 | 92 | */ |
akashvibhute | 6:b2bb387caf3f | 93 | MAG3110(PinName sda, PinName scl); |
SomeRandomBloke | 1:5a0e7a58d980 | 94 | /** |
SomeRandomBloke | 1:5a0e7a58d980 | 95 | * Setup the Magnetometer |
akashvibhute | 12:2680e94139bb | 96 | * @param off_x magnetometer offset for X-Axis |
akashvibhute | 12:2680e94139bb | 97 | * @param off_y magnetometer offset for Y-Axis |
akashvibhute | 12:2680e94139bb | 98 | * @param off_z magnetometer offset for Z-Axis |
SomeRandomBloke | 1:5a0e7a58d980 | 99 | * |
SomeRandomBloke | 1:5a0e7a58d980 | 100 | */ |
akashvibhute | 12:2680e94139bb | 101 | void begin(int16_t off_x, int16_t off_y, int16_t off_z); |
SomeRandomBloke | 1:5a0e7a58d980 | 102 | /** |
SomeRandomBloke | 1:5a0e7a58d980 | 103 | * Read a register, return its value as int |
SomeRandomBloke | 1:5a0e7a58d980 | 104 | * @param regAddr The address to read |
SomeRandomBloke | 1:5a0e7a58d980 | 105 | * @return value in register |
SomeRandomBloke | 1:5a0e7a58d980 | 106 | */ |
SomeRandomBloke | 1:5a0e7a58d980 | 107 | int readReg(char regAddr); |
SomeRandomBloke | 1:5a0e7a58d980 | 108 | /** |
SomeRandomBloke | 1:5a0e7a58d980 | 109 | * Read a value from a pair of registers, return as int |
SomeRandomBloke | 1:5a0e7a58d980 | 110 | * @param regAddr The address to read |
SomeRandomBloke | 1:5a0e7a58d980 | 111 | * @return Value from 2 consecutive registers |
SomeRandomBloke | 1:5a0e7a58d980 | 112 | */ |
SomeRandomBloke | 1:5a0e7a58d980 | 113 | int readVal(char regAddr); |
SomeRandomBloke | 1:5a0e7a58d980 | 114 | /** |
SomeRandomBloke | 1:5a0e7a58d980 | 115 | * Perform a read on the X, Y and Z values. |
SomeRandomBloke | 1:5a0e7a58d980 | 116 | * @param xVal Pointer to X value |
SomeRandomBloke | 1:5a0e7a58d980 | 117 | * @param yVal Pointer to Y value |
SomeRandomBloke | 1:5a0e7a58d980 | 118 | * @param zVal Pointer to Z value |
SomeRandomBloke | 1:5a0e7a58d980 | 119 | */ |
SomeRandomBloke | 1:5a0e7a58d980 | 120 | void getValues(int *xVal, int *yVal, int *zVal); |
SomeRandomBloke | 1:5a0e7a58d980 | 121 | /** |
SomeRandomBloke | 1:5a0e7a58d980 | 122 | * Set the calibration parameters if required. |
SomeRandomBloke | 1:5a0e7a58d980 | 123 | * @param minX Minimum value for X range |
SomeRandomBloke | 1:5a0e7a58d980 | 124 | * @param maxX Maximum value for X range |
SomeRandomBloke | 1:5a0e7a58d980 | 125 | * @param minY Minimum value for Y range |
SomeRandomBloke | 1:5a0e7a58d980 | 126 | * @param maxY maximum value for Y range |
akashvibhute | 6:b2bb387caf3f | 127 | * @param minZ Minimum value for Z range |
akashvibhute | 6:b2bb387caf3f | 128 | * @param maxZ maximum value for Z range |
SomeRandomBloke | 1:5a0e7a58d980 | 129 | */ |
akashvibhute | 6:b2bb387caf3f | 130 | void setCalibration(float minX, float maxX, float minY, float maxY, float minZ, float maxZ); |
akashvibhute | 6:b2bb387caf3f | 131 | /** |
akashvibhute | 6:b2bb387caf3f | 132 | * Acquire data from all axes in floating and return in uT, floating point format. |
akashvibhute | 6:b2bb387caf3f | 133 | * @param uT Pointer to 1x3 floating point array to store micro tesla values from X,Y,Z axes |
akashvibhute | 6:b2bb387caf3f | 134 | */ |
akashvibhute | 11:31b140f32906 | 135 | void get_uT(float * uT); |
SomeRandomBloke | 2:fb8024297377 | 136 | |
SomeRandomBloke | 2:fb8024297377 | 137 | private: |
SomeRandomBloke | 4:cf40601402b7 | 138 | I2C _i2c; |
akashvibhute | 6:b2bb387caf3f | 139 | float _avgX, _avgY, _avgZ; |
SomeRandomBloke | 2:fb8024297377 | 140 | |
SomeRandomBloke | 0:63a8594a3866 | 141 | }; |
SomeRandomBloke | 1:5a0e7a58d980 | 142 | #endif |