lsm6ds3 library working on mbed-os 5

Dependents:   STM32_LOGGER

Committer:
5hel2l2y
Date:
Thu Jun 16 20:07:13 2016 +0000
Revision:
0:46630122dec9
Child:
1:924c7dea286e
modified to work LSM6DS3

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 0:46630122dec9 47 // Once everything is initialized, return the WHO_AM_I registers we read:
5hel2l2y 0:46630122dec9 48 return xgTest;
5hel2l2y 0:46630122dec9 49 }
5hel2l2y 0:46630122dec9 50
5hel2l2y 0:46630122dec9 51 void LSM6DS3::initGyro()
5hel2l2y 0:46630122dec9 52 {
5hel2l2y 0:46630122dec9 53 char cmd[4] = {
5hel2l2y 0:46630122dec9 54 CTRL2_G,
5hel2l2y 0:46630122dec9 55 gScale | G_ODR_119_BW_14,
5hel2l2y 0:46630122dec9 56 0, // Default data out and int out
5hel2l2y 0:46630122dec9 57 0 // Default power mode and high pass settings
5hel2l2y 0:46630122dec9 58 };
5hel2l2y 0:46630122dec9 59
5hel2l2y 0:46630122dec9 60 // Write the data to the gyro control registers
5hel2l2y 0:46630122dec9 61 i2c.write(xgAddress, cmd, 4);
5hel2l2y 0:46630122dec9 62 }
5hel2l2y 0:46630122dec9 63
5hel2l2y 0:46630122dec9 64 void LSM6DS3::initAccel()
5hel2l2y 0:46630122dec9 65 {
5hel2l2y 0:46630122dec9 66 char cmd[4] = {
5hel2l2y 0:46630122dec9 67 CTRL1_XL,
5hel2l2y 0:46630122dec9 68 0x38, // Enable all axis and don't decimate data in out Registers
5hel2l2y 0:46630122dec9 69 (A_ODR_119 << 5) | (aScale << 3) | (A_BW_AUTO_SCALE), // 119 Hz ODR, set scale, and auto BW
5hel2l2y 0:46630122dec9 70 0 // Default resolution mode and filtering settings
5hel2l2y 0:46630122dec9 71 };
5hel2l2y 0:46630122dec9 72
5hel2l2y 0:46630122dec9 73 // Write the data to the accel control registers
5hel2l2y 0:46630122dec9 74 i2c.write(xgAddress, cmd, 4);
5hel2l2y 0:46630122dec9 75 }
5hel2l2y 0:46630122dec9 76
5hel2l2y 0:46630122dec9 77 void LSM6DS3::readAccel()
5hel2l2y 0:46630122dec9 78 {
5hel2l2y 0:46630122dec9 79 // The data we are going to read from the accel
5hel2l2y 0:46630122dec9 80 char data[6];
5hel2l2y 0:46630122dec9 81
5hel2l2y 0:46630122dec9 82 // Set addresses
5hel2l2y 0:46630122dec9 83 char subAddressXL = OUTX_L_XL;
5hel2l2y 0:46630122dec9 84 char subAddressXH = OUTX_H_XL;
5hel2l2y 0:46630122dec9 85 char subAddressYL = OUTY_L_XL;
5hel2l2y 0:46630122dec9 86 char subAddressYH = OUTY_H_XL;
5hel2l2y 0:46630122dec9 87 char subAddressZL = OUTZ_L_XL;
5hel2l2y 0:46630122dec9 88 char subAddressZH = OUTZ_H_XL;
5hel2l2y 0:46630122dec9 89
5hel2l2y 0:46630122dec9 90 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 91 i2c.write(xgAddress, &subAddressXL, 1, true);
5hel2l2y 0:46630122dec9 92 // Read in register containing the axes data and alocated to the correct index
5hel2l2y 0:46630122dec9 93 i2c.read(xgAddress, data, 1);
5hel2l2y 0:46630122dec9 94
5hel2l2y 0:46630122dec9 95 i2c.write(xgAddress, &subAddressXH, 1, true);
5hel2l2y 0:46630122dec9 96 i2c.read(xgAddress, (data + 1), 1);
5hel2l2y 0:46630122dec9 97 i2c.write(xgAddress, &subAddressYL, 1, true);
5hel2l2y 0:46630122dec9 98 i2c.read(xgAddress, (data + 2), 1);
5hel2l2y 0:46630122dec9 99 i2c.write(xgAddress, &subAddressYH, 1, true);
5hel2l2y 0:46630122dec9 100 i2c.read(xgAddress, (data + 3), 1);
5hel2l2y 0:46630122dec9 101 i2c.write(xgAddress, &subAddressZL, 1, true);
5hel2l2y 0:46630122dec9 102 i2c.read(xgAddress, (data + 4), 1);
5hel2l2y 0:46630122dec9 103 i2c.write(xgAddress, &subAddressZH, 1, true);
5hel2l2y 0:46630122dec9 104 i2c.read(xgAddress, (data + 5), 1);
5hel2l2y 0:46630122dec9 105
5hel2l2y 0:46630122dec9 106 // Reassemble the data and convert to g
5hel2l2y 0:46630122dec9 107 ax_raw = data[0] | (data[1] << 8);
5hel2l2y 0:46630122dec9 108 ay_raw = data[2] | (data[3] << 8);
5hel2l2y 0:46630122dec9 109 az_raw = data[4] | (data[5] << 8);
5hel2l2y 0:46630122dec9 110 ax = ax_raw * aRes;
5hel2l2y 0:46630122dec9 111 ay = ay_raw * aRes;
5hel2l2y 0:46630122dec9 112 az = az_raw * aRes;
5hel2l2y 0:46630122dec9 113 }
5hel2l2y 0:46630122dec9 114
5hel2l2y 0:46630122dec9 115 void LSM6DS3::readTemp()
5hel2l2y 0:46630122dec9 116 {
5hel2l2y 0:46630122dec9 117 // The data we are going to read from the temp
5hel2l2y 0:46630122dec9 118 char data[2];
5hel2l2y 0:46630122dec9 119
5hel2l2y 0:46630122dec9 120 // Set addresses
5hel2l2y 0:46630122dec9 121 char subAddressL = OUT_TEMP_L;
5hel2l2y 0:46630122dec9 122 char subAddressH = OUT_TEMP_H;
5hel2l2y 0:46630122dec9 123
5hel2l2y 0:46630122dec9 124 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 125 i2c.write(xgAddress, &subAddressL, 1, true);
5hel2l2y 0:46630122dec9 126 // Read in register containing the temperature data and alocated to the correct index
5hel2l2y 0:46630122dec9 127 i2c.read(xgAddress, data, 1);
5hel2l2y 0:46630122dec9 128
5hel2l2y 0:46630122dec9 129 i2c.write(xgAddress, &subAddressH, 1, true);
5hel2l2y 0:46630122dec9 130 i2c.read(xgAddress, (data + 1), 1);
5hel2l2y 0:46630122dec9 131
5hel2l2y 0:46630122dec9 132 // Temperature is a 12-bit signed integer
5hel2l2y 0:46630122dec9 133 temperature_raw = data[0] | (data[1] << 8);
5hel2l2y 0:46630122dec9 134
5hel2l2y 0:46630122dec9 135 temperature_c = (float)temperature_raw / 16.0 + 25.0;
5hel2l2y 0:46630122dec9 136 temperature_f = temperature_c * 1.8 + 32.0;
5hel2l2y 0:46630122dec9 137 }
5hel2l2y 0:46630122dec9 138
5hel2l2y 0:46630122dec9 139
5hel2l2y 0:46630122dec9 140 void LSM6DS3::readGyro()
5hel2l2y 0:46630122dec9 141 {
5hel2l2y 0:46630122dec9 142 // The data we are going to read from the gyro
5hel2l2y 0:46630122dec9 143 char data[6];
5hel2l2y 0:46630122dec9 144
5hel2l2y 0:46630122dec9 145 // Set addresses
5hel2l2y 0:46630122dec9 146 char subAddressXL = OUTX_L_G;
5hel2l2y 0:46630122dec9 147 char subAddressXH = OUTX_H_G;
5hel2l2y 0:46630122dec9 148 char subAddressYL = OUTY_L_G;
5hel2l2y 0:46630122dec9 149 char subAddressYH = OUTY_H_G;
5hel2l2y 0:46630122dec9 150 char subAddressZL = OUTZ_L_G;
5hel2l2y 0:46630122dec9 151 char subAddressZH = OUTZ_H_G;
5hel2l2y 0:46630122dec9 152
5hel2l2y 0:46630122dec9 153 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 154 i2c.write(xgAddress, &subAddressXL, 1, true);
5hel2l2y 0:46630122dec9 155 // Read in register containing the axes data and alocated to the correct index
5hel2l2y 0:46630122dec9 156 i2c.read(xgAddress, data, 1);
5hel2l2y 0:46630122dec9 157
5hel2l2y 0:46630122dec9 158 i2c.write(xgAddress, &subAddressXH, 1, true);
5hel2l2y 0:46630122dec9 159 i2c.read(xgAddress, (data + 1), 1);
5hel2l2y 0:46630122dec9 160 i2c.write(xgAddress, &subAddressYL, 1, true);
5hel2l2y 0:46630122dec9 161 i2c.read(xgAddress, (data + 2), 1);
5hel2l2y 0:46630122dec9 162 i2c.write(xgAddress, &subAddressYH, 1, true);
5hel2l2y 0:46630122dec9 163 i2c.read(xgAddress, (data + 3), 1);
5hel2l2y 0:46630122dec9 164 i2c.write(xgAddress, &subAddressZL, 1, true);
5hel2l2y 0:46630122dec9 165 i2c.read(xgAddress, (data + 4), 1);
5hel2l2y 0:46630122dec9 166 i2c.write(xgAddress, &subAddressZH, 1, true);
5hel2l2y 0:46630122dec9 167 i2c.read(xgAddress, (data + 5), 1);
5hel2l2y 0:46630122dec9 168
5hel2l2y 0:46630122dec9 169 // Reassemble the data and convert to degrees/sec
5hel2l2y 0:46630122dec9 170 gx_raw = data[0] | (data[1] << 8);
5hel2l2y 0:46630122dec9 171 gy_raw = data[2] | (data[3] << 8);
5hel2l2y 0:46630122dec9 172 gz_raw = data[4] | (data[5] << 8);
5hel2l2y 0:46630122dec9 173 gx = gx_raw * gRes;
5hel2l2y 0:46630122dec9 174 gy = gy_raw * gRes;
5hel2l2y 0:46630122dec9 175 gz = gz_raw * gRes;
5hel2l2y 0:46630122dec9 176 }
5hel2l2y 0:46630122dec9 177
5hel2l2y 0:46630122dec9 178 void LSM6DS3::setGyroScale(gyro_scale gScl)
5hel2l2y 0:46630122dec9 179 {
5hel2l2y 0:46630122dec9 180 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 181 char cmd[2] = {
5hel2l2y 0:46630122dec9 182 CTRL2_G,
5hel2l2y 0:46630122dec9 183 0
5hel2l2y 0:46630122dec9 184 };
5hel2l2y 0:46630122dec9 185
5hel2l2y 0:46630122dec9 186 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 187 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 188 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 189 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 190
5hel2l2y 0:46630122dec9 191 // Then mask out the gyro scale bits:
5hel2l2y 0:46630122dec9 192 cmd[1] &= 0xFF^(0x3 << 3);
5hel2l2y 0:46630122dec9 193 // Then shift in our new scale bits:
5hel2l2y 0:46630122dec9 194 cmd[1] |= gScl << 3;
5hel2l2y 0:46630122dec9 195
5hel2l2y 0:46630122dec9 196 // Write the gyroscale out to the gyro
5hel2l2y 0:46630122dec9 197 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 198
5hel2l2y 0:46630122dec9 199 // We've updated the sensor, but we also need to update our class variables
5hel2l2y 0:46630122dec9 200 // First update gScale:
5hel2l2y 0:46630122dec9 201 gScale = gScl;
5hel2l2y 0:46630122dec9 202 // Then calculate a new gRes, which relies on gScale being set correctly:
5hel2l2y 0:46630122dec9 203 calcgRes();
5hel2l2y 0:46630122dec9 204 }
5hel2l2y 0:46630122dec9 205
5hel2l2y 0:46630122dec9 206 void LSM6DS3::setAccelScale(accel_scale aScl)
5hel2l2y 0:46630122dec9 207 {
5hel2l2y 0:46630122dec9 208 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 209 char cmd[2] = {
5hel2l2y 0:46630122dec9 210 CTRL1_XL,
5hel2l2y 0:46630122dec9 211 0
5hel2l2y 0:46630122dec9 212 };
5hel2l2y 0:46630122dec9 213
5hel2l2y 0:46630122dec9 214 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 215 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 216 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 217 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 218
5hel2l2y 0:46630122dec9 219 // Then mask out the accel scale bits:
5hel2l2y 0:46630122dec9 220 cmd[1] &= 0xFF^(0x3 << 3);
5hel2l2y 0:46630122dec9 221 // Then shift in our new scale bits:
5hel2l2y 0:46630122dec9 222 cmd[1] |= aScl << 3;
5hel2l2y 0:46630122dec9 223
5hel2l2y 0:46630122dec9 224 // Write the accelscale out to the accel
5hel2l2y 0:46630122dec9 225 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 226
5hel2l2y 0:46630122dec9 227 // We've updated the sensor, but we also need to update our class variables
5hel2l2y 0:46630122dec9 228 // First update aScale:
5hel2l2y 0:46630122dec9 229 aScale = aScl;
5hel2l2y 0:46630122dec9 230 // Then calculate a new aRes, which relies on aScale being set correctly:
5hel2l2y 0:46630122dec9 231 calcaRes();
5hel2l2y 0:46630122dec9 232 }
5hel2l2y 0:46630122dec9 233
5hel2l2y 0:46630122dec9 234 void LSM6DS3::setGyroODR(gyro_odr gRate)
5hel2l2y 0:46630122dec9 235 {
5hel2l2y 0:46630122dec9 236 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 237 char cmd[2] = {
5hel2l2y 0:46630122dec9 238 CTRL2_G,
5hel2l2y 0:46630122dec9 239 0
5hel2l2y 0:46630122dec9 240 };
5hel2l2y 0:46630122dec9 241
5hel2l2y 0:46630122dec9 242 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 243 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 244 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 245 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 246
5hel2l2y 0:46630122dec9 247 // Then mask out the gyro odr bits:
5hel2l2y 0:46630122dec9 248 cmd[1] &= (0x3 << 3);
5hel2l2y 0:46630122dec9 249 // Then shift in our new odr bits:
5hel2l2y 0:46630122dec9 250 cmd[1] |= gRate;
5hel2l2y 0:46630122dec9 251
5hel2l2y 0:46630122dec9 252 // Write the gyroodr out to the gyro
5hel2l2y 0:46630122dec9 253 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 254 }
5hel2l2y 0:46630122dec9 255
5hel2l2y 0:46630122dec9 256 void LSM6DS3::setAccelODR(accel_odr aRate)
5hel2l2y 0:46630122dec9 257 {
5hel2l2y 0:46630122dec9 258 // The start of the addresses we want to read from
5hel2l2y 0:46630122dec9 259 char cmd[2] = {
5hel2l2y 0:46630122dec9 260 CTRL1_XL,
5hel2l2y 0:46630122dec9 261 0
5hel2l2y 0:46630122dec9 262 };
5hel2l2y 0:46630122dec9 263
5hel2l2y 0:46630122dec9 264 // Write the address we are going to read from and don't end the transaction
5hel2l2y 0:46630122dec9 265 i2c.write(xgAddress, cmd, 1, true);
5hel2l2y 0:46630122dec9 266 // Read in all the 8 bits of data
5hel2l2y 0:46630122dec9 267 i2c.read(xgAddress, cmd+1, 1);
5hel2l2y 0:46630122dec9 268
5hel2l2y 0:46630122dec9 269 // Then mask out the accel odr bits:
5hel2l2y 0:46630122dec9 270 cmd[1] &= 0xFF^(0x7 << 5);
5hel2l2y 0:46630122dec9 271 // Then shift in our new odr bits:
5hel2l2y 0:46630122dec9 272 cmd[1] |= aRate << 5;
5hel2l2y 0:46630122dec9 273
5hel2l2y 0:46630122dec9 274 // Write the accelodr out to the accel
5hel2l2y 0:46630122dec9 275 i2c.write(xgAddress, cmd, 2);
5hel2l2y 0:46630122dec9 276 }
5hel2l2y 0:46630122dec9 277
5hel2l2y 0:46630122dec9 278 void LSM6DS3::calcgRes()
5hel2l2y 0:46630122dec9 279 {
5hel2l2y 0:46630122dec9 280 // Possible gyro scales (and their register bit settings) are:
5hel2l2y 0:46630122dec9 281 // 245 DPS (00), 500 DPS (01), 2000 DPS (10).
5hel2l2y 0:46630122dec9 282 switch (gScale)
5hel2l2y 0:46630122dec9 283 {
5hel2l2y 0:46630122dec9 284 case G_SCALE_245DPS:
5hel2l2y 0:46630122dec9 285 gRes = 245.0 / 32768.0;
5hel2l2y 0:46630122dec9 286 break;
5hel2l2y 0:46630122dec9 287 case G_SCALE_500DPS:
5hel2l2y 0:46630122dec9 288 gRes = 500.0 / 32768.0;
5hel2l2y 0:46630122dec9 289 break;
5hel2l2y 0:46630122dec9 290 case G_SCALE_2000DPS:
5hel2l2y 0:46630122dec9 291 gRes = 2000.0 / 32768.0;
5hel2l2y 0:46630122dec9 292 break;
5hel2l2y 0:46630122dec9 293 }
5hel2l2y 0:46630122dec9 294 }
5hel2l2y 0:46630122dec9 295
5hel2l2y 0:46630122dec9 296 void LSM6DS3::calcaRes()
5hel2l2y 0:46630122dec9 297 {
5hel2l2y 0:46630122dec9 298 // Possible accelerometer scales (and their register bit settings) are:
5hel2l2y 0:46630122dec9 299 // 2 g (000), 4g (001), 6g (010) 8g (011), 16g (100).
5hel2l2y 0:46630122dec9 300 switch (aScale)
5hel2l2y 0:46630122dec9 301 {
5hel2l2y 0:46630122dec9 302 case A_SCALE_2G:
5hel2l2y 0:46630122dec9 303 aRes = 2.0 / 32768.0;
5hel2l2y 0:46630122dec9 304 break;
5hel2l2y 0:46630122dec9 305 case A_SCALE_4G:
5hel2l2y 0:46630122dec9 306 aRes = 4.0 / 32768.0;
5hel2l2y 0:46630122dec9 307 break;
5hel2l2y 0:46630122dec9 308 case A_SCALE_8G:
5hel2l2y 0:46630122dec9 309 aRes = 8.0 / 32768.0;
5hel2l2y 0:46630122dec9 310 break;
5hel2l2y 0:46630122dec9 311 case A_SCALE_16G:
5hel2l2y 0:46630122dec9 312 aRes = 16.0 / 32768.0;
5hel2l2y 0:46630122dec9 313 break;
5hel2l2y 0:46630122dec9 314 }
5hel2l2y 0:46630122dec9 315 }