Hi,
I found this program for Gyro.
#include <Wire.h>
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define CTRL_REG6 0x25
int gyroI2CAddr=105;
int gyroRaw[3];
double gyroDPS[3];
float heading[3]={0.0f};
int gyroZeroRate[3];
int gyroThreshold[3];
#define NUM_GYRO_SAMPLES 100
#define GYRO_SIGMA_MULTIPLE 1
float dpsPerDigit=.0175f;
void setup() {
Serial.begin(9600);
Wire.begin();
setupGyro();
calibrateGyro();
}
void loop() {
updateGyroValues();
updateHeadings();
//testCalibration();
printDPS();
Serial.print(" --> ");
printHeadings();
Serial.println();
}
void printDPS()
{
Serial.print("DPS X: ");
Serial.print(gyroDPS[0]);
Serial.print(" Y: ");
Serial.print(gyroDPS[1]);
Serial.print(" Z: ");
Serial.print(gyroDPS[2]);
}
void printHeadings()
{
Serial.print("Heading X: ");
Serial.print(heading[0]);
Serial.print(" Y: ");
Serial.print(heading[1]);
Serial.print(" Z: ");
Serial.print(heading[2]);
}
void updateHeadings()
{
float deltaT=getDeltaTMicros();
for (int j=0;j<3;j++)
heading[j] -= (gyroDPS[j]*deltaT)/1000000.0f;
}
unsigned long getDeltaTMicros()
{
static unsigned long lastTime=0;
unsigned long currentTime=micros();
unsigned long deltaT=currentTime-lastTime;
if (deltaT < 0.0)
deltaT=currentTime+(4294967295-lastTime);
lastTime=currentTime;
return deltaT;
}
void testCalibration()
{
calibrateGyro();
for (int j=0;j<3;j++)
{
Serial.print(gyroZeroRate[j]);
Serial.print(" ");
Serial.print(gyroThreshold[j]);
Serial.print(" ");
}
Serial.println();
return;
}
void setupGyro()
{
gyroWriteI2C(CTRL_REG1, 0x1F);
gyroWriteI2C(CTRL_REG3, 0x08);
setGyroSensitivity500();
delay(100);
}
void calibrateGyro()
{
long int gyroSums[3]={0};
long int gyroSigma[3]={0};
for (int i=0;i<NUM_GYRO_SAMPLES;i++)
{
updateGyroValues();
for (int j=0;j<3;j++)
{
gyroSums[j]+=gyroRaw[j];
gyroSigma[j]+=gyroRaw[j]*gyroRaw[j];
}
}
for (int j=0;j<3;j++)
{
int averageRate=gyroSums[j]/NUM_GYRO_SAMPLES;
gyroZeroRate[j]=averageRate;
gyroThreshold[j]=sqrt((double(gyroSigma[j]) / NUM_GYRO_SAMPLES) - (averageRate * averageRate)) * GYRO_SIGMA_MULTIPLE;
}
}
void updateGyroValues() {
while (!(gyroReadI2C(0x27) & B00001000)){}
int reg=0x28;
for (int j=0;j<3;j++)
{
gyroRaw[j]=(gyroReadI2C(reg) | (gyroReadI2C(reg+1)<<8));
reg+=2;
}
int deltaGyro[3];
for (int j=0;j<3;j++)
{
deltaGyro[j]=gyroRaw[j]-gyroZeroRate[j];
if (abs(deltaGyro[j]) < gyroThreshold[j])
deltaGyro[j]=0;
gyroDPS[j]= dpsPerDigit * deltaGyro[j];
}
}
void setGyroSensitivity500(void)
{
dpsPerDigit=.0175f;
gyroWriteI2C(CTRL_REG4, 0x90);
}
int gyroReadI2C (byte regAddr)
{
Wire.beginTransmission(gyroI2CAddr);
Wire.write(regAddr);
Wire.endTransmission();
Wire.requestFrom(gyroI2CAddr, 1);
while(!Wire.available()) {};
return (Wire.read());
}
int gyroWriteI2C( byte regAddr, byte val)
{
Wire.beginTransmission(gyroI2CAddr);
Wire.write(regAddr);
Wire.write(val);
Wire.endTransmission();
}
I have uploaded this program to my Arduino Mega 2560R3 microcontroller. At the starting the Headings(x,y,z) are at 0.00 .... but Heading Y is getting incremented every time.. it does not come to zero when i place it at the starting position and the Heading X is showing fluctuations.
Connection details:
Vin = 5 volt
Gnd= Ground
Vio= 3.3 volt
SCL= 21
SDA=20
SDO=3.3 volt
CS= not connected
DRD= not connected
INT1- not connected
Please give me some hints to overcome this problem