Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accelerometer and Gyroscope are switched #2

Open
Cacciuc opened this issue Dec 12, 2019 · 1 comment
Open

Accelerometer and Gyroscope are switched #2

Cacciuc opened this issue Dec 12, 2019 · 1 comment

Comments

@Cacciuc
Copy link

Cacciuc commented Dec 12, 2019

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
Copy link

Hello Cacciuc,

the answer ist no, because of multiple reasons:

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 and 0x49 are the

  • Accelerometer (0x3B to 0x40)
  • Temperature (0x41 to 0x42)
  • Gyroscope (0x43 to 0x48)

So the function does everything correct

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants