A project we are working on requires a MEMs inclinometer to assure coplanarity between a flat surface and a sensor.
The Problem
The sensor has a high gain section before an analog-to-digital converter feeds data to the microprocessor. Unless the sensor is located in a Faraday cage, the 120 hz hum generated causes severe instability in the digital data. It was determined that the center-line of this signal is the stable signal output.
At the extreme high and low outputs, the AC signal is compressed, but the center-line signal is still correct.
The code takes 20 asynchronous X and Y axis readings, finds the highest and and lowest, determines their difference, and divides by two to get the current X and Y inclination.
This code snippet just covers the search for highest and lowest value. We have left off the hardware code as it is different for each platform.
tiltDataXhiAccum = tiltDataX[0];
tiltDataYhiAccum = tiltDataY[0];
tiltDataXloAccum = tiltDataX[0];
tiltDataYloAccum = tiltDataY[0];
//this one finds the highest value
for ( j = 1; j < 20; j++ )
{
if ( tiltDataXhiAccum < tiltDataX[j] )
{
tiltDataXhiAccum = tiltDataX[j];
}
if ( tiltDataYhiAccum < tiltDataY[j] )
{
tiltDataYhiAccum = tiltDataY[j];
}
}
//this one finds the lowest value
for ( j = 1; j < 20; j++ )
{
if ( tiltDataXloAccum > tiltDataX[j] )
{
tiltDataXloAccum = tiltDataX[j];
}
if ( tiltDataYloAccum > tiltDataY[j] )
{
tiltDataYloAccum = tiltDataY[j];
}
}
tiltDataXAccum = ( tiltDataXhiAccum - tiltDataXloAccum ) / 2;
tiltDataYAccum = ( tiltDataYhiAccum - tiltDataYloAccum ) / 2;
This solution allowed us to go from a realtime bobble rate of over 5 per second with differences of 10 or more, to a stable reading that might change every few seconds with a worst case difference of 1.