1st
Fork of LSM9DS0 by
LSM9DS0.h@5:993011c773be, 2017-02-13 (annotated)
- Committer:
- alan82914
- Date:
- Mon Feb 13 04:50:33 2017 +0000
- Revision:
- 5:993011c773be
- Parent:
- 3:bc0db184f092
sensor_correction;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YCTung | 0:0dbf7ee73651 | 1 | //Original author |
YCTung | 0:0dbf7ee73651 | 2 | /****************************************************************************** |
YCTung | 0:0dbf7ee73651 | 3 | SFE_LSM9DS0.h |
YCTung | 0:0dbf7ee73651 | 4 | SFE_LSM9DS0 Library Header File |
YCTung | 0:0dbf7ee73651 | 5 | Jim Lindblom @ SparkFun Electronics |
YCTung | 0:0dbf7ee73651 | 6 | Original Creation Date: February 14, 2014 (Happy Valentines Day!) |
YCTung | 0:0dbf7ee73651 | 7 | https://github.com/sparkfun/LSM9DS0_Breakout |
YCTung | 0:0dbf7ee73651 | 8 | This file prototypes the LSM9DS0 class, implemented in SFE_LSM9DS0.cpp. In |
YCTung | 0:0dbf7ee73651 | 9 | addition, it defines every register in the LSM9DS0 (both the Gyro and Accel/ |
YCTung | 0:0dbf7ee73651 | 10 | Magnetometer registers). |
YCTung | 0:0dbf7ee73651 | 11 | Development environment specifics: |
YCTung | 0:0dbf7ee73651 | 12 | IDE: Arduino 1.0.5 |
YCTung | 0:0dbf7ee73651 | 13 | Hardware Platform: Arduino Pro 3.3V/8MHz |
YCTung | 0:0dbf7ee73651 | 14 | LSM9DS0 Breakout Version: 1.0 |
YCTung | 0:0dbf7ee73651 | 15 | This code is beerware; if you see me (or any other SparkFun employee) at the |
YCTung | 0:0dbf7ee73651 | 16 | local, and you've found our code helpful, please buy us a round! |
YCTung | 0:0dbf7ee73651 | 17 | Distributed as-is; no warranty is given. |
YCTung | 0:0dbf7ee73651 | 18 | ******************************************************************************/ |
YCTung | 0:0dbf7ee73651 | 19 | #ifndef __SFE_LSM9DS0_H__ |
YCTung | 0:0dbf7ee73651 | 20 | #define __SFE_LSM9DS0_H__ |
YCTung | 0:0dbf7ee73651 | 21 | |
YCTung | 0:0dbf7ee73651 | 22 | #include "mbed.h" |
YCTung | 0:0dbf7ee73651 | 23 | |
adam_z | 2:48eb33afd0fa | 24 | |
adam_z | 3:bc0db184f092 | 25 | #define PI 3.14159 |
YCTung | 0:0dbf7ee73651 | 26 | //////////////////////////// |
YCTung | 0:0dbf7ee73651 | 27 | // LSM9DS0 Gyro Registers // |
YCTung | 0:0dbf7ee73651 | 28 | //////////////////////////// |
YCTung | 0:0dbf7ee73651 | 29 | #define WHO_AM_I_G 0x0F |
YCTung | 0:0dbf7ee73651 | 30 | #define CTRL_REG1_G 0x20 |
YCTung | 0:0dbf7ee73651 | 31 | #define CTRL_REG2_G 0x21 |
YCTung | 0:0dbf7ee73651 | 32 | #define CTRL_REG3_G 0x22 |
YCTung | 0:0dbf7ee73651 | 33 | #define CTRL_REG4_G 0x23 |
YCTung | 0:0dbf7ee73651 | 34 | #define CTRL_REG5_G 0x24 |
YCTung | 0:0dbf7ee73651 | 35 | #define REFERENCE_G 0x25 |
YCTung | 0:0dbf7ee73651 | 36 | #define STATUS_REG_G 0x27 |
YCTung | 0:0dbf7ee73651 | 37 | #define OUT_X_L_G 0x28 |
YCTung | 0:0dbf7ee73651 | 38 | #define OUT_X_H_G 0x29 |
YCTung | 0:0dbf7ee73651 | 39 | #define OUT_Y_L_G 0x2A |
YCTung | 0:0dbf7ee73651 | 40 | #define OUT_Y_H_G 0x2B |
YCTung | 0:0dbf7ee73651 | 41 | #define OUT_Z_L_G 0x2C |
YCTung | 0:0dbf7ee73651 | 42 | #define OUT_Z_H_G 0x2D |
YCTung | 0:0dbf7ee73651 | 43 | #define FIFO_CTRL_REG_G 0x2E |
YCTung | 0:0dbf7ee73651 | 44 | #define FIFO_SRC_REG_G 0x2F |
YCTung | 0:0dbf7ee73651 | 45 | #define INT1_CFG_G 0x30 |
YCTung | 0:0dbf7ee73651 | 46 | #define INT1_SRC_G 0x31 |
YCTung | 0:0dbf7ee73651 | 47 | #define INT1_THS_XH_G 0x32 |
YCTung | 0:0dbf7ee73651 | 48 | #define INT1_THS_XL_G 0x33 |
YCTung | 0:0dbf7ee73651 | 49 | #define INT1_THS_YH_G 0x34 |
YCTung | 0:0dbf7ee73651 | 50 | #define INT1_THS_YL_G 0x35 |
YCTung | 0:0dbf7ee73651 | 51 | #define INT1_THS_ZH_G 0x36 |
YCTung | 0:0dbf7ee73651 | 52 | #define INT1_THS_ZL_G 0x37 |
YCTung | 0:0dbf7ee73651 | 53 | #define INT1_DURATION_G 0x38 |
YCTung | 0:0dbf7ee73651 | 54 | |
YCTung | 0:0dbf7ee73651 | 55 | ////////////////////////////////////////// |
YCTung | 0:0dbf7ee73651 | 56 | // LSM9DS0 Accel/Magneto (XM) Registers // |
YCTung | 0:0dbf7ee73651 | 57 | ////////////////////////////////////////// |
YCTung | 0:0dbf7ee73651 | 58 | #define OUT_TEMP_L_XM 0x05 |
YCTung | 0:0dbf7ee73651 | 59 | #define OUT_TEMP_H_XM 0x06 |
YCTung | 0:0dbf7ee73651 | 60 | #define STATUS_REG_M 0x07 |
YCTung | 0:0dbf7ee73651 | 61 | #define OUT_X_L_M 0x08 |
YCTung | 0:0dbf7ee73651 | 62 | #define OUT_X_H_M 0x09 |
YCTung | 0:0dbf7ee73651 | 63 | #define OUT_Y_L_M 0x0A |
YCTung | 0:0dbf7ee73651 | 64 | #define OUT_Y_H_M 0x0B |
YCTung | 0:0dbf7ee73651 | 65 | #define OUT_Z_L_M 0x0C |
YCTung | 0:0dbf7ee73651 | 66 | #define OUT_Z_H_M 0x0D |
YCTung | 0:0dbf7ee73651 | 67 | #define WHO_AM_I_XM 0x0F |
YCTung | 0:0dbf7ee73651 | 68 | #define INT_CTRL_REG_M 0x12 |
YCTung | 0:0dbf7ee73651 | 69 | #define INT_SRC_REG_M 0x13 |
YCTung | 0:0dbf7ee73651 | 70 | #define INT_THS_L_M 0x14 |
YCTung | 0:0dbf7ee73651 | 71 | #define INT_THS_H_M 0x15 |
YCTung | 0:0dbf7ee73651 | 72 | #define OFFSET_X_L_M 0x16 |
YCTung | 0:0dbf7ee73651 | 73 | #define OFFSET_X_H_M 0x17 |
YCTung | 0:0dbf7ee73651 | 74 | #define OFFSET_Y_L_M 0x18 |
YCTung | 0:0dbf7ee73651 | 75 | #define OFFSET_Y_H_M 0x19 |
YCTung | 0:0dbf7ee73651 | 76 | #define OFFSET_Z_L_M 0x1A |
YCTung | 0:0dbf7ee73651 | 77 | #define OFFSET_Z_H_M 0x1B |
YCTung | 0:0dbf7ee73651 | 78 | #define REFERENCE_X 0x1C |
YCTung | 0:0dbf7ee73651 | 79 | #define REFERENCE_Y 0x1D |
YCTung | 0:0dbf7ee73651 | 80 | #define REFERENCE_Z 0x1E |
YCTung | 0:0dbf7ee73651 | 81 | #define CTRL_REG0_XM 0x1F |
YCTung | 0:0dbf7ee73651 | 82 | #define CTRL_REG1_XM 0x20 |
YCTung | 0:0dbf7ee73651 | 83 | #define CTRL_REG2_XM 0x21 |
YCTung | 0:0dbf7ee73651 | 84 | #define CTRL_REG3_XM 0x22 |
YCTung | 0:0dbf7ee73651 | 85 | #define CTRL_REG4_XM 0x23 |
YCTung | 0:0dbf7ee73651 | 86 | #define CTRL_REG5_XM 0x24 |
YCTung | 0:0dbf7ee73651 | 87 | #define CTRL_REG6_XM 0x25 |
YCTung | 0:0dbf7ee73651 | 88 | #define CTRL_REG7_XM 0x26 |
YCTung | 0:0dbf7ee73651 | 89 | #define STATUS_REG_A 0x27 |
YCTung | 0:0dbf7ee73651 | 90 | #define OUT_X_L_A 0x28 |
YCTung | 0:0dbf7ee73651 | 91 | #define OUT_X_H_A 0x29 |
YCTung | 0:0dbf7ee73651 | 92 | #define OUT_Y_L_A 0x2A |
YCTung | 0:0dbf7ee73651 | 93 | #define OUT_Y_H_A 0x2B |
YCTung | 0:0dbf7ee73651 | 94 | #define OUT_Z_L_A 0x2C |
YCTung | 0:0dbf7ee73651 | 95 | #define OUT_Z_H_A 0x2D |
YCTung | 0:0dbf7ee73651 | 96 | #define FIFO_CTRL_REG 0x2E |
YCTung | 0:0dbf7ee73651 | 97 | #define FIFO_SRC_REG 0x2F |
YCTung | 0:0dbf7ee73651 | 98 | #define INT_GEN_1_REG 0x30 |
YCTung | 0:0dbf7ee73651 | 99 | #define INT_GEN_1_SRC 0x31 |
YCTung | 0:0dbf7ee73651 | 100 | #define INT_GEN_1_THS 0x32 |
YCTung | 0:0dbf7ee73651 | 101 | #define INT_GEN_1_DURATION 0x33 |
YCTung | 0:0dbf7ee73651 | 102 | #define INT_GEN_2_REG 0x34 |
YCTung | 0:0dbf7ee73651 | 103 | #define INT_GEN_2_SRC 0x35 |
YCTung | 0:0dbf7ee73651 | 104 | #define INT_GEN_2_THS 0x36 |
YCTung | 0:0dbf7ee73651 | 105 | #define INT_GEN_2_DURATION 0x37 |
YCTung | 0:0dbf7ee73651 | 106 | #define CLICK_CFG 0x38 |
YCTung | 0:0dbf7ee73651 | 107 | #define CLICK_SRC 0x39 |
YCTung | 0:0dbf7ee73651 | 108 | #define CLICK_THS 0x3A |
YCTung | 0:0dbf7ee73651 | 109 | #define TIME_LIMIT 0x3B |
YCTung | 0:0dbf7ee73651 | 110 | #define TIME_LATENCY 0x3C |
YCTung | 0:0dbf7ee73651 | 111 | #define TIME_WINDOW 0x3D |
YCTung | 0:0dbf7ee73651 | 112 | #define ACT_THS 0x3E |
YCTung | 0:0dbf7ee73651 | 113 | #define ACT_DUR 0x3F |
YCTung | 0:0dbf7ee73651 | 114 | |
YCTung | 0:0dbf7ee73651 | 115 | // The LSM9DS0 functions over both I2C or SPI. This library supports both. |
YCTung | 0:0dbf7ee73651 | 116 | // But the interface mode used must be sent to the LSM9DS0 constructor. Use |
YCTung | 0:0dbf7ee73651 | 117 | // one of these two as the first parameter of the constructor. |
YCTung | 0:0dbf7ee73651 | 118 | enum interface_mode |
YCTung | 0:0dbf7ee73651 | 119 | { |
YCTung | 0:0dbf7ee73651 | 120 | SPI_MODE = 1, |
YCTung | 0:0dbf7ee73651 | 121 | I2C_MODE = 0, |
YCTung | 0:0dbf7ee73651 | 122 | }; |
YCTung | 0:0dbf7ee73651 | 123 | |
YCTung | 0:0dbf7ee73651 | 124 | class LSM9DS0 |
YCTung | 0:0dbf7ee73651 | 125 | { |
YCTung | 0:0dbf7ee73651 | 126 | public: |
YCTung | 0:0dbf7ee73651 | 127 | // gyro_scale defines the possible full-scale ranges of the gyroscope: |
YCTung | 0:0dbf7ee73651 | 128 | enum gyro_scale |
YCTung | 0:0dbf7ee73651 | 129 | { |
YCTung | 0:0dbf7ee73651 | 130 | G_SCALE_245DPS = 0x0, // 00: 245 degrees per second |
YCTung | 0:0dbf7ee73651 | 131 | G_SCALE_500DPS = 0x1, // 01: 500 dps |
YCTung | 0:0dbf7ee73651 | 132 | G_SCALE_2000DPS = 0x2, // 10: 2000 dps |
YCTung | 0:0dbf7ee73651 | 133 | }; |
YCTung | 0:0dbf7ee73651 | 134 | // accel_scale defines all possible FSR's of the accelerometer: |
YCTung | 0:0dbf7ee73651 | 135 | enum accel_scale |
YCTung | 0:0dbf7ee73651 | 136 | { |
YCTung | 0:0dbf7ee73651 | 137 | A_SCALE_2G = 0x0, // 000: 2g |
YCTung | 0:0dbf7ee73651 | 138 | A_SCALE_4G = 0x1, // 001: 4g |
YCTung | 0:0dbf7ee73651 | 139 | A_SCALE_6G = 0x2, // 010: 6g |
YCTung | 0:0dbf7ee73651 | 140 | A_SCALE_8G = 0x3, // 011: 8g |
YCTung | 0:0dbf7ee73651 | 141 | A_SCALE_16G = 0x4, // 100: 16g |
YCTung | 0:0dbf7ee73651 | 142 | }; |
YCTung | 0:0dbf7ee73651 | 143 | // mag_scale defines all possible FSR's of the magnetometer: |
YCTung | 0:0dbf7ee73651 | 144 | enum mag_scale |
YCTung | 0:0dbf7ee73651 | 145 | { |
YCTung | 0:0dbf7ee73651 | 146 | M_SCALE_2GS = 0x0, // 00: 2Gs |
YCTung | 0:0dbf7ee73651 | 147 | M_SCALE_4GS = 0x1, // 01: 4Gs |
YCTung | 0:0dbf7ee73651 | 148 | M_SCALE_8GS = 0x2, // 10: 8Gs |
YCTung | 0:0dbf7ee73651 | 149 | M_SCALE_12GS= 0x3, // 11: 12Gs |
YCTung | 0:0dbf7ee73651 | 150 | }; |
YCTung | 0:0dbf7ee73651 | 151 | // gyro_odr defines all possible data rate/bandwidth combos of the gyro: |
YCTung | 0:0dbf7ee73651 | 152 | enum gyro_odr |
YCTung | 0:0dbf7ee73651 | 153 | { // ODR (Hz) --- Cutoff |
YCTung | 0:0dbf7ee73651 | 154 | G_ODR_95_BW_125 = 0x0, // 95 12.5 |
YCTung | 0:0dbf7ee73651 | 155 | G_ODR_95_BW_25 = 0x1, // 95 25 |
YCTung | 0:0dbf7ee73651 | 156 | // 0x2 and 0x3 define the same data rate and bandwidth |
YCTung | 0:0dbf7ee73651 | 157 | G_ODR_190_BW_125 = 0x4, // 190 12.5 |
YCTung | 0:0dbf7ee73651 | 158 | G_ODR_190_BW_25 = 0x5, // 190 25 |
YCTung | 0:0dbf7ee73651 | 159 | G_ODR_190_BW_50 = 0x6, // 190 50 |
YCTung | 0:0dbf7ee73651 | 160 | G_ODR_190_BW_70 = 0x7, // 190 70 |
YCTung | 0:0dbf7ee73651 | 161 | G_ODR_380_BW_20 = 0x8, // 380 20 |
YCTung | 0:0dbf7ee73651 | 162 | G_ODR_380_BW_25 = 0x9, // 380 25 |
YCTung | 0:0dbf7ee73651 | 163 | G_ODR_380_BW_50 = 0xA, // 380 50 |
YCTung | 0:0dbf7ee73651 | 164 | G_ODR_380_BW_100 = 0xB, // 380 100 |
YCTung | 0:0dbf7ee73651 | 165 | G_ODR_760_BW_30 = 0xC, // 760 30 |
YCTung | 0:0dbf7ee73651 | 166 | G_ODR_760_BW_35 = 0xD, // 760 35 |
YCTung | 0:0dbf7ee73651 | 167 | G_ODR_760_BW_50 = 0xE, // 760 50 |
YCTung | 0:0dbf7ee73651 | 168 | G_ODR_760_BW_100 = 0xF, // 760 100 |
YCTung | 0:0dbf7ee73651 | 169 | }; |
YCTung | 0:0dbf7ee73651 | 170 | // accel_oder defines all possible output data rates of the accelerometer: |
YCTung | 0:0dbf7ee73651 | 171 | enum accel_odr |
YCTung | 0:0dbf7ee73651 | 172 | { |
YCTung | 0:0dbf7ee73651 | 173 | A_POWER_DOWN= 0x00, // Power-down mode (0x0) |
YCTung | 0:0dbf7ee73651 | 174 | A_ODR_3125 = 0x01, // 3.125 Hz (0x1) |
YCTung | 0:0dbf7ee73651 | 175 | A_ODR_625 = 0x02, // 6.25 Hz (0x2) |
YCTung | 0:0dbf7ee73651 | 176 | A_ODR_125 = 0x03, // 12.5 Hz (0x3) |
YCTung | 0:0dbf7ee73651 | 177 | A_ODR_25 = 0x04, // 25 Hz (0x4) |
YCTung | 0:0dbf7ee73651 | 178 | A_ODR_50 = 0x05, // 50 Hz (0x5) |
YCTung | 0:0dbf7ee73651 | 179 | A_ODR_100 = 0x06, // 100 Hz (0x6) |
YCTung | 0:0dbf7ee73651 | 180 | A_ODR_200 = 0x07, // 200 Hz (0x7) |
YCTung | 0:0dbf7ee73651 | 181 | A_ODR_400 = 0x08, // 400 Hz (0x8) |
YCTung | 0:0dbf7ee73651 | 182 | A_ODR_800 = 0x09, // 800 Hz (9) |
YCTung | 0:0dbf7ee73651 | 183 | A_ODR_1600 = 0x0A, // 1600 Hz (0xA) |
YCTung | 0:0dbf7ee73651 | 184 | }; |
YCTung | 0:0dbf7ee73651 | 185 | |
YCTung | 0:0dbf7ee73651 | 186 | // accel_abw defines all possible anti-aliasing filter rates of the accelerometer: |
YCTung | 0:0dbf7ee73651 | 187 | enum accel_abw |
YCTung | 0:0dbf7ee73651 | 188 | { |
YCTung | 0:0dbf7ee73651 | 189 | A_ABW_773 = 0x0, // 773 Hz (0x0) |
YCTung | 0:0dbf7ee73651 | 190 | A_ABW_194 = 0x1, // 194 Hz (0x1) |
YCTung | 0:0dbf7ee73651 | 191 | A_ABW_362 = 0x2, // 362 Hz (0x2) |
YCTung | 0:0dbf7ee73651 | 192 | A_ABW_50 = 0x3, // 50 Hz (0x3) |
YCTung | 0:0dbf7ee73651 | 193 | }; |
YCTung | 0:0dbf7ee73651 | 194 | |
YCTung | 0:0dbf7ee73651 | 195 | |
YCTung | 0:0dbf7ee73651 | 196 | // mag_oder defines all possible output data rates of the magnetometer: |
YCTung | 0:0dbf7ee73651 | 197 | enum mag_odr |
YCTung | 0:0dbf7ee73651 | 198 | { |
YCTung | 0:0dbf7ee73651 | 199 | M_ODR_3125 = 0x00, // 3.125 Hz (0x00) |
YCTung | 0:0dbf7ee73651 | 200 | M_ODR_625 = 0x01, // 6.25 Hz (0x01) |
YCTung | 0:0dbf7ee73651 | 201 | M_ODR_125 = 0x02, // 12.5 Hz (0x02) |
YCTung | 0:0dbf7ee73651 | 202 | M_ODR_25 = 0x03, // 25 Hz (0x03) |
YCTung | 0:0dbf7ee73651 | 203 | M_ODR_50 = 0x04, // 50 (0x04) |
YCTung | 0:0dbf7ee73651 | 204 | M_ODR_100 = 0x05, // 100 Hz (0x05) |
YCTung | 0:0dbf7ee73651 | 205 | }; |
YCTung | 0:0dbf7ee73651 | 206 | |
YCTung | 0:0dbf7ee73651 | 207 | // We'll store the gyro, accel, and magnetometer readings in a series of |
YCTung | 0:0dbf7ee73651 | 208 | // public class variables. Each sensor gets three variables -- one for each |
YCTung | 0:0dbf7ee73651 | 209 | // axis. Call readGyro(), readAccel(), and readMag() first, before using |
YCTung | 0:0dbf7ee73651 | 210 | // these variables! |
YCTung | 0:0dbf7ee73651 | 211 | // These values are the RAW signed 16-bit readings from the sensors. |
YCTung | 0:0dbf7ee73651 | 212 | int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope |
YCTung | 0:0dbf7ee73651 | 213 | int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer |
YCTung | 0:0dbf7ee73651 | 214 | int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer |
YCTung | 0:0dbf7ee73651 | 215 | int16_t temperature; |
YCTung | 0:0dbf7ee73651 | 216 | float abias[3]; |
YCTung | 0:0dbf7ee73651 | 217 | float gbias[3]; |
YCTung | 1:2c34ccab5256 | 218 | |
YCTung | 1:2c34ccab5256 | 219 | int16_t gyroOffset[3]; |
YCTung | 1:2c34ccab5256 | 220 | int16_t accelOffset[3]; |
YCTung | 1:2c34ccab5256 | 221 | int16_t magOffset[3]; |
YCTung | 1:2c34ccab5256 | 222 | void setGyroOffset(int16_t, int16_t, int16_t); |
YCTung | 1:2c34ccab5256 | 223 | void setAccelOffset(int16_t, int16_t, int16_t); |
YCTung | 1:2c34ccab5256 | 224 | void setMagOffset(int16_t, int16_t, int16_t); |
YCTung | 0:0dbf7ee73651 | 225 | |
YCTung | 0:0dbf7ee73651 | 226 | // LSM9DS0 -- LSM9DS0 class constructor |
YCTung | 0:0dbf7ee73651 | 227 | // The constructor will set up a handful of private variables, and set the |
YCTung | 0:0dbf7ee73651 | 228 | // communication mode as well. |
YCTung | 0:0dbf7ee73651 | 229 | // Input: |
YCTung | 0:0dbf7ee73651 | 230 | // - interface = Either SPI_MODE or I2C_MODE, whichever you're using |
YCTung | 0:0dbf7ee73651 | 231 | // to talk to the IC. |
YCTung | 0:0dbf7ee73651 | 232 | // - gAddr = If I2C_MODE, this is the I2C address of the gyroscope. |
YCTung | 0:0dbf7ee73651 | 233 | // If SPI_MODE, this is the chip select pin of the gyro (CSG) |
YCTung | 0:0dbf7ee73651 | 234 | // - xmAddr = If I2C_MODE, this is the I2C address of the accel/mag. |
YCTung | 0:0dbf7ee73651 | 235 | // If SPI_MODE, this is the cs pin of the accel/mag (CSXM) |
YCTung | 0:0dbf7ee73651 | 236 | LSM9DS0(interface_mode interface, uint8_t gAddr, uint8_t xmAddr); |
YCTung | 0:0dbf7ee73651 | 237 | |
YCTung | 0:0dbf7ee73651 | 238 | // begin() -- Initialize the gyro, accelerometer, and magnetometer. |
YCTung | 0:0dbf7ee73651 | 239 | // This will set up the scale and output rate of each sensor. It'll also |
YCTung | 0:0dbf7ee73651 | 240 | // "turn on" every sensor and every axis of every sensor. |
YCTung | 0:0dbf7ee73651 | 241 | // Input: |
YCTung | 0:0dbf7ee73651 | 242 | // - gScl = The scale of the gyroscope. This should be a gyro_scale value. |
YCTung | 0:0dbf7ee73651 | 243 | // - aScl = The scale of the accelerometer. Should be a accel_scale value. |
YCTung | 0:0dbf7ee73651 | 244 | // - mScl = The scale of the magnetometer. Should be a mag_scale value. |
YCTung | 0:0dbf7ee73651 | 245 | // - gODR = Output data rate of the gyroscope. gyro_odr value. |
YCTung | 0:0dbf7ee73651 | 246 | // - aODR = Output data rate of the accelerometer. accel_odr value. |
YCTung | 0:0dbf7ee73651 | 247 | // - mODR = Output data rate of the magnetometer. mag_odr value. |
YCTung | 0:0dbf7ee73651 | 248 | // Output: The function will return an unsigned 16-bit value. The most-sig |
YCTung | 0:0dbf7ee73651 | 249 | // bytes of the output are the WHO_AM_I reading of the accel. The |
YCTung | 0:0dbf7ee73651 | 250 | // least significant two bytes are the WHO_AM_I reading of the gyro. |
YCTung | 0:0dbf7ee73651 | 251 | // All parameters have a defaulted value, so you can call just "begin()". |
YCTung | 0:0dbf7ee73651 | 252 | // Default values are FSR's of: 2000DPS, 8g, 8Gs; ODRs of 760 Hz for |
YCTung | 0:0dbf7ee73651 | 253 | // gyro, 800 Hz for accelerometer, 100 Hz for magnetometer. |
YCTung | 0:0dbf7ee73651 | 254 | // Use the return value of this function to verify communication. |
YCTung | 0:0dbf7ee73651 | 255 | uint16_t begin(gyro_scale gScl = G_SCALE_2000DPS, |
YCTung | 0:0dbf7ee73651 | 256 | accel_scale aScl = A_SCALE_8G, mag_scale mScl = M_SCALE_8GS, |
YCTung | 0:0dbf7ee73651 | 257 | gyro_odr gODR = G_ODR_760_BW_100, accel_odr aODR = A_ODR_800, |
YCTung | 0:0dbf7ee73651 | 258 | mag_odr mODR = M_ODR_100); |
YCTung | 0:0dbf7ee73651 | 259 | |
YCTung | 0:0dbf7ee73651 | 260 | // readGyro() -- Read the gyroscope output registers. |
YCTung | 0:0dbf7ee73651 | 261 | // This function will read all six gyroscope output registers. |
YCTung | 0:0dbf7ee73651 | 262 | // The readings are stored in the class' gx, gy, and gz variables. Read |
YCTung | 0:0dbf7ee73651 | 263 | // those _after_ calling readGyro(). |
YCTung | 0:0dbf7ee73651 | 264 | void readGyro(); |
YCTung | 1:2c34ccab5256 | 265 | int16_t readRawGyroX( void ); |
YCTung | 1:2c34ccab5256 | 266 | int16_t readRawGyroY( void ); |
YCTung | 1:2c34ccab5256 | 267 | int16_t readRawGyroZ( void ); |
YCTung | 1:2c34ccab5256 | 268 | float readFloatGyroX( void ); |
YCTung | 1:2c34ccab5256 | 269 | float readFloatGyroY( void ); |
YCTung | 1:2c34ccab5256 | 270 | float readFloatGyroZ( void ); |
YCTung | 0:0dbf7ee73651 | 271 | |
YCTung | 0:0dbf7ee73651 | 272 | // readAccel() -- Read the accelerometer output registers. |
YCTung | 0:0dbf7ee73651 | 273 | // This function will read all six accelerometer output registers. |
YCTung | 0:0dbf7ee73651 | 274 | // The readings are stored in the class' ax, ay, and az variables. Read |
YCTung | 0:0dbf7ee73651 | 275 | // those _after_ calling readAccel(). |
YCTung | 0:0dbf7ee73651 | 276 | void readAccel(); |
YCTung | 1:2c34ccab5256 | 277 | int16_t readRawAccelX( void ); |
YCTung | 1:2c34ccab5256 | 278 | int16_t readRawAccelY( void ); |
YCTung | 1:2c34ccab5256 | 279 | int16_t readRawAccelZ( void ); |
YCTung | 1:2c34ccab5256 | 280 | float readFloatAccelX( void ); |
YCTung | 1:2c34ccab5256 | 281 | float readFloatAccelY( void ); |
YCTung | 1:2c34ccab5256 | 282 | float readFloatAccelZ( void ); |
YCTung | 0:0dbf7ee73651 | 283 | |
YCTung | 0:0dbf7ee73651 | 284 | // readMag() -- Read the magnetometer output registers. |
YCTung | 0:0dbf7ee73651 | 285 | // This function will read all six magnetometer output registers. |
YCTung | 0:0dbf7ee73651 | 286 | // The readings are stored in the class' mx, my, and mz variables. Read |
YCTung | 0:0dbf7ee73651 | 287 | // those _after_ calling readMag(). |
YCTung | 0:0dbf7ee73651 | 288 | void readMag(); |
YCTung | 1:2c34ccab5256 | 289 | int16_t readRawMagX( void ); |
YCTung | 1:2c34ccab5256 | 290 | int16_t readRawMagY( void ); |
YCTung | 1:2c34ccab5256 | 291 | int16_t readRawMagZ( void ); |
YCTung | 1:2c34ccab5256 | 292 | float readFloatMagX( void ); |
YCTung | 1:2c34ccab5256 | 293 | float readFloatMagY( void ); |
YCTung | 1:2c34ccab5256 | 294 | float readFloatMagZ( void ); |
YCTung | 0:0dbf7ee73651 | 295 | |
YCTung | 0:0dbf7ee73651 | 296 | // readTemp() -- Read the temperature output register. |
YCTung | 0:0dbf7ee73651 | 297 | // This function will read two temperature output registers. |
YCTung | 0:0dbf7ee73651 | 298 | // The combined readings are stored in the class' temperature variables. Read |
YCTung | 0:0dbf7ee73651 | 299 | // those _after_ calling readTemp(). |
YCTung | 0:0dbf7ee73651 | 300 | void readTemp(); |
YCTung | 0:0dbf7ee73651 | 301 | |
YCTung | 0:0dbf7ee73651 | 302 | // calcGyro() -- Convert from RAW signed 16-bit value to degrees per second |
YCTung | 0:0dbf7ee73651 | 303 | // This function reads in a signed 16-bit value and returns the scaled |
YCTung | 0:0dbf7ee73651 | 304 | // DPS. This function relies on gScale and gRes being correct. |
YCTung | 0:0dbf7ee73651 | 305 | // Input: |
YCTung | 0:0dbf7ee73651 | 306 | // - gyro = A signed 16-bit raw reading from the gyroscope. |
YCTung | 0:0dbf7ee73651 | 307 | float calcGyro(int16_t gyro); |
YCTung | 0:0dbf7ee73651 | 308 | |
YCTung | 0:0dbf7ee73651 | 309 | // calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's). |
YCTung | 0:0dbf7ee73651 | 310 | // This function reads in a signed 16-bit value and returns the scaled |
YCTung | 0:0dbf7ee73651 | 311 | // g's. This function relies on aScale and aRes being correct. |
YCTung | 0:0dbf7ee73651 | 312 | // Input: |
YCTung | 0:0dbf7ee73651 | 313 | // - accel = A signed 16-bit raw reading from the accelerometer. |
YCTung | 0:0dbf7ee73651 | 314 | float calcAccel(int16_t accel); |
YCTung | 0:0dbf7ee73651 | 315 | |
YCTung | 0:0dbf7ee73651 | 316 | // calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs) |
YCTung | 0:0dbf7ee73651 | 317 | // This function reads in a signed 16-bit value and returns the scaled |
YCTung | 0:0dbf7ee73651 | 318 | // Gs. This function relies on mScale and mRes being correct. |
YCTung | 0:0dbf7ee73651 | 319 | // Input: |
YCTung | 0:0dbf7ee73651 | 320 | // - mag = A signed 16-bit raw reading from the magnetometer. |
YCTung | 0:0dbf7ee73651 | 321 | float calcMag(int16_t mag); |
YCTung | 0:0dbf7ee73651 | 322 | |
YCTung | 0:0dbf7ee73651 | 323 | // setGyroScale() -- Set the full-scale range of the gyroscope. |
YCTung | 0:0dbf7ee73651 | 324 | // This function can be called to set the scale of the gyroscope to |
YCTung | 0:0dbf7ee73651 | 325 | // 245, 500, or 200 degrees per second. |
YCTung | 0:0dbf7ee73651 | 326 | // Input: |
YCTung | 0:0dbf7ee73651 | 327 | // - gScl = The desired gyroscope scale. Must be one of three possible |
YCTung | 0:0dbf7ee73651 | 328 | // values from the gyro_scale enum. |
YCTung | 0:0dbf7ee73651 | 329 | void setGyroScale(gyro_scale gScl); |
YCTung | 0:0dbf7ee73651 | 330 | |
YCTung | 0:0dbf7ee73651 | 331 | // setAccelScale() -- Set the full-scale range of the accelerometer. |
YCTung | 0:0dbf7ee73651 | 332 | // This function can be called to set the scale of the accelerometer to |
YCTung | 0:0dbf7ee73651 | 333 | // 2, 4, 6, 8, or 16 g's. |
YCTung | 0:0dbf7ee73651 | 334 | // Input: |
YCTung | 0:0dbf7ee73651 | 335 | // - aScl = The desired accelerometer scale. Must be one of five possible |
YCTung | 0:0dbf7ee73651 | 336 | // values from the accel_scale enum. |
YCTung | 0:0dbf7ee73651 | 337 | void setAccelScale(accel_scale aScl); |
YCTung | 0:0dbf7ee73651 | 338 | |
YCTung | 0:0dbf7ee73651 | 339 | // setMagScale() -- Set the full-scale range of the magnetometer. |
YCTung | 0:0dbf7ee73651 | 340 | // This function can be called to set the scale of the magnetometer to |
YCTung | 0:0dbf7ee73651 | 341 | // 2, 4, 8, or 12 Gs. |
YCTung | 0:0dbf7ee73651 | 342 | // Input: |
YCTung | 0:0dbf7ee73651 | 343 | // - mScl = The desired magnetometer scale. Must be one of four possible |
YCTung | 0:0dbf7ee73651 | 344 | // values from the mag_scale enum. |
YCTung | 0:0dbf7ee73651 | 345 | void setMagScale(mag_scale mScl); |
YCTung | 0:0dbf7ee73651 | 346 | |
YCTung | 0:0dbf7ee73651 | 347 | // setGyroODR() -- Set the output data rate and bandwidth of the gyroscope |
YCTung | 0:0dbf7ee73651 | 348 | // Input: |
YCTung | 0:0dbf7ee73651 | 349 | // - gRate = The desired output rate and cutoff frequency of the gyro. |
YCTung | 0:0dbf7ee73651 | 350 | // Must be a value from the gyro_odr enum (check above, there're 14). |
YCTung | 0:0dbf7ee73651 | 351 | void setGyroODR(gyro_odr gRate); |
YCTung | 0:0dbf7ee73651 | 352 | |
YCTung | 0:0dbf7ee73651 | 353 | // setAccelODR() -- Set the output data rate of the accelerometer |
YCTung | 0:0dbf7ee73651 | 354 | // Input: |
YCTung | 0:0dbf7ee73651 | 355 | // - aRate = The desired output rate of the accel. |
YCTung | 0:0dbf7ee73651 | 356 | // Must be a value from the accel_odr enum (check above, there're 11). |
YCTung | 0:0dbf7ee73651 | 357 | void setAccelODR(accel_odr aRate); |
YCTung | 0:0dbf7ee73651 | 358 | |
YCTung | 1:2c34ccab5256 | 359 | // setAccelABW() -- Set the anti-aliasing filter rate of the accelerometer |
YCTung | 0:0dbf7ee73651 | 360 | // Input: |
YCTung | 0:0dbf7ee73651 | 361 | // - abwRate = The desired anti-aliasing filter rate of the accel. |
YCTung | 0:0dbf7ee73651 | 362 | // Must be a value from the accel_abw enum (check above, there're 4). |
YCTung | 0:0dbf7ee73651 | 363 | void setAccelABW(accel_abw abwRate); |
YCTung | 0:0dbf7ee73651 | 364 | |
YCTung | 0:0dbf7ee73651 | 365 | // setMagODR() -- Set the output data rate of the magnetometer |
YCTung | 0:0dbf7ee73651 | 366 | // Input: |
YCTung | 0:0dbf7ee73651 | 367 | // - mRate = The desired output rate of the mag. |
YCTung | 0:0dbf7ee73651 | 368 | // Must be a value from the mag_odr enum (check above, there're 6). |
YCTung | 0:0dbf7ee73651 | 369 | void setMagODR(mag_odr mRate); |
YCTung | 0:0dbf7ee73651 | 370 | |
YCTung | 0:0dbf7ee73651 | 371 | // configGyroInt() -- Configure the gyro interrupt output. |
YCTung | 0:0dbf7ee73651 | 372 | // Triggers can be set to either rising above or falling below a specified |
YCTung | 0:0dbf7ee73651 | 373 | // threshold. This function helps setup the interrupt configuration and |
YCTung | 0:0dbf7ee73651 | 374 | // threshold values for all axes. |
YCTung | 0:0dbf7ee73651 | 375 | // Input: |
YCTung | 0:0dbf7ee73651 | 376 | // - int1Cfg = A 8-bit value that is sent directly to the INT1_CFG_G |
YCTung | 0:0dbf7ee73651 | 377 | // register. This sets AND/OR and high/low interrupt gen for each axis |
YCTung | 0:0dbf7ee73651 | 378 | // - int1ThsX = 16-bit interrupt threshold value for x-axis |
YCTung | 0:0dbf7ee73651 | 379 | // - int1ThsY = 16-bit interrupt threshold value for y-axis |
YCTung | 0:0dbf7ee73651 | 380 | // - int1ThsZ = 16-bit interrupt threshold value for z-axis |
YCTung | 0:0dbf7ee73651 | 381 | // - duration = Duration an interrupt holds after triggered. This value |
YCTung | 0:0dbf7ee73651 | 382 | // is copied directly into the INT1_DURATION_G register. |
YCTung | 0:0dbf7ee73651 | 383 | // Before using this function, read about the INT1_CFG_G register and |
YCTung | 0:0dbf7ee73651 | 384 | // the related INT1* registers in the LMS9DS0 datasheet. |
YCTung | 0:0dbf7ee73651 | 385 | void configGyroInt(uint8_t int1Cfg, uint16_t int1ThsX = 0, |
YCTung | 0:0dbf7ee73651 | 386 | uint16_t int1ThsY = 0, uint16_t int1ThsZ = 0, |
YCTung | 0:0dbf7ee73651 | 387 | uint8_t duration = 0); |
YCTung | 0:0dbf7ee73651 | 388 | |
YCTung | 0:0dbf7ee73651 | 389 | |
YCTung | 0:0dbf7ee73651 | 390 | void calLSM9DS0(float gbias[3], float abias[3]); |
YCTung | 0:0dbf7ee73651 | 391 | |
YCTung | 0:0dbf7ee73651 | 392 | SPI spi_; |
YCTung | 0:0dbf7ee73651 | 393 | I2C i2c_; |
YCTung | 0:0dbf7ee73651 | 394 | DigitalOut csG_; |
YCTung | 0:0dbf7ee73651 | 395 | DigitalOut csXM_; |
adam_z | 2:48eb33afd0fa | 396 | |
adam_z | 2:48eb33afd0fa | 397 | float pitch, roll; |
adam_z | 2:48eb33afd0fa | 398 | void complementaryFilter(float * data, float dt); |
YCTung | 0:0dbf7ee73651 | 399 | |
YCTung | 0:0dbf7ee73651 | 400 | private: |
YCTung | 0:0dbf7ee73651 | 401 | // xmAddress and gAddress store the I2C address or SPI chip select pin |
YCTung | 0:0dbf7ee73651 | 402 | // for each sensor. |
YCTung | 0:0dbf7ee73651 | 403 | uint8_t xmAddress, gAddress; |
YCTung | 0:0dbf7ee73651 | 404 | // interfaceMode keeps track of whether we're using SPI or I2C to talk |
YCTung | 0:0dbf7ee73651 | 405 | interface_mode interfaceMode; |
YCTung | 0:0dbf7ee73651 | 406 | |
YCTung | 0:0dbf7ee73651 | 407 | // gScale, aScale, and mScale store the current scale range for each |
YCTung | 0:0dbf7ee73651 | 408 | // sensor. Should be updated whenever that value changes. |
YCTung | 0:0dbf7ee73651 | 409 | gyro_scale gScale; |
YCTung | 0:0dbf7ee73651 | 410 | accel_scale aScale; |
YCTung | 0:0dbf7ee73651 | 411 | mag_scale mScale; |
YCTung | 0:0dbf7ee73651 | 412 | |
YCTung | 0:0dbf7ee73651 | 413 | // gRes, aRes, and mRes store the current resolution for each sensor. |
YCTung | 0:0dbf7ee73651 | 414 | // Units of these values would be DPS (or g's or Gs's) per ADC tick. |
YCTung | 0:0dbf7ee73651 | 415 | // This value is calculated as (sensor scale) / (2^15). |
YCTung | 0:0dbf7ee73651 | 416 | float gRes, aRes, mRes; |
YCTung | 0:0dbf7ee73651 | 417 | |
YCTung | 0:0dbf7ee73651 | 418 | // initGyro() -- Sets up the gyroscope to begin reading. |
YCTung | 0:0dbf7ee73651 | 419 | // This function steps through all five gyroscope control registers. |
YCTung | 0:0dbf7ee73651 | 420 | // Upon exit, the following parameters will be set: |
YCTung | 0:0dbf7ee73651 | 421 | // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled. |
YCTung | 0:0dbf7ee73651 | 422 | // 95 Hz ODR, 12.5 Hz cutoff frequency. |
YCTung | 0:0dbf7ee73651 | 423 | // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency |
YCTung | 0:0dbf7ee73651 | 424 | // set to 7.2 Hz (depends on ODR). |
YCTung | 0:0dbf7ee73651 | 425 | // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and |
YCTung | 0:0dbf7ee73651 | 426 | // active high). Data-ready output enabled on DRDY_G. |
YCTung | 0:0dbf7ee73651 | 427 | // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower |
YCTung | 0:0dbf7ee73651 | 428 | // address. Scale set to 245 DPS. SPI mode set to 4-wire. |
YCTung | 0:0dbf7ee73651 | 429 | // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled. |
YCTung | 0:0dbf7ee73651 | 430 | void initGyro(); |
YCTung | 0:0dbf7ee73651 | 431 | |
YCTung | 0:0dbf7ee73651 | 432 | // initAccel() -- Sets up the accelerometer to begin reading. |
YCTung | 0:0dbf7ee73651 | 433 | // This function steps through all accelerometer related control registers. |
YCTung | 0:0dbf7ee73651 | 434 | // Upon exit these registers will be set as: |
YCTung | 0:0dbf7ee73651 | 435 | // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode. |
YCTung | 0:0dbf7ee73651 | 436 | // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update. |
YCTung | 0:0dbf7ee73651 | 437 | // all axes enabled. |
YCTung | 0:0dbf7ee73651 | 438 | // - CTRL_REG2_XM = 0x00: 2g scale. 773 Hz anti-alias filter BW. |
YCTung | 0:0dbf7ee73651 | 439 | // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin. |
YCTung | 0:0dbf7ee73651 | 440 | void initAccel(); |
YCTung | 0:0dbf7ee73651 | 441 | |
YCTung | 0:0dbf7ee73651 | 442 | // initMag() -- Sets up the magnetometer to begin reading. |
YCTung | 0:0dbf7ee73651 | 443 | // This function steps through all magnetometer-related control registers. |
YCTung | 0:0dbf7ee73651 | 444 | // Upon exit these registers will be set as: |
YCTung | 0:0dbf7ee73651 | 445 | // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin. |
YCTung | 0:0dbf7ee73651 | 446 | // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt |
YCTung | 0:0dbf7ee73651 | 447 | // requests don't latch. Temperature sensor disabled. |
YCTung | 0:0dbf7ee73651 | 448 | // - CTRL_REG6_XM = 0x00: 2 Gs scale. |
YCTung | 0:0dbf7ee73651 | 449 | // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode. |
YCTung | 0:0dbf7ee73651 | 450 | // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts. |
YCTung | 0:0dbf7ee73651 | 451 | void initMag(); |
YCTung | 0:0dbf7ee73651 | 452 | |
YCTung | 0:0dbf7ee73651 | 453 | // gReadByte() -- Reads a byte from a specified gyroscope register. |
YCTung | 0:0dbf7ee73651 | 454 | // Input: |
YCTung | 0:0dbf7ee73651 | 455 | // - subAddress = Register to be read from. |
YCTung | 0:0dbf7ee73651 | 456 | // Output: |
YCTung | 0:0dbf7ee73651 | 457 | // - An 8-bit value read from the requested address. |
YCTung | 0:0dbf7ee73651 | 458 | uint8_t gReadByte(uint8_t subAddress); |
YCTung | 0:0dbf7ee73651 | 459 | |
YCTung | 0:0dbf7ee73651 | 460 | // gReadBytes() -- Reads a number of bytes -- beginning at an address |
YCTung | 0:0dbf7ee73651 | 461 | // and incrementing from there -- from the gyroscope. |
YCTung | 0:0dbf7ee73651 | 462 | // Input: |
YCTung | 0:0dbf7ee73651 | 463 | // - subAddress = Register to be read from. |
YCTung | 0:0dbf7ee73651 | 464 | // - * dest = A pointer to an array of uint8_t's. Values read will be |
YCTung | 0:0dbf7ee73651 | 465 | // stored in here on return. |
YCTung | 0:0dbf7ee73651 | 466 | // - count = The number of bytes to be read. |
YCTung | 0:0dbf7ee73651 | 467 | // Output: No value is returned, but the `dest` array will store |
YCTung | 0:0dbf7ee73651 | 468 | // the data read upon exit. |
YCTung | 0:0dbf7ee73651 | 469 | void gReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count); |
YCTung | 0:0dbf7ee73651 | 470 | |
YCTung | 0:0dbf7ee73651 | 471 | // gWriteByte() -- Write a byte to a register in the gyroscope. |
YCTung | 0:0dbf7ee73651 | 472 | // Input: |
YCTung | 0:0dbf7ee73651 | 473 | // - subAddress = Register to be written to. |
YCTung | 0:0dbf7ee73651 | 474 | // - data = data to be written to the register. |
YCTung | 0:0dbf7ee73651 | 475 | void gWriteByte(uint8_t subAddress, uint8_t data); |
YCTung | 0:0dbf7ee73651 | 476 | |
YCTung | 0:0dbf7ee73651 | 477 | // xmReadByte() -- Read a byte from a register in the accel/mag sensor |
YCTung | 0:0dbf7ee73651 | 478 | // Input: |
YCTung | 0:0dbf7ee73651 | 479 | // - subAddress = Register to be read from. |
YCTung | 0:0dbf7ee73651 | 480 | // Output: |
YCTung | 0:0dbf7ee73651 | 481 | // - An 8-bit value read from the requested register. |
YCTung | 0:0dbf7ee73651 | 482 | uint8_t xmReadByte(uint8_t subAddress); |
YCTung | 0:0dbf7ee73651 | 483 | |
YCTung | 0:0dbf7ee73651 | 484 | // xmReadBytes() -- Reads a number of bytes -- beginning at an address |
YCTung | 0:0dbf7ee73651 | 485 | // and incrementing from there -- from the accelerometer/magnetometer. |
YCTung | 0:0dbf7ee73651 | 486 | // Input: |
YCTung | 0:0dbf7ee73651 | 487 | // - subAddress = Register to be read from. |
YCTung | 0:0dbf7ee73651 | 488 | // - * dest = A pointer to an array of uint8_t's. Values read will be |
YCTung | 0:0dbf7ee73651 | 489 | // stored in here on return. |
YCTung | 0:0dbf7ee73651 | 490 | // - count = The number of bytes to be read. |
YCTung | 0:0dbf7ee73651 | 491 | // Output: No value is returned, but the `dest` array will store |
YCTung | 0:0dbf7ee73651 | 492 | // the data read upon exit. |
YCTung | 0:0dbf7ee73651 | 493 | void xmReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count); |
YCTung | 0:0dbf7ee73651 | 494 | |
YCTung | 0:0dbf7ee73651 | 495 | // xmWriteByte() -- Write a byte to a register in the accel/mag sensor. |
YCTung | 0:0dbf7ee73651 | 496 | // Input: |
YCTung | 0:0dbf7ee73651 | 497 | // - subAddress = Register to be written to. |
YCTung | 0:0dbf7ee73651 | 498 | // - data = data to be written to the register. |
YCTung | 0:0dbf7ee73651 | 499 | void xmWriteByte(uint8_t subAddress, uint8_t data); |
YCTung | 0:0dbf7ee73651 | 500 | |
YCTung | 0:0dbf7ee73651 | 501 | // calcgRes() -- Calculate the resolution of the gyroscope. |
YCTung | 0:0dbf7ee73651 | 502 | // This function will set the value of the gRes variable. gScale must |
YCTung | 0:0dbf7ee73651 | 503 | // be set prior to calling this function. |
YCTung | 0:0dbf7ee73651 | 504 | void calcgRes(); |
YCTung | 0:0dbf7ee73651 | 505 | |
YCTung | 0:0dbf7ee73651 | 506 | // calcmRes() -- Calculate the resolution of the magnetometer. |
YCTung | 0:0dbf7ee73651 | 507 | // This function will set the value of the mRes variable. mScale must |
YCTung | 0:0dbf7ee73651 | 508 | // be set prior to calling this function. |
YCTung | 0:0dbf7ee73651 | 509 | void calcmRes(); |
YCTung | 0:0dbf7ee73651 | 510 | |
YCTung | 0:0dbf7ee73651 | 511 | // calcaRes() -- Calculate the resolution of the accelerometer. |
YCTung | 0:0dbf7ee73651 | 512 | // This function will set the value of the aRes variable. aScale must |
YCTung | 0:0dbf7ee73651 | 513 | // be set prior to calling this function. |
YCTung | 0:0dbf7ee73651 | 514 | void calcaRes(); |
YCTung | 0:0dbf7ee73651 | 515 | |
YCTung | 0:0dbf7ee73651 | 516 | /////////////////// |
YCTung | 0:0dbf7ee73651 | 517 | // SPI Functions // |
YCTung | 0:0dbf7ee73651 | 518 | /////////////////// |
YCTung | 0:0dbf7ee73651 | 519 | // initSPI() -- Initialize the SPI hardware. |
YCTung | 0:0dbf7ee73651 | 520 | // This function will setup all SPI pins and related hardware. |
YCTung | 0:0dbf7ee73651 | 521 | void initSPI(); |
YCTung | 0:0dbf7ee73651 | 522 | |
YCTung | 0:0dbf7ee73651 | 523 | // SPIwriteByte() -- Write a byte out of SPI to a register in the device |
YCTung | 0:0dbf7ee73651 | 524 | // Input: |
YCTung | 0:0dbf7ee73651 | 525 | // - csPin = The chip select pin of the slave device. |
YCTung | 0:0dbf7ee73651 | 526 | // - subAddress = The register to be written to. |
YCTung | 0:0dbf7ee73651 | 527 | // - data = Byte to be written to the register. |
YCTung | 0:0dbf7ee73651 | 528 | void SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data); |
YCTung | 0:0dbf7ee73651 | 529 | |
YCTung | 0:0dbf7ee73651 | 530 | // SPIreadByte() -- Read a single byte from a register over SPI. |
YCTung | 0:0dbf7ee73651 | 531 | // Input: |
YCTung | 0:0dbf7ee73651 | 532 | // - csPin = The chip select pin of the slave device. |
YCTung | 0:0dbf7ee73651 | 533 | // - subAddress = The register to be read from. |
YCTung | 0:0dbf7ee73651 | 534 | // Output: |
YCTung | 0:0dbf7ee73651 | 535 | // - The byte read from the requested address. |
YCTung | 0:0dbf7ee73651 | 536 | uint8_t SPIreadByte(uint8_t csPin, uint8_t subAddress); |
YCTung | 0:0dbf7ee73651 | 537 | |
YCTung | 0:0dbf7ee73651 | 538 | // SPIreadBytes() -- Read a series of bytes, starting at a register via SPI |
YCTung | 0:0dbf7ee73651 | 539 | // Input: |
YCTung | 0:0dbf7ee73651 | 540 | // - csPin = The chip select pin of a slave device. |
YCTung | 0:0dbf7ee73651 | 541 | // - subAddress = The register to begin reading. |
YCTung | 0:0dbf7ee73651 | 542 | // - * dest = Pointer to an array where we'll store the readings. |
YCTung | 0:0dbf7ee73651 | 543 | // - count = Number of registers to be read. |
YCTung | 0:0dbf7ee73651 | 544 | // Output: No value is returned by the function, but the registers read are |
YCTung | 0:0dbf7ee73651 | 545 | // all stored in the *dest array given. |
YCTung | 0:0dbf7ee73651 | 546 | void SPIreadBytes(uint8_t csPin, uint8_t subAddress, |
YCTung | 0:0dbf7ee73651 | 547 | uint8_t * dest, uint8_t count); |
YCTung | 0:0dbf7ee73651 | 548 | |
YCTung | 0:0dbf7ee73651 | 549 | /////////////////// |
YCTung | 0:0dbf7ee73651 | 550 | // I2C Functions // |
YCTung | 0:0dbf7ee73651 | 551 | /////////////////// |
YCTung | 0:0dbf7ee73651 | 552 | // initI2C() -- Initialize the I2C hardware. |
YCTung | 0:0dbf7ee73651 | 553 | // This function will setup all I2C pins and related hardware. |
YCTung | 0:0dbf7ee73651 | 554 | void initI2C(); |
YCTung | 0:0dbf7ee73651 | 555 | |
YCTung | 0:0dbf7ee73651 | 556 | // I2CwriteByte() -- Write a byte out of I2C to a register in the device |
YCTung | 0:0dbf7ee73651 | 557 | // Input: |
YCTung | 0:0dbf7ee73651 | 558 | // - address = The 7-bit I2C address of the slave device. |
YCTung | 0:0dbf7ee73651 | 559 | // - subAddress = The register to be written to. |
YCTung | 0:0dbf7ee73651 | 560 | // - data = Byte to be written to the register. |
YCTung | 0:0dbf7ee73651 | 561 | void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data); |
YCTung | 0:0dbf7ee73651 | 562 | |
YCTung | 0:0dbf7ee73651 | 563 | // I2CreadByte() -- Read a single byte from a register over I2C. |
YCTung | 0:0dbf7ee73651 | 564 | // Input: |
YCTung | 0:0dbf7ee73651 | 565 | // - address = The 7-bit I2C address of the slave device. |
YCTung | 0:0dbf7ee73651 | 566 | // - subAddress = The register to be read from. |
YCTung | 0:0dbf7ee73651 | 567 | // Output: |
YCTung | 0:0dbf7ee73651 | 568 | // - The byte read from the requested address. |
YCTung | 0:0dbf7ee73651 | 569 | uint8_t I2CreadByte(uint8_t address, uint8_t subAddress); |
YCTung | 0:0dbf7ee73651 | 570 | |
YCTung | 0:0dbf7ee73651 | 571 | // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI |
YCTung | 0:0dbf7ee73651 | 572 | // Input: |
YCTung | 0:0dbf7ee73651 | 573 | // - address = The 7-bit I2C address of the slave device. |
YCTung | 0:0dbf7ee73651 | 574 | // - subAddress = The register to begin reading. |
YCTung | 0:0dbf7ee73651 | 575 | // - * dest = Pointer to an array where we'll store the readings. |
YCTung | 0:0dbf7ee73651 | 576 | // - count = Number of registers to be read. |
YCTung | 0:0dbf7ee73651 | 577 | // Output: No value is returned by the function, but the registers read are |
YCTung | 0:0dbf7ee73651 | 578 | // all stored in the *dest array given. |
YCTung | 0:0dbf7ee73651 | 579 | void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count); |
adam_z | 2:48eb33afd0fa | 580 | |
adam_z | 2:48eb33afd0fa | 581 | |
YCTung | 0:0dbf7ee73651 | 582 | }; |
YCTung | 0:0dbf7ee73651 | 583 | |
YCTung | 0:0dbf7ee73651 | 584 | #endif // SFE_LSM9DS0_H // |