| import processing.gainer.*;
Gainer gainer;
PFont myFont;
MovingAverageFilter maFilterX;
MovingAverageFilter maFilterY;
MovingAverageFilter maFilterZ;
int xMin = 75; int xMax = 184;
int yMin = 75; int yMax = 184;
void setup() {
size(300, 500);
myFont = loadFont("Eureka90.vlw");
textFont(myFont, 24);
gainer = new Gainer(this);
gainer.beginAnalogInput();
maFilterX = new MovingAverageFilter(16);
maFilterY = new MovingAverageFilter(16);
maFilterZ = new MovingAverageFilter(16);
}
void draw()
{
int aveX = 0;
int aveY = 0;
int aveZ = 0;
float xInDegree = 0.0;
float yInDegree = 0.0;
background(0);
gainer.peekAnalogInput();
int rawX = gainer.analogInput[0];
int rawY = gainer.analogInput[1];
int rawZ = gainer.analogInput[2];
aveX = maFilterX.processSample(rawX);
aveY = maFilterY.processSample(rawY);
aveZ = maFilterZ.processSample(rawZ);
text("rawX: " + rawX, 10, 50);
text("rawY: " + rawY, 10, 80);
text("rawZ: " + rawZ, 10, 110);
xInDegree = getDegree(aveX, xMin, xMax);
yInDegree = getDegree(aveY, yMin, yMax);
text("averagedX: " + aveX, 10, 170);
text("averagedY: " + aveY, 10, 200);
text("averagedZ: " + aveZ, 10, 230);
text("deg1: " + round(xInDegree), 10, 290);
text("deg2: " + round(yInDegree), 10, 320);
}
float getDegree(int in, int min, int max)
{
float theta = 0;
theta = map(in, min, max, -1.0, 1.0);
if(theta >= 1.0)
{
theta = 1.0;
}
else if(theta <= -1.0)
{
theta = -1.0;
}
float deg = asin(theta)/ PI * 180.0;
return deg;
}
public class MovingAverageFilter
{
int taps;
float[] values;
public MovingAverageFilter(int requestedTaps)
{
taps = requestedTaps;
values = new float[taps];
}
int processSample(int in)
{
float acc = 0;
values[0] = in;
for(int i = 0; i < taps; i++)
{
acc += values[i];
}
acc /= taps;
for(int i = taps - 1; i > 0; i--)
{
values[i] = values[i - 1];
}
return round(acc);
}
}
|