# Integrating accelerometer and gyroscope using complementary filter

This is what ive done:

graceAcc = xlsread('graceAcc.csv');

X= graceAcc(:,2);

Y= graceAcc(:,3);

Z= graceAcc(:,4);

graceGyro = xlsread('graceGyro.csv');

T= graceGyro(:,1);

X1= graceGyro(:,2);

Y1= graceGyro(:,3);

Z1= graceGyro(:,4);

hpf = 0.98;

lpf = 0.02;

TX = atand((X)./sqrt(Y.^2 + Z.^2));

TY = atand((Y)./sqrt(X.^2 + Z.^2));

TZ = atand(sqrt(Y.^2 + X.^2)./(Z));

X2 = 0;

Y2 = 0;

Z2 = 0;

X3 = X2 + (X1.*T);

for a = 1:length(X1)

if a == 1

X2 = ((X1.*T).*hpf) + (TX(1:110).*lpf);

Y2 = (Y1.*T).*hpf + (TY(1:110).*lpf);

Z2 = (Z1.*T).*hpf + (TZ(1:110).*lpf);

else

X2 = (X3).*hpf + (TX(1:110).*lpf);

Y2 = (Y2 + (Y1.*T)).*hpf + (TY(1:110).*lpf);

Z2 = (Z2 + (Z1.*T)).*hpf + (TZ(1:110).*lpf);

end

end

plot(TX,'g');

hold on

plot(TY,'r');

hold on

plot(TZ,'b');

title('Change in Angle of Accelerometer and Gyroscope')

xlabel('samples')

ylabel('tilt angle')

legend('Complementary filter X','Accelerometer X','Gyroscope X')

This discussion has been closed.

## Comments