2021-06-25 09:15 AM
I have collected 200 samples of the gyroscope raw data and get the average to calculate the offset. After that, I leave the sensor on table and try to read the raw data. The raw data read should be close to the average value that I obtained at the beginning but they are not the same. I have printed out the value and attached the photo below. The first line is the offset (obtained by getting the average of 200 samples of raw data when the sensor is at rest), all the data after first line is the gyroscope raw data read after collecting the 200 samples. The code is also attached. Anyone has any idea on what's going on here?
Edit: I felt that the problem might be caused by the data type/ size of buffer but I can't point out the exact problem.
Edit: I have changed the type of sprintf buffer to uint8_t since the HAL_UART_Transmit() is expecting uint8_t. This corrected the value of X but Y is still wrong. I received this warning though: pointer targets in passing argument 1 of 'sprintf' differ in signedness [-Wpointer-sign]
2021-06-25 09:57 AM
Y and Z seem fine, right? Some drift is to be expected.
All of your X values have 0x01 as the lower byte. Only the upper byte changes value. Doesn't seem to be a problem with how you're reading it as far as I can tell though.
All your variables that get modified in the interrupt like gyro_x_raw should be defined volatile, although I'd be surprised if that fixed it.
2021-06-25 10:03 AM
Z is fine. But Y should be somewhere around -60 if we look at the offset.
2021-06-25 10:09 AM
Defining the variable as volatile does not solve the problem.
2021-06-25 10:13 AM
If I print Z first, the value becomes wrong. Changing the size of the sprintf buffer seems to affect the value as well.