The gyro data looks to be a 16 bit signed value, from which I have assumed a data value of 32768, with the gyro initialed at 500DPS, equals the board rotating at 500 degree/s , and -32768 would be 500 degree/s in the opposite direction. Is my understanding of the board output correct?
I'm using that data, plus timing from millis() to try to determine the current position of the board. I'm going to be using the gyro data to control colors using the color wheel code on a strand of WS2801, so I'm converting a 360 degree circle into 256 positions. You can see below the code I'm using in my testing. I've noticed the data contains a fair bit of noise, so I'm excluding any values between -200 and 200.
My expectation is that 1 full rotation around the x axis should result in a change of position of 256, in practice though it winds up taking about 1 1/2 - 2 full rotations to measure 256. I realize that an accelerometer might be better for what I'm trying to do, and I will probably add one to my project, but any direction on getting my gyro code working would be greatly appreciated.
[Edit - moderator - please use the 'code' button when submitting code]
Code: Select all
#include <Wire.h>
#include <Adafruit_L3GD20.h>
// By default, uses I2C
//Adafruit_L3GD20 gyro;
// Alternately, you can use SPI, but you have to define the pins
#define GYRO_CS 4 // labeled CS
#define GYRO_DO 5 // labeled SA0
#define GYRO_DI 8 // labeled SDA
#define GYRO_CLK 7 // labeled SCL
Adafruit_L3GD20 gyro(GYRO_CS, GYRO_DO, GYRO_DI, GYRO_CLK);
float pos=0;
unsigned long last_time, this_time, diff_time;
void setup()
{
Serial.begin(9600);
if (!gyro.begin(gyro.L3DS20_RANGE_500DPS))
{
Serial.println("Oops ... unable to initialize the L3GD20. Check your wiring!");
while (1);
}
last_time=millis();
}
void loop()
{
gyro.read();
this_time=millis();
diff_time=this_time-last_time;
last_time=this_time;
if (gyro.data.x>200||gyro.data.x<-200)
{pos+=diff_time*gyro.data.x/92160;}
Serial.print("X: "); Serial.print((int)gyro.data.x); Serial.print(" ");
Serial.print("POS: "); Serial.println((float)pos); Serial.print(" ");
}