Go Down

Topic: Ajuda com acelerometro ADXL345 (GY-80) (Read 516 times) previous topic - next topic

ricardosutana

Hi Folks!

Pessoal, estou precisando da ajuda de vocês.
Estou trabalhando em um modulo GY-80 (Acelerometro, Giroscopio, Barômetro e Magnetómetro) comunicando via I2C.

O codigo compila sem erros, etretanto quando estou recuperando os dados dos eixos do acelerometro, o eixo Z esta retornando um valor de 2.00g constante.
Quando abro o serial monitor estes sao os valores retornados

ACC_X= -0.17 ACC_Y= 0.11 ACC_Z= 1.00
GYRO_X= 7 GYRO_Y= -11 GYRO_Z= 0
ACC_X= -0.25 ACC_Y= 0.16 ACC_Z= 1.50
GYRO_X= 6 GYRO_Y= -6 GYRO_Z= 3
ACC_X= -0.29 ACC_Y= 0.19 ACC_Z= 1.75
GYRO_X= 5 GYRO_Y= -7 GYRO_Z= 0
ACC_X= -0.32 ACC_Y= 0.20 ACC_Z= 1.87
GYRO_X= 8 GYRO_Y= -7 GYRO_Z= -2
ACC_X= -0.33 ACC_Y= 0.21 ACC_Z= 1.93
GYRO_X= 11 GYRO_Y= -6 GYRO_Z= 2
ACC_X= -0.33 ACC_Y= 0.21 ACC_Z= 1.96
GYRO_X= 8 GYRO_Y= -9 GYRO_Z= -1
ACC_X= -0.34 ACC_Y= 0.21 ACC_Z= 1.98
GYRO_X= 7 GYRO_Y= -9 GYRO_Z= 3
ACC_X= -0.34 ACC_Y= 0.22 ACC_Z= 1.99
GYRO_X= 10 GYRO_Y= -7 GYRO_Z= 6
ACC_X= -0.34 ACC_Y= 0.22 ACC_Z= 1.99
GYRO_X= 9 GYRO_Y= -5 GYRO_Z= 2
ACC_X= -0.34 ACC_Y= 0.22 ACC_Z= 1.99
GYRO_X= 8 GYRO_Y= -12 GYRO_Z= 1
ACC_X= -0.34 ACC_Y= 0.21 ACC_Z= 2.00

se noterm, o eixo começa com um valor de Z=1.00 e depois vai aumentando ate chegar a 2.00.
Estou um pouco perdido, ja procurei respostas mas sempre volto ao mesmo lugar.
Caso alguem puder ajudar ficarei grato.



Code: [Select]
[code]
/*************************************************************************************
 * RICARDO SUTANA DE MELLO
 * PROGRAMA PARA TESTE DO ADXL345 E L3G4200D ACOPLADO AO GY-80
 * DATASHEET http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00265057.pdf
 *           http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00265057.pdf
 *
 * DATA: 08/10/2015
 * CONTATO: ricardosutana@gmail.com
 * **************************************************************************************
 */


#include <Wire.h>

// Definicao dos registradores ADXL345
#define Register_ID 0     //Device ID
#define Register_2D 0x2D  //Power-saving features control
#define Register_X0 0x32  //X-Axis Data 0
#define Register_X1 0x33  //X-Axis Data 1
#define Register_Y0 0x34  //Y-Axis Data 0
#define Register_Y1 0x35  //Y-Axis Data 1
#define Register_Z0 0x36  //Z-Axis Data 0
#define Register_Z1 0x37  //Z-Axis Data 1


//Definicao dos registradores LGD4200D
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

const float alpha = 0.5;


//Enderecamento I2C
int ADX_Address = 0x53;  //Endereco I2C do sensor : 83 em decimal ou 0x53
int L3G4200D_Address = 105; //Endereco I2C do L3G4200D


//Variaveis Acelerometro
int reading = 0;
int val=0;
int Acc_X0, Acc_X1, Acc_X_out;
int Acc_Y0, Acc_Y1, Acc_Y_out;
int Acc_Z0, Acc_Z1, Acc_Z_out;

double Acc_Xg, Acc_Yg, Acc_Zg;

//variaveis para o filtro passa baixa
double LP_Xg=0;
double LP_Yg=0;
double LP_Zg=0;


//Variaveis Giroscopio
int Gyro_x;
int Gyro_y;
int Gyro_z;


