Skip to content

Commit 77daccb

Browse files
committed
fix qmi8658 stupidness
1 parent bcfbc39 commit 77daccb

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

src/sensors/F_QMI8658.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ int QMI8658::init(calData cal, uint8_t address)
2626

2727
writeByteI2C(wire, IMUAddress, QMI8658_CTRL1, 0x40); // Enable auto increment
2828

29-
writeByteI2C(wire, IMUAddress, QMI8658_CTRL2, 0x04); // Set up full scale Accel range. +-2G, 500hz ODR
30-
writeByteI2C(wire, IMUAddress, QMI8658_CTRL3, 0x34); // Set up Gyro range. +-128dps, 500hz ODR
29+
writeByteI2C(wire, IMUAddress, QMI8658_CTRL2, 0x34); // Set up full scale Accel range. +-16G, 500hz ODR
30+
writeByteI2C(wire, IMUAddress, QMI8658_CTRL3, 0x74); // Set up Gyro range. +-2048dps, 500hz ODR
3131

3232
writeByteI2C(wire, IMUAddress, QMI8658_CTRL5, 0x55); // Enable LPF for both accel and gyro, set to 14% of odr for around 70hz
3333

3434
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x03); // Start up accelerometer and gyro, disable sync
3535
delay(100); //wait until they're done starting up...
3636

37-
aRes = 2.f / 32768.f; //ares value for full range (16g) readings
38-
gRes = 128.f / 32768.f; //gres value for full range (2048dps) readings
37+
aRes = 16.f / 32768.f; //ares value for full range (16g) readings
38+
gRes = 2048.f / 32768.f; //gres value for full range (2048dps) readings
3939

4040
return 0;
4141
}
@@ -130,7 +130,6 @@ void QMI8658::getGyro(GyroData* out)
130130
}
131131

132132
int QMI8658::setAccelRange(int range) {
133-
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x00);
134133
uint8_t c;
135134
if (range == 16) {
136135
aRes = 16.f / 32768.f;
@@ -151,37 +150,38 @@ int QMI8658::setAccelRange(int range) {
151150
else {
152151
return -1;
153152
}
153+
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x00);
154154
rmwByteI2C(wire, IMUAddress, QMI8658_CTRL2, 0x70, c);
155155
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x03);
156156
return 0;
157157
}
158158

159159
int QMI8658::setGyroRange(int range) {
160-
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x00);
161160
uint8_t c;
162-
if (range == 2048) {
161+
if (range == 2048 || range == 2000) {
163162
gRes = 2048.f / 32768.f;
164163
c = 0x70; // gFS = 111 at bits[6:4]
165164
}
166-
else if (range == 1024) {
165+
else if (range == 1024 || range == 1000) {
167166
gRes = 1024.f / 32768.f;
168167
c = 0x60; // gFS = 110 at bits[6:4]
169168
}
170-
else if (range == 512) {
169+
else if (range == 512 || range == 500) {
171170
gRes = 512.f / 32768.f;
172171
c = 0x50; // gFS = 101 at bits[6:4]
173172
}
174-
else if (range == 256){
173+
else if (range == 256 || range == 250){
175174
gRes = 256.f / 32768.f;
176175
c = 0x40; // gFS = 100 at bits[6:4]
177176
}
178-
else if (range == 128){
177+
else if (range == 128 || range == 125){
179178
gRes = 128.f / 32768.f;
180179
c = 0x30; // gFS = 011 at bits[6:4]
181180
}
182181
else {
183182
return -1;
184183
}
184+
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x00);
185185
rmwByteI2C(wire, IMUAddress, QMI8658_CTRL3, 0x70, c);
186186
writeByteI2C(wire, IMUAddress, QMI8658_CTRL7, 0x03);
187187
return 0;
@@ -293,18 +293,17 @@ void QMI8658::calibrateAccelGyro(calData* cal)
293293
cal->valid = true;
294294
}
295295

296-
// Ascending ODR table (same encoding for both accel and gyro).
297-
// Accel ODR field: CTRL2 bits[6:4]. Gyro ODR field: CTRL3 bits[7:5].
296+
// ODR field is bits[3:0] in both CTRL2 (accel) and CTRL3 (gyro).
297+
// Init value 0x04 → 500 Hz confirms ascending encoding starting at 0x01.
298298
static const int QMI8658_ODR_TABLE[] = {62, 125, 250, 500, 1000, 2000, 4000, 8000};
299-
static const uint8_t QMI8658_ACCEL_ODR_REG[] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70};
300-
static const uint8_t QMI8658_GYRO_ODR_REG[] = {0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0};
299+
static const uint8_t QMI8658_ODR_REG[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
301300

302301
int QMI8658::setAccelODR(int odr_hz) {
303302
if (odr_hz <= 0) return -1;
304303
int actual = nearestHigherODR(QMI8658_ODR_TABLE, 8, odr_hz);
305304
int idx = 0;
306305
while (QMI8658_ODR_TABLE[idx] != actual) idx++;
307-
rmwByteI2C(wire, IMUAddress, QMI8658_CTRL2, 0x70, QMI8658_ACCEL_ODR_REG[idx]);
306+
rmwByteI2C(wire, IMUAddress, QMI8658_CTRL2, 0x0F, QMI8658_ODR_REG[idx]);
308307
currentAccelODR = actual;
309308
return actual;
310309
}
@@ -314,7 +313,7 @@ int QMI8658::setGyroODR(int odr_hz) {
314313
int actual = nearestHigherODR(QMI8658_ODR_TABLE, 8, odr_hz);
315314
int idx = 0;
316315
while (QMI8658_ODR_TABLE[idx] != actual) idx++;
317-
rmwByteI2C(wire, IMUAddress, QMI8658_CTRL3, 0xE0, QMI8658_GYRO_ODR_REG[idx]);
316+
rmwByteI2C(wire, IMUAddress, QMI8658_CTRL3, 0x0F, QMI8658_ODR_REG[idx]);
318317
currentGyroODR = actual;
319318
return actual;
320319
}

0 commit comments

Comments
 (0)