I have run this code in a teensy 3.5 and the code works perfect, it adds the results
(droneGyroscope.rawData. is int16_t)
But when i run the same code on a teensy 4.1 it does not work, the problem in question is that all the addition lines misbehave, it just make the additions wrong. To see this i first printed all the different values of droneGyroscope.rawData.X in screen during the loop and the numbers were aroud 80
(This code performs the average of all values later, so it adds all and then devide them by CALIB_MEASUREMENTS obtaining the average), but the total number added was way over the expected value, (CALIB_MEASUREMENTS = 1000, rawData.X ~ 80, then tempGyroX ~80000, well the obtained value was 538529236), then i tried to print tempGyroX on each loop to see what was beeing added and to my surprise, putting that line there fixed the problem,
After that i thought that maybe a delay was needed between additions for some reason, but i tried with several waits and no one did work. This seems like a bug or something that i'm not seeing. It would not help to post more code because this is insisde a function and anything outside this function interferes with this problem, also i'm using custom hardware to run this.
(droneGyroscope.rawData. is int16_t)
Code:
int32_t tempGyroX, tempGyroY, tempGyroZ;
int32_t tempAccelX, tempAccelY, tempAccelZ;
for(int i = 0; i < CALIB_MEASUREMENTS; i++){
//Updating the sensors
updateSensors(false);
//Adding the measure data to a temporal variable
tempGyroX += droneGyroscope.rawData.X;
tempGyroY += droneGyroscope.rawData.Y;
tempGyroZ += droneGyroscope.rawData.Z;
tempAccelX += droneAccelerometer.rawData.X;
tempAccelY += droneAccelerometer.rawData.Y;
tempAccelZ += droneAccelerometer.rawData.Z;
//Dampening delay
delay(1);
}
Code:
int32_t tempGyroX, tempGyroY, tempGyroZ;
int32_t tempAccelX, tempAccelY, tempAccelZ;
for(int i = 0; i < CALIB_MEASUREMENTS; i++){
//Updating the sensors
updateSensors(false);
//Adding the measure data to a temporal variable
tempGyroX += droneGyroscope.rawData.X;
Serial.println(droneGyroscope.rawData.X);
tempGyroY += droneGyroscope.rawData.Y;
tempGyroZ += droneGyroscope.rawData.Z;
tempAccelX += droneAccelerometer.rawData.X;
tempAccelY += droneAccelerometer.rawData.Y;
tempAccelZ += droneAccelerometer.rawData.Z;
//Dampening delay
delay(1);
}
Code:
int32_t tempGyroX, tempGyroY, tempGyroZ;
int32_t tempAccelX, tempAccelY, tempAccelZ;
for(int i = 0; i < CALIB_MEASUREMENTS; i++){
//Updating the sensors
updateSensors(false);
//Adding the measure data to a temporal variable
tempGyroX += droneGyroscope.rawData.X;
Serial.println(tempGyroX);
tempGyroY += droneGyroscope.rawData.Y;
tempGyroZ += droneGyroscope.rawData.Z;
tempAccelX += droneAccelerometer.rawData.X;
tempAccelY += droneAccelerometer.rawData.Y;
tempAccelZ += droneAccelerometer.rawData.Z;
//Dampening delay
delay(1);
}