sinadarvi / SD_HAL_MPU6050

Running MPU6050 with HAL drivers on STM32
67 stars 24 forks source link

Accelerometer and Gyroscope are switched #2

Open Cacciuc opened 4 years ago

Cacciuc commented 4 years ago

When using just the simplest implementation, it seems that the accelerometer values and the gyroscope values are switched. My implementation in main ` . . . //initialisations from CubeMx SD_MPU6050 mpu; if (SD_MPU6050_Init(&hi2c1, &mpu, SD_MPU6050_Device_1, SD_MPU6050_Accelerometer_4G, SD_MPU6050_Gyroscope_1000s) != SD_MPU6050_Result_Ok) { LedRed(1); asm("nop"); return -1; } / USER CODE END 2 /

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
    for (int i = 0; i < 5; i++) {
        if (SD_MPU6050_ReadAll(&hi2c1, &mpu)
                != SD_MPU6050_Result_Ok) {
            LedRed(1);
        }
        HAL_Delay(100);
    }

}` In the STMStudio the values are shown in a bar graph. This is a screenshot of the MPU6050 sitting still in a weird angle. All values are stable. I will switch the accelerometer and gyroscope variables in my Code, but since no one else has the same error, i will not make a pull request. mpu6050

git-oliver commented 2 years ago

Hello Cacciuc,

the answer ist no, because of multiple reasons:

1) If you look insideSD_MPU6050_READ_ALL() the register to read out is MPU6050_ACCEL_XOUT_H (0x3B). And from this reg we will read 14 bytes (0xE) If you now do 0x3B + 0xE = 0x49.
And if you look inside the datasheet [1] (page. 29 to 31) all reg between 0x3B and0x49 are the

So the function does everything correct

2) If you mount the MPU6050 sensor on your table and fix them with simple tape, then only the z-Acceleration should be unequal to zero. Set the following properties:

SD_MPU6050_Init(    hi2c1,
                    mpu1,
                    SD_MPU6050_Device_0,
                    SD_MPU6050_Accelerometer_2G,
                    SD_MPU6050_Gyroscope_250s
                     );

After setting these properties you need to calculate the Offset. For that read out 1000 datapoints. Then calculate the Offset with the code. Note I don't use the first 400 values. Thats only for safety, because I found out that the the first values are garbage. To be sure to not get garbage values, I dont care about the first 400 values.

%% Offset für acc_x
offset_acc_x = raw_acc_x(400:end);
offset_acc_x = (mean(offset_acc_x)); 

%% Offset für acc_y
offset_acc_y = raw_acc_y(400:end);
offset_acc_y = (mean(offset_acc_y));

%% Offset für acc_z
offset_acc_z = raw_acc_z(400:end);
offset_acc_z = (mean(offset_acc_z));
offset_acc_z = offset_acc_z - 16384;    % z =! 1g (@range +-2g)

%% Offset für gyro_x
offset_gyro_x = raw_gyro_x(400:end);
offset_gyro_x =(mean(offset_gyro_x));

%% Offset für gyro_y
offset_gyro_y = raw_gyro_y(400:end);
offset_gyro_y =(mean(offset_gyro_y));

%% Offset für gyro_z
offset_gyro_z = raw_gyro_z(400:end);
offset_gyro_z =(mean(offset_gyro_z));

After that you can read out new datapoints and subtract them with you offset values. You should get nearly zeros on x and y acceleration axis and 16384 on the z-axis. This is because the z-axis measures the gravity.

So long story short:

The function works as expected and you your assumption is wrong

In a few weeks I will fork this repo and add a complementary filter to calculate the angle. And some others improvements of this repo/code.

[1] https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf