Hey all, first off thank you for any help you can provide.
I'm trying to calculate some values on an Arduino Due. My problem is that I have run the calculations in Matlab and I get the right answer, but when I run the same equations on my Arduino the output is not the same. I'm guessing it has to be a coding issue, but I can't find it. I've been staring at my code for hours. The other possibility is just numerical accuracy issues. A few of the numbers are very small, but the Due has true double capability, and changing all of my variables to doubles did help improve the calculations, but they are still off.
Overview of code: Take in gyroscope and accelerometer data, ax, az, and gy specifically, process the readings to generate estimates of the pitch angle, output the data.
There's a lot, I hope this is enough to find the problem. I can't include it all as it's too long to post. As far as I can tell the error lies in this bit of code:
Arduino Code
// state update
//
fhat1 = W[0][0]+W[1][0]*phi[0]+W[2][0]*phi[1];
fhat2 = W[0][1]+W[1][1]*phi[0]+W[2][1]*phi[1];
thetahat = thetahat + F[1][0]*thetahatdot + fhat1 + Kmso[0]*ytildetheta;
thetahatdot = thetahatdot + fhat2 + Kmso[1]*ytildethetadot;
// weight updates
W[0][0] = W[0][0] + deltat*Gamma*(P[0]*ytildetheta - sigma*W[0][0]);
W[1][0] = W[1][0] + deltat*Gamma*(P[0]*phi[0]*ytildetheta - sigma*W[1][0]);
W[2][0] = W[2][0] + deltat*Gamma*(P[0]*phi[1]*ytildetheta - sigma*W[2][0]);
W[0][1] = W[0][1] + deltat*Gamma*(P[1]*ytildethetadot - sigma*W[0][1]);
W[1][1] = W[1][1] + deltat*Gamma*(P[1]*phi[0]*ytildethetadot - sigma*W[1][1]);
W[2][1] = W[2][1] + deltat*Gamma*(P[1]*phi[1]*ytildethetadot - sigma*W[2][1]);
Compared to
Matlab code
% // state update
fhat(1) = w(1,1)+w(2,1)*phi(1)+w(3,1)*phi(2);
fhat(2) = w(1,2)+w(2,2)*phi(1)+w(3,2)*phi(2);
xhat(1) = x(1) + F(1,2)*x(2) + fhat(1) + K1*ytildetheta;
xhat(2) = x(2) + fhat(2) + K2*ytildethetadot;
% // weight updates
what(1,1) = w(1,1)+ deltat*gamma*(P(1)*ytildetheta - sigma*w(1,1));
what(2,1) = w(2,1)+ deltat*gamma*(P(1)*phi(1)*ytildetheta - sigma*w(2,1));
what(3,1) = w(3,1)+ deltat*gamma*(P(1)*phi(2)*ytildetheta - sigma*w(3,1));
what(1,2) = w(1,2)+ deltat*gamma*(P(2)*ytildethetadot - sigma*w(1,2));
what(2,2) = w(2,2)+ deltat*gamma*(P(2)*phi(1)*ytildethetadot - sigma*w(2,2));
what(3,2) = w(3,2)+ deltat*gamma*(P(2)*phi(2)*ytildethetadot - sigma*w(3,2));
The values of fhat do not match, which leads me to believe that the w equations are not being calculated correctly. Possibly an indexing issue? I'm used to Matlab's indexing, so zero indexing and arrays of arrays get a little confusing when dealing with C code.
twip_v4.zip (189 KB)