i'm using a 8.8 fixed format for building a complementary filter for self balancing robot, i get correct angle for positive values but it doesn't work for negative values of the angle, i get 0 on the serial monitor.
here is the code,
#include "complementary_filter.h" uint8_t MPU6050_data[12]; volatile short int acx,gyy,acx_lut_index,gyy_lut_index,acx_deg,gyy_deg,anglex = 0; void COMPFILT_Initialize() { TCCR0 = (1 << WGM01) | (1 << CS00) | (1 << CS02); OCR0 = 156; //100.16hz sample rate TIMSK = (1 << OCIE0); } ISR(TIMER0_COMP_vect) { MPU6050_ReadByteS(ACXH,&MPU6050_data[0],6); MPU6050_ReadByteS(GYXH,&MPU6050_data[6],6); acx = MPU6050_data[0] << 8 | MPU6050_data[1]; gyy = MPU6050_data[8] << 8 | MPU6050_data[9]; if (acx < 0) { acx_lut_index = -acx; } else { acx_lut_index = acx; } acx_lut_index = acx_lut_index >> 6; acx_lut_index = acx_lut_index << 1; acx_deg = (pgm_read_byte(&arcsinelut[acx_lut_index]) << 8 | pgm_read_byte(&arcsinelut[acx_lut_index + 1])); if (acx < 0) { //UART_WriteInteger(-(ac_deg >> 8),5); acx_deg = -acx_deg; } if (gyy < 0) { gyy_lut_index = -gyy; } else { gyy_lut_index = gyy; } gyy_lut_index = gyy_lut_index >> 7; gyy_lut_index = gyy_lut_index << 1; gyy_deg = (pgm_read_byte(&gyrolut[gyy_lut_index]) << 8 | pgm_read_byte(&gyrolut[gyy_lut_index + 1])); if (gyy < 0) { gyy_deg = -gyy_deg; } anglex = multfix(0x00fb,anglex) + gyy_deg + multfix(0x0005,acx_deg); //00fb,0005 if (anglex < 0) { anglex = -anglex; UART_WriteInteger(-(anglex >> 8),5); }else { UART_WriteInteger(anglex >> 8,5); } UART_Write('\n'); }