Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
Sensors/HMC5843/HMC5843.h@25:bb5356402687, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 16:11:53 2018 +0000
- Revision:
- 25:bb5356402687
- Parent:
- 0:a6a169de725f
Initial publish of revised version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a6a169de725f | 1 | /** |
shimniok | 0:a6a169de725f | 2 | * @author Uwe Gartmann |
shimniok | 0:a6a169de725f | 3 | * @author Used HMC5843 library developed by Jose R. Padron and Aaron Berk as template |
shimniok | 0:a6a169de725f | 4 | * |
shimniok | 0:a6a169de725f | 5 | * @section LICENSE |
shimniok | 0:a6a169de725f | 6 | * |
shimniok | 0:a6a169de725f | 7 | * Copyright (c) 2010 ARM Limited |
shimniok | 0:a6a169de725f | 8 | * |
shimniok | 0:a6a169de725f | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
shimniok | 0:a6a169de725f | 10 | * of this software and associated documentation files (the "Software"), to deal |
shimniok | 0:a6a169de725f | 11 | * in the Software without restriction, including without limitation the rights |
shimniok | 0:a6a169de725f | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
shimniok | 0:a6a169de725f | 13 | * copies of the Software, and to permit persons to whom the Software is |
shimniok | 0:a6a169de725f | 14 | * furnished to do so, subject to the following conditions: |
shimniok | 0:a6a169de725f | 15 | * |
shimniok | 0:a6a169de725f | 16 | * The above copyright notice and this permission notice shall be included in |
shimniok | 0:a6a169de725f | 17 | * all copies or substantial portions of the Software. |
shimniok | 0:a6a169de725f | 18 | * |
shimniok | 0:a6a169de725f | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
shimniok | 0:a6a169de725f | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
shimniok | 0:a6a169de725f | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
shimniok | 0:a6a169de725f | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
shimniok | 0:a6a169de725f | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
shimniok | 0:a6a169de725f | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
shimniok | 0:a6a169de725f | 25 | * THE SOFTWARE. |
shimniok | 0:a6a169de725f | 26 | * |
shimniok | 0:a6a169de725f | 27 | * @section DESCRIPTION |
shimniok | 0:a6a169de725f | 28 | * |
shimniok | 0:a6a169de725f | 29 | * Honeywell HMC5843 digital compass. |
shimniok | 0:a6a169de725f | 30 | * |
shimniok | 0:a6a169de725f | 31 | * Datasheet: |
shimniok | 0:a6a169de725f | 32 | * |
shimniok | 0:a6a169de725f | 33 | * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf |
shimniok | 0:a6a169de725f | 34 | */ |
shimniok | 0:a6a169de725f | 35 | |
shimniok | 0:a6a169de725f | 36 | #ifndef HMC5843_H |
shimniok | 0:a6a169de725f | 37 | #define HMC5843_H |
shimniok | 0:a6a169de725f | 38 | |
shimniok | 0:a6a169de725f | 39 | #include "mbed.h" |
shimniok | 0:a6a169de725f | 40 | |
shimniok | 0:a6a169de725f | 41 | #define HMC5843_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read. |
shimniok | 0:a6a169de725f | 42 | #define HMC5843_I2C_WRITE 0x3C |
shimniok | 0:a6a169de725f | 43 | #define HMC5843_I2C_READ 0x3D |
shimniok | 0:a6a169de725f | 44 | |
shimniok | 0:a6a169de725f | 45 | //Values Config A |
shimniok | 0:a6a169de725f | 46 | #define HMC5843_0_5HZ_NORMAL 0x00 |
shimniok | 0:a6a169de725f | 47 | #define HMC5843_0_5HZ_POSITIVE 0x01 |
shimniok | 0:a6a169de725f | 48 | #define HMC5843_0_5HZ_NEGATIVE 0x02 |
shimniok | 0:a6a169de725f | 49 | |
shimniok | 0:a6a169de725f | 50 | #define HMC5843_1HZ_NORMAL 0x04 |
shimniok | 0:a6a169de725f | 51 | #define HMC5843_1HZ_POSITIVE 0x05 |
shimniok | 0:a6a169de725f | 52 | #define HMC5843_1HZ_NEGATIVE 0x06 |
shimniok | 0:a6a169de725f | 53 | |
shimniok | 0:a6a169de725f | 54 | #define HMC5843_2HZ_NORMAL 0x08 |
shimniok | 0:a6a169de725f | 55 | #define HMC5843_2HZ_POSITIVE 0x09 |
shimniok | 0:a6a169de725f | 56 | #define HMC5843_2HZ_NEGATIVE 0x0A |
shimniok | 0:a6a169de725f | 57 | |
shimniok | 0:a6a169de725f | 58 | #define HMC5843_5HZ_NORMAL 0x0C |
shimniok | 0:a6a169de725f | 59 | #define HMC5843_5HZ_POSITIVE 0x0D |
shimniok | 0:a6a169de725f | 60 | #define HMC5843_5HZ_NEGATIVE 0x0E |
shimniok | 0:a6a169de725f | 61 | |
shimniok | 0:a6a169de725f | 62 | #define HMC5843_10HZ_NORMAL 0x10 |
shimniok | 0:a6a169de725f | 63 | #define HMC5843_10HZ_POSITIVE 0x11 |
shimniok | 0:a6a169de725f | 64 | #define HMC5843_10HZ_NEGATIVE 0x12 |
shimniok | 0:a6a169de725f | 65 | |
shimniok | 0:a6a169de725f | 66 | #define HMC5843_20HZ_NORMAL 0x14 |
shimniok | 0:a6a169de725f | 67 | #define HMC5843_20HZ_POSITIVE 0x15 |
shimniok | 0:a6a169de725f | 68 | #define HMC5843_20HZ_NEGATIVE 0x16 |
shimniok | 0:a6a169de725f | 69 | |
shimniok | 0:a6a169de725f | 70 | #define HMC5843_50HZ_NORMAL 0x18 |
shimniok | 0:a6a169de725f | 71 | #define HMC5843_50HZ_POSITIVE 0x19 |
shimniok | 0:a6a169de725f | 72 | #define HMC5843_50HZ_NEGATIVE 0x1A |
shimniok | 0:a6a169de725f | 73 | |
shimniok | 0:a6a169de725f | 74 | //Values Config B |
shimniok | 0:a6a169de725f | 75 | #define HMC5843_0_7GA 0x00 |
shimniok | 0:a6a169de725f | 76 | #define HMC5843_1_0GA 0x20 |
shimniok | 0:a6a169de725f | 77 | #define HMC5843_1_5GA 0x40 |
shimniok | 0:a6a169de725f | 78 | #define HMC5843_2_0GA 0x60 |
shimniok | 0:a6a169de725f | 79 | #define HMC5843_3_2GA 0x80 |
shimniok | 0:a6a169de725f | 80 | #define HMC5843_3_8GA 0xA0 |
shimniok | 0:a6a169de725f | 81 | #define HMC5843_4_5GA 0xC0 |
shimniok | 0:a6a169de725f | 82 | #define HMC5843_6_5GA 0xE0 |
shimniok | 0:a6a169de725f | 83 | |
shimniok | 0:a6a169de725f | 84 | //Values MODE |
shimniok | 0:a6a169de725f | 85 | #define HMC5843_CONTINUOUS 0x00 |
shimniok | 0:a6a169de725f | 86 | #define HMC5843_SINGLE 0x01 |
shimniok | 0:a6a169de725f | 87 | #define HMC5843_IDLE 0x02 |
shimniok | 0:a6a169de725f | 88 | #define HMC5843_SLEEP 0x03 |
shimniok | 0:a6a169de725f | 89 | |
shimniok | 0:a6a169de725f | 90 | |
shimniok | 0:a6a169de725f | 91 | |
shimniok | 0:a6a169de725f | 92 | #define HMC5843_CONFIG_A 0x00 |
shimniok | 0:a6a169de725f | 93 | #define HMC5843_CONFIG_B 0x01 |
shimniok | 0:a6a169de725f | 94 | #define HMC5843_MODE 0x02 |
shimniok | 0:a6a169de725f | 95 | #define HMC5843_X_MSB 0x03 |
shimniok | 0:a6a169de725f | 96 | #define HMC5843_X_LSB 0x04 |
shimniok | 0:a6a169de725f | 97 | #define HMC5843_Y_MSB 0x05 |
shimniok | 0:a6a169de725f | 98 | #define HMC5843_Y_LSB 0x06 |
shimniok | 0:a6a169de725f | 99 | #define HMC5843_Z_MSB 0x07 |
shimniok | 0:a6a169de725f | 100 | #define HMC5843_Z_LSB 0x08 |
shimniok | 0:a6a169de725f | 101 | #define HMC5843_STATUS 0x09 |
shimniok | 0:a6a169de725f | 102 | #define HMC5843_IDENT_A 0x0A |
shimniok | 0:a6a169de725f | 103 | #define HMC5843_IDENT_B 0x0B |
shimniok | 0:a6a169de725f | 104 | #define HMC5843_IDENT_C 0x0C |
shimniok | 0:a6a169de725f | 105 | |
shimniok | 0:a6a169de725f | 106 | |
shimniok | 0:a6a169de725f | 107 | |
shimniok | 0:a6a169de725f | 108 | /** |
shimniok | 0:a6a169de725f | 109 | * Interface library for the Honeywell HMC5843 digital compass. |
shimniok | 0:a6a169de725f | 110 | * @author Michael Shimniok http://www.bot-thoughts.com/ |
shimniok | 0:a6a169de725f | 111 | * @author Based on library by Uwe Gartmann |
shimniok | 0:a6a169de725f | 112 | * @author In turn based on HMC5843 library developed by Jose R. Padron and Aaron Berk as template |
shimniok | 0:a6a169de725f | 113 | * |
shimniok | 0:a6a169de725f | 114 | * This version is modified to follow the same member functions of my LSM303DLH library. The intent |
shimniok | 0:a6a169de725f | 115 | * is to make it drop in compatible with my other magnetometer libraries. For now, setScale() and |
shimniok | 0:a6a169de725f | 116 | * setOffset() have no effect at this time. |
shimniok | 0:a6a169de725f | 117 | * |
shimniok | 0:a6a169de725f | 118 | * @code |
shimniok | 0:a6a169de725f | 119 | * #include "mbed.h" |
shimniok | 0:a6a169de725f | 120 | * #include "HMC5843.h" |
shimniok | 0:a6a169de725f | 121 | * |
shimniok | 0:a6a169de725f | 122 | * HMC5843 compass(p28, p27); |
shimniok | 0:a6a169de725f | 123 | * ... |
shimniok | 0:a6a169de725f | 124 | * int m[3]; |
shimniok | 0:a6a169de725f | 125 | * ... |
shimniok | 0:a6a169de725f | 126 | * compass.readMag(m); |
shimniok | 0:a6a169de725f | 127 | * |
shimniok | 0:a6a169de725f | 128 | * @endcode |
shimniok | 0:a6a169de725f | 129 | */ |
shimniok | 0:a6a169de725f | 130 | class HMC5843 { |
shimniok | 0:a6a169de725f | 131 | |
shimniok | 0:a6a169de725f | 132 | public: |
shimniok | 0:a6a169de725f | 133 | |
shimniok | 0:a6a169de725f | 134 | /** |
shimniok | 0:a6a169de725f | 135 | * Create a new interface for an HMC5843. |
shimniok | 0:a6a169de725f | 136 | * |
shimniok | 0:a6a169de725f | 137 | * @param sda mbed pin to use for SDA line of I2C interface. |
shimniok | 0:a6a169de725f | 138 | * @param scl mbed pin to use for SCL line of I2C interface. |
shimniok | 0:a6a169de725f | 139 | */ |
shimniok | 0:a6a169de725f | 140 | HMC5843(PinName sda, PinName scl); |
shimniok | 0:a6a169de725f | 141 | |
shimniok | 0:a6a169de725f | 142 | |
shimniok | 0:a6a169de725f | 143 | /** sets the x, y, and z offset corrections for hard iron calibration |
shimniok | 0:a6a169de725f | 144 | * |
shimniok | 0:a6a169de725f | 145 | * Calibration details here: |
shimniok | 0:a6a169de725f | 146 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
shimniok | 0:a6a169de725f | 147 | * |
shimniok | 0:a6a169de725f | 148 | * If you gather raw magnetometer data and find, for example, x is offset |
shimniok | 0:a6a169de725f | 149 | * by hard iron by -20 then pass +20 to this member function to correct |
shimniok | 0:a6a169de725f | 150 | * for hard iron. |
shimniok | 0:a6a169de725f | 151 | * |
shimniok | 0:a6a169de725f | 152 | * @param x is the offset correction for the x axis |
shimniok | 0:a6a169de725f | 153 | * @param y is the offset correction for the y axis |
shimniok | 0:a6a169de725f | 154 | * @param z is the offset correction for the z axis |
shimniok | 0:a6a169de725f | 155 | */ |
shimniok | 0:a6a169de725f | 156 | void setOffset(float x, float y, float z); |
shimniok | 0:a6a169de725f | 157 | |
shimniok | 0:a6a169de725f | 158 | |
shimniok | 0:a6a169de725f | 159 | /** sets the scale factor for the x, y, and z axes |
shimniok | 0:a6a169de725f | 160 | * |
shimniok | 0:a6a169de725f | 161 | * Calibratio details here: |
shimniok | 0:a6a169de725f | 162 | * http://mbed.org/users/shimniok/notebook/quick-and-dirty-3d-compass-calibration/ |
shimniok | 0:a6a169de725f | 163 | * |
shimniok | 0:a6a169de725f | 164 | * Sensitivity of the three axes is never perfectly identical and this |
shimniok | 0:a6a169de725f | 165 | * function can help to correct differences in sensitivity. You're |
shimniok | 0:a6a169de725f | 166 | * supplying a multipler such that x, y and z will be normalized to the |
shimniok | 0:a6a169de725f | 167 | * same max/min values |
shimniok | 0:a6a169de725f | 168 | */ |
shimniok | 0:a6a169de725f | 169 | void setScale(float x, float y, float z); |
shimniok | 0:a6a169de725f | 170 | |
shimniok | 0:a6a169de725f | 171 | |
shimniok | 0:a6a169de725f | 172 | /** |
shimniok | 0:a6a169de725f | 173 | * Enter into sleep mode. |
shimniok | 0:a6a169de725f | 174 | */ |
shimniok | 0:a6a169de725f | 175 | void setSleepMode(); |
shimniok | 0:a6a169de725f | 176 | |
shimniok | 0:a6a169de725f | 177 | |
shimniok | 0:a6a169de725f | 178 | /** |
shimniok | 0:a6a169de725f | 179 | * Set Device in Default Mode. |
shimniok | 0:a6a169de725f | 180 | * HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA |
shimniok | 0:a6a169de725f | 181 | */ |
shimniok | 0:a6a169de725f | 182 | void setDefault(); |
shimniok | 0:a6a169de725f | 183 | |
shimniok | 0:a6a169de725f | 184 | |
shimniok | 0:a6a169de725f | 185 | /** |
shimniok | 0:a6a169de725f | 186 | * Set the operation mode. |
shimniok | 0:a6a169de725f | 187 | * |
shimniok | 0:a6a169de725f | 188 | * @param mode 0x00 -> Continuous |
shimniok | 0:a6a169de725f | 189 | * 0x01 -> Single |
shimniok | 0:a6a169de725f | 190 | * 0x02 -> Idle |
shimniok | 0:a6a169de725f | 191 | * @param ConfigA values |
shimniok | 0:a6a169de725f | 192 | * @param ConfigB values |
shimniok | 0:a6a169de725f | 193 | */ |
shimniok | 0:a6a169de725f | 194 | void setOpMode(int mode, int ConfigA, int ConfigB); |
shimniok | 0:a6a169de725f | 195 | |
shimniok | 0:a6a169de725f | 196 | |
shimniok | 0:a6a169de725f | 197 | /** |
shimniok | 0:a6a169de725f | 198 | * Write to a register on the device. |
shimniok | 0:a6a169de725f | 199 | * |
shimniok | 0:a6a169de725f | 200 | * @param address is the register address to be written. |
shimniok | 0:a6a169de725f | 201 | * @param data is the data to write. |
shimniok | 0:a6a169de725f | 202 | */ |
shimniok | 0:a6a169de725f | 203 | void write(int address, int data); |
shimniok | 0:a6a169de725f | 204 | |
shimniok | 0:a6a169de725f | 205 | |
shimniok | 0:a6a169de725f | 206 | /** read the calibrated accelerometer and magnetometer values |
shimniok | 0:a6a169de725f | 207 | * |
shimniok | 0:a6a169de725f | 208 | * @param m is the magnetometer 3d vector, written by the function |
shimniok | 0:a6a169de725f | 209 | */ |
shimniok | 0:a6a169de725f | 210 | void read(int m[3]); |
shimniok | 0:a6a169de725f | 211 | |
shimniok | 0:a6a169de725f | 212 | |
shimniok | 0:a6a169de725f | 213 | /** read the calibrated magnetometer values |
shimniok | 0:a6a169de725f | 214 | * |
shimniok | 0:a6a169de725f | 215 | * @param m is the magnetometer 3d vector, written by the function |
shimniok | 0:a6a169de725f | 216 | */ |
shimniok | 0:a6a169de725f | 217 | void readMag(int m[3]); |
shimniok | 0:a6a169de725f | 218 | |
shimniok | 0:a6a169de725f | 219 | |
shimniok | 0:a6a169de725f | 220 | /** |
shimniok | 0:a6a169de725f | 221 | * Get the output of all three axes. |
shimniok | 0:a6a169de725f | 222 | * |
shimniok | 0:a6a169de725f | 223 | * @param Pointer to a buffer to hold the magnetics value for the |
shimniok | 0:a6a169de725f | 224 | * x-axis, y-axis and z-axis [in that order]. |
shimniok | 0:a6a169de725f | 225 | */ |
shimniok | 0:a6a169de725f | 226 | void readData(int* readings); |
shimniok | 0:a6a169de725f | 227 | |
shimniok | 0:a6a169de725f | 228 | |
shimniok | 0:a6a169de725f | 229 | /** |
shimniok | 0:a6a169de725f | 230 | * Get the output of X axis. |
shimniok | 0:a6a169de725f | 231 | * |
shimniok | 0:a6a169de725f | 232 | * @return x-axis magnetic value |
shimniok | 0:a6a169de725f | 233 | */ |
shimniok | 0:a6a169de725f | 234 | int getMx(); |
shimniok | 0:a6a169de725f | 235 | |
shimniok | 0:a6a169de725f | 236 | |
shimniok | 0:a6a169de725f | 237 | /** |
shimniok | 0:a6a169de725f | 238 | * Get the output of Y axis. |
shimniok | 0:a6a169de725f | 239 | * |
shimniok | 0:a6a169de725f | 240 | * @return y-axis magnetic value |
shimniok | 0:a6a169de725f | 241 | */ |
shimniok | 0:a6a169de725f | 242 | int getMy(); |
shimniok | 0:a6a169de725f | 243 | |
shimniok | 0:a6a169de725f | 244 | |
shimniok | 0:a6a169de725f | 245 | /** |
shimniok | 0:a6a169de725f | 246 | * Get the output of Z axis. |
shimniok | 0:a6a169de725f | 247 | * |
shimniok | 0:a6a169de725f | 248 | * @return z-axis magnetic value |
shimniok | 0:a6a169de725f | 249 | */ |
shimniok | 0:a6a169de725f | 250 | int getMz(); |
shimniok | 0:a6a169de725f | 251 | |
shimniok | 0:a6a169de725f | 252 | /** |
shimniok | 0:a6a169de725f | 253 | * Get the current operation mode. |
shimniok | 0:a6a169de725f | 254 | * |
shimniok | 0:a6a169de725f | 255 | * @return Status register values |
shimniok | 0:a6a169de725f | 256 | */ |
shimniok | 0:a6a169de725f | 257 | int getStatus(void); |
shimniok | 0:a6a169de725f | 258 | |
shimniok | 0:a6a169de725f | 259 | |
shimniok | 0:a6a169de725f | 260 | /** |
shimniok | 0:a6a169de725f | 261 | * Read the memory location on the device which contains the address. |
shimniok | 0:a6a169de725f | 262 | * |
shimniok | 0:a6a169de725f | 263 | * @param Pointer to a buffer to hold the address value |
shimniok | 0:a6a169de725f | 264 | * Expected H, 4 and 3. |
shimniok | 0:a6a169de725f | 265 | */ |
shimniok | 0:a6a169de725f | 266 | void getAddress(char * address); |
shimniok | 0:a6a169de725f | 267 | |
shimniok | 0:a6a169de725f | 268 | |
shimniok | 0:a6a169de725f | 269 | /** sets the I2C bus frequency |
shimniok | 0:a6a169de725f | 270 | * |
shimniok | 0:a6a169de725f | 271 | * @param frequency is the I2C bus/clock frequency, either standard (100000) or fast (400000) |
shimniok | 0:a6a169de725f | 272 | */ |
shimniok | 0:a6a169de725f | 273 | void frequency(int hz); |
shimniok | 0:a6a169de725f | 274 | |
shimniok | 0:a6a169de725f | 275 | private: |
shimniok | 0:a6a169de725f | 276 | I2C* i2c_; |
shimniok | 0:a6a169de725f | 277 | |
shimniok | 0:a6a169de725f | 278 | }; |
shimniok | 0:a6a169de725f | 279 | |
shimniok | 0:a6a169de725f | 280 | #endif /* HMC5843_H */ |