Mems Gyro
Tuesday Oct 07, 2008

So I'm catching up on some of my "Stupid SPOT Tricks" and figured I should post this one. A long time ago now I got a couple of the IDG300 Dual Axis Mems Gyros from Sparkfun. Awesome little part. As usual, frighteningly easy to work with.
I wired it up to the Sun SPOT as follows:
| Gyro Breakout Board | Sun SPOT Pin |
|---|---|
| 3.3V | +3 |
| GND | GND |
| Yout | A0 |
| Xout | A1 |
| Vref | None |

Upon running the code the first time, I noticed that the gyro has a small amount of 'jitter.' The readings vary by a small amount even when the device is perfectly still. So I had to account for the jitter and quiet it somewhat. The basic class is very small:
public class MemsGyro extends MIDlet {
private static final int C_SLEEP = 100;
private static final int READ_SLEEP = 100;
private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
IScalarInput yout = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A1];
IScalarInput xout = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A2];
private int xDiff, yDiff;
And the method I added to account for the jitter is:
protected void startApp() throws MIDletStateChangeException {
System.out.println("Hello, world");
new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host
ISwitch sw1 = EDemoBoard.getInstance().getSwitches()[EDemoBoard.SW1];
calibrateGyro();
int x=0,y=0;
try{
while (sw1.isOpen()) { // done when switch is pressed
leds[0].setOn(!leds[0].isOn()); // Blink LED
int tx = xout.getValue();
if(tx > x+xDiff || tx < x-xDiff){
System.out.println("X Rotation: " + tx);
x = tx;
}
int ty = yout.getValue();
if(ty > y+yDiff || ty < y-yDiff){
System.out.println("Y Rotation: " + ty);
y = ty;
}
Utils.sleep(READ_SLEEP);
}
} catch (IOException ex) {
ex.printStackTrace();
}
notifyDestroyed(); // cause the MIDlet to exit
}
And that just samples the Gyro for a few seconds, and determines what the maximum variation is while the Gyro is 'quiet' and then uses that later to tell us if the Gyro is actually on the move.
private void calibrateGyro(){
int xMax = 0, yMax = 0;
int xMin = 100000, yMin = 100000;
leds[0].setRGB(100,0,0); // blink red while calibrating
try {
for(int z = 0; z<100;z++){
int y = yout.getValue();
int x = xout.getValue();
if(y > yMax)
yMax = y;
else if(y < yMin)
yMin = y;
if(x > xMax)
xMax = x;
else if(x < xMin)
xMin = x;
leds[0].setOn(!leds[0].isOn()); // Blink LED
Utils.sleep(C_SLEEP);
}
xDiff = xMax - xMin;
yDiff = yMax - yMin;
System.out.println("X Variation: " + xDiff + " Y Variation: " + yDiff);
} catch (IOException ex) {
ex.printStackTrace();
}
leds[0].setRGB(0,100,0); // otherwise blink green
}
Now, what you use this for is entirely up to you!
[ Down with categorical imperative! ]












Thanks much for this posting. I have hooked up the...
That will not give you correct results. You're rea...
Thanks much. That's what I did.
hi can you help please with more information abo...