unwanted variable change

Hi all,

Im encountering a change of a variable i do not understand, probably due to a lack of programming experience.
For a ball on a beam project im trying to build a kalman filter in using Arduino Playground - MatrixMath, most is working fine, until i get to this piece of code:

Serial.println(a[0],10);

x_kalman = X00[0];
v_kalman = X00[1];

Serial.println(a[0],10);

in line 2 and 3 im assigning the result the previous code to x_kalman and v_kalman coming from a array X00[2].
this seems to work great, however in these two lines the array a[1] appears to change it's value.

the piece of code results in:

0.0018310546
0.0003591835

What is happening here? the first value is the correct value, how can a[0] change value?

Thanks,

What is happening here? the first value is the correct value, how can a[0] change value?

Without seeing your code, we'd only be guessing, but I'm guess that you are writing beyond the end of an array.

PaulS,

How would i check, if that is the case?

Thanks,

How would i check, if that is the case?

Check that every write to an array is writing within bounds. Post your code, like you were supposed to, and we'll help.

This is what i have so-far (total code is to large to paste):

void loop (){

a[0] = 2.000;//(float)analogRead(angle_read);
float x_meas = 15.0000;
//a[0] = calib_apow(a[0],3)+calib_bpow(a[0],2)+calib_c*a[0]+calib_d;

// Kalman filter----------------------------------------

// X01[1] = F[1]X11[1]+F[2]X11[2]+B[1]a;
// X01[2] = F[3]X11[1]+F[4]X11[2]+B[1]a;
Matrix.Print((float
)H,1,N,"H");
Matrix.Print((float
)F,N,N,"F");
Matrix.Print((float
)B,N,1,"B");
Matrix.Print((float
)Q,N,N,"Q");
Matrix.Print((float
)X11,N,1,"X11");
Matrix.Print((float
)P11,N,N,"P11");
// X01------------------------------------------------------
Matrix.Multiply((float*)F,(float*)X11,N,N,1,(float*)temp2);
Matrix.Print((float*)temp2,N,N,"temp2");
Matrix.Multiply((float*)B,(float*)a,N,1,1,(float*)X01);
Matrix.Add((float*) temp2, (float*) X01, N, 1, (float*) X01);
//Matrix.Print((float*)temp2,N,N,"temp2");
Matrix.Print((float*)X01,N,1,"X01");
Matrix.Print((float*)temp2,N,N,"temp2");

// P01------------------------------------------------------
Matrix.Multiply((float*)P11,(float*)F,N,N,N,(float*)temp33);
//Matrix.Print((float*)temp22,N,N,"temp22");
//Matrix.Print((float*)temp2,N,N,"temp2");
Matrix.Multiply((float*)temp33,(float*)F1,N,N,N,(float*)P01);
//Matrix.Print((float*)P01,N,N,"P01");
Matrix.Add((float*) Q, (float*) P01, N, N, (float*) P01);

Matrix.Print((float*)P01,N,N,"P01");

float temp2[N] = {0,0};
float temp[1] = {0};

//K--------------------------------------------------------
Matrix.Print((float*)temp2,N,N,"temp2");
Matrix.Multiply((float*)H,(float*)P01,1,N,N,(float*)temp2);
Matrix.Print((float*)temp2,1,N,"P01H");
Matrix.Print((float
)H,1,N,"H");
Matrix.Multiply((float*)temp2,(float*)H,1,2,1,(float*)temp);
Matrix.Print((float*)temp,1,1,"P01HH'");
Matrix.Add((float*) temp, (float*) R, 1, 1, (float*) temp);
Matrix.Print((float*)temp,1,1,"P01HH'+r");
Matrix.Invert((float*)temp,1);
Matrix.Print((float*)temp,1,1,"inv P01HH'+r");
Matrix.Multiply((float*)temp,(float*)H,1,1,N,(float*)temp2);
Matrix.Multiply((float*)P01,(float*)temp2,N,N,1,(float*)K);
Matrix.Print((float*)K,N,1,"K");

//X00-----------------------------------------------------
//float temp2[N] = {0,0};
temp[0]=0;
temp2[0]=0;
temp2[1]=0;

Matrix.Multiply((float*)H,(float*)X01,1,N,1,(float*)temp);
Matrix.Print((float*)temp,1,1,"temp");
temp[0]=x_meas-temp[0];
Matrix.Print((float*)temp,1,1,"temp");
Matrix.Print((float*)K,1,N,"K");
K2[0] = K[0]temp[0];
K2[1] = K[1]temp[0];
Matrix.Print((float
)K2,1,N,"K'");
Matrix.Add((float
) K2, (float*) X01, 1, N, (float*) X00);
Matrix.Print((float*)X00,N,1,"X00");

//P00----------------------------------------------------
temp[0]=0;
temp2[0]=0;
temp2[1]=0;
temp33[0][0] = 0;
temp33[0][1] = 0;
temp33[1][0] = 0;
temp33[1][1] = 0;

Matrix.Multiply((float*)K,(float*)H,N,1,N,(float*)temp33);
Matrix.Print((float*)temp33,N,N,"temp33");
Matrix.Print((float*)I,N,N,"I");
Matrix.Subtract((float*) I, (float*)temp33,N,N, (float*)temp33);
Matrix.Print((float*)temp33,N,N,"temp33");
Matrix.Multiply((float*)temp33,(float*)P01,N,N,N,(float*)P00);
Matrix.Print((float*)P00,N,N,"P00");
x_kalman = X00[0];
v_kalman = X00[1];

;
delay (1000000);
// -----------------------------------------------------

dt=millis()-t0;
}

the error occurs @ "x_kalman = X00[0];" . Here a[0] changes for some reason

You can post your full code as an attachment. Please do so.

How to use this forum

 float temp[1]  = {0};
...
  Matrix.Invert((float*)temp,1);

What is the point of an array with only one element? How do you Matrix.Invert a single float? I thought a matrix was ... well ... more than one thing.

Nick,

You are completely right, in this case useless.
I was hoping to end up with a more general form, in some cases this matrix is larger than 1 element.
But got a bit diverted from this goal trying to understand the MatrixMath code (and Arduino coding in general).
Thanks for reading it that thoroughly

Attached is the complete file

kalman.ino (6.61 KB)

float a[0];
...
  a[0] = 2.000;//(float)analogRead(angle_read);

You are declaring the variable "a" to have no dimensions (effectively, it occupies no memory) and then you assign a value 2 to element 0, which is out of range.

Plus, this is confusing:

float temp[0];   // no elements
float temp2[N];

...

 float temp2[N] = {0,0};
 float temp[1]  = {0};

This is redefining a local variable "temp" which shadows the global variable "temp". The global variable has no elements.

PaulS:

What is happening here? the first value is the correct value, how can a[0] change value?

Without seeing your code, we'd only be guessing, but I'm guess that you are writing beyond the end of an array.

PaulS guessed correctly, it seems. :slight_smile:

That seems to be the source of my problems.
Got a bit confused in pointing to the correct position in the array (0= first position) and defining the length of the array ( 0 is none)

Thanks for the help!

mathematically 1x1 matrix is perfectly valid, inversion is same as reciprocal in this case.
A matrix library needs to support them in fact as the product of a 1xN row and Nx1
column matrix is a 1x1 matrix.

MarkT,

As you mention the code (inversion) off course still has a function.
In this specific case where the size of the array is hard coded to be 1, it could be done simpler (what i assume was the point of Nick)