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

MPU6050 accel and gyro values swapped #37784

Closed
ric96 opened this issue Aug 19, 2021 · 1 comment
Closed

MPU6050 accel and gyro values swapped #37784

ric96 opened this issue Aug 19, 2021 · 1 comment
Labels
bug The issue is a bug, or the PR is fixing a bug

Comments

@ric96
Copy link
Contributor

ric96 commented Aug 19, 2021

I am using the following code to print out MPU6050's values:

sensor_channel_get(mpu6050, SENSOR_CHAN_ACCEL_XYZ, accel);
sensor_channel_get(mpu6050, SENSOR_CHAN_GYRO_XYZ, gyro);
sensor_channel_get(mpu6050, SENSOR_CHAN_AMBIENT_TEMP, &temperature);

printf("Temp: %d.%06d\n"
          "Accel\tX: %d.%06d\tY: %d.%06d\tZ: %d.%06d\n"
          "Gyro\tX:%d.%06d\tY :%d.%06d\tZ: %d.%06d\n",
          temperature.val1, temperature.val2,
          accel[0].val1, accel[0].val2, accel[1].val1, accel[1].val2, accel[2].val1, accel[2].val2,
          gyro[0].val1, gyro[0].val2, gyro[1].val1, gyro[1].val2, gyro[1].val1, gyro[1].val2);

And with the sensor still at an angle, here is the output i am getting:

Temp: 30.224118
Accel   X: -3.-320758   Y: -1.-86968    Z: 8.693346
Gyro    X:0.-08393      Y :0.004263     Z: 0.004263

Ans you can see its showing a constant acceleration, when i gently move the unit the accel values change as if they were gyro values and when i shake the sensor the gyro values spike and they are normally stable at approx 0.0

The are clearly swapped.

when looking at this bit of the driver code, I thought that it might be a byte order issue, so gyro_z becomes buf[0] and accel_x becomes buf[6] but that doesn't seem to be the case as it does put the accel and gyro data in the correct place but the axis are all swapped

drv_data->accel_x = sys_be16_to_cpu(buf[0]);
drv_data->accel_y = sys_be16_to_cpu(buf[1]);
drv_data->accel_z = sys_be16_to_cpu(buf[2]);
drv_data->temp = sys_be16_to_cpu(buf[3]);
drv_data->gyro_x = sys_be16_to_cpu(buf[4]);
drv_data->gyro_y = sys_be16_to_cpu(buf[5]);
drv_data->gyro_z = sys_be16_to_cpu(buf[6]);

For whatever reason this assignment gave me the nearest reading, it doesn't follow the datasheet at all and so its probably not the correct solution or not even where the issue might be

drv_data->accel_x = sys_be16_to_cpu(buf[4]);
drv_data->accel_y = sys_be16_to_cpu(buf[5]);
drv_data->accel_z = sys_be16_to_cpu(buf[6]);
drv_data->temp = sys_be16_to_cpu(buf[3]);
drv_data->gyro_x = sys_be16_to_cpu(buf[1]);
drv_data->gyro_y = sys_be16_to_cpu(buf[0]);
drv_data->gyro_z = sys_be16_to_cpu(buf[2]);
@ric96 ric96 added the bug The issue is a bug, or the PR is fixing a bug label Aug 19, 2021
@ric96
Copy link
Contributor Author

ric96 commented Aug 19, 2021

Closing, sorry for the noise. For some reason my understanding of accel and gyro was swapped.

@ric96 ric96 closed this as completed Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

No branches or pull requests

1 participant