Skip to content

Commit aefd2d0

Browse files
committed
Add mpu6886 fifo read api, Update read freq to 500HZ
1 parent 5d83db7 commit aefd2d0

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/utility/MPU6886.cpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@ int MPU6886::Init(void) {
4141
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_PWR_MGMT_1, 1, &regdata);
4242
delay(10);
4343

44+
// +- 8g
4445
regdata = 0x10;
4546
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_ACCEL_CONFIG, 1, &regdata);
4647
delay(1);
4748

49+
// +- 2000 dps
4850
regdata = 0x18;
4951
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_GYRO_CONFIG, 1, &regdata);
5052
delay(1);
5153

54+
// 1khz output
5255
regdata = 0x01;
5356
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_CONFIG, 1, &regdata);
5457
delay(1);
5558

56-
regdata = 0x05;
57-
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_SMPLRT_DIV, 1,&regdata);
59+
// 2 div, FIFO 500hz out
60+
regdata = 0x01;
61+
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_SMPLRT_DIV, 1, &regdata);
5862
delay(1);
5963

6064
regdata = 0x00;
@@ -126,7 +130,6 @@ void MPU6886::getAhrsData(float *pitch, float *roll, float *yaw) {
126130

127131
getGyroData(&gyroX, &gyroY, &gyroZ);
128132
getAccelData(&accX, &accY, &accZ);
129-
130133
MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ, pitch, roll, yaw);
131134
}
132135

@@ -215,3 +218,52 @@ void MPU6886::getTempData(float *t) {
215218

216219
*t = (float)temp / 326.8 + 25.0;
217220
}
221+
222+
void MPU6886::setGyroOffset(uint16_t x, uint16_t y, uint16_t z) {
223+
uint8_t buf_out[6];
224+
buf_out[0] = x >> 8;
225+
buf_out[1] = x & 0xff;
226+
buf_out[2] = y >> 8;
227+
buf_out[3] = y & 0xff;
228+
buf_out[4] = z >> 8;
229+
buf_out[5] = z & 0xff;
230+
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_GYRO_OFFSET, 6, buf_out);
231+
}
232+
233+
void MPU6886::setFIFOEnable( bool enableflag ) {
234+
uint8_t regdata = 0;
235+
regdata = enableflag ? 0x18 : 0x00;
236+
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_FIFO_ENABLE, 1, &regdata);
237+
regdata = enableflag ? 0x40 : 0x00;
238+
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_USER_CTRL, 1, &regdata);
239+
}
240+
241+
uint8_t MPU6886::ReadFIFO() {
242+
uint8_t ReData = 0;
243+
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_FIFO_R_W , 1 , &ReData);
244+
return ReData;
245+
}
246+
247+
void MPU6886::ReadFIFOBuff(uint8_t *DataBuff ,uint16_t Length) {
248+
uint8_t number = Length / 210;
249+
for(uint8_t i = 0; i < number; i++) {
250+
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_FIFO_R_W, 210, &DataBuff[i*210]);
251+
}
252+
253+
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_FIFO_R_W, Length % 210, &DataBuff[number*210]);
254+
}
255+
256+
void MPU6886::RestFIFO() {
257+
uint8_t buf_out;
258+
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_USER_CTRL, 1, &buf_out);
259+
buf_out |= 0x04;
260+
I2C_Write_NBytes(MPU6886_ADDRESS, MPU6886_USER_CTRL, 1, &buf_out);
261+
}
262+
263+
uint16_t MPU6886::ReadFIFOCount() {
264+
uint8_t Buff[2];
265+
uint16_t ReData = 0;
266+
I2C_Read_NBytes( MPU6886_ADDRESS, MPU6886_FIFO_COUNT, 2, Buff);
267+
ReData = (Buff[0] << 8) | Buff[1];
268+
return ReData;
269+
}

src/utility/MPU6886.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#define MPU6886_ACCEL_CONFIG2 0x1D
4444
#define MPU6886_FIFO_EN 0x23
4545

46+
#define MPU6886_FIFO_ENABLE 0x23
47+
#define MPU6886_FIFO_COUNT 0x72
48+
#define MPU6886_FIFO_R_W 0x74
49+
#define MPU6886_GYRO_OFFSET 0x13
4650
//#define G (9.8)
4751
#define RtA 57.324841
4852
#define AtR 0.0174533
@@ -81,6 +85,12 @@ class MPU6886 {
8185
void setAccelFsr(Ascale scale);
8286

8387
void getAhrsData(float *pitch,float *roll,float *yaw);
88+
void setFIFOEnable( bool enableflag );
89+
uint8_t ReadFIFO();
90+
void ReadFIFOBuff( uint8_t *DataBuff ,uint16_t Length );
91+
uint16_t ReadFIFOCount();
92+
void RestFIFO();
93+
void setGyroOffset(uint16_t x, uint16_t y, uint16_t z);
8494

8595
public:
8696
float aRes, gRes, imuId;

0 commit comments

Comments
 (0)