Hi. First of all thanks a lot and sorry for my horrible english.
Second, I am trying make sensor fusions of the sensors in the subject, but I’m having problems reading them.
I am having weird peaks in the lectures, ie: the accelerometer is still, and the z axis read in the 15000-16500 range, and suddenly, a value drops to 300.
I make a peak filter for that, but now I’m experiencing the same problem with the magnetometer.
Can you please check my code?
#include <Wire.h>
#define BMA180 0x40 //address of the accelerometer
#define RESET 0x10
#define PWR 0x0D
#define BW 0X20
#define RANGE 0X35
#define DATA 0x02
double accX, accY, accZ = 0;
void AccelerometerInit()
//
{
byte temp;
//
i2cWrite(BMA180,RESET,0xB6);
//wake up mode
i2cWrite(BMA180,PWR,0x10);
// low pass filter,
temp = i2cReadByte(BMA180, BW);
temp=temp&0x0F;
i2cWrite(BMA180, BW, temp);
// range +/- 2g
temp = i2cReadByte(BMA180, RANGE);
temp=(temp&0xF1) | 0x04;
i2cWrite(BMA180,RANGE,temp);
}
void updateAcc(){
uint8_t* data = i2cRead(BMA180, DATA, 6);
accX = (data[0] | (data[1] << 8));
accY = (data[2] | (data[3] << 8));
accZ = (data[4] | (data[5] << 8));
}
void setup(){
Serial.begin(115200);
Wire.begin();
AccelerometerInit();
}
void loop(){
updateAcc();
Serial.println(accZ);
delay(20);
}
And my I2C library:
#include <Wire.h>
void i2cWrite(uint8_t address, uint8_t registerAddress, uint8_t data) {
Wire.beginTransmission(address);
Wire.write(registerAddress);
Wire.write(data);
Wire.endTransmission();
}
uint8_t* i2cRead(uint8_t address, uint8_t registerAddress, uint8_t nbytes) {
byte data[nbytes];
for (uint8_t i=0;i<nbytes;i++){
Wire.beginTransmission(address);
Wire.write(registerAddress+i);
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.requestFrom(address,uint8_t(1));
if(Wire.available()){
data[i] = Wire.read();
Wire.endTransmission();
}
}
return data;
}
byte i2cReadByte(char address, char registerAddress)
{
//This variable will hold the contents read from the i2c device.
byte data=0;
//Send the register address to be read.
Wire.beginTransmission(address);
//Send the Register Address
Wire.write(registerAddress);
//End the communication sequence.
Wire.endTransmission();
//Ask the I2C device for data
Wire.beginTransmission(address);
Wire.requestFrom(address, 1);
//Wait for a response from the I2C device
if(Wire.available()){
//Save the data sent from the I2C device
data = Wire.read();
}
//End the communication sequence.
Wire.endTransmission();
//Return the data read during the operation
return data;
}
Thanks a lot!!