EDIT: I would like to keep this up as a great example as to why you shouldn't code at 2:00 AM. I fixed it.
I am simply trying to find the mean of 250 samples from the gyroscope. I tried to copy the code here so that it is readable for you all to look at. If I am not declaring variables in the code below, just know that I have zero upload errors on my end.
#include <Wire.h>
int16_t AccXLSB, AccYLSB, AccZLSB;
float gX, gY, gZ;
int16_t gyroXLSB, gyroYLSB, gyroZLSB;
float wX, wY, wZ;
float angleRollAcc, anglePitchAcc, angleYawAcc;
float OFFSET_ACCEL_X=0,OFFSET_ACCEL_Y=0,OFFSET_ACCEL_Z=0;
float angleRollGyro,anglePitchGyro,angleYawGyro;
float OFFSET_GYRO_X=0,OFFSET_GYRO_Y=0,OFFSET_GYRO_Z=0;
void setup() {
Serial.begin(9600);
Wire.setClock(300000);
Wire.begin();
setupMPU();
initializeOffsets();
//code that works
}
void setupMPU(){
//more code that works fine
}
void initializeOffsets(){
//declare local variables
float offsetsX=0,offsetsY=0,offsetsZ=0;
//loads of code that works fine
//
//calculate offsets for gyro data
for (int i=0;i<250;i++){
getGyroData();
offsetsX+=wX;
offsetsY+=wY;
offsetsZ+=wZ;
delay(1);
}
OFFSET_GYRO_X = offsetsX/250;
OFFSET_GYRO_Y = offsetsY/250;
OFFSET_GYRO_Z = offsetsZ/250;
}
void getGyroData(){
Wire.beginTransmission(0b1101000);
Wire.write(0x43);//+- 65.5 LSB/(deg/s)
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while (Wire.available() < 6);
gyroYLSB = Wire.read()<<8|Wire.read(); //Stores the first two bytes
gyroXLSB = Wire.read()<<8|Wire.read(); //Stores the next two bytes
gyroZLSB = Wire.read()<<8|Wire.read(); //Stores the last two byres
//LSB --> deg/s
wY = (gyroYLSB / 65.5) - OFFSET_GYRO_X;
wX = (gyroXLSB / 65.5) - OFFSET_GYRO_Y;
wZ = (gyroZLSB / 65.5) - OFFSET_GYRO_Z;
}
The issues arise is when I subtract OFFSET_GYRO_X/Y to their respective values inside getGyroData(). The wZ value gets zeroed correctly, but the wY and wX values are close to 15 and -15. I've ran this loop with i values ranging from 200-2000 and the offsetsX/Y/Z values are always very close in value. I'm wondering if I am missing something small or if I am misusing the float class.
Thank you