VGR
Dependents: VITI_motor_angle_1 VITI_motor_angle_2 VITI_motor_angle_3
LSM9DS1.cpp@3:f96b287c0bf7, 2016-06-21 (annotated)
- Committer:
- 5hel2l2y
- Date:
- Tue Jun 21 22:27:17 2016 +0000
- Revision:
- 3:f96b287c0bf7
- Parent:
- 2:ac3b69ccd3dd
- Child:
- 4:7ffcb378cfd4
Added interrupts.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beanmachine44 | 0:622e8874902e | 1 | #include "LSM9DS1.h" |
beanmachine44 | 0:622e8874902e | 2 | |
beanmachine44 | 0:622e8874902e | 3 | LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr) : i2c(sda, scl) |
beanmachine44 | 0:622e8874902e | 4 | { |
beanmachine44 | 0:622e8874902e | 5 | // xgAddress and mAddress will store the 7-bit I2C address, if using I2C. |
beanmachine44 | 0:622e8874902e | 6 | xgAddress = xgAddr; |
beanmachine44 | 0:622e8874902e | 7 | mAddress = mAddr; |
beanmachine44 | 0:622e8874902e | 8 | } |
beanmachine44 | 0:622e8874902e | 9 | |
beanmachine44 | 0:622e8874902e | 10 | uint16_t LSM9DS1::begin(gyro_scale gScl, accel_scale aScl, mag_scale mScl, |
beanmachine44 | 0:622e8874902e | 11 | gyro_odr gODR, accel_odr aODR, mag_odr mODR) |
beanmachine44 | 0:622e8874902e | 12 | { |
beanmachine44 | 0:622e8874902e | 13 | // Store the given scales in class variables. These scale variables |
beanmachine44 | 0:622e8874902e | 14 | // are used throughout to calculate the actual g's, DPS,and Gs's. |
beanmachine44 | 0:622e8874902e | 15 | gScale = gScl; |
beanmachine44 | 0:622e8874902e | 16 | aScale = aScl; |
beanmachine44 | 0:622e8874902e | 17 | mScale = mScl; |
beanmachine44 | 0:622e8874902e | 18 | |
beanmachine44 | 0:622e8874902e | 19 | // Once we have the scale values, we can calculate the resolution |
beanmachine44 | 0:622e8874902e | 20 | // of each sensor. That's what these functions are for. One for each sensor |
beanmachine44 | 0:622e8874902e | 21 | calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable |
beanmachine44 | 0:622e8874902e | 22 | calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable |
beanmachine44 | 0:622e8874902e | 23 | calcaRes(); // Calculate g / ADC tick, stored in aRes variable |
beanmachine44 | 0:622e8874902e | 24 | |
beanmachine44 | 0:622e8874902e | 25 | |
beanmachine44 | 0:622e8874902e | 26 | // To verify communication, we can read from the WHO_AM_I register of |
beanmachine44 | 0:622e8874902e | 27 | // each device. Store those in a variable so we can return them. |
beanmachine44 | 0:622e8874902e | 28 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 29 | char cmd[2] = { |
beanmachine44 | 0:622e8874902e | 30 | WHO_AM_I_XG, |
beanmachine44 | 0:622e8874902e | 31 | 0 |
beanmachine44 | 0:622e8874902e | 32 | }; |
beanmachine44 | 0:622e8874902e | 33 | |
beanmachine44 | 0:622e8874902e | 34 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 35 | i2c.write(xgAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 36 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 37 | i2c.read(xgAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 38 | uint8_t xgTest = cmd[1]; // Read the accel/gyro WHO_AM_I |
beanmachine44 | 0:622e8874902e | 39 | |
beanmachine44 | 0:622e8874902e | 40 | // Reset to the address of the mag who am i |
beanmachine44 | 0:622e8874902e | 41 | cmd[1] = WHO_AM_I_M; |
beanmachine44 | 0:622e8874902e | 42 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 43 | i2c.write(mAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 44 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 45 | i2c.read(mAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 46 | uint8_t mTest = cmd[1]; // Read the mag WHO_AM_I |
beanmachine44 | 0:622e8874902e | 47 | |
beanmachine44 | 0:622e8874902e | 48 | // Gyro initialization stuff: |
beanmachine44 | 0:622e8874902e | 49 | initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc. |
beanmachine44 | 0:622e8874902e | 50 | setGyroODR(gODR); // Set the gyro output data rate and bandwidth. |
beanmachine44 | 0:622e8874902e | 51 | setGyroScale(gScale); // Set the gyro range |
beanmachine44 | 0:622e8874902e | 52 | |
beanmachine44 | 0:622e8874902e | 53 | // Accelerometer initialization stuff: |
beanmachine44 | 0:622e8874902e | 54 | initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc. |
beanmachine44 | 0:622e8874902e | 55 | setAccelODR(aODR); // Set the accel data rate. |
beanmachine44 | 0:622e8874902e | 56 | setAccelScale(aScale); // Set the accel range. |
beanmachine44 | 0:622e8874902e | 57 | |
beanmachine44 | 0:622e8874902e | 58 | // Magnetometer initialization stuff: |
beanmachine44 | 0:622e8874902e | 59 | initMag(); // "Turn on" all axes of the mag. Set up interrupts, etc. |
beanmachine44 | 0:622e8874902e | 60 | setMagODR(mODR); // Set the magnetometer output data rate. |
beanmachine44 | 0:622e8874902e | 61 | setMagScale(mScale); // Set the magnetometer's range. |
beanmachine44 | 0:622e8874902e | 62 | |
5hel2l2y | 3:f96b287c0bf7 | 63 | initIntr(); |
5hel2l2y | 3:f96b287c0bf7 | 64 | |
beanmachine44 | 0:622e8874902e | 65 | // Once everything is initialized, return the WHO_AM_I registers we read: |
beanmachine44 | 0:622e8874902e | 66 | return (xgTest << 8) | mTest; |
beanmachine44 | 0:622e8874902e | 67 | } |
beanmachine44 | 0:622e8874902e | 68 | |
beanmachine44 | 0:622e8874902e | 69 | void LSM9DS1::initGyro() |
beanmachine44 | 0:622e8874902e | 70 | { |
beanmachine44 | 0:622e8874902e | 71 | char cmd[4] = { |
beanmachine44 | 0:622e8874902e | 72 | CTRL_REG1_G, |
beanmachine44 | 0:622e8874902e | 73 | gScale | G_ODR_119_BW_14, |
beanmachine44 | 0:622e8874902e | 74 | 0, // Default data out and int out |
beanmachine44 | 0:622e8874902e | 75 | 0 // Default power mode and high pass settings |
beanmachine44 | 0:622e8874902e | 76 | }; |
beanmachine44 | 0:622e8874902e | 77 | |
beanmachine44 | 0:622e8874902e | 78 | // Write the data to the gyro control registers |
beanmachine44 | 0:622e8874902e | 79 | i2c.write(xgAddress, cmd, 4); |
beanmachine44 | 0:622e8874902e | 80 | } |
beanmachine44 | 0:622e8874902e | 81 | |
beanmachine44 | 0:622e8874902e | 82 | void LSM9DS1::initAccel() |
beanmachine44 | 0:622e8874902e | 83 | { |
beanmachine44 | 0:622e8874902e | 84 | char cmd[4] = { |
beanmachine44 | 0:622e8874902e | 85 | CTRL_REG5_XL, |
beanmachine44 | 0:622e8874902e | 86 | 0x38, // Enable all axis and don't decimate data in out Registers |
beanmachine44 | 0:622e8874902e | 87 | (A_ODR_119 << 5) | (aScale << 3) | (A_BW_AUTO_SCALE), // 119 Hz ODR, set scale, and auto BW |
beanmachine44 | 0:622e8874902e | 88 | 0 // Default resolution mode and filtering settings |
beanmachine44 | 0:622e8874902e | 89 | }; |
beanmachine44 | 0:622e8874902e | 90 | |
beanmachine44 | 0:622e8874902e | 91 | // Write the data to the accel control registers |
beanmachine44 | 0:622e8874902e | 92 | i2c.write(xgAddress, cmd, 4); |
beanmachine44 | 0:622e8874902e | 93 | } |
beanmachine44 | 0:622e8874902e | 94 | |
beanmachine44 | 0:622e8874902e | 95 | void LSM9DS1::initMag() |
beanmachine44 | 0:622e8874902e | 96 | { |
beanmachine44 | 0:622e8874902e | 97 | char cmd[4] = { |
beanmachine44 | 0:622e8874902e | 98 | CTRL_REG1_M, |
beanmachine44 | 0:622e8874902e | 99 | 0x10, // Default data rate, xy axes mode, and temp comp |
beanmachine44 | 0:622e8874902e | 100 | mScale << 5, // Set mag scale |
beanmachine44 | 0:622e8874902e | 101 | 0 // Enable I2C, write only SPI, not LP mode, Continuous conversion mode |
beanmachine44 | 0:622e8874902e | 102 | }; |
beanmachine44 | 0:622e8874902e | 103 | |
beanmachine44 | 0:622e8874902e | 104 | // Write the data to the mag control registers |
beanmachine44 | 0:622e8874902e | 105 | i2c.write(mAddress, cmd, 4); |
beanmachine44 | 0:622e8874902e | 106 | } |
beanmachine44 | 0:622e8874902e | 107 | |
5hel2l2y | 3:f96b287c0bf7 | 108 | void LSM9DS1::initIntr() |
5hel2l2y | 3:f96b287c0bf7 | 109 | { |
5hel2l2y | 3:f96b287c0bf7 | 110 | char cmd[2]; |
5hel2l2y | 3:f96b287c0bf7 | 111 | |
5hel2l2y | 3:f96b287c0bf7 | 112 | cmd[0] = INT_GEN_CFG_G; |
5hel2l2y | 3:f96b287c0bf7 | 113 | cmd[1] = (1 << 5); |
5hel2l2y | 3:f96b287c0bf7 | 114 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 115 | cmd[0] = INT_GEN_THS_XH_G + 6; |
5hel2l2y | 3:f96b287c0bf7 | 116 | cmd[1] = (500 & 0x7F) >> 8; |
5hel2l2y | 3:f96b287c0bf7 | 117 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 118 | cmd[0] = INT_GEN_THS_XL_G + 6; |
5hel2l2y | 3:f96b287c0bf7 | 119 | cmd[1] = (500 & 0x7F); |
5hel2l2y | 3:f96b287c0bf7 | 120 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 121 | cmd[0] = INT_GEN_DUR_G; |
5hel2l2y | 3:f96b287c0bf7 | 122 | cmd[1] = 0x80; |
5hel2l2y | 3:f96b287c0bf7 | 123 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 124 | |
5hel2l2y | 3:f96b287c0bf7 | 125 | cmd[0] = INT_GEN_CFG_XL; |
5hel2l2y | 3:f96b287c0bf7 | 126 | cmd[1] = (1 << 1); |
5hel2l2y | 3:f96b287c0bf7 | 127 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 128 | cmd[0] = INT_GEN_THS_X_XL + 1; |
5hel2l2y | 3:f96b287c0bf7 | 129 | cmd[1] = 20; |
5hel2l2y | 3:f96b287c0bf7 | 130 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 131 | cmd[0] = INT_GEN_DUR_XL; |
5hel2l2y | 3:f96b287c0bf7 | 132 | cmd[1] = (1 & 0x7F); |
5hel2l2y | 3:f96b287c0bf7 | 133 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 134 | |
5hel2l2y | 3:f96b287c0bf7 | 135 | cmd[0] = INT1_CTRL; |
5hel2l2y | 3:f96b287c0bf7 | 136 | cmd[1] = (1 << 7) | (1 << 6); |
5hel2l2y | 3:f96b287c0bf7 | 137 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 138 | cmd[0] = CTRL_REG8; |
5hel2l2y | 3:f96b287c0bf7 | 139 | cmd[1] = (1 << 5); |
5hel2l2y | 3:f96b287c0bf7 | 140 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 141 | |
5hel2l2y | 3:f96b287c0bf7 | 142 | cmd[0] = INT2_CTRL; |
5hel2l2y | 3:f96b287c0bf7 | 143 | cmd[1] = (1 << 0) | (1 << 1); |
5hel2l2y | 3:f96b287c0bf7 | 144 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 145 | cmd[0] = CTRL_REG8; |
5hel2l2y | 3:f96b287c0bf7 | 146 | cmd[1] = (1 << 5); |
5hel2l2y | 3:f96b287c0bf7 | 147 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 148 | |
5hel2l2y | 3:f96b287c0bf7 | 149 | cmd[0] = INT_CFG_M; |
5hel2l2y | 3:f96b287c0bf7 | 150 | cmd[1] = (1 << 0); |
5hel2l2y | 3:f96b287c0bf7 | 151 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 152 | |
5hel2l2y | 3:f96b287c0bf7 | 153 | cmd[0] = INT_THS_H_M; |
5hel2l2y | 3:f96b287c0bf7 | 154 | cmd[1] = uint8_t((10000 & 0x7F00) >> 8); |
5hel2l2y | 3:f96b287c0bf7 | 155 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 156 | cmd[0] = INT_THS_L_M; |
5hel2l2y | 3:f96b287c0bf7 | 157 | cmd[1] = uint8_t(10000 & 0x00FF); |
5hel2l2y | 3:f96b287c0bf7 | 158 | i2c.write(xgAddress, cmd, 2); |
5hel2l2y | 3:f96b287c0bf7 | 159 | } |
5hel2l2y | 3:f96b287c0bf7 | 160 | |
beanmachine44 | 0:622e8874902e | 161 | void LSM9DS1::readAccel() |
beanmachine44 | 0:622e8874902e | 162 | { |
beanmachine44 | 0:622e8874902e | 163 | // The data we are going to read from the accel |
beanmachine44 | 0:622e8874902e | 164 | char data[6]; |
beanmachine44 | 0:622e8874902e | 165 | |
beanmachine44 | 0:622e8874902e | 166 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 167 | char subAddress = OUT_X_L_XL; |
beanmachine44 | 0:622e8874902e | 168 | |
beanmachine44 | 0:622e8874902e | 169 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 170 | i2c.write(xgAddress, &subAddress, 1, true); |
beanmachine44 | 0:622e8874902e | 171 | // Read in all 8 bit registers containing the axes data |
beanmachine44 | 0:622e8874902e | 172 | i2c.read(xgAddress, data, 6); |
beanmachine44 | 0:622e8874902e | 173 | |
beanmachine44 | 0:622e8874902e | 174 | // Reassemble the data and convert to g |
beanmachine44 | 0:622e8874902e | 175 | ax_raw = data[0] | (data[1] << 8); |
beanmachine44 | 0:622e8874902e | 176 | ay_raw = data[2] | (data[3] << 8); |
beanmachine44 | 0:622e8874902e | 177 | az_raw = data[4] | (data[5] << 8); |
beanmachine44 | 0:622e8874902e | 178 | ax = ax_raw * aRes; |
beanmachine44 | 0:622e8874902e | 179 | ay = ay_raw * aRes; |
beanmachine44 | 0:622e8874902e | 180 | az = az_raw * aRes; |
beanmachine44 | 0:622e8874902e | 181 | } |
beanmachine44 | 0:622e8874902e | 182 | |
beanmachine44 | 0:622e8874902e | 183 | void LSM9DS1::readMag() |
beanmachine44 | 0:622e8874902e | 184 | { |
beanmachine44 | 0:622e8874902e | 185 | // The data we are going to read from the mag |
beanmachine44 | 0:622e8874902e | 186 | char data[6]; |
beanmachine44 | 0:622e8874902e | 187 | |
beanmachine44 | 0:622e8874902e | 188 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 189 | char subAddress = OUT_X_L_M; |
beanmachine44 | 0:622e8874902e | 190 | |
beanmachine44 | 0:622e8874902e | 191 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 192 | i2c.write(mAddress, &subAddress, 1, true); |
beanmachine44 | 0:622e8874902e | 193 | // Read in all 8 bit registers containing the axes data |
beanmachine44 | 0:622e8874902e | 194 | i2c.read(mAddress, data, 6); |
beanmachine44 | 0:622e8874902e | 195 | |
beanmachine44 | 0:622e8874902e | 196 | // Reassemble the data and convert to degrees |
beanmachine44 | 0:622e8874902e | 197 | mx_raw = data[0] | (data[1] << 8); |
beanmachine44 | 0:622e8874902e | 198 | my_raw = data[2] | (data[3] << 8); |
beanmachine44 | 0:622e8874902e | 199 | mz_raw = data[4] | (data[5] << 8); |
beanmachine44 | 0:622e8874902e | 200 | mx = mx_raw * mRes; |
beanmachine44 | 0:622e8874902e | 201 | my = my_raw * mRes; |
beanmachine44 | 0:622e8874902e | 202 | mz = mz_raw * mRes; |
beanmachine44 | 0:622e8874902e | 203 | } |
beanmachine44 | 0:622e8874902e | 204 | |
5hel2l2y | 3:f96b287c0bf7 | 205 | void LSM9DS1::readIntr() |
5hel2l2y | 3:f96b287c0bf7 | 206 | { |
5hel2l2y | 3:f96b287c0bf7 | 207 | char data[1]; |
5hel2l2y | 3:f96b287c0bf7 | 208 | char subAddress = INT_GEN_SRC_G; |
5hel2l2y | 3:f96b287c0bf7 | 209 | |
5hel2l2y | 3:f96b287c0bf7 | 210 | i2c.write(xgAddress, &subAddress, 1, true); |
5hel2l2y | 3:f96b287c0bf7 | 211 | i2c.read(xgAddress, data, 1); |
5hel2l2y | 3:f96b287c0bf7 | 212 | |
5hel2l2y | 3:f96b287c0bf7 | 213 | intr = (float)data[0]; |
5hel2l2y | 3:f96b287c0bf7 | 214 | } |
5hel2l2y | 3:f96b287c0bf7 | 215 | |
beanmachine44 | 0:622e8874902e | 216 | void LSM9DS1::readTemp() |
beanmachine44 | 0:622e8874902e | 217 | { |
beanmachine44 | 0:622e8874902e | 218 | // The data we are going to read from the temp |
beanmachine44 | 0:622e8874902e | 219 | char data[2]; |
beanmachine44 | 0:622e8874902e | 220 | |
beanmachine44 | 0:622e8874902e | 221 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 222 | char subAddress = OUT_TEMP_L; |
beanmachine44 | 0:622e8874902e | 223 | |
beanmachine44 | 0:622e8874902e | 224 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 225 | i2c.write(xgAddress, &subAddress, 1, true); |
beanmachine44 | 0:622e8874902e | 226 | // Read in all 8 bit registers containing the axes data |
beanmachine44 | 0:622e8874902e | 227 | i2c.read(xgAddress, data, 2); |
beanmachine44 | 0:622e8874902e | 228 | |
beanmachine44 | 0:622e8874902e | 229 | // Temperature is a 12-bit signed integer |
beanmachine44 | 0:622e8874902e | 230 | temperature_raw = data[0] | (data[1] << 8); |
beanmachine44 | 0:622e8874902e | 231 | |
beanmachine44 | 0:622e8874902e | 232 | temperature_c = (float)temperature_raw / 8.0 + 25; |
beanmachine44 | 0:622e8874902e | 233 | temperature_f = temperature_c * 1.8 + 32; |
beanmachine44 | 0:622e8874902e | 234 | } |
beanmachine44 | 0:622e8874902e | 235 | |
beanmachine44 | 0:622e8874902e | 236 | void LSM9DS1::readGyro() |
beanmachine44 | 0:622e8874902e | 237 | { |
beanmachine44 | 0:622e8874902e | 238 | // The data we are going to read from the gyro |
beanmachine44 | 0:622e8874902e | 239 | char data[6]; |
beanmachine44 | 0:622e8874902e | 240 | |
beanmachine44 | 0:622e8874902e | 241 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 242 | char subAddress = OUT_X_L_G; |
beanmachine44 | 0:622e8874902e | 243 | |
beanmachine44 | 0:622e8874902e | 244 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 245 | i2c.write(xgAddress, &subAddress, 1, true); |
beanmachine44 | 0:622e8874902e | 246 | // Read in all 8 bit registers containing the axes data |
beanmachine44 | 0:622e8874902e | 247 | i2c.read(xgAddress, data, 6); |
beanmachine44 | 0:622e8874902e | 248 | |
beanmachine44 | 0:622e8874902e | 249 | // Reassemble the data and convert to degrees/sec |
beanmachine44 | 0:622e8874902e | 250 | gx_raw = data[0] | (data[1] << 8); |
beanmachine44 | 0:622e8874902e | 251 | gy_raw = data[2] | (data[3] << 8); |
beanmachine44 | 0:622e8874902e | 252 | gz_raw = data[4] | (data[5] << 8); |
beanmachine44 | 0:622e8874902e | 253 | gx = gx_raw * gRes; |
beanmachine44 | 0:622e8874902e | 254 | gy = gy_raw * gRes; |
beanmachine44 | 0:622e8874902e | 255 | gz = gz_raw * gRes; |
beanmachine44 | 0:622e8874902e | 256 | } |
beanmachine44 | 0:622e8874902e | 257 | |
beanmachine44 | 0:622e8874902e | 258 | void LSM9DS1::setGyroScale(gyro_scale gScl) |
beanmachine44 | 0:622e8874902e | 259 | { |
beanmachine44 | 0:622e8874902e | 260 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 261 | char cmd[2] = { |
beanmachine44 | 0:622e8874902e | 262 | CTRL_REG1_G, |
beanmachine44 | 0:622e8874902e | 263 | 0 |
beanmachine44 | 0:622e8874902e | 264 | }; |
beanmachine44 | 0:622e8874902e | 265 | |
beanmachine44 | 0:622e8874902e | 266 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 267 | i2c.write(xgAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 268 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 269 | i2c.read(xgAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 270 | |
beanmachine44 | 0:622e8874902e | 271 | // Then mask out the gyro scale bits: |
beanmachine44 | 0:622e8874902e | 272 | cmd[1] &= 0xFF^(0x3 << 3); |
beanmachine44 | 0:622e8874902e | 273 | // Then shift in our new scale bits: |
beanmachine44 | 0:622e8874902e | 274 | cmd[1] |= gScl << 3; |
beanmachine44 | 0:622e8874902e | 275 | |
beanmachine44 | 0:622e8874902e | 276 | // Write the gyroscale out to the gyro |
beanmachine44 | 0:622e8874902e | 277 | i2c.write(xgAddress, cmd, 2); |
beanmachine44 | 0:622e8874902e | 278 | |
beanmachine44 | 0:622e8874902e | 279 | // We've updated the sensor, but we also need to update our class variables |
beanmachine44 | 0:622e8874902e | 280 | // First update gScale: |
beanmachine44 | 0:622e8874902e | 281 | gScale = gScl; |
beanmachine44 | 0:622e8874902e | 282 | // Then calculate a new gRes, which relies on gScale being set correctly: |
beanmachine44 | 0:622e8874902e | 283 | calcgRes(); |
beanmachine44 | 0:622e8874902e | 284 | } |
beanmachine44 | 0:622e8874902e | 285 | |
beanmachine44 | 0:622e8874902e | 286 | void LSM9DS1::setAccelScale(accel_scale aScl) |
beanmachine44 | 0:622e8874902e | 287 | { |
beanmachine44 | 0:622e8874902e | 288 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 289 | char cmd[2] = { |
beanmachine44 | 0:622e8874902e | 290 | CTRL_REG6_XL, |
beanmachine44 | 0:622e8874902e | 291 | 0 |
beanmachine44 | 0:622e8874902e | 292 | }; |
beanmachine44 | 0:622e8874902e | 293 | |
beanmachine44 | 0:622e8874902e | 294 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 295 | i2c.write(xgAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 296 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 297 | i2c.read(xgAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 298 | |
beanmachine44 | 0:622e8874902e | 299 | // Then mask out the accel scale bits: |
beanmachine44 | 0:622e8874902e | 300 | cmd[1] &= 0xFF^(0x3 << 3); |
beanmachine44 | 0:622e8874902e | 301 | // Then shift in our new scale bits: |
beanmachine44 | 0:622e8874902e | 302 | cmd[1] |= aScl << 3; |
beanmachine44 | 0:622e8874902e | 303 | |
beanmachine44 | 0:622e8874902e | 304 | // Write the accelscale out to the accel |
beanmachine44 | 0:622e8874902e | 305 | i2c.write(xgAddress, cmd, 2); |
beanmachine44 | 0:622e8874902e | 306 | |
beanmachine44 | 0:622e8874902e | 307 | // We've updated the sensor, but we also need to update our class variables |
beanmachine44 | 0:622e8874902e | 308 | // First update aScale: |
beanmachine44 | 0:622e8874902e | 309 | aScale = aScl; |
beanmachine44 | 0:622e8874902e | 310 | // Then calculate a new aRes, which relies on aScale being set correctly: |
beanmachine44 | 0:622e8874902e | 311 | calcaRes(); |
beanmachine44 | 0:622e8874902e | 312 | } |
beanmachine44 | 0:622e8874902e | 313 | |
beanmachine44 | 0:622e8874902e | 314 | void LSM9DS1::setMagScale(mag_scale mScl) |
beanmachine44 | 0:622e8874902e | 315 | { |
beanmachine44 | 0:622e8874902e | 316 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 317 | char cmd[2] = { |
beanmachine44 | 0:622e8874902e | 318 | CTRL_REG2_M, |
beanmachine44 | 0:622e8874902e | 319 | 0 |
beanmachine44 | 0:622e8874902e | 320 | }; |
beanmachine44 | 0:622e8874902e | 321 | |
beanmachine44 | 0:622e8874902e | 322 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 323 | i2c.write(mAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 324 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 325 | i2c.read(mAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 326 | |
beanmachine44 | 0:622e8874902e | 327 | // Then mask out the mag scale bits: |
beanmachine44 | 0:622e8874902e | 328 | cmd[1] &= 0xFF^(0x3 << 5); |
beanmachine44 | 0:622e8874902e | 329 | // Then shift in our new scale bits: |
beanmachine44 | 0:622e8874902e | 330 | cmd[1] |= mScl << 5; |
beanmachine44 | 0:622e8874902e | 331 | |
beanmachine44 | 0:622e8874902e | 332 | // Write the magscale out to the mag |
beanmachine44 | 0:622e8874902e | 333 | i2c.write(mAddress, cmd, 2); |
beanmachine44 | 0:622e8874902e | 334 | |
beanmachine44 | 0:622e8874902e | 335 | // We've updated the sensor, but we also need to update our class variables |
beanmachine44 | 0:622e8874902e | 336 | // First update mScale: |
beanmachine44 | 0:622e8874902e | 337 | mScale = mScl; |
beanmachine44 | 0:622e8874902e | 338 | // Then calculate a new mRes, which relies on mScale being set correctly: |
beanmachine44 | 0:622e8874902e | 339 | calcmRes(); |
beanmachine44 | 0:622e8874902e | 340 | } |
beanmachine44 | 0:622e8874902e | 341 | |
beanmachine44 | 0:622e8874902e | 342 | void LSM9DS1::setGyroODR(gyro_odr gRate) |
beanmachine44 | 0:622e8874902e | 343 | { |
5hel2l2y | 2:ac3b69ccd3dd | 344 | char cmd[2]; |
5hel2l2y | 2:ac3b69ccd3dd | 345 | char cmdLow[2]; |
5hel2l2y | 2:ac3b69ccd3dd | 346 | |
5hel2l2y | 2:ac3b69ccd3dd | 347 | if(gRate == G_ODR_15_BW_0 | gRate == G_ODR_60_BW_16 | gRate == G_ODR_119_BW_14 | gRate == G_ODR_119_BW_31) { |
5hel2l2y | 2:ac3b69ccd3dd | 348 | cmdLow[0] = CTRL_REG3_G; |
5hel2l2y | 2:ac3b69ccd3dd | 349 | cmdLow[1] = 1; |
5hel2l2y | 2:ac3b69ccd3dd | 350 | |
5hel2l2y | 2:ac3b69ccd3dd | 351 | i2c.write(xgAddress, cmdLow, 2); |
5hel2l2y | 2:ac3b69ccd3dd | 352 | } |
5hel2l2y | 2:ac3b69ccd3dd | 353 | |
beanmachine44 | 0:622e8874902e | 354 | // The start of the addresses we want to read from |
5hel2l2y | 2:ac3b69ccd3dd | 355 | cmd[0] = CTRL_REG1_G; |
5hel2l2y | 2:ac3b69ccd3dd | 356 | cmd[1] = 0; |
beanmachine44 | 0:622e8874902e | 357 | |
beanmachine44 | 0:622e8874902e | 358 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 359 | i2c.write(xgAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 360 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 361 | i2c.read(xgAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 362 | |
beanmachine44 | 0:622e8874902e | 363 | // Then mask out the gyro odr bits: |
beanmachine44 | 0:622e8874902e | 364 | cmd[1] &= (0x3 << 3); |
beanmachine44 | 0:622e8874902e | 365 | // Then shift in our new odr bits: |
beanmachine44 | 0:622e8874902e | 366 | cmd[1] |= gRate; |
beanmachine44 | 0:622e8874902e | 367 | |
beanmachine44 | 0:622e8874902e | 368 | // Write the gyroodr out to the gyro |
beanmachine44 | 0:622e8874902e | 369 | i2c.write(xgAddress, cmd, 2); |
beanmachine44 | 0:622e8874902e | 370 | } |
beanmachine44 | 0:622e8874902e | 371 | |
beanmachine44 | 0:622e8874902e | 372 | void LSM9DS1::setAccelODR(accel_odr aRate) |
beanmachine44 | 0:622e8874902e | 373 | { |
beanmachine44 | 0:622e8874902e | 374 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 375 | char cmd[2] = { |
beanmachine44 | 0:622e8874902e | 376 | CTRL_REG6_XL, |
beanmachine44 | 0:622e8874902e | 377 | 0 |
beanmachine44 | 0:622e8874902e | 378 | }; |
beanmachine44 | 0:622e8874902e | 379 | |
beanmachine44 | 0:622e8874902e | 380 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 381 | i2c.write(xgAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 382 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 383 | i2c.read(xgAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 384 | |
beanmachine44 | 0:622e8874902e | 385 | // Then mask out the accel odr bits: |
beanmachine44 | 0:622e8874902e | 386 | cmd[1] &= 0xFF^(0x7 << 5); |
beanmachine44 | 0:622e8874902e | 387 | // Then shift in our new odr bits: |
beanmachine44 | 0:622e8874902e | 388 | cmd[1] |= aRate << 5; |
beanmachine44 | 0:622e8874902e | 389 | |
beanmachine44 | 0:622e8874902e | 390 | // Write the accelodr out to the accel |
beanmachine44 | 0:622e8874902e | 391 | i2c.write(xgAddress, cmd, 2); |
beanmachine44 | 0:622e8874902e | 392 | } |
beanmachine44 | 0:622e8874902e | 393 | |
beanmachine44 | 0:622e8874902e | 394 | void LSM9DS1::setMagODR(mag_odr mRate) |
beanmachine44 | 0:622e8874902e | 395 | { |
beanmachine44 | 0:622e8874902e | 396 | // The start of the addresses we want to read from |
beanmachine44 | 0:622e8874902e | 397 | char cmd[2] = { |
beanmachine44 | 0:622e8874902e | 398 | CTRL_REG1_M, |
beanmachine44 | 0:622e8874902e | 399 | 0 |
beanmachine44 | 0:622e8874902e | 400 | }; |
beanmachine44 | 0:622e8874902e | 401 | |
beanmachine44 | 0:622e8874902e | 402 | // Write the address we are going to read from and don't end the transaction |
beanmachine44 | 0:622e8874902e | 403 | i2c.write(mAddress, cmd, 1, true); |
beanmachine44 | 0:622e8874902e | 404 | // Read in all the 8 bits of data |
beanmachine44 | 0:622e8874902e | 405 | i2c.read(mAddress, cmd+1, 1); |
beanmachine44 | 0:622e8874902e | 406 | |
beanmachine44 | 0:622e8874902e | 407 | // Then mask out the mag odr bits: |
beanmachine44 | 0:622e8874902e | 408 | cmd[1] &= 0xFF^(0x7 << 2); |
beanmachine44 | 0:622e8874902e | 409 | // Then shift in our new odr bits: |
beanmachine44 | 0:622e8874902e | 410 | cmd[1] |= mRate << 2; |
beanmachine44 | 0:622e8874902e | 411 | |
beanmachine44 | 0:622e8874902e | 412 | // Write the magodr out to the mag |
beanmachine44 | 0:622e8874902e | 413 | i2c.write(mAddress, cmd, 2); |
beanmachine44 | 0:622e8874902e | 414 | } |
beanmachine44 | 0:622e8874902e | 415 | |
beanmachine44 | 0:622e8874902e | 416 | void LSM9DS1::calcgRes() |
beanmachine44 | 0:622e8874902e | 417 | { |
beanmachine44 | 0:622e8874902e | 418 | // Possible gyro scales (and their register bit settings) are: |
beanmachine44 | 0:622e8874902e | 419 | // 245 DPS (00), 500 DPS (01), 2000 DPS (10). |
beanmachine44 | 0:622e8874902e | 420 | switch (gScale) |
beanmachine44 | 0:622e8874902e | 421 | { |
beanmachine44 | 0:622e8874902e | 422 | case G_SCALE_245DPS: |
beanmachine44 | 0:622e8874902e | 423 | gRes = 245.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 424 | break; |
beanmachine44 | 0:622e8874902e | 425 | case G_SCALE_500DPS: |
beanmachine44 | 0:622e8874902e | 426 | gRes = 500.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 427 | break; |
beanmachine44 | 0:622e8874902e | 428 | case G_SCALE_2000DPS: |
beanmachine44 | 0:622e8874902e | 429 | gRes = 2000.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 430 | break; |
beanmachine44 | 0:622e8874902e | 431 | } |
beanmachine44 | 0:622e8874902e | 432 | } |
beanmachine44 | 0:622e8874902e | 433 | |
beanmachine44 | 0:622e8874902e | 434 | void LSM9DS1::calcaRes() |
beanmachine44 | 0:622e8874902e | 435 | { |
beanmachine44 | 0:622e8874902e | 436 | // Possible accelerometer scales (and their register bit settings) are: |
beanmachine44 | 0:622e8874902e | 437 | // 2 g (000), 4g (001), 6g (010) 8g (011), 16g (100). |
beanmachine44 | 0:622e8874902e | 438 | switch (aScale) |
beanmachine44 | 0:622e8874902e | 439 | { |
beanmachine44 | 0:622e8874902e | 440 | case A_SCALE_2G: |
beanmachine44 | 0:622e8874902e | 441 | aRes = 2.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 442 | break; |
beanmachine44 | 0:622e8874902e | 443 | case A_SCALE_4G: |
beanmachine44 | 0:622e8874902e | 444 | aRes = 4.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 445 | break; |
beanmachine44 | 0:622e8874902e | 446 | case A_SCALE_8G: |
beanmachine44 | 0:622e8874902e | 447 | aRes = 8.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 448 | break; |
beanmachine44 | 0:622e8874902e | 449 | case A_SCALE_16G: |
beanmachine44 | 0:622e8874902e | 450 | aRes = 16.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 451 | break; |
beanmachine44 | 0:622e8874902e | 452 | } |
beanmachine44 | 0:622e8874902e | 453 | } |
beanmachine44 | 0:622e8874902e | 454 | |
beanmachine44 | 0:622e8874902e | 455 | void LSM9DS1::calcmRes() |
beanmachine44 | 0:622e8874902e | 456 | { |
beanmachine44 | 0:622e8874902e | 457 | // Possible magnetometer scales (and their register bit settings) are: |
beanmachine44 | 0:622e8874902e | 458 | // 2 Gs (00), 4 Gs (01), 8 Gs (10) 12 Gs (11). |
beanmachine44 | 0:622e8874902e | 459 | switch (mScale) |
beanmachine44 | 0:622e8874902e | 460 | { |
beanmachine44 | 0:622e8874902e | 461 | case M_SCALE_4GS: |
beanmachine44 | 0:622e8874902e | 462 | mRes = 4.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 463 | break; |
beanmachine44 | 0:622e8874902e | 464 | case M_SCALE_8GS: |
beanmachine44 | 0:622e8874902e | 465 | mRes = 8.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 466 | break; |
beanmachine44 | 0:622e8874902e | 467 | case M_SCALE_12GS: |
beanmachine44 | 0:622e8874902e | 468 | mRes = 12.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 469 | break; |
beanmachine44 | 0:622e8874902e | 470 | case M_SCALE_16GS: |
beanmachine44 | 0:622e8874902e | 471 | mRes = 16.0 / 32768.0; |
beanmachine44 | 0:622e8874902e | 472 | break; |
beanmachine44 | 0:622e8874902e | 473 | } |
beanmachine44 | 0:622e8874902e | 474 | } |