Interlink Electronics Ring Sensor User Manual
Page 12
www.interlinkelectronics.com
10
Ring Sensor
Integration Guide
5.2
Averaging Multiple Samples
Because consecutive samples may straddle the rollover from 359° to 0°, a special trick is required
for averaging multiple samples. For example a simple average of 358° and 2° would give 180° but
it should give 0°.
Although several methods are possible, the following is very simple and does not require much
code. In this description, theta[0] is the most recent measurement, theta[1] is the next most recent,
etc.
First, calculate the average. Then compare the average with theta[0]. If the average and theta[0]
are too different (we have chosen 50°), then we assume that we have encountered the zero-
crossing problem. In that case, we then offset all small angles by 360 and re-calculate the average.
If the result ends up greater than 359°, then subtract 360°. Here is a code example:
tempint=(theta[0]+theta[1]+theta[2]+theta[3])/4;
if(abs(tempint-theta[0])>50)
//If avg is weird
{
tempint=0;
for(loop=0; loop<4; loop++)
{
tempint+=theta[loop];
if(theta[loop] < 50)
//If angle is small
tempint+=360; // add 360°
}
tempint/=4;
}
theta_out=tempint/4;
if(theta_out>359)
theta_out-=360;