void setup() {
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Inicializando ADX345");
  Serial.println("Inicializando L3G4200D");
  delay(100);
  //Comunicacao Acelerometro
  Wire.beginTransmission(ADX_Address);
  Wire.write(Register_2D);
  Wire.write(8);                //medição ativada
  Wire.endTransmission();     // fim transmissao

  setupL3G4200D(2000); // Configura o L3G4200 para 200, 500 ou 2000 graus/seg

  delay(1500);

}

void loop() {
 

//CALCULO DAS COORDENADAS
  Acc_X_out = getAccData(Acc_X0, Acc_X1, ADX_Address, Register_X0, Register_X1);
  Acc_Y_out = getAccData(Acc_Y0, Acc_Y1, ADX_Address, Register_Y0, Register_Y1);
  Acc_Z_out = getAccData(Acc_Z0, Acc_Z1, ADX_Address, Register_Z0, Register_Z1);

  Acc_Xg = Acc_X_out/256.0;
  Acc_Yg = Acc_Y_out/256.0;
  Acc_Zg = Acc_Z_out/256.0;

//FILTRO PASSA BAIXA
  LP_Xg = Acc_Xg * alpha + (LP_Xg*(1.0 - alpha));
  LP_Yg = Acc_Yg * alpha + (LP_Yg*(1.0 - alpha));
  LP_Zg = Acc_Zg * alpha + (LP_Zg*(1.0 - alpha));

//LEITURA DO GIROSCOPIO
  getGyroValues();

   
//IMPRIME COODENADAS DO ACELEROMETRO
  Serial.print("ACC_X= ");
  Serial.print(LP_Xg);
  Serial.print("       ");
 
  Serial.print("ACC_Y= ");
  Serial.print(LP_Yg);
  Serial.print("       ");
 
  Serial.print("ACC_Z= ");
  Serial.print(LP_Zg);
  Serial.println("  ");

  //
  Serial.print("GYRO_X= ");
  Serial.print(Gyro_x);
  Serial.print("       ");

  Serial.print("GYRO_Y= ");
  Serial.print(Gyro_y);
  Serial.print("       ");

  Serial.print("GYRO_Z= ");
  Serial.println(Gyro_z);
  Serial.println("  ");

}


/*----------------------------------------------------------+
 *                                                                      +
 *                                                                      +
 *      D E C L A R A C A O   D O S   M E T O D O S    +
 *                                                                      +
 *                                                                      +
 * ---------------------------------------------------------+
 */


//*************************************************************************************
int getAccData(int Acc_D0, int Acc_D1, int ADX_Address,  uint8_t Register_0,  uint8_t Register_1 ){

  int value;
  Wire.beginTransmission(ADX_Address); // transmit to device
  Wire.write(Register_0);
  Wire.write(Register_1);
  Wire.endTransmission();
  Wire.requestFrom(ADX_Address,2);
  if(Wire.available()<=2)   
  {
    Acc_D0 = Wire.read();
    Acc_D1 = Wire.read();
    Acc_D1 = Acc_D1 << 8;
    value = Acc_D0 + Acc_D1;
   
  }
  return value;
}
//*************************************************************************************


//*********************************************************
void getGyroValues()
{
 
  byte xMSB = readRegister(L3G4200D_Address, 0x29);
  byte xLSB = readRegister(L3G4200D_Address, 0x28);
  Gyro_x = ((xMSB << 8) | xLSB);

  byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  Gyro_y = ((yMSB << 8) | yLSB);

  byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  Gyro_z = ((zMSB << 8) | zLSB);
}
//*********************************************************



//*********************************************************
int setupL3G4200D(int scale)
{
  //From  Jim Lindblom of Sparkfun's code

  // Enable x, y, z and turn off power down:
  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

  // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

  // Configure CTRL_REG3 to generate data ready interrupt on INT2
  // No interrupts used on INT1, if you'd like to configure INT1
  // or INT2 otherwise, consult the datasheet:
  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

  // CTRL_REG4 controls the full-scale range, among other things:
  if(scale == 250){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  }else if(scale == 500){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
  }else{
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
  }

  // CTRL_REG5 controls high-pass filtering of outputs, use it
  // if you'd like:
  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
//*********************************************************



//*********************************************************
void writeRegister(int deviceAddress, byte address, byte val)
{
    Wire.beginTransmission(deviceAddress); // start transmission to device
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission
}
//*********************************************************



//*********************************************************
int readRegister(int deviceAddress, byte address)
{
    int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // register to read
    Wire.endTransmission();

    Wire.requestFrom(deviceAddress, 1); // read a byte

    while(!Wire.available())
    {
        // waiting
    }
    v = Wire.read();
    return v;
}
//*********************************************************
[/code]

Att

Go Up