[url=][/url]
1 do{2 accelerationx[1]=accelerationx[1]+Sample_X;//filtering routine for noise attenuation3 accelerationy[1]=accelerationy[1]+Sample_Y;//64 samples are averaged. The resulting4 count2++;// average represents the acceleration of5 // an instant.6 }while(count2!=0x40);// 64 sums of the acceleration sample7 accelerationx[1]= accelerationx[1]>>6;// division by 648 accelerationy[1]= accelerationy[1]>>6;[url=][/url]
3. 机械滤波窗口
当处于无运动状态时,加速度上的微小错误可能会被当作一个常量速度,因为在采样值加和之后,它并不等于0。在没有运动的理想情况下,所有的采样值都为0。该常速表示一个连续的移动和不稳定的位置。
即使之前过滤的一些数据可能不正确,因此一个用于区别无运动状态的"有效数据"和"无效数据"的窗口必须实现。
1 if ((accelerationx[1] <=3)&&(accelerationx[1] >= -3)) //Discrimination window applied to2 {3 accelerationx[1] = 0;4 } // the X axis acceleration variable
[url=][/url]
1 if (positionX[1]>=0) 2 { //This line compares the sign of the X direction data 3 direction= (direction | 0x10); // if its positive the most significant byte 4 posx_seg[0]= positionX[1] & 0x000000FF; // is set to 1 else it is set to 8 5 posx_seg[1]= (positionX[1]>>8) & 0x000000FF; // the data is also managed in the 6 // subsequent lines in order to be sent. 7 posx_seg[2]= (positionX[1]>>16) & 0x000000FF;// The 32 bit variable must be split into 8 posx_seg[3]= (positionX[1]>>24) & 0x000000FF;// 4 different 8 bit variables in order to 9 // be sent via the 8 bit SCI frame10 }11 else 12 {13 direction=(direction | 0x80);14 positionXbkp=positionX[1]-1;15 positionXbkp=positionXbkp^0xFFFFFFFF;16 posx_seg[0]= positionXbkp & 0x000000FF;17 posx_seg[1]= (positionXbkp>>8) & 0x000000FF;18 posx_seg[2]= (positionXbkp>>16) & 0x000000FF;19 posx_seg[3]= (positionXbkp>>24) & 0x000000FF;20 }[url=][/url]
正因为如此,将速度强制减为0非常关键。这是通过不断读取加速度值和0进行比较而实现的。如果在一定数量的采样中,这种情况存在(sample==0)的话,速度将简单地返回为0。 [url=][/url]
1 if (accelerationx[1]==0) // we count the number of acceleration samples that equals zero 2 { 3 countx++; 4 } 5 else 6 { 7 countx =0; 8 } 9 if (countx>=25) // if this number exceeds 25, we can assume that velocity is zero10 {11 velocityx[1]=0;12 velocityx[0]=0;13 }[url=][/url]