Chen Wei Ting
/
LSM9DS1_project_4_torque
read encoder and Dynamixel angle
LSM9DS1.h@0:2dbbe7d28fab, 2018-08-04 (annotated)
- Committer:
- JJting
- Date:
- Sat Aug 04 05:55:50 2018 +0000
- Revision:
- 0:2dbbe7d28fab
can read encoder and Dynamixel angle
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JJting | 0:2dbbe7d28fab | 1 | #ifndef _LSM9DS1_H__ |
JJting | 0:2dbbe7d28fab | 2 | #define _LSM9DS1_H__ |
JJting | 0:2dbbe7d28fab | 3 | |
JJting | 0:2dbbe7d28fab | 4 | #include "mbed.h" |
JJting | 0:2dbbe7d28fab | 5 | |
JJting | 0:2dbbe7d28fab | 6 | #define Acce_gain_z_2 -9.825751072961373f |
JJting | 0:2dbbe7d28fab | 7 | #define Acce_gain_y_2 -9.810204081632654f |
JJting | 0:2dbbe7d28fab | 8 | #define Acce_gain_x_2 -9.579654120040694 |
JJting | 0:2dbbe7d28fab | 9 | #define Acce_gain_z -10.0512295081967213f |
JJting | 0:2dbbe7d28fab | 10 | #define Acce_gain_y -9.9090909090909090f |
JJting | 0:2dbbe7d28fab | 11 | #define Acce_gain_x -9.4236311239193f |
JJting | 0:2dbbe7d28fab | 12 | #define Gyro_gain_x 0.02035972509905115f |
JJting | 0:2dbbe7d28fab | 13 | #define Gyro_gain_y 0.01825192507334282f |
JJting | 0:2dbbe7d28fab | 14 | #define Gyro_gain_z 0.02154843475f |
JJting | 0:2dbbe7d28fab | 15 | #define Gyro_gain_x_2 -0.02035972509905115f |
JJting | 0:2dbbe7d28fab | 16 | #define Gyro_gain_y_2 -0.01825192507334282f |
JJting | 0:2dbbe7d28fab | 17 | #define Gyro_gain_z_2 -0.02154843475f |
JJting | 0:2dbbe7d28fab | 18 | #define pi 3.1415926f |
JJting | 0:2dbbe7d28fab | 19 | ///////////////////////////////////////// |
JJting | 0:2dbbe7d28fab | 20 | // LSM9DS1 Accel/Gyro (XL/G) Registers // |
JJting | 0:2dbbe7d28fab | 21 | ///////////////////////////////////////// |
JJting | 0:2dbbe7d28fab | 22 | #define ACT_THS 0x04 |
JJting | 0:2dbbe7d28fab | 23 | #define ACT_DUR 0x05 |
JJting | 0:2dbbe7d28fab | 24 | #define INT_GEN_CFG_XL 0x06 |
JJting | 0:2dbbe7d28fab | 25 | #define INT_GEN_THS_X_XL 0x07 |
JJting | 0:2dbbe7d28fab | 26 | #define INT_GEN_THS_Y_XL 0x08 |
JJting | 0:2dbbe7d28fab | 27 | #define INT_GEN_THS_Z_XL 0x09 |
JJting | 0:2dbbe7d28fab | 28 | #define INT_GEN_DUR_XL 0x0A |
JJting | 0:2dbbe7d28fab | 29 | #define REFERENCE_G 0x0B |
JJting | 0:2dbbe7d28fab | 30 | #define INT1_CTRL 0x0C |
JJting | 0:2dbbe7d28fab | 31 | #define INT2_CTRL 0x0D |
JJting | 0:2dbbe7d28fab | 32 | #define WHO_AM_I_XG 0x0F |
JJting | 0:2dbbe7d28fab | 33 | #define CTRL_REG1_G 0x10 |
JJting | 0:2dbbe7d28fab | 34 | #define CTRL_REG2_G 0x11 |
JJting | 0:2dbbe7d28fab | 35 | #define CTRL_REG3_G 0x12 |
JJting | 0:2dbbe7d28fab | 36 | #define ORIENT_CFG_G 0x13 |
JJting | 0:2dbbe7d28fab | 37 | #define INT_GEN_SRC_G 0x14 |
JJting | 0:2dbbe7d28fab | 38 | #define OUT_TEMP_L 0x15 |
JJting | 0:2dbbe7d28fab | 39 | #define OUT_TEMP_H 0x16 |
JJting | 0:2dbbe7d28fab | 40 | #define STATUS_REG_0 0x17 |
JJting | 0:2dbbe7d28fab | 41 | #define OUT_X_L_G 0x18 |
JJting | 0:2dbbe7d28fab | 42 | #define OUT_X_H_G 0x19 |
JJting | 0:2dbbe7d28fab | 43 | #define OUT_Y_L_G 0x1A |
JJting | 0:2dbbe7d28fab | 44 | #define OUT_Y_H_G 0x1B |
JJting | 0:2dbbe7d28fab | 45 | #define OUT_Z_L_G 0x1C |
JJting | 0:2dbbe7d28fab | 46 | #define OUT_Z_H_G 0x1D |
JJting | 0:2dbbe7d28fab | 47 | #define CTRL_REG4 0x1E |
JJting | 0:2dbbe7d28fab | 48 | #define CTRL_REG5_XL 0x1F |
JJting | 0:2dbbe7d28fab | 49 | #define CTRL_REG6_XL 0x20 |
JJting | 0:2dbbe7d28fab | 50 | #define CTRL_REG7_XL 0x21 |
JJting | 0:2dbbe7d28fab | 51 | #define CTRL_REG8 0x22 |
JJting | 0:2dbbe7d28fab | 52 | #define CTRL_REG9 0x23 |
JJting | 0:2dbbe7d28fab | 53 | #define CTRL_REG10 0x24 |
JJting | 0:2dbbe7d28fab | 54 | #define INT_GEN_SRC_XL 0x26 |
JJting | 0:2dbbe7d28fab | 55 | #define STATUS_REG_1 0x27 |
JJting | 0:2dbbe7d28fab | 56 | #define OUT_X_L_XL 0x28 |
JJting | 0:2dbbe7d28fab | 57 | #define OUT_X_H_XL 0x29 |
JJting | 0:2dbbe7d28fab | 58 | #define OUT_Y_L_XL 0x2A |
JJting | 0:2dbbe7d28fab | 59 | #define OUT_Y_H_XL 0x2B |
JJting | 0:2dbbe7d28fab | 60 | #define OUT_Z_L_XL 0x2C |
JJting | 0:2dbbe7d28fab | 61 | #define OUT_Z_H_XL 0x2D |
JJting | 0:2dbbe7d28fab | 62 | #define FIFO_CTRL 0x2E |
JJting | 0:2dbbe7d28fab | 63 | #define FIFO_SRC 0x2F |
JJting | 0:2dbbe7d28fab | 64 | #define INT_GEN_CFG_G 0x30 |
JJting | 0:2dbbe7d28fab | 65 | #define INT_GEN_THS_XH_G 0x31 |
JJting | 0:2dbbe7d28fab | 66 | #define INT_GEN_THS_XL_G 0x32 |
JJting | 0:2dbbe7d28fab | 67 | #define INT_GEN_THS_YH_G 0x33 |
JJting | 0:2dbbe7d28fab | 68 | #define INT_GEN_THS_YL_G 0x34 |
JJting | 0:2dbbe7d28fab | 69 | #define INT_GEN_THS_ZH_G 0x35 |
JJting | 0:2dbbe7d28fab | 70 | #define INT_GEN_THS_ZL_G 0x36 |
JJting | 0:2dbbe7d28fab | 71 | #define INT_GEN_DUR_G 0x37 |
JJting | 0:2dbbe7d28fab | 72 | |
JJting | 0:2dbbe7d28fab | 73 | /////////////////////////////// |
JJting | 0:2dbbe7d28fab | 74 | // LSM9DS1 Magneto Registers // |
JJting | 0:2dbbe7d28fab | 75 | /////////////////////////////// |
JJting | 0:2dbbe7d28fab | 76 | #define OFFSET_X_REG_L_M 0x05 |
JJting | 0:2dbbe7d28fab | 77 | #define OFFSET_X_REG_H_M 0x06 |
JJting | 0:2dbbe7d28fab | 78 | #define OFFSET_Y_REG_L_M 0x07 |
JJting | 0:2dbbe7d28fab | 79 | #define OFFSET_Y_REG_H_M 0x08 |
JJting | 0:2dbbe7d28fab | 80 | #define OFFSET_Z_REG_L_M 0x09 |
JJting | 0:2dbbe7d28fab | 81 | #define OFFSET_Z_REG_H_M 0x0A |
JJting | 0:2dbbe7d28fab | 82 | #define WHO_AM_I_M 0x0F |
JJting | 0:2dbbe7d28fab | 83 | #define CTRL_REG1_M 0x20 |
JJting | 0:2dbbe7d28fab | 84 | #define CTRL_REG2_M 0x21 |
JJting | 0:2dbbe7d28fab | 85 | #define CTRL_REG3_M 0x22 |
JJting | 0:2dbbe7d28fab | 86 | #define CTRL_REG4_M 0x23 |
JJting | 0:2dbbe7d28fab | 87 | #define CTRL_REG5_M 0x24 |
JJting | 0:2dbbe7d28fab | 88 | #define STATUS_REG_M 0x27 |
JJting | 0:2dbbe7d28fab | 89 | #define OUT_X_L_M 0x28 |
JJting | 0:2dbbe7d28fab | 90 | #define OUT_X_H_M 0x29 |
JJting | 0:2dbbe7d28fab | 91 | #define OUT_Y_L_M 0x2A |
JJting | 0:2dbbe7d28fab | 92 | #define OUT_Y_H_M 0x2B |
JJting | 0:2dbbe7d28fab | 93 | #define OUT_Z_L_M 0x2C |
JJting | 0:2dbbe7d28fab | 94 | #define OUT_Z_H_M 0x2D |
JJting | 0:2dbbe7d28fab | 95 | #define INT_CFG_M 0x30 |
JJting | 0:2dbbe7d28fab | 96 | #define INT_SRC_M 0x30 |
JJting | 0:2dbbe7d28fab | 97 | #define INT_THS_L_M 0x32 |
JJting | 0:2dbbe7d28fab | 98 | #define INT_THS_H_M 0x33 |
JJting | 0:2dbbe7d28fab | 99 | |
JJting | 0:2dbbe7d28fab | 100 | //////////////////////////////// |
JJting | 0:2dbbe7d28fab | 101 | // LSM9DS1 WHO_AM_I Responses // |
JJting | 0:2dbbe7d28fab | 102 | //////////////////////////////// |
JJting | 0:2dbbe7d28fab | 103 | #define WHO_AM_I_AG_RSP 0x68 |
JJting | 0:2dbbe7d28fab | 104 | #define WHO_AM_I_M_RSP 0x3D |
JJting | 0:2dbbe7d28fab | 105 | |
JJting | 0:2dbbe7d28fab | 106 | // Possible I2C addresses for the accel/gyro and mag |
JJting | 0:2dbbe7d28fab | 107 | #define LSM9DS1_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4) |
JJting | 0:2dbbe7d28fab | 108 | #define LSM9DS1_M_I2C_ADDR(sa1) ((sa1) ? 0x3C : 0x38) |
JJting | 0:2dbbe7d28fab | 109 | |
JJting | 0:2dbbe7d28fab | 110 | /** |
JJting | 0:2dbbe7d28fab | 111 | * LSM9DS1 Class - driver for the 9 DoF IMU |
JJting | 0:2dbbe7d28fab | 112 | */ |
JJting | 0:2dbbe7d28fab | 113 | class LSM9DS1 |
JJting | 0:2dbbe7d28fab | 114 | { |
JJting | 0:2dbbe7d28fab | 115 | public: |
JJting | 0:2dbbe7d28fab | 116 | |
JJting | 0:2dbbe7d28fab | 117 | /// gyro_scale defines the possible full-scale ranges of the gyroscope: |
JJting | 0:2dbbe7d28fab | 118 | enum gyro_scale |
JJting | 0:2dbbe7d28fab | 119 | { |
JJting | 0:2dbbe7d28fab | 120 | G_SCALE_245DPS = 0x0 << 3, // 00 << 3: +/- 245 degrees per second |
JJting | 0:2dbbe7d28fab | 121 | G_SCALE_500DPS = 0x1 << 3, // 01 << 3: +/- 500 dps |
JJting | 0:2dbbe7d28fab | 122 | G_SCALE_2000DPS = 0x3 << 3 // 11 << 3: +/- 2000 dps |
JJting | 0:2dbbe7d28fab | 123 | }; |
JJting | 0:2dbbe7d28fab | 124 | |
JJting | 0:2dbbe7d28fab | 125 | /// gyro_odr defines all possible data rate/bandwidth combos of the gyro: |
JJting | 0:2dbbe7d28fab | 126 | enum gyro_odr |
JJting | 0:2dbbe7d28fab | 127 | { // ODR (Hz) --- Cutoff |
JJting | 0:2dbbe7d28fab | 128 | G_POWER_DOWN = 0x00, // 0 0 |
JJting | 0:2dbbe7d28fab | 129 | G_ODR_15_BW_0 = 0x20, // 14.9 0 |
JJting | 0:2dbbe7d28fab | 130 | G_ODR_60_BW_16 = 0x40, // 59.5 16 |
JJting | 0:2dbbe7d28fab | 131 | G_ODR_119_BW_14 = 0x60, // 119 14 |
JJting | 0:2dbbe7d28fab | 132 | G_ODR_119_BW_31 = 0x61, // 119 31 |
JJting | 0:2dbbe7d28fab | 133 | G_ODR_238_BW_14 = 0x80, // 238 14 |
JJting | 0:2dbbe7d28fab | 134 | G_ODR_238_BW_29 = 0x81, // 238 29 |
JJting | 0:2dbbe7d28fab | 135 | G_ODR_238_BW_63 = 0x82, // 238 63 |
JJting | 0:2dbbe7d28fab | 136 | G_ODR_238_BW_78 = 0x83, // 238 78 |
JJting | 0:2dbbe7d28fab | 137 | G_ODR_476_BW_21 = 0xA0, // 476 21 |
JJting | 0:2dbbe7d28fab | 138 | G_ODR_476_BW_28 = 0xA1, // 476 28 |
JJting | 0:2dbbe7d28fab | 139 | G_ODR_476_BW_57 = 0xA2, // 476 57 |
JJting | 0:2dbbe7d28fab | 140 | G_ODR_476_BW_100 = 0xA3, // 476 100 |
JJting | 0:2dbbe7d28fab | 141 | G_ODR_952_BW_33 = 0xC0, // 952 33 |
JJting | 0:2dbbe7d28fab | 142 | G_ODR_952_BW_40 = 0xC1, // 952 40 |
JJting | 0:2dbbe7d28fab | 143 | G_ODR_952_BW_58 = 0xC2, // 952 58 |
JJting | 0:2dbbe7d28fab | 144 | G_ODR_952_BW_100 = 0xC3 // 952 100 |
JJting | 0:2dbbe7d28fab | 145 | }; |
JJting | 0:2dbbe7d28fab | 146 | |
JJting | 0:2dbbe7d28fab | 147 | /// accel_scale defines all possible FSR's of the accelerometer: |
JJting | 0:2dbbe7d28fab | 148 | enum accel_scale |
JJting | 0:2dbbe7d28fab | 149 | { |
JJting | 0:2dbbe7d28fab | 150 | A_SCALE_2G, // 00: +/- 2g |
JJting | 0:2dbbe7d28fab | 151 | A_SCALE_16G,// 01: +/- 16g |
JJting | 0:2dbbe7d28fab | 152 | A_SCALE_4G, // 10: +/- 4g |
JJting | 0:2dbbe7d28fab | 153 | A_SCALE_8G // 11: +/- 8g |
JJting | 0:2dbbe7d28fab | 154 | }; |
JJting | 0:2dbbe7d28fab | 155 | |
JJting | 0:2dbbe7d28fab | 156 | /// accel_oder defines all possible output data rates of the accelerometer: |
JJting | 0:2dbbe7d28fab | 157 | enum accel_odr |
JJting | 0:2dbbe7d28fab | 158 | { |
JJting | 0:2dbbe7d28fab | 159 | A_POWER_DOWN, // Power-down mode (0x0) |
JJting | 0:2dbbe7d28fab | 160 | A_ODR_10, // 10 Hz (0x1) |
JJting | 0:2dbbe7d28fab | 161 | A_ODR_50, // 50 Hz (0x2) |
JJting | 0:2dbbe7d28fab | 162 | A_ODR_119, // 119 Hz (0x3) |
JJting | 0:2dbbe7d28fab | 163 | A_ODR_238, // 238 Hz (0x4) |
JJting | 0:2dbbe7d28fab | 164 | A_ODR_476, // 476 Hz (0x5) |
JJting | 0:2dbbe7d28fab | 165 | A_ODR_952 // 952 Hz (0x6) |
JJting | 0:2dbbe7d28fab | 166 | }; |
JJting | 0:2dbbe7d28fab | 167 | |
JJting | 0:2dbbe7d28fab | 168 | // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer: |
JJting | 0:2dbbe7d28fab | 169 | enum accel_bw |
JJting | 0:2dbbe7d28fab | 170 | { |
JJting | 0:2dbbe7d28fab | 171 | A_BW_AUTO_SCALE = 0x0, // Automatic BW scaling (0x0) |
JJting | 0:2dbbe7d28fab | 172 | A_BW_408 = 0x4, // 408 Hz (0x4) |
JJting | 0:2dbbe7d28fab | 173 | A_BW_211 = 0x5, // 211 Hz (0x5) |
JJting | 0:2dbbe7d28fab | 174 | A_BW_105 = 0x6, // 105 Hz (0x6) |
JJting | 0:2dbbe7d28fab | 175 | A_BW_50 = 0x7 // 50 Hz (0x7) |
JJting | 0:2dbbe7d28fab | 176 | }; |
JJting | 0:2dbbe7d28fab | 177 | |
JJting | 0:2dbbe7d28fab | 178 | /// mag_scale defines all possible FSR's of the magnetometer: |
JJting | 0:2dbbe7d28fab | 179 | enum mag_scale |
JJting | 0:2dbbe7d28fab | 180 | { |
JJting | 0:2dbbe7d28fab | 181 | M_SCALE_4GS, // 00: +/- 4Gs |
JJting | 0:2dbbe7d28fab | 182 | M_SCALE_8GS, // 01: +/- 8Gs |
JJting | 0:2dbbe7d28fab | 183 | M_SCALE_12GS, // 10: +/- 12Gs |
JJting | 0:2dbbe7d28fab | 184 | M_SCALE_16GS, // 11: +/- 16Gs |
JJting | 0:2dbbe7d28fab | 185 | }; |
JJting | 0:2dbbe7d28fab | 186 | |
JJting | 0:2dbbe7d28fab | 187 | /// mag_odr defines all possible output data rates of the magnetometer: |
JJting | 0:2dbbe7d28fab | 188 | enum mag_odr |
JJting | 0:2dbbe7d28fab | 189 | { |
JJting | 0:2dbbe7d28fab | 190 | M_ODR_0625, // 0.625 Hz (0x00) |
JJting | 0:2dbbe7d28fab | 191 | M_ODR_125, // 1.25 Hz (0x01) |
JJting | 0:2dbbe7d28fab | 192 | M_ODR_25, // 2.5 Hz (0x02) |
JJting | 0:2dbbe7d28fab | 193 | M_ODR_5, // 5 Hz (0x03) |
JJting | 0:2dbbe7d28fab | 194 | M_ODR_10, // 10 (0x04) |
JJting | 0:2dbbe7d28fab | 195 | M_ODR_20, // 20 Hz (0x05) |
JJting | 0:2dbbe7d28fab | 196 | M_ODR_40, // 40 Hz (0x06) |
JJting | 0:2dbbe7d28fab | 197 | M_ODR_80 // 80 Hz (0x07) |
JJting | 0:2dbbe7d28fab | 198 | }; |
JJting | 0:2dbbe7d28fab | 199 | |
JJting | 0:2dbbe7d28fab | 200 | // We'll store the gyro, accel, and magnetometer readings in a series of |
JJting | 0:2dbbe7d28fab | 201 | // public class variables. Each sensor gets three variables -- one for each |
JJting | 0:2dbbe7d28fab | 202 | // axis. Call readGyro(), readAccel(), and readMag() first, before using |
JJting | 0:2dbbe7d28fab | 203 | // these variables! |
JJting | 0:2dbbe7d28fab | 204 | // These values are the RAW signed 16-bit readings from the sensors. |
JJting | 0:2dbbe7d28fab | 205 | int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope |
JJting | 0:2dbbe7d28fab | 206 | int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer |
JJting | 0:2dbbe7d28fab | 207 | int16_t mx_raw, my_raw, mz_raw; // x, y, and z axis readings of the magnetometer |
JJting | 0:2dbbe7d28fab | 208 | int16_t temperature_raw; |
JJting | 0:2dbbe7d28fab | 209 | |
JJting | 0:2dbbe7d28fab | 210 | // floating-point values of scaled data in real-world units |
JJting | 0:2dbbe7d28fab | 211 | float gx, gy, gz; |
JJting | 0:2dbbe7d28fab | 212 | float ax, ay, az; |
JJting | 0:2dbbe7d28fab | 213 | float mx, my, mz; |
JJting | 0:2dbbe7d28fab | 214 | float temperature_c, temperature_f; // temperature in celcius and fahrenheit |
JJting | 0:2dbbe7d28fab | 215 | |
JJting | 0:2dbbe7d28fab | 216 | |
JJting | 0:2dbbe7d28fab | 217 | /** LSM9DS1 -- LSM9DS1 class constructor |
JJting | 0:2dbbe7d28fab | 218 | * The constructor will set up a handful of private variables, and set the |
JJting | 0:2dbbe7d28fab | 219 | * communication mode as well. |
JJting | 0:2dbbe7d28fab | 220 | * Input: |
JJting | 0:2dbbe7d28fab | 221 | * - interface = Either MODE_SPI or MODE_I2C, whichever you're using |
JJting | 0:2dbbe7d28fab | 222 | * to talk to the IC. |
JJting | 0:2dbbe7d28fab | 223 | * - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro. |
JJting | 0:2dbbe7d28fab | 224 | * If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G) |
JJting | 0:2dbbe7d28fab | 225 | * - mAddr = If MODE_I2C, this is the I2C address of the mag. |
JJting | 0:2dbbe7d28fab | 226 | * If MODE_SPI, this is the cs pin of the mag (CS_M) |
JJting | 0:2dbbe7d28fab | 227 | */ |
JJting | 0:2dbbe7d28fab | 228 | LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr = LSM9DS1_AG_I2C_ADDR(1), uint8_t mAddr = LSM9DS1_M_I2C_ADDR(1)); |
JJting | 0:2dbbe7d28fab | 229 | |
JJting | 0:2dbbe7d28fab | 230 | /** begin() -- Initialize the gyro, accelerometer, and magnetometer. |
JJting | 0:2dbbe7d28fab | 231 | * This will set up the scale and output rate of each sensor. It'll also |
JJting | 0:2dbbe7d28fab | 232 | * "turn on" every sensor and every axis of every sensor. |
JJting | 0:2dbbe7d28fab | 233 | * Input: |
JJting | 0:2dbbe7d28fab | 234 | * - gScl = The scale of the gyroscope. This should be a gyro_scale value. |
JJting | 0:2dbbe7d28fab | 235 | * - aScl = The scale of the accelerometer. Should be a accel_scale value. |
JJting | 0:2dbbe7d28fab | 236 | * - mScl = The scale of the magnetometer. Should be a mag_scale value. |
JJting | 0:2dbbe7d28fab | 237 | * - gODR = Output data rate of the gyroscope. gyro_odr value. |
JJting | 0:2dbbe7d28fab | 238 | * - aODR = Output data rate of the accelerometer. accel_odr value. |
JJting | 0:2dbbe7d28fab | 239 | * - mODR = Output data rate of the magnetometer. mag_odr value. |
JJting | 0:2dbbe7d28fab | 240 | * Output: The function will return an unsigned 16-bit value. The most-sig |
JJting | 0:2dbbe7d28fab | 241 | * bytes of the output are the WHO_AM_I reading of the accel/gyro. The |
JJting | 0:2dbbe7d28fab | 242 | * least significant two bytes are the WHO_AM_I reading of the mag. |
JJting | 0:2dbbe7d28fab | 243 | * All parameters have a defaulted value, so you can call just "begin()". |
JJting | 0:2dbbe7d28fab | 244 | * Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for |
JJting | 0:2dbbe7d28fab | 245 | * gyro, 119 Hz for accelerometer, 80 Hz for magnetometer. |
JJting | 0:2dbbe7d28fab | 246 | * Use the return value of this function to verify communication. |
JJting | 0:2dbbe7d28fab | 247 | */ |
JJting | 0:2dbbe7d28fab | 248 | uint16_t begin(gyro_scale gScl = G_SCALE_245DPS, |
JJting | 0:2dbbe7d28fab | 249 | accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_4GS, |
JJting | 0:2dbbe7d28fab | 250 | gyro_odr gODR = G_ODR_119_BW_14, accel_odr aODR = A_ODR_119, |
JJting | 0:2dbbe7d28fab | 251 | mag_odr mODR = M_ODR_80); |
JJting | 0:2dbbe7d28fab | 252 | |
JJting | 0:2dbbe7d28fab | 253 | /** readGyro() -- Read the gyroscope output registers. |
JJting | 0:2dbbe7d28fab | 254 | * This function will read all six gyroscope output registers. |
JJting | 0:2dbbe7d28fab | 255 | * The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read |
JJting | 0:2dbbe7d28fab | 256 | * those _after_ calling readGyro(). |
JJting | 0:2dbbe7d28fab | 257 | */ |
JJting | 0:2dbbe7d28fab | 258 | void readGyro(); |
JJting | 0:2dbbe7d28fab | 259 | |
JJting | 0:2dbbe7d28fab | 260 | /** readAccel() -- Read the accelerometer output registers. |
JJting | 0:2dbbe7d28fab | 261 | * This function will read all six accelerometer output registers. |
JJting | 0:2dbbe7d28fab | 262 | * The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read |
JJting | 0:2dbbe7d28fab | 263 | * those _after_ calling readAccel(). |
JJting | 0:2dbbe7d28fab | 264 | */ |
JJting | 0:2dbbe7d28fab | 265 | void readAccel(); |
JJting | 0:2dbbe7d28fab | 266 | |
JJting | 0:2dbbe7d28fab | 267 | /** readMag() -- Read the magnetometer output registers. |
JJting | 0:2dbbe7d28fab | 268 | * This function will read all six magnetometer output registers. |
JJting | 0:2dbbe7d28fab | 269 | * The readings are stored in the class' mx_raw, my_raw, and mz_raw variables. Read |
JJting | 0:2dbbe7d28fab | 270 | * those _after_ calling readMag(). |
JJting | 0:2dbbe7d28fab | 271 | */ |
JJting | 0:2dbbe7d28fab | 272 | void readMag(); |
JJting | 0:2dbbe7d28fab | 273 | |
JJting | 0:2dbbe7d28fab | 274 | /** readTemp() -- Read the temperature output register. |
JJting | 0:2dbbe7d28fab | 275 | * This function will read two temperature output registers. |
JJting | 0:2dbbe7d28fab | 276 | * The combined readings are stored in the class' temperature variables. Read |
JJting | 0:2dbbe7d28fab | 277 | * those _after_ calling readTemp(). |
JJting | 0:2dbbe7d28fab | 278 | */ |
JJting | 0:2dbbe7d28fab | 279 | void readTemp(); |
JJting | 0:2dbbe7d28fab | 280 | |
JJting | 0:2dbbe7d28fab | 281 | /** setGyroScale() -- Set the full-scale range of the gyroscope. |
JJting | 0:2dbbe7d28fab | 282 | * This function can be called to set the scale of the gyroscope to |
JJting | 0:2dbbe7d28fab | 283 | * 245, 500, or 2000 degrees per second. |
JJting | 0:2dbbe7d28fab | 284 | * Input: |
JJting | 0:2dbbe7d28fab | 285 | * - gScl = The desired gyroscope scale. Must be one of three possible |
JJting | 0:2dbbe7d28fab | 286 | * values from the gyro_scale enum. |
JJting | 0:2dbbe7d28fab | 287 | */ |
JJting | 0:2dbbe7d28fab | 288 | void setGyroScale(gyro_scale gScl); |
JJting | 0:2dbbe7d28fab | 289 | |
JJting | 0:2dbbe7d28fab | 290 | /** setAccelScale() -- Set the full-scale range of the accelerometer. |
JJting | 0:2dbbe7d28fab | 291 | * This function can be called to set the scale of the accelerometer to |
JJting | 0:2dbbe7d28fab | 292 | * 2, 4, 8, or 16 g's. |
JJting | 0:2dbbe7d28fab | 293 | * Input: |
JJting | 0:2dbbe7d28fab | 294 | * - aScl = The desired accelerometer scale. Must be one of five possible |
JJting | 0:2dbbe7d28fab | 295 | * values from the accel_scale enum. |
JJting | 0:2dbbe7d28fab | 296 | */ |
JJting | 0:2dbbe7d28fab | 297 | void setAccelScale(accel_scale aScl); |
JJting | 0:2dbbe7d28fab | 298 | |
JJting | 0:2dbbe7d28fab | 299 | /** setMagScale() -- Set the full-scale range of the magnetometer. |
JJting | 0:2dbbe7d28fab | 300 | * This function can be called to set the scale of the magnetometer to |
JJting | 0:2dbbe7d28fab | 301 | * 4, 8, 12, or 16 Gs. |
JJting | 0:2dbbe7d28fab | 302 | * Input: |
JJting | 0:2dbbe7d28fab | 303 | * - mScl = The desired magnetometer scale. Must be one of four possible |
JJting | 0:2dbbe7d28fab | 304 | * values from the mag_scale enum. |
JJting | 0:2dbbe7d28fab | 305 | */ |
JJting | 0:2dbbe7d28fab | 306 | void setMagScale(mag_scale mScl); |
JJting | 0:2dbbe7d28fab | 307 | |
JJting | 0:2dbbe7d28fab | 308 | /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope |
JJting | 0:2dbbe7d28fab | 309 | * Input: |
JJting | 0:2dbbe7d28fab | 310 | * - gRate = The desired output rate and cutoff frequency of the gyro. |
JJting | 0:2dbbe7d28fab | 311 | * Must be a value from the gyro_odr enum (check above). |
JJting | 0:2dbbe7d28fab | 312 | */ |
JJting | 0:2dbbe7d28fab | 313 | void setGyroODR(gyro_odr gRate); |
JJting | 0:2dbbe7d28fab | 314 | |
JJting | 0:2dbbe7d28fab | 315 | /** setAccelODR() -- Set the output data rate of the accelerometer |
JJting | 0:2dbbe7d28fab | 316 | * Input: |
JJting | 0:2dbbe7d28fab | 317 | * - aRate = The desired output rate of the accel. |
JJting | 0:2dbbe7d28fab | 318 | * Must be a value from the accel_odr enum (check above). |
JJting | 0:2dbbe7d28fab | 319 | */ |
JJting | 0:2dbbe7d28fab | 320 | void setAccelODR(accel_odr aRate); |
JJting | 0:2dbbe7d28fab | 321 | |
JJting | 0:2dbbe7d28fab | 322 | /** setMagODR() -- Set the output data rate of the magnetometer |
JJting | 0:2dbbe7d28fab | 323 | * Input: |
JJting | 0:2dbbe7d28fab | 324 | * - mRate = The desired output rate of the mag. |
JJting | 0:2dbbe7d28fab | 325 | * Must be a value from the mag_odr enum (check above). |
JJting | 0:2dbbe7d28fab | 326 | */ |
JJting | 0:2dbbe7d28fab | 327 | void setMagODR(mag_odr mRate); |
JJting | 0:2dbbe7d28fab | 328 | |
JJting | 0:2dbbe7d28fab | 329 | |
JJting | 0:2dbbe7d28fab | 330 | private: |
JJting | 0:2dbbe7d28fab | 331 | /** xgAddress and mAddress store the I2C address |
JJting | 0:2dbbe7d28fab | 332 | * for each sensor. |
JJting | 0:2dbbe7d28fab | 333 | */ |
JJting | 0:2dbbe7d28fab | 334 | uint8_t xgAddress, mAddress; |
JJting | 0:2dbbe7d28fab | 335 | |
JJting | 0:2dbbe7d28fab | 336 | // I2C bus |
JJting | 0:2dbbe7d28fab | 337 | I2C i2c; |
JJting | 0:2dbbe7d28fab | 338 | |
JJting | 0:2dbbe7d28fab | 339 | /** gScale, aScale, and mScale store the current scale range for each |
JJting | 0:2dbbe7d28fab | 340 | * sensor. Should be updated whenever that value changes. |
JJting | 0:2dbbe7d28fab | 341 | */ |
JJting | 0:2dbbe7d28fab | 342 | gyro_scale gScale; |
JJting | 0:2dbbe7d28fab | 343 | accel_scale aScale; |
JJting | 0:2dbbe7d28fab | 344 | mag_scale mScale; |
JJting | 0:2dbbe7d28fab | 345 | |
JJting | 0:2dbbe7d28fab | 346 | /** gRes, aRes, and mRes store the current resolution for each sensor. |
JJting | 0:2dbbe7d28fab | 347 | * Units of these values would be DPS (or g's or Gs's) per ADC tick. |
JJting | 0:2dbbe7d28fab | 348 | * This value is calculated as (sensor scale) / (2^15). |
JJting | 0:2dbbe7d28fab | 349 | */ |
JJting | 0:2dbbe7d28fab | 350 | float gRes, aRes, mRes; |
JJting | 0:2dbbe7d28fab | 351 | |
JJting | 0:2dbbe7d28fab | 352 | /** initGyro() -- Sets up the gyroscope to begin reading. |
JJting | 0:2dbbe7d28fab | 353 | * This function steps through all three gyroscope control registers. |
JJting | 0:2dbbe7d28fab | 354 | */ |
JJting | 0:2dbbe7d28fab | 355 | void initGyro(); |
JJting | 0:2dbbe7d28fab | 356 | |
JJting | 0:2dbbe7d28fab | 357 | /** initAccel() -- Sets up the accelerometer to begin reading. |
JJting | 0:2dbbe7d28fab | 358 | * This function steps through all accelerometer related control registers. |
JJting | 0:2dbbe7d28fab | 359 | */ |
JJting | 0:2dbbe7d28fab | 360 | void initAccel(); |
JJting | 0:2dbbe7d28fab | 361 | |
JJting | 0:2dbbe7d28fab | 362 | /** initMag() -- Sets up the magnetometer to begin reading. |
JJting | 0:2dbbe7d28fab | 363 | * This function steps through all magnetometer-related control registers. |
JJting | 0:2dbbe7d28fab | 364 | */ |
JJting | 0:2dbbe7d28fab | 365 | void initMag(); |
JJting | 0:2dbbe7d28fab | 366 | |
JJting | 0:2dbbe7d28fab | 367 | /** calcgRes() -- Calculate the resolution of the gyroscope. |
JJting | 0:2dbbe7d28fab | 368 | * This function will set the value of the gRes variable. gScale must |
JJting | 0:2dbbe7d28fab | 369 | * be set prior to calling this function. |
JJting | 0:2dbbe7d28fab | 370 | */ |
JJting | 0:2dbbe7d28fab | 371 | void calcgRes(); |
JJting | 0:2dbbe7d28fab | 372 | |
JJting | 0:2dbbe7d28fab | 373 | /** calcmRes() -- Calculate the resolution of the magnetometer. |
JJting | 0:2dbbe7d28fab | 374 | * This function will set the value of the mRes variable. mScale must |
JJting | 0:2dbbe7d28fab | 375 | * be set prior to calling this function. |
JJting | 0:2dbbe7d28fab | 376 | */ |
JJting | 0:2dbbe7d28fab | 377 | void calcmRes(); |
JJting | 0:2dbbe7d28fab | 378 | |
JJting | 0:2dbbe7d28fab | 379 | /** calcaRes() -- Calculate the resolution of the accelerometer. |
JJting | 0:2dbbe7d28fab | 380 | * This function will set the value of the aRes variable. aScale must |
JJting | 0:2dbbe7d28fab | 381 | * be set prior to calling this function. |
JJting | 0:2dbbe7d28fab | 382 | */ |
JJting | 0:2dbbe7d28fab | 383 | void calcaRes(); |
JJting | 0:2dbbe7d28fab | 384 | }; |
JJting | 0:2dbbe7d28fab | 385 | |
JJting | 0:2dbbe7d28fab | 386 | #endif // _LSM9DS1_H // |