Fala pessoal.

Estou mexendo com um magnetometro HMC5883L.

Li e reli vários tutoriais consegui pegar os dados apontar para o norte, mas ele me mostra alguns dados errados.

O norte está certo, no caso me mostra em 180 graus, mas o resto, está bem esquisito.

Segue um gráfico do que estou recebendo.

O código é esse:

#define HMC5883_Address 0x1E
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand (uint8_t)0x00
#define HMC5883_DataOutputXMSBAddress 0x03

int regbdata = 0x40;
float gauss = 2.5;

int outputData[6];
int xCompass, yCompass, zCompass, regb;

void compass_setup(){
if(gauss == 0.88){
regb = 0x00;
}else if(gauss == 1.3){
regb = 0x01;
}else if(gauss == 1.9){
regb = 0x02;
}else if(gauss == 2.5){
regb = 0x03;
}else if(gauss == 4.0){
regb = 0x04;
}else if(gauss == 4.7){
regb = 0x05;
}else if(gauss == 5.6){
regb = 0x06;
}else if(gauss == 8.1){
regb = 0x07;
}

Wire.beginTransmission(HMC5883_Address);
Wire.write(regb);
Wire.write(regbdata);
Wire.endTransmission();
delay(1000);
}

void compass_loop(){
Wire.beginTransmission(HMC5883_Address); //Initiate a transmission with HMC5883 (Write address).
Wire.write(HMC5883_ModeRegisterAddress); //Place the Mode Register Address in send-buffer.
Wire.write(HMC5883_ContinuousModeCommand); //Place the command for Continuous operation Mode in send-buffer.
Wire.endTransmission(); //Send the send-buffer to HMC5883 and end the I2C transmission.

Wire.beginTransmission(HMC5883_Address); //Initiate a transmission with HMC5883 (Write address).
Wire.requestFrom(HMC5883_Address,6);

if(6 <= Wire.available()){ // If the number of bytes available for reading be <=6.
for(int i=0;i<6;i++){
outputData[i] = Wire.read(); //Store the data in outputData buffer
}
}

xCompass = outputData[0] 8 | outputData[1]; //Combine MSB and LSB of X Data output register
zCompass = outputData[2] 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
yCompass = outputData[4] 8 | outputData[5]; //Combine MSB and LSB of Y Data output register

getDegrees();
}

float getDegrees(){
double angle = atan2((double)yCompass,(double)xCompass) * (180 / PI) + 180;

Serial.print("angle: ");
Serial.println(angle);

return angle;
}

Alguém já teve algum problema desses?

OFF: Como cola código aqui?

Exibições: 1124

Responder esta

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço