Strange code execution bug in teensy 4.1

Status
Not open for further replies.

Pasblo

New member
I have run this code in a teensy 3.5 and the code works perfect, it adds the results
(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);
	}
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
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);
	}
(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,
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);
	}
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.
 
These values are entering the loop for "+=" without a known starting value?
Code:
int32_t tempGyroX, tempGyroY, tempGyroZ;
int32_t tempAccelX, tempAccelY, tempAccelZ;

Are they stack values unset on each entry to the function?
 
I added a staring value of 0 and it worked! But i dont understand why, first i thought that when declaring variables were automatically initialized to the default value. Also i dont understand why it worked with the teensy 3.5 without the initialization and why it also worked when printing the tempGyro.
 
Values in functions are created on the stack and not automatically pre-initialized on entry to the function. Globals and static values are typically placed in memory that is set to zero before execution and they persist. Local function variables allocated on the stack end up with the value that was there before if not explicitly set. The code on the T_3.5 is just lucky to have had zero or ignorably small values that got lost in the average as the code ran and compiled.

If Verbose code warnings are enabled - those += assignments should show a warning from the compiler for values used before being initialized.
 
Ah okey, it makes sense the explanation about the stack, i will take care the next tme, I will enable the verbose code warnings the next time
 
Status
Not open for further replies.
Back
Top