jarzebski / Arduino-MPU6050

MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library
GNU General Public License v3.0
429 stars 278 forks source link

readRawAccel() conversion error #23

Open TzOk83 opened 4 years ago

TzOk83 commented 4 years ago

There is a bug which reveals itself on 16 or 32 bit MPUs. RAW acceleration is returned via 2 8-bit registers, but is signed, and 2's complement encoded. Registers are mapped as uint8_t (unsigned 8-bit), which is fine, but they are bitshifted and ORed together and assigned to float variable. xha << 8 | xla creates xa, but it's type is uint16_t, while it should be int16_t... both can be assigned to float variable, but only second one is valid.

Is:

ra.XAxis = xha << 8 | xla;
ra.YAxis = yha << 8 | yla;
ra.ZAxis = zha << 8 | zla;

Should be:

ra.XAxis = (int16_t)(xha << 8 | xla);
ra.YAxis = (int16_t)(yha << 8 | yla);
ra.ZAxis = (int16_t)(zha << 8 | zla);
I-Connect commented 4 years ago

thx man, this saves me a lot of time and searching! (was running it on esp32)