IMU 10dof MEMS from DR Robot adapted from HK10DOF Changed gyro to ITG3200

Fork of HK10DOF by Aloïs Wolff

WARNING: This project is not complete, but this library seems ok so far.

I have the module DFRobotics.com 10DOF MEMS IMU. I wanted a concise module for resolving direction and movement.

I found HK10DOF library (http://developer.mbed.org/users/pommzorz/code/HK10DOF/) with quaternions. But it used a different gyro. So I modified that code to use the same higher level calls but use the ITG3200 low level calls.

Committer:
svkatielee
Date:
Tue Nov 18 06:28:56 2014 +0000
Revision:
4:c4db4e2ffdd7
Parent:
0:9a1682a09c50
Changed gyro sensor to ITG3200

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pommzorz 0:9a1682a09c50 1 /*
pommzorz 0:9a1682a09c50 2 * @file HMC5883L.h
pommzorz 0:9a1682a09c50 3 * @author Tyler Weaver
pommzorz 0:9a1682a09c50 4 *
pommzorz 0:9a1682a09c50 5 * @section LICENSE
pommzorz 0:9a1682a09c50 6 *
pommzorz 0:9a1682a09c50 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
pommzorz 0:9a1682a09c50 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
pommzorz 0:9a1682a09c50 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
pommzorz 0:9a1682a09c50 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
pommzorz 0:9a1682a09c50 11 * furnished to do so, subject to the following conditions:
pommzorz 0:9a1682a09c50 12 *
pommzorz 0:9a1682a09c50 13 * The above copyright notice and this permission notice shall be included in all copies or
pommzorz 0:9a1682a09c50 14 * substantial portions of the Software.
pommzorz 0:9a1682a09c50 15 *
pommzorz 0:9a1682a09c50 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
pommzorz 0:9a1682a09c50 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
pommzorz 0:9a1682a09c50 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
pommzorz 0:9a1682a09c50 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pommzorz 0:9a1682a09c50 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
pommzorz 0:9a1682a09c50 21 *
pommzorz 0:9a1682a09c50 22 * @section DESCRIPTION
pommzorz 0:9a1682a09c50 23 *
pommzorz 0:9a1682a09c50 24 * HMC5883L 3-Axis Digital Compas IC
pommzorz 0:9a1682a09c50 25 * For use with the Sparkfun 9 Degrees of Freedom - Sensor Stick
pommzorz 0:9a1682a09c50 26 *
pommzorz 0:9a1682a09c50 27 * Datasheet:
pommzorz 0:9a1682a09c50 28 *
pommzorz 0:9a1682a09c50 29 * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Magneto/HMC5883L-FDS.pdf
pommzorz 0:9a1682a09c50 30 */
pommzorz 0:9a1682a09c50 31
pommzorz 0:9a1682a09c50 32 #ifndef HMC5883L_H
pommzorz 0:9a1682a09c50 33 #define HMC5883L_H
pommzorz 0:9a1682a09c50 34
pommzorz 0:9a1682a09c50 35 #include "mbed.h"
pommzorz 0:9a1682a09c50 36
pommzorz 0:9a1682a09c50 37 /*
pommzorz 0:9a1682a09c50 38 * Defines
pommzorz 0:9a1682a09c50 39 */
pommzorz 0:9a1682a09c50 40
pommzorz 0:9a1682a09c50 41 //-----------
pommzorz 0:9a1682a09c50 42 // Registers
pommzorz 0:9a1682a09c50 43 //-----------
pommzorz 0:9a1682a09c50 44 #define CONFIG_A_REG 0x00
pommzorz 0:9a1682a09c50 45 #define CONFIG_B_REG 0x01
pommzorz 0:9a1682a09c50 46 #define MODE_REG 0x02
pommzorz 0:9a1682a09c50 47 #define OUTPUT_REG 0x03
pommzorz 0:9a1682a09c50 48 #define STATUS_REG 0x09
pommzorz 0:9a1682a09c50 49
pommzorz 0:9a1682a09c50 50 // configuration register a
pommzorz 0:9a1682a09c50 51 #define AVG1_SAMPLES 0x00
pommzorz 0:9a1682a09c50 52 #define AVG2_SAMPLES 0x20
pommzorz 0:9a1682a09c50 53 #define AVG4_SAMPLES 0x80
pommzorz 0:9a1682a09c50 54 #define AVG8_SAMPLES 0xC0
pommzorz 0:9a1682a09c50 55
pommzorz 0:9a1682a09c50 56 #define OUTPUT_RATE_0_75 0x00
pommzorz 0:9a1682a09c50 57 #define OUTPUT_RATE_1_5 0x04
pommzorz 0:9a1682a09c50 58 #define OUTPUT_RATE_3 0x08
pommzorz 0:9a1682a09c50 59 #define OUTPUT_RATE_7_5 0x0C
pommzorz 0:9a1682a09c50 60 #define OUTPUT_RATE_15 0x10
pommzorz 0:9a1682a09c50 61 #define OUTPUT_RATE_30 0x14
pommzorz 0:9a1682a09c50 62 #define OUTPUT_RATE_75 0x18
pommzorz 0:9a1682a09c50 63
pommzorz 0:9a1682a09c50 64 #define NORMAL_MEASUREMENT 0x00
pommzorz 0:9a1682a09c50 65 #define POSITIVE_BIAS 0x01
pommzorz 0:9a1682a09c50 66 #define NEGATIVE_BIAS 0x02
pommzorz 0:9a1682a09c50 67
pommzorz 0:9a1682a09c50 68 // mode register
pommzorz 0:9a1682a09c50 69 #define CONTINUOUS_MODE 0x00
pommzorz 0:9a1682a09c50 70 #define SINGLE_MODE 0x01
pommzorz 0:9a1682a09c50 71 #define IDLE_MODE 0x02
pommzorz 0:9a1682a09c50 72
pommzorz 0:9a1682a09c50 73 // status register
pommzorz 0:9a1682a09c50 74 #define STATUS_LOCK 0x02
pommzorz 0:9a1682a09c50 75 #define STATUS_READY 0x01
pommzorz 0:9a1682a09c50 76
pommzorz 0:9a1682a09c50 77 // Utility
pommzorz 0:9a1682a09c50 78 #ifndef M_PI
pommzorz 0:9a1682a09c50 79 #define M_PI 3.1415926535897932384626433832795
pommzorz 0:9a1682a09c50 80 #endif
pommzorz 0:9a1682a09c50 81
pommzorz 0:9a1682a09c50 82 #define PI2 (2*M_PI)
pommzorz 0:9a1682a09c50 83 #define RAD_TO_DEG (180.0/M_PI)
pommzorz 0:9a1682a09c50 84 #define DEG_TO_RAD (M_PI/180.0)
pommzorz 0:9a1682a09c50 85
pommzorz 0:9a1682a09c50 86 /**
pommzorz 0:9a1682a09c50 87 * The HMC5883L 3-Axis Digital Compass IC
pommzorz 0:9a1682a09c50 88 */
pommzorz 0:9a1682a09c50 89 class HMC5883L
pommzorz 0:9a1682a09c50 90 {
pommzorz 0:9a1682a09c50 91
pommzorz 0:9a1682a09c50 92 public:
pommzorz 0:9a1682a09c50 93
pommzorz 0:9a1682a09c50 94 /**
pommzorz 0:9a1682a09c50 95 * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left).
pommzorz 0:9a1682a09c50 96 */
pommzorz 0:9a1682a09c50 97 static const int I2C_ADDRESS = 0x3D;
pommzorz 0:9a1682a09c50 98
pommzorz 0:9a1682a09c50 99 /**
pommzorz 0:9a1682a09c50 100 * Constructor.
pommzorz 0:9a1682a09c50 101 *
pommzorz 0:9a1682a09c50 102 * Calls init function
pommzorz 0:9a1682a09c50 103 *
pommzorz 0:9a1682a09c50 104 * @param sda - mbed pin to use for the SDA I2C line.
pommzorz 0:9a1682a09c50 105 * @param scl - mbed pin to use for the SCL I2C line.
pommzorz 0:9a1682a09c50 106 */
pommzorz 0:9a1682a09c50 107 HMC5883L(PinName sda, PinName scl);
pommzorz 0:9a1682a09c50 108
pommzorz 0:9a1682a09c50 109 /**
pommzorz 0:9a1682a09c50 110 * Constructor that accepts external i2c interface object.
pommzorz 0:9a1682a09c50 111 *
pommzorz 0:9a1682a09c50 112 * Calls init function
pommzorz 0:9a1682a09c50 113 *
pommzorz 0:9a1682a09c50 114 * @param i2c The I2C interface object to use.
pommzorz 0:9a1682a09c50 115 */
pommzorz 0:9a1682a09c50 116 HMC5883L(I2C &i2c) : i2c_(i2c) {
pommzorz 0:9a1682a09c50 117 init();
pommzorz 0:9a1682a09c50 118 }
pommzorz 0:9a1682a09c50 119
pommzorz 0:9a1682a09c50 120 ~HMC5883L();
pommzorz 0:9a1682a09c50 121
pommzorz 0:9a1682a09c50 122 /**
pommzorz 0:9a1682a09c50 123 * Initalize function called by all constructors.
pommzorz 0:9a1682a09c50 124 *
pommzorz 0:9a1682a09c50 125 * Place startup code in here.
pommzorz 0:9a1682a09c50 126 */
pommzorz 0:9a1682a09c50 127 void init();
pommzorz 0:9a1682a09c50 128
pommzorz 0:9a1682a09c50 129 /**
pommzorz 0:9a1682a09c50 130 * Function for setting configuration register A
pommzorz 0:9a1682a09c50 131 *
pommzorz 0:9a1682a09c50 132 * Defined constants should be ored together to create value.
pommzorz 0:9a1682a09c50 133 * Defualt is 0x10 - 1 Sample per output, 15Hz Data output rate, normal measurement mode
pommzorz 0:9a1682a09c50 134 *
pommzorz 0:9a1682a09c50 135 * Refer to datasheet for instructions for setting Configuration Register A.
pommzorz 0:9a1682a09c50 136 *
pommzorz 0:9a1682a09c50 137 * @param config the value to place in Configuration Register A
pommzorz 0:9a1682a09c50 138 */
pommzorz 0:9a1682a09c50 139 void setConfigurationA(char);
pommzorz 0:9a1682a09c50 140
pommzorz 0:9a1682a09c50 141 /**
pommzorz 0:9a1682a09c50 142 * Function for retrieving the contents of configuration register A
pommzorz 0:9a1682a09c50 143 *
pommzorz 0:9a1682a09c50 144 * @returns Configuration Register A
pommzorz 0:9a1682a09c50 145 */
pommzorz 0:9a1682a09c50 146 char getConfigurationA();
pommzorz 0:9a1682a09c50 147
pommzorz 0:9a1682a09c50 148 /**
pommzorz 0:9a1682a09c50 149 * Function for setting configuration register B
pommzorz 0:9a1682a09c50 150 *
pommzorz 0:9a1682a09c50 151 * Configuration Register B is for setting the device gain.
pommzorz 0:9a1682a09c50 152 * Default value is 0x20
pommzorz 0:9a1682a09c50 153 *
pommzorz 0:9a1682a09c50 154 * Refer to datasheet for instructions for setting Configuration Register B
pommzorz 0:9a1682a09c50 155 *
pommzorz 0:9a1682a09c50 156 * @param config the value to place in Configuration Register B
pommzorz 0:9a1682a09c50 157 */
pommzorz 0:9a1682a09c50 158 void setConfigurationB(char);
pommzorz 0:9a1682a09c50 159
pommzorz 0:9a1682a09c50 160 /**
pommzorz 0:9a1682a09c50 161 * Function for retrieving the contents of configuration register B
pommzorz 0:9a1682a09c50 162 *
pommzorz 0:9a1682a09c50 163 * @returns Configuration Register B
pommzorz 0:9a1682a09c50 164 */
pommzorz 0:9a1682a09c50 165 char getConfigurationB();
pommzorz 0:9a1682a09c50 166
pommzorz 0:9a1682a09c50 167 /**
pommzorz 0:9a1682a09c50 168 * Funciton for setting the mode register
pommzorz 0:9a1682a09c50 169 *
pommzorz 0:9a1682a09c50 170 * Constants: CONTINUOUS_MODE, SINGLE_MODE, IDLE_MODE
pommzorz 0:9a1682a09c50 171 *
pommzorz 0:9a1682a09c50 172 * When you send a the Single-Measurement Mode instruction to the mode register
pommzorz 0:9a1682a09c50 173 * a single measurement is made, the RDY bit is set in the status register,
pommzorz 0:9a1682a09c50 174 * and the mode is placed in idle mode.
pommzorz 0:9a1682a09c50 175 *
pommzorz 0:9a1682a09c50 176 * When in Continous-Measurement Mode the device continuously performs measurements
pommzorz 0:9a1682a09c50 177 * and places the results in teh data register. After being placed in this mode
pommzorz 0:9a1682a09c50 178 * it takes two periods at the rate set in the data output rate before the first
pommzorz 0:9a1682a09c50 179 * sample is avaliable.
pommzorz 0:9a1682a09c50 180 *
pommzorz 0:9a1682a09c50 181 * Refer to datasheet for more detailed instructions for setting the mode register.
pommzorz 0:9a1682a09c50 182 *
pommzorz 0:9a1682a09c50 183 * @param mode the value for setting in the Mode Register
pommzorz 0:9a1682a09c50 184 */
pommzorz 0:9a1682a09c50 185 void setMode(char);
pommzorz 0:9a1682a09c50 186
pommzorz 0:9a1682a09c50 187 /**
pommzorz 0:9a1682a09c50 188 * Function for retrieving the contents of mode register
pommzorz 0:9a1682a09c50 189 *
pommzorz 0:9a1682a09c50 190 * @returns mode register
pommzorz 0:9a1682a09c50 191 */
pommzorz 0:9a1682a09c50 192 char getMode();
pommzorz 0:9a1682a09c50 193
pommzorz 0:9a1682a09c50 194 /**
pommzorz 0:9a1682a09c50 195 * Function for retriaval of the raw data
pommzorz 0:9a1682a09c50 196 *
pommzorz 0:9a1682a09c50 197 * @param output buffer that is atleast 3 in length
pommzorz 0:9a1682a09c50 198 */
pommzorz 0:9a1682a09c50 199 void getXYZ(int16_t raw[3]);
pommzorz 0:9a1682a09c50 200
pommzorz 0:9a1682a09c50 201 /**
pommzorz 0:9a1682a09c50 202 * Function for retrieving the contents of status register
pommzorz 0:9a1682a09c50 203 *
pommzorz 0:9a1682a09c50 204 * Bit1: LOCK, Bit0: RDY
pommzorz 0:9a1682a09c50 205 *
pommzorz 0:9a1682a09c50 206 * @returns status register
pommzorz 0:9a1682a09c50 207 */
pommzorz 0:9a1682a09c50 208 char getStatus();
pommzorz 0:9a1682a09c50 209
pommzorz 0:9a1682a09c50 210 /**
pommzorz 0:9a1682a09c50 211 * Function for getting radian heading using 2-dimensional calculation.
pommzorz 0:9a1682a09c50 212 *
pommzorz 0:9a1682a09c50 213 * Compass must be held flat and away from an magnetic field generating
pommzorz 0:9a1682a09c50 214 * devices such as cell phones and speakers.
pommzorz 0:9a1682a09c50 215 *
pommzorz 0:9a1682a09c50 216 * TODO: declenation angle compensation
pommzorz 0:9a1682a09c50 217 *
pommzorz 0:9a1682a09c50 218 * @returns heading in radians
pommzorz 0:9a1682a09c50 219 */
pommzorz 0:9a1682a09c50 220 double getHeadingXY();
pommzorz 0:9a1682a09c50 221
pommzorz 0:9a1682a09c50 222 /**
pommzorz 0:9a1682a09c50 223 * Function for getting degree heading using 2-dimensional calculation.
pommzorz 0:9a1682a09c50 224 *
pommzorz 0:9a1682a09c50 225 * Compass must be held flat and away from an magnetic field generating
pommzorz 0:9a1682a09c50 226 * devices such as cell phones and speakers.
pommzorz 0:9a1682a09c50 227 *
pommzorz 0:9a1682a09c50 228 * TODO: declenation angle compensation
pommzorz 0:9a1682a09c50 229 *
pommzorz 0:9a1682a09c50 230 * @returns heading in degrees
pommzorz 0:9a1682a09c50 231 */
pommzorz 0:9a1682a09c50 232 double getHeadingXYDeg() {
pommzorz 0:9a1682a09c50 233 return (getHeadingXY() * RAD_TO_DEG);
pommzorz 0:9a1682a09c50 234 }
pommzorz 0:9a1682a09c50 235
pommzorz 0:9a1682a09c50 236 private:
pommzorz 0:9a1682a09c50 237
pommzorz 0:9a1682a09c50 238 I2C &i2c_;
pommzorz 0:9a1682a09c50 239
pommzorz 0:9a1682a09c50 240 /**
pommzorz 0:9a1682a09c50 241 * The raw buffer for allocating I2C object in its own without heap memory.
pommzorz 0:9a1682a09c50 242 */
pommzorz 0:9a1682a09c50 243 char i2cRaw[sizeof(I2C)];
pommzorz 0:9a1682a09c50 244 };
pommzorz 0:9a1682a09c50 245
pommzorz 0:9a1682a09c50 246 #endif // HMC5883L