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