forked from arduino-libraries/Arduino_LSM6DS3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSM6DS3.cpp
273 lines (217 loc) · 5.81 KB
/
LSM6DS3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
This file is part of the Arduino_LSM6DS3 library.
Copyright (c) 2019 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "LSM6DS3.h"
#define LSM6DS3_ADDRESS 0x6A
#define LSM6DS3_WHO_AM_I_REG 0X0F
#define LSM6DS3_CTRL1_XL 0X10
#define LSM6DS3_CTRL2_G 0X11
#define LSM6DS3_STATUS_REG 0X1E
#define LSM6DS3_CTRL6_C 0X15
#define LSM6DS3_CTRL7_G 0X16
#define LSM6DS3_CTRL8_XL 0X17
#define LSM6DS3_OUT_TEMP_L 0X20
#define LSM6DS3_OUTX_L_G 0X22
#define LSM6DS3_OUTX_H_G 0X23
#define LSM6DS3_OUTY_L_G 0X24
#define LSM6DS3_OUTY_H_G 0X25
#define LSM6DS3_OUTZ_L_G 0X26
#define LSM6DS3_OUTZ_H_G 0X27
#define LSM6DS3_OUTX_L_XL 0X28
#define LSM6DS3_OUTX_H_XL 0X29
#define LSM6DS3_OUTY_L_XL 0X2A
#define LSM6DS3_OUTY_H_XL 0X2B
#define LSM6DS3_OUTZ_L_XL 0X2C
#define LSM6DS3_OUTZ_H_XL 0X2D
LSM6DS3Class::LSM6DS3Class(TwoWire& wire, uint8_t slaveAddress) :
_wire(&wire),
_spi(NULL),
_slaveAddress(slaveAddress)
{
}
LSM6DS3Class::LSM6DS3Class(SPIClass& spi, int csPin, int irqPin) :
_wire(NULL),
_spi(&spi),
_csPin(csPin),
_irqPin(irqPin),
_spiSettings(10E6, MSBFIRST, SPI_MODE0)
{
}
LSM6DS3Class::~LSM6DS3Class()
{
}
int LSM6DS3Class::begin()
{
if (_spi != NULL) {
pinMode(_csPin, OUTPUT);
digitalWrite(_csPin, HIGH);
_spi->begin();
} else {
_wire->begin();
}
if (readRegister(LSM6DS3_WHO_AM_I_REG) != 0x69) {
end();
return 0;
}
//set the gyroscope control register to work at 104 Hz, 2000 dps and in bypass mode
writeRegister(LSM6DS3_CTRL2_G, 0x4C);
// Set the Accelerometer control register to work at 104 Hz, 4G,and in bypass mode and enable ODR/4
// low pass filter(check figure9 of LSM6DS3's datasheet)
writeRegister(LSM6DS3_CTRL1_XL, 0x4A);
// set gyroscope power mode to high performance and bandwidth to 16 MHz
writeRegister(LSM6DS3_CTRL7_G, 0x00);
// Set the ODR config register to ODR/4
writeRegister(LSM6DS3_CTRL8_XL, 0x09);
return 1;
}
void LSM6DS3Class::end()
{
if (_spi != NULL) {
_spi->end();
digitalWrite(_csPin, LOW);
pinMode(_csPin, INPUT);
} else {
writeRegister(LSM6DS3_CTRL2_G, 0x00);
writeRegister(LSM6DS3_CTRL1_XL, 0x00);
_wire->end();
}
}
int LSM6DS3Class::readAcceleration(float& x, float& y, float& z)
{
int16_t data[3];
if (!readRegisters(LSM6DS3_OUTX_L_XL, (uint8_t*)data, sizeof(data))) {
x = NAN;
y = NAN;
z = NAN;
return 0;
}
x = data[0] * 4.0 / 32768.0;
y = data[1] * 4.0 / 32768.0;
z = data[2] * 4.0 / 32768.0;
return 1;
}
int LSM6DS3Class::accelerationAvailable()
{
if (readRegister(LSM6DS3_STATUS_REG) & 0x01) {
return 1;
}
return 0;
}
float LSM6DS3Class::accelerationSampleRate()
{
return 104.0F;
}
int LSM6DS3Class::readGyroscope(float& x, float& y, float& z)
{
int16_t data[3];
if (!readRegisters(LSM6DS3_OUTX_L_G, (uint8_t*)data, sizeof(data))) {
x = NAN;
y = NAN;
z = NAN;
return 0;
}
x = data[0] * 2000.0 / 32768.0;
y = data[1] * 2000.0 / 32768.0;
z = data[2] * 2000.0 / 32768.0;
return 1;
}
int LSM6DS3Class::gyroscopeAvailable()
{
if (readRegister(LSM6DS3_STATUS_REG) & 0x02) {
return 1;
}
return 0;
}
float LSM6DS3Class::gyroscopeSampleRate()
{
return 104.0F;
}
int LSM6DS3Class::readTemperature(float& t)
{
int16_t data[1];
if (!readRegisters(LSM6DS3_OUT_TEMP_L, (uint8_t*)data, sizeof(data))) {
t = NAN;
return 0;
}
t = data[0] / 16.0 + 25;
return 1;
}
int LSM6DS3Class::temperatureAvailable()
{
if (readRegister(LSM6DS3_STATUS_REG) & 0x04) {
return 1;
}
return 0;
}
float LSM6DS3Class::temperatureSampleRate()
{
return 52.0F;
}
int LSM6DS3Class::readRegister(uint8_t address)
{
uint8_t value;
if (readRegisters(address, &value, sizeof(value)) != 1) {
return -1;
}
return value;
}
int LSM6DS3Class::readRegisters(uint8_t address, uint8_t* data, size_t length)
{
if (_spi != NULL) {
_spi->beginTransaction(_spiSettings);
digitalWrite(_csPin, LOW);
_spi->transfer(0x80 | address);
_spi->transfer(data, length);
digitalWrite(_csPin, HIGH);
_spi->endTransaction();
} else {
_wire->beginTransmission(_slaveAddress);
_wire->write(address);
if (_wire->endTransmission(false) != 0) {
return -1;
}
if (_wire->requestFrom(_slaveAddress, length) != length) {
return 0;
}
for (size_t i = 0; i < length; i++) {
*data++ = _wire->read();
}
}
return 1;
}
int LSM6DS3Class::writeRegister(uint8_t address, uint8_t value)
{
if (_spi != NULL) {
_spi->beginTransaction(_spiSettings);
digitalWrite(_csPin, LOW);
_spi->transfer(address);
_spi->transfer(value);
digitalWrite(_csPin, HIGH);
_spi->endTransaction();
} else {
_wire->beginTransmission(_slaveAddress);
_wire->write(address);
_wire->write(value);
if (_wire->endTransmission() != 0) {
return 0;
}
}
return 1;
}
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
LSM6DS3Class IMU(SPI, SPIIMU_SS, SPIIMU_INT);
#else
LSM6DS3Class IMU(Wire, LSM6DS3_ADDRESS);
#endif