lsm6ds3 library working on mbed-os 5

Dependents:   STM32_LOGGER

Committer:
einsteingustavo
Date:
Fri Jan 10 11:25:40 2020 +0000
Revision:
3:69c5c66c3f2f
Parent:
2:ed14e6196255
(const char) declaration because narrowing conversion fault

Who changed what in which revision?

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