I have a simple function
#define AMB_FILTER 0.7f
int32_t fValue; (this is declared in the class header)
int32_t Ambient::filter(uint32_t raw)
{
// If we have no preliminary fValue we don't need to calculate a filter
if(fValue == -1)
{
fValue = raw;
return fValue;
}
float y, yy;
y = (1.0f - AMB_FILTER) * (float) raw;
yy = AMB_FILTER * (float) fValue;
fValue = (int32_t) (y + yy);
printf("filter raw %d y %f yy %f fValue %d\n",raw, y, yy, fValue);
return fValue;
}
It takes in a value that was read from smbus and returns a value that was filtered with the last value it received. Here is its output from the printf statement
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy -731751040.000000 fValue -731750912
filter raw 455 y 136.500000 yy -512225632.000000 fValue -512225504
filter raw 455 y 136.500000 yy -358557856.000000 fValue -358557728
filter raw 455 y 136.500000 yy -250990400.000000 fValue -250990256
filter raw 454 y 136.200012 yy -175693184.000000 fValue -175693040
So what is happening? How is it that it still received the same input but all of a sudden went crazy? I don't set the fValue anywhere but this function.
I made these variables (y and yy) very internalized to the function because I was worried they were somehow being modified or collided with something else. But now that they are completely local I have no idea what is happening. I am using C++ classes so this should all be in its own space anyhow.
EDIT: In fact if I let it run a little longer the variables I keep for the different i2c Chip addresses also become corrupted to -1075766188. What the hell?
Sometime between t1 and t2 the value of
fValue
was corrupted. The corruption ofyy
is a consequence offValue
's corruption. Look elsewhere in your program for the culprit.valgrind
, then sprinkle sanity checks on the valueoffvalue
throughout your program.this
in your routine. See if it becomes unusual.Ambient
. See if they become unusual.As to how it was corrupted, there are many possibilities. Perhaps you dereference a wild pointer somewhere. Perhaps you write beyond the end of an array. Perhaps you are operating on a previously destroyed
Ambient
.