My code is spontaneously forgetting how to subtract numbers

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

Try to replace

with

float offsetsX=0;
float offsetsY=0;
float offsetsZ=0;

Initializing more than one variable in the same line could be a side effects

1 Like

I've had that issue in the past, but I found the error in this case.

  wY = (gyroYLSB / 65.5) - OFFSET_GYRO_X;
  wX = (gyroXLSB / 65.5) - OFFSET_GYRO_Y;
  wZ = (gyroZLSB / 65.5) - OFFSET_GYRO_Z;

I mismatched the X and Y offsets...

1 Like

No.

Consider the examples

const int x =0, y =0;

or

volatile int x =0, y =0;

or even

char* x, y, z;

Better still, consider the sample

See?
No side-effects.

ok, I'm too dug in... :slight_smile:

Not an issue of side effects, but tell me the type of each of the three variables you've defined with that line of code.

Did I know? Yes. Was I right? I just ran some code to see Yes.

Would I write that line with confidence that no one would wonder?

No, no I would not.

a7

two chars and one pointer...

I have no idea what are you talking about.... sorry

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.