LSM6DS33 Library

Dependents:   teensyIMU LSM6DS33 ALTIMU_v6

Fork of LSM6DS3 by Sherry Yang

Committer:
bclaus
Date:
Thu Oct 06 23:28:32 2016 +0000
Revision:
4:4e7d663e26bd
Parent:
3:b1d064895178
functional;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bclaus 3:b1d064895178 1 #include "LSM6DS33.h"
5hel2l2y 0:46630122dec9 2
bclaus 3:b1d064895178 3 LSM6DS33::LSM6DS33(PinName sda, PinName scl, uint8_t xgAddr) : i2c(sda, scl)
5hel2l2y 0:46630122dec9 4 {
5hel2l2y 0:46630122dec9 5 // xgAddress will store the 7-bit I2C address, if using I2C.
5hel2l2y 0:46630122dec9 6 xgAddress = xgAddr;
5hel2l2y 0:46630122dec9 7 }
5hel2l2y 0:46630122dec9 8
bclaus 3:b1d064895178 9 uint16_t LSM6DS33::begin(gyro_scale gScl, accel_scale aScl,
5hel2l2y 0:46630122dec9 10 gyro_odr gODR, accel_odr aODR)
5hel2l2y 0:46630122dec9 11 {
bclaus 4:4e7d663e26bd 12
5hel2l2y 0:46630122dec9 13 // Store the given scales in class variables. These scale variables
5hel2l2y 0:46630122dec9 14 // are used throughout to calculate the actual g's, DPS,and Gs's.
5hel2l2y 0:46630122dec9 15 gScale = gScl;
5hel2l2y 0:46630122dec9 16 aScale = aScl;
5hel2l2y 0:46630122dec9 17
5hel2l2y 0:46630122dec9 18 // Once we have the scale values, we can calculate the resolution
5hel2l2y 0:46630122dec9 19 // of each sensor. That's what these functions are for. One for each sensor
5hel2l2y 0:46630122dec9 20 calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable
5hel2l2y 0:46630122dec9 21 calcaRes(); // Calculate g / ADC tick, stored in aRes variable
5hel2l2y 0:46630122dec9 22
5hel2l2y 0:46630122dec9 23
5hel2l2y 0:46630122dec9 24 // To verify communication, we can read from the WHO_AM_I register of
5hel2l2y 0:46630122dec9 25 // each device. Store those in a variable so we can return them.
5hel2l2y 0:46630122dec9 26 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 27 char cmd[2] = {
5hel2l2y 0:46630122dec9 28 WHO_AM_I_REG,
5hel2l2y 0:46630122dec9 29 0
5hel2l2y 0:46630122dec9 30 };
5hel2l2y 0:46630122dec9 31
5hel2l2y 0:46630122dec9 32 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 33 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 34 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 35 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 36 uint8_t xgTest = cmd[1]; // Read the accel/gyro WHO_AM_I
5hel2l2y 0:46630122dec9 37
5hel2l2y 0:46630122dec9 38 // Gyro initialization stuff:
5hel2l2y 0:46630122dec9 39 initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc.
5hel2l2y 0:46630122dec9 40 setGyroODR(gODR); // Set the gyro output data rate and bandwidth.
5hel2l2y 0:46630122dec9 41 setGyroScale(gScale); // Set the gyro range
5hel2l2y 0:46630122dec9 42
5hel2l2y 0:46630122dec9 43 // Accelerometer initialization stuff:
5hel2l2y 0:46630122dec9 44 initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc.
5hel2l2y 0:46630122dec9 45 setAccelODR(aODR); // Set the accel data rate.
5hel2l2y 0:46630122dec9 46 setAccelScale(aScale); // Set the accel range.
5hel2l2y 0:46630122dec9 47
bclaus 3:b1d064895178 48 //set high res timestamp where LSB is 25us
bclaus 3:b1d064895178 49 cmd[0] = WAKE_UP_DUR;
bclaus 3:b1d064895178 50 cmd[1] = 0x10;
bclaus 3:b1d064895178 51 i2c.write(xgAddress, cmd, 2);
5hel2l2y 2:ed14e6196255 52
bclaus 4:4e7d663e26bd 53
5hel2l2y 0:46630122dec9 54 // Once everything is initialized, return the WHO_AM_I registers we read:
5hel2l2y 0:46630122dec9 55 return xgTest;
5hel2l2y 0:46630122dec9 56 }
5hel2l2y 0:46630122dec9 57
bclaus 3:b1d064895178 58 void LSM6DS33::initGyro()
5hel2l2y 0:46630122dec9 59 {
bclaus 4:4e7d663e26bd 60 char cmd[2] = {
5hel2l2y 0:46630122dec9 61 CTRL2_G,
bclaus 4:4e7d663e26bd 62 gScale | G_ODR_104
5hel2l2y 0:46630122dec9 63 };
5hel2l2y 0:46630122dec9 64
5hel2l2y 0:46630122dec9 65 // Write the data to the gyro control registers
bclaus 4:4e7d663e26bd 66 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 67 }
5hel2l2y 0:46630122dec9 68
bclaus 3:b1d064895178 69 void LSM6DS33::initAccel()
5hel2l2y 0:46630122dec9 70 {
5hel2l2y 0:46630122dec9 71 char cmd[4] = {
5hel2l2y 0:46630122dec9 72 CTRL1_XL,
bclaus 4:4e7d663e26bd 73 0x30};
5hel2l2y 0:46630122dec9 74
5hel2l2y 0:46630122dec9 75 // Write the data to the accel control registers
bclaus 4:4e7d663e26bd 76 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 77 }
5hel2l2y 0:46630122dec9 78
bclaus 3:b1d064895178 79 void LSM6DS33::initIntr()
5hel2l2y 2:ed14e6196255 80 {
bclaus 3:b1d064895178 81
5hel2l2y 2:ed14e6196255 82 }
bclaus 3:b1d064895178 83 void LSM6DS33::readAll(){
bclaus 3:b1d064895178 84 // The data we are going to read from the temp/gyr/acc/timestamp
bclaus 4:4e7d663e26bd 85 //char data[14];//from 0x20 to 0x42
bclaus 3:b1d064895178 86
bclaus 4:4e7d663e26bd 87 char data[14];
bclaus 4:4e7d663e26bd 88 char tsdata[3];
bclaus 4:4e7d663e26bd 89
bclaus 4:4e7d663e26bd 90 i2c.start();
bclaus 4:4e7d663e26bd 91 i2c.write(xgAddress);
bclaus 4:4e7d663e26bd 92 i2c.write(OUT_TEMP_L);
bclaus 4:4e7d663e26bd 93 i2c.start();
bclaus 4:4e7d663e26bd 94 i2c.write(xgAddress | 0x1);
bclaus 4:4e7d663e26bd 95 for(int i =0;i<13;i++){
bclaus 4:4e7d663e26bd 96 data[i]=i2c.read(1);
bclaus 4:4e7d663e26bd 97 }
bclaus 4:4e7d663e26bd 98 data[13]=i2c.read(0);
bclaus 4:4e7d663e26bd 99 i2c.stop();
bclaus 4:4e7d663e26bd 100
bclaus 3:b1d064895178 101 // Temperature is a 12-bit signed integer
bclaus 3:b1d064895178 102 temperature_raw = data[0] | (data[1] << 8);
bclaus 3:b1d064895178 103 gx_raw = data[2] | (data[3] << 8);
bclaus 3:b1d064895178 104 gy_raw = data[4] | (data[5] << 8);
bclaus 3:b1d064895178 105 gz_raw = data[6] | (data[7] << 8);
bclaus 3:b1d064895178 106 ax_raw = data[8] | (data[9] << 8);
bclaus 3:b1d064895178 107 ay_raw = data[10] | (data[11] << 8);
bclaus 3:b1d064895178 108 az_raw = data[12] | (data[13] << 8);
bclaus 4:4e7d663e26bd 109
bclaus 4:4e7d663e26bd 110 i2c.start();
bclaus 4:4e7d663e26bd 111 i2c.write(xgAddress);
bclaus 4:4e7d663e26bd 112 i2c.write(TIMESTAMP0_REG);
bclaus 4:4e7d663e26bd 113 i2c.start();
bclaus 4:4e7d663e26bd 114 i2c.write(xgAddress | 0x1);
bclaus 4:4e7d663e26bd 115 for(int i =0;i<3;i++){
bclaus 4:4e7d663e26bd 116 tsdata[i]=i2c.read(1);
bclaus 4:4e7d663e26bd 117 }
bclaus 4:4e7d663e26bd 118 tsdata[3]=i2c.read(0);
bclaus 4:4e7d663e26bd 119 i2c.stop();
bclaus 4:4e7d663e26bd 120
bclaus 3:b1d064895178 121 time_raw = tsdata[0] | (tsdata[1] << 8) | (tsdata[2] << 16);
bclaus 3:b1d064895178 122
bclaus 3:b1d064895178 123
bclaus 3:b1d064895178 124 temperature_c = (float)temperature_raw / 16.0 + 25.0;
bclaus 3:b1d064895178 125 gx = gx_raw * gRes;
bclaus 3:b1d064895178 126 gy = gy_raw * gRes;
bclaus 3:b1d064895178 127 gz = gz_raw * gRes;
bclaus 3:b1d064895178 128 ax = ax_raw * aRes;
bclaus 3:b1d064895178 129 ay = ay_raw * aRes;
bclaus 3:b1d064895178 130 az = az_raw * aRes;
bclaus 3:b1d064895178 131 time = time_raw*(0.000025);
bclaus 3:b1d064895178 132
bclaus 3:b1d064895178 133
bclaus 3:b1d064895178 134 }
bclaus 3:b1d064895178 135
bclaus 3:b1d064895178 136
bclaus 3:b1d064895178 137 void LSM6DS33::readAccel()
5hel2l2y 0:46630122dec9 138 {
5hel2l2y 0:46630122dec9 139 // The data we are going to read from the accel
5hel2l2y 0:46630122dec9 140 char data[6];
5hel2l2y 0:46630122dec9 141
5hel2l2y 0:46630122dec9 142 // Set addresses
5hel2l2y 0:46630122dec9 143 char subAddressXL = OUTX_L_XL;
5hel2l2y 0:46630122dec9 144 char subAddressXH = OUTX_H_XL;
5hel2l2y 0:46630122dec9 145 char subAddressYL = OUTY_L_XL;
5hel2l2y 0:46630122dec9 146 char subAddressYH = OUTY_H_XL;
5hel2l2y 0:46630122dec9 147 char subAddressZL = OUTZ_L_XL;
5hel2l2y 0:46630122dec9 148 char subAddressZH = OUTZ_H_XL;
5hel2l2y 0:46630122dec9 149
5hel2l2y 0:46630122dec9 150 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 151 i2c.write(xgAddress, &subAddressXL, 1, true);
5hel2l2y 0:46630122dec9 152 // Read in register containing the axes data and alocated to the correct index
5hel2l2y 0:46630122dec9 153 i2c.read(xgAddress, data, 1);
5hel2l2y 0:46630122dec9 154
5hel2l2y 0:46630122dec9 155 i2c.write(xgAddress, &subAddressXH, 1, true);
5hel2l2y 0:46630122dec9 156 i2c.read(xgAddress, (data + 1), 1);
5hel2l2y 0:46630122dec9 157 i2c.write(xgAddress, &subAddressYL, 1, true);
5hel2l2y 0:46630122dec9 158 i2c.read(xgAddress, (data + 2), 1);
5hel2l2y 0:46630122dec9 159 i2c.write(xgAddress, &subAddressYH, 1, true);
5hel2l2y 0:46630122dec9 160 i2c.read(xgAddress, (data + 3), 1);
5hel2l2y 0:46630122dec9 161 i2c.write(xgAddress, &subAddressZL, 1, true);
5hel2l2y 0:46630122dec9 162 i2c.read(xgAddress, (data + 4), 1);
5hel2l2y 0:46630122dec9 163 i2c.write(xgAddress, &subAddressZH, 1, true);
5hel2l2y 0:46630122dec9 164 i2c.read(xgAddress, (data + 5), 1);
5hel2l2y 0:46630122dec9 165
5hel2l2y 0:46630122dec9 166 // Reassemble the data and convert to g
5hel2l2y 0:46630122dec9 167 ax_raw = data[0] | (data[1] << 8);
5hel2l2y 0:46630122dec9 168 ay_raw = data[2] | (data[3] << 8);
5hel2l2y 0:46630122dec9 169 az_raw = data[4] | (data[5] << 8);
5hel2l2y 0:46630122dec9 170 ax = ax_raw * aRes;
5hel2l2y 0:46630122dec9 171 ay = ay_raw * aRes;
5hel2l2y 0:46630122dec9 172 az = az_raw * aRes;
5hel2l2y 0:46630122dec9 173 }
5hel2l2y 0:46630122dec9 174
bclaus 3:b1d064895178 175 void LSM6DS33::readIntr()
5hel2l2y 2:ed14e6196255 176 {
5hel2l2y 2:ed14e6196255 177 char data[1];
5hel2l2y 2:ed14e6196255 178 char subAddress = TAP_SRC;
5hel2l2y 2:ed14e6196255 179
5hel2l2y 2:ed14e6196255 180 i2c.write(xgAddress, &subAddress, 1, true);
5hel2l2y 2:ed14e6196255 181 i2c.read(xgAddress, data, 1);
5hel2l2y 2:ed14e6196255 182
5hel2l2y 2:ed14e6196255 183 intr = (float)data[0];
5hel2l2y 2:ed14e6196255 184 }
5hel2l2y 2:ed14e6196255 185
bclaus 3:b1d064895178 186 void LSM6DS33::readTemp()
5hel2l2y 0:46630122dec9 187 {
5hel2l2y 0:46630122dec9 188 // The data we are going to read from the temp
5hel2l2y 0:46630122dec9 189 char data[2];
5hel2l2y 0:46630122dec9 190
5hel2l2y 0:46630122dec9 191 // Set addresses
5hel2l2y 0:46630122dec9 192 char subAddressL = OUT_TEMP_L;
5hel2l2y 0:46630122dec9 193 char subAddressH = OUT_TEMP_H;
5hel2l2y 0:46630122dec9 194
5hel2l2y 0:46630122dec9 195 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 196 i2c.write(xgAddress, &subAddressL, 1, true);
5hel2l2y 0:46630122dec9 197 // Read in register containing the temperature data and alocated to the correct index
5hel2l2y 0:46630122dec9 198 i2c.read(xgAddress, data, 1);
5hel2l2y 0:46630122dec9 199
5hel2l2y 0:46630122dec9 200 i2c.write(xgAddress, &subAddressH, 1, true);
5hel2l2y 0:46630122dec9 201 i2c.read(xgAddress, (data + 1), 1);
5hel2l2y 0:46630122dec9 202
5hel2l2y 0:46630122dec9 203 // Temperature is a 12-bit signed integer
5hel2l2y 0:46630122dec9 204 temperature_raw = data[0] | (data[1] << 8);
5hel2l2y 0:46630122dec9 205
5hel2l2y 0:46630122dec9 206 temperature_c = (float)temperature_raw / 16.0 + 25.0;
5hel2l2y 0:46630122dec9 207 temperature_f = temperature_c * 1.8 + 32.0;
5hel2l2y 0:46630122dec9 208 }
5hel2l2y 0:46630122dec9 209
5hel2l2y 0:46630122dec9 210
bclaus 3:b1d064895178 211 void LSM6DS33::readGyro()
5hel2l2y 0:46630122dec9 212 {
5hel2l2y 0:46630122dec9 213 // The data we are going to read from the gyro
5hel2l2y 0:46630122dec9 214 char data[6];
5hel2l2y 0:46630122dec9 215
5hel2l2y 0:46630122dec9 216 // Set addresses
5hel2l2y 0:46630122dec9 217 char subAddressXL = OUTX_L_G;
5hel2l2y 0:46630122dec9 218 char subAddressXH = OUTX_H_G;
5hel2l2y 0:46630122dec9 219 char subAddressYL = OUTY_L_G;
5hel2l2y 0:46630122dec9 220 char subAddressYH = OUTY_H_G;
5hel2l2y 0:46630122dec9 221 char subAddressZL = OUTZ_L_G;
5hel2l2y 0:46630122dec9 222 char subAddressZH = OUTZ_H_G;
5hel2l2y 0:46630122dec9 223
5hel2l2y 0:46630122dec9 224 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 225 i2c.write(xgAddress, &subAddressXL, 1, true);
5hel2l2y 0:46630122dec9 226 // Read in register containing the axes data and alocated to the correct index
5hel2l2y 0:46630122dec9 227 i2c.read(xgAddress, data, 1);
5hel2l2y 0:46630122dec9 228
5hel2l2y 0:46630122dec9 229 i2c.write(xgAddress, &subAddressXH, 1, true);
5hel2l2y 0:46630122dec9 230 i2c.read(xgAddress, (data + 1), 1);
5hel2l2y 0:46630122dec9 231 i2c.write(xgAddress, &subAddressYL, 1, true);
5hel2l2y 0:46630122dec9 232 i2c.read(xgAddress, (data + 2), 1);
5hel2l2y 0:46630122dec9 233 i2c.write(xgAddress, &subAddressYH, 1, true);
5hel2l2y 0:46630122dec9 234 i2c.read(xgAddress, (data + 3), 1);
5hel2l2y 0:46630122dec9 235 i2c.write(xgAddress, &subAddressZL, 1, true);
5hel2l2y 0:46630122dec9 236 i2c.read(xgAddress, (data + 4), 1);
5hel2l2y 0:46630122dec9 237 i2c.write(xgAddress, &subAddressZH, 1, true);
5hel2l2y 0:46630122dec9 238 i2c.read(xgAddress, (data + 5), 1);
5hel2l2y 0:46630122dec9 239
5hel2l2y 0:46630122dec9 240 // Reassemble the data and convert to degrees/sec
5hel2l2y 0:46630122dec9 241 gx_raw = data[0] | (data[1] << 8);
5hel2l2y 0:46630122dec9 242 gy_raw = data[2] | (data[3] << 8);
5hel2l2y 0:46630122dec9 243 gz_raw = data[4] | (data[5] << 8);
5hel2l2y 0:46630122dec9 244 gx = gx_raw * gRes;
5hel2l2y 0:46630122dec9 245 gy = gy_raw * gRes;
5hel2l2y 0:46630122dec9 246 gz = gz_raw * gRes;
5hel2l2y 0:46630122dec9 247 }
5hel2l2y 0:46630122dec9 248
bclaus 3:b1d064895178 249 void LSM6DS33::setGyroScale(gyro_scale gScl)
5hel2l2y 0:46630122dec9 250 {
5hel2l2y 0:46630122dec9 251 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 252 char cmd[2] = {
5hel2l2y 0:46630122dec9 253 CTRL2_G,
5hel2l2y 0:46630122dec9 254 0
5hel2l2y 0:46630122dec9 255 };
5hel2l2y 0:46630122dec9 256
5hel2l2y 0:46630122dec9 257 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 258 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 259 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 260 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 261
5hel2l2y 0:46630122dec9 262 // Then mask out the gyro scale bits:
5hel2l2y 0:46630122dec9 263 cmd[1] &= 0xFF^(0x3 << 3);
5hel2l2y 0:46630122dec9 264 // Then shift in our new scale bits:
5hel2l2y 0:46630122dec9 265 cmd[1] |= gScl << 3;
5hel2l2y 0:46630122dec9 266
5hel2l2y 0:46630122dec9 267 // Write the gyroscale out to the gyro
5hel2l2y 0:46630122dec9 268 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 269
5hel2l2y 0:46630122dec9 270 // We've updated the sensor, but we also need to update our class variables
5hel2l2y 0:46630122dec9 271 // First update gScale:
5hel2l2y 0:46630122dec9 272 gScale = gScl;
5hel2l2y 0:46630122dec9 273 // Then calculate a new gRes, which relies on gScale being set correctly:
5hel2l2y 0:46630122dec9 274 calcgRes();
5hel2l2y 0:46630122dec9 275 }
5hel2l2y 0:46630122dec9 276
bclaus 3:b1d064895178 277 void LSM6DS33::setAccelScale(accel_scale aScl)
5hel2l2y 0:46630122dec9 278 {
5hel2l2y 0:46630122dec9 279 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 280 char cmd[2] = {
5hel2l2y 0:46630122dec9 281 CTRL1_XL,
5hel2l2y 0:46630122dec9 282 0
5hel2l2y 0:46630122dec9 283 };
5hel2l2y 0:46630122dec9 284
5hel2l2y 0:46630122dec9 285 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 286 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 287 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 288 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 289
5hel2l2y 0:46630122dec9 290 // Then mask out the accel scale bits:
5hel2l2y 0:46630122dec9 291 cmd[1] &= 0xFF^(0x3 << 3);
5hel2l2y 0:46630122dec9 292 // Then shift in our new scale bits:
5hel2l2y 0:46630122dec9 293 cmd[1] |= aScl << 3;
5hel2l2y 0:46630122dec9 294
5hel2l2y 0:46630122dec9 295 // Write the accelscale out to the accel
5hel2l2y 0:46630122dec9 296 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 297
5hel2l2y 0:46630122dec9 298 // We've updated the sensor, but we also need to update our class variables
5hel2l2y 0:46630122dec9 299 // First update aScale:
5hel2l2y 0:46630122dec9 300 aScale = aScl;
5hel2l2y 0:46630122dec9 301 // Then calculate a new aRes, which relies on aScale being set correctly:
5hel2l2y 0:46630122dec9 302 calcaRes();
5hel2l2y 0:46630122dec9 303 }
5hel2l2y 0:46630122dec9 304
bclaus 3:b1d064895178 305 void LSM6DS33::setGyroODR(gyro_odr gRate)
5hel2l2y 0:46630122dec9 306 {
5hel2l2y 0:46630122dec9 307 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 308 char cmd[2] = {
5hel2l2y 0:46630122dec9 309 CTRL2_G,
5hel2l2y 0:46630122dec9 310 0
5hel2l2y 0:46630122dec9 311 };
5hel2l2y 1:924c7dea286e 312
5hel2l2y 1:924c7dea286e 313 // Set low power based on ODR, else keep sensor on high performance
5hel2l2y 1:924c7dea286e 314 if(gRate == G_ODR_13_BW_0 | gRate == G_ODR_26_BW_2 | gRate == G_ODR_52_BW_16) {
5hel2l2y 1:924c7dea286e 315 char cmdLow[2] ={
5hel2l2y 1:924c7dea286e 316 CTRL7_G,
5hel2l2y 1:924c7dea286e 317 1
5hel2l2y 1:924c7dea286e 318 };
5hel2l2y 1:924c7dea286e 319
5hel2l2y 1:924c7dea286e 320 i2c.write(xgAddress, cmdLow, 2);
5hel2l2y 1:924c7dea286e 321 }
5hel2l2y 1:924c7dea286e 322 else {
5hel2l2y 1:924c7dea286e 323 char cmdLow[2] ={
5hel2l2y 1:924c7dea286e 324 CTRL7_G,
5hel2l2y 1:924c7dea286e 325 0
5hel2l2y 1:924c7dea286e 326 };
5hel2l2y 1:924c7dea286e 327
5hel2l2y 1:924c7dea286e 328 i2c.write(xgAddress, cmdLow, 2);
5hel2l2y 1:924c7dea286e 329 }
5hel2l2y 0:46630122dec9 330
5hel2l2y 0:46630122dec9 331 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 332 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 333 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 334 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 335
5hel2l2y 0:46630122dec9 336 // Then mask out the gyro odr bits:
5hel2l2y 0:46630122dec9 337 cmd[1] &= (0x3 << 3);
5hel2l2y 0:46630122dec9 338 // Then shift in our new odr bits:
5hel2l2y 0:46630122dec9 339 cmd[1] |= gRate;
5hel2l2y 0:46630122dec9 340
5hel2l2y 0:46630122dec9 341 // Write the gyroodr out to the gyro
5hel2l2y 0:46630122dec9 342 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 343 }
5hel2l2y 0:46630122dec9 344
bclaus 3:b1d064895178 345 void LSM6DS33::setAccelODR(accel_odr aRate)
5hel2l2y 0:46630122dec9 346 {
5hel2l2y 0:46630122dec9 347 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 348 char cmd[2] = {
5hel2l2y 0:46630122dec9 349 CTRL1_XL,
5hel2l2y 0:46630122dec9 350 0
5hel2l2y 0:46630122dec9 351 };
5hel2l2y 1:924c7dea286e 352
5hel2l2y 1:924c7dea286e 353 // Set low power based on ODR, else keep sensor on high performance
5hel2l2y 1:924c7dea286e 354 if(aRate == A_ODR_13 | aRate == A_ODR_26 | aRate == A_ODR_52) {
5hel2l2y 1:924c7dea286e 355 char cmdLow[2] ={
5hel2l2y 1:924c7dea286e 356 CTRL6_C,
5hel2l2y 1:924c7dea286e 357 1
5hel2l2y 1:924c7dea286e 358 };
5hel2l2y 1:924c7dea286e 359
5hel2l2y 1:924c7dea286e 360 i2c.write(xgAddress, cmdLow, 2);
5hel2l2y 1:924c7dea286e 361 }
5hel2l2y 1:924c7dea286e 362 else {
5hel2l2y 1:924c7dea286e 363 char cmdLow[2] ={
5hel2l2y 1:924c7dea286e 364 CTRL6_C,
5hel2l2y 1:924c7dea286e 365 0
5hel2l2y 1:924c7dea286e 366 };
5hel2l2y 1:924c7dea286e 367
5hel2l2y 1:924c7dea286e 368 i2c.write(xgAddress, cmdLow, 2);
5hel2l2y 1:924c7dea286e 369 }
5hel2l2y 0:46630122dec9 370
5hel2l2y 0:46630122dec9 371 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 372 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 373 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 374 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 375
5hel2l2y 0:46630122dec9 376 // Then mask out the accel odr bits:
5hel2l2y 0:46630122dec9 377 cmd[1] &= 0xFF^(0x7 << 5);
5hel2l2y 0:46630122dec9 378 // Then shift in our new odr bits:
5hel2l2y 0:46630122dec9 379 cmd[1] |= aRate << 5;
5hel2l2y 0:46630122dec9 380
5hel2l2y 0:46630122dec9 381 // Write the accelodr out to the accel
5hel2l2y 0:46630122dec9 382 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 383 }
5hel2l2y 0:46630122dec9 384
bclaus 3:b1d064895178 385 void LSM6DS33::calcgRes()
5hel2l2y 0:46630122dec9 386 {
5hel2l2y 0:46630122dec9 387 // Possible gyro scales (and their register bit settings) are:
5hel2l2y 0:46630122dec9 388 // 245 DPS (00), 500 DPS (01), 2000 DPS (10).
5hel2l2y 0:46630122dec9 389 switch (gScale)
5hel2l2y 0:46630122dec9 390 {
5hel2l2y 0:46630122dec9 391 case G_SCALE_245DPS:
5hel2l2y 0:46630122dec9 392 gRes = 245.0 / 32768.0;
5hel2l2y 0:46630122dec9 393 break;
5hel2l2y 0:46630122dec9 394 case G_SCALE_500DPS:
5hel2l2y 0:46630122dec9 395 gRes = 500.0 / 32768.0;
5hel2l2y 0:46630122dec9 396 break;
5hel2l2y 0:46630122dec9 397 case G_SCALE_2000DPS:
5hel2l2y 0:46630122dec9 398 gRes = 2000.0 / 32768.0;
5hel2l2y 0:46630122dec9 399 break;
5hel2l2y 0:46630122dec9 400 }
5hel2l2y 0:46630122dec9 401 }
5hel2l2y 0:46630122dec9 402
bclaus 3:b1d064895178 403 void LSM6DS33::calcaRes()
5hel2l2y 0:46630122dec9 404 {
5hel2l2y 0:46630122dec9 405 // Possible accelerometer scales (and their register bit settings) are:
5hel2l2y 0:46630122dec9 406 // 2 g (000), 4g (001), 6g (010) 8g (011), 16g (100).
5hel2l2y 0:46630122dec9 407 switch (aScale)
5hel2l2y 0:46630122dec9 408 {
5hel2l2y 0:46630122dec9 409 case A_SCALE_2G:
5hel2l2y 0:46630122dec9 410 aRes = 2.0 / 32768.0;
5hel2l2y 0:46630122dec9 411 break;
5hel2l2y 0:46630122dec9 412 case A_SCALE_4G:
5hel2l2y 0:46630122dec9 413 aRes = 4.0 / 32768.0;
5hel2l2y 0:46630122dec9 414 break;
5hel2l2y 0:46630122dec9 415 case A_SCALE_8G:
5hel2l2y 0:46630122dec9 416 aRes = 8.0 / 32768.0;
5hel2l2y 0:46630122dec9 417 break;
5hel2l2y 0:46630122dec9 418 case A_SCALE_16G:
5hel2l2y 0:46630122dec9 419 aRes = 16.0 / 32768.0;
5hel2l2y 0:46630122dec9 420 break;
5hel2l2y 0:46630122dec9 421 }
bclaus 4:4e7d663e26bd 422 }
bclaus 4:4e7d663e26bd 423
bclaus 4:4e7d663e26bd 424
bclaus 4:4e7d663e26bd 425