doubts about SparkFun Triple Axis Accelerometer Breakout - KX132 (Qwiic)

Hi!

first i apologize for my english, i’m not fluent yet.

I have two difficulties with this product.

  1. the first is in relation to the union scheme with arduino. To use the library provided by you at: https://github.com/sparkfun/Qwiic_KX13X

should i use the links in this image?

Circuito-Arduino-i2c-kx132.png

  1. I’m having a hard time adjusting this code to set the pitch, roll, and yaw angles. like in this example with another sensor: https://github.com/emanbuc/ESP8266_MPU6 … _roll_yaw

can you help me?

Are you getting values back from the sensor? The Accel values should be close to stable but the gyro values do vary a bunch. I just had some fun (not) in Python with the ICm20948 IMU and some AHRS code and fixed the issue by converting the IMU output from degrees to radians and now get reasonable values.

I mostly use the ESP32 when messing with Arduino code and use the Qwiic. What library are you using for AHRS? I have tried several with several different libraries but at the time did not understand the need to convert degrees to radians for the input.

Can you post your code?

Dale the mechanic from Stump Town in the Beaver State

Oops! I just was messing with my files and realized the KX13x are accel only. The MPU6050 also has 3 axis gyro. the KX13x will work for leveling or a crash sensor (impact). The MPU6050 can be used for AHRS as is or better with a 3 axis mag sensor

What do you want to use it for?

Dale

Hi,

I’m a beginner and I still have some difficulties.

I would like to develop a system that calculates its tilt and rotation with respect to space.

The idea is that this system can be used in various applications. Example:

By a drone to know what its tilt and rotation angle would be. because when, for example, I have to accelerate for it to go forward, the drone will have an inclination, I would like to know what the inclination is.

This is just one of the many apps you can have.

What I need is for the system to perform the pitch and roll calculations like this program.

https://github.com/emanbuc/ESP8266_MPU6 … _roll_yaw

the big difference is that the mpu has accelerometer and gyroscope and the kx 132 i’m using is just an accelerometer.

As I understand it when analyzing the two libraries.

what is missing from the Sparkfun library is adding the part that normalizes the data x, y and z.

and then add the formulas to calculate the pitch and roll.

Viewing the library: https://github.com/emanbuc/ESP8266_MPU6 … h_roll_yaw

I saw that there is this part in the .cpp file that seems to do the normalization.

void MPU6050::setScale(mpu6050_dps_t scale)
{
    uint8_t value;

    switch (scale)
    {
	case MPU6050_SCALE_250DPS:
	    dpsPerDigit = .007633f;
	    break;
	case MPU6050_SCALE_500DPS:
	    dpsPerDigit = .015267f;
	    break;
	case MPU6050_SCALE_1000DPS:
	    dpsPerDigit = .030487f;
	    break;
	case MPU6050_SCALE_2000DPS:
	    dpsPerDigit = .060975f;
	    break;
	default:
	    break;
    }

    value = readRegister8(MPU6050_REG_GYRO_CONFIG);
    value &= 0b11100111;
    value |= (scale << 3);
    writeRegister8(MPU6050_REG_GYRO_CONFIG, value);
}

void MPU6050::setRange(mpu6050_range_t range)
{
    uint8_t value;

    switch (range)
    {
	case MPU6050_RANGE_2G:
	    rangePerDigit = .000061f;
	    break;
	case MPU6050_RANGE_4G:
	    rangePerDigit = .000122f;
	    break;
	case MPU6050_RANGE_8G:
	    rangePerDigit = .000244f;
	    break;
	case MPU6050_RANGE_16G:
	    rangePerDigit = .0004882f;
	    break;
	default:
	    break;
    }

    value = readRegister8(MPU6050_REG_ACCEL_CONFIG);
    value &= 0b11100111;
    value |= (range << 3);
    writeRegister8(MPU6050_REG_ACCEL_CONFIG, value);
}


Vector MPU6050::readRawAccel(void)
{
    Wire.beginTransmission(mpuAddress);
    #if ARDUINO >= 100
	Wire.write(MPU6050_REG_ACCEL_XOUT_H);
    #else
	Wire.send(MPU6050_REG_ACCEL_XOUT_H);
    #endif
    Wire.endTransmission();

    Wire.beginTransmission(mpuAddress);
    Wire.requestFrom(mpuAddress, 6);

    while (Wire.available() < 6);

    #if ARDUINO >= 100
	uint8_t xha = Wire.read();
	uint8_t xla = Wire.read();
        uint8_t yha = Wire.read();
	uint8_t yla = Wire.read();
	uint8_t zha = Wire.read();
	uint8_t zla = Wire.read();
    #else
	uint8_t xha = Wire.receive();
	uint8_t xla = Wire.receive();
	uint8_t yha = Wire.receive();
	uint8_t yla = Wire.receive();
	uint8_t zha = Wire.receive();
	uint8_t zla = Wire.receive();
    #endif


    ra.XAxis = (int16_t) (xha << 8 | xla);
    ra.YAxis = (int16_t) (yha << 8 | yla);
    ra.ZAxis = (int16_t) (zha << 8 | xla);

    return ra;
}




Vector MPU6050::readNormalizeAccel(void)
{
    readRawAccel();

    na.XAxis = ra.XAxis * rangePerDigit * 9.80665f;
    na.YAxis = ra.YAxis * rangePerDigit * 9.80665f;
    na.ZAxis = ra.ZAxis * rangePerDigit * 9.80665f;

    return na;
}

I don’t know how to integrate this part in this other library.

https://github.com/sparkfun/Qwiic_KX13X

and then use these two formulas to calculate pitch and roll.

// Calculate Pitch & Roll
  int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis*normAccel.YAxis + normAccel.ZAxis*normAccel.ZAxis))*180.0)/M_PI;
  int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;

  // Output
  Serial.print(" Pitch = ");
  Serial.print(pitch);
  Serial.print(" Roll = ");
  Serial.print(roll);

You don’t need normalized acceleration values to calculate pitch and roll. The scale factors are always divided out, when these two lines are executed.

// Calculate Pitch & Roll
  int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis*normAccel.YAxis + normAccel.ZAxis*normAccel.ZAxis))*180.0)/M_PI;
  int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;

However, the calculation results will be accurate only when the sensor is held still, or is in uniform linear motion (for example, approximately level flight). For nonuniform motion, a gyro can to some extent help to correct the errors due to the additional acceleration.

This tutorial on calculating tilt angles is helpful: https://wiki.dfrobot.com/How_to_Use_a_T … lt_Sensing

Some background on me. I am a mechanic (FAA A&P) with mechanical design training and a few hours of flight time.

For instrument fight light aircraft use gyros, a gyro horizon, a directional gyro and a needle and ball. The needle is a rate gyro and the ball responds to acceleration (not gravity) for turn coordination.

To get attitude while in motion you will need both the gyro and the accel. Accel alone can and will be fooled by the motion of the vehicle. I am working with Python now as I wanted video, control and data over a net connection and the ESP32 wasn’t doing it. But the Arduino libs wanted both gyro, accel, and mag for best results with gyro and accel as an option. There are libs for Mahony and Madgwick AHRS that you can install in Arduno but they need gyro and accel to work.

One of the things I have done is to use the h file and keyword file to find out all the functions and the they want and what they send back. But that only gives data types and not what units. I am also doing something similar in Python.

Dale

jremington:
You don’t need normalized acceleration values to calculate pitch and roll. The scale factors are always divided out, when these two lines are executed.

// Calculate Pitch & Roll

int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxisnormAccel.YAxis + normAccel.ZAxisnormAccel.ZAxis))*180.0)/M_PI;
int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;




However, the calculation results will be accurate only when the sensor is held still, or is in uniform linear motion (for example, approximately level flight). For nonuniform motion, a gyro can to some extent help to correct the errors due to the additional acceleration.



This tutorial on calculating tilt angles is helpful: [https://wiki.dfrobot.com/How_to_Use_a_T ... lt_Sensing](https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing)

Hello jremington.

first thank you very much for the reply.

I still don’t understand why you don’t need to normalize the values, I thought that in the code with mpu the values ​​were normalized. But I will study more about it, thanks.

just putting in the pitch and roll formula worked as you said.

Another question I still have is the following: if I add a kalman filter will my sensor responses be more stable? Or will it not get better?

tech-mech:
Some background on me. I am a mechanic (FAA A&P) with mechanical design training and a few hours of flight time.

For instrument fight light aircraft use gyros, a gyro horizon, a directional gyro and a needle and ball. The needle is a rate gyro and the ball responds to acceleration (not gravity) for turn coordination.

To get attitude while in motion you will need both the gyro and the accel. Accel alone can and will be fooled by the motion of the vehicle. I am working with Python now as I wanted video, control and data over a net connection and the ESP32 wasn’t doing it. But the Arduino libs wanted both gyro, accel, and mag for best results with gyro and accel as an option. There are libs for Mahony and Madgwick AHRS that you can install in Arduno but they need gyro and accel to work.

One of the things I have done is to use the h file and keyword file to find out all the functions and the they want and what they send back. But that only gives data types and not what units. I am also doing something similar in Python.

Dale

Hello mechanical technician.

First of all, thank you very much for the reply, it helped me a lot.

Because I was able to review some details of my project proposal and realized that only with the kx accelerometer I will not be able to meet the two situations: static objects and non-linear moving objects.

And I saw that for me it will be more interesting to focus only on static solutions since kx will serve well.

Another question I still have is the following: if I add a kalman filter will my sensor responses be more stable? Or will it not get better?

I still don’t understand why you don’t need to normalize the values

atan2(y,x) is equivalent to atan(y/x), except that atan2 takes care of assigning the correct angle quadrant.

It should be clear that if you multiply both x and y by the same number, the ratio (y/x) is unchanged.

I am still learning this stuff myself but just try it with the filter and without and see if it helps. When modifying code (or anything) do only one modification at a time so you know it it helps or not. if you do multiple modifications at a time how do you pick which helped and which didn’t?

Another thing to think on is the fact that you will need an known level surface to work from or you will not get repeatable readings. I intend to make a small adjustable platform for this reason but at this point close enough is good enough.

Dale

if I add a kalman filter will my sensor responses be more stable?

Yes, any averaging you do will smooth out the short term noise and variations. A simple moving average makes much more sense for something like this than a Kalman filter, which is primarily intended for fusion of data from navigational sensors.

For a single variable (e.g. either pitch or roll) a 1D Kalman filter is equivalent to a moving average exponential filter, which can be coded in one line. Two lines, for two angles.

https://www.powertronika.com/2020/04/ex … ilter.html

thanks a lot guys, it helped me a lot.

I wanted to ask the last two questions about implementations with this sensor.

  1. How do I put this sensor in and out of hibernation? is there any command? I did not find.

  2. How to calibrate the sensor? let’s suppose I fix the sensor on a surface and it gets crooked, so your measurements will deviate from the real one, I read that to make it work, you need several reference points.

But I can’t understand how to implement this in code. Because let’s assume I have a 5 degree difference between the actual value and the sensor reading against a measurement. If I just subtract 5 degrees from the measurements it will go wrong because it uses the other axes to do the calculation.

so how do i make this correction in the code?

  1. See the chip data sheet for power reduction and sleep options.

  2. Best overview and tutorial on calibration of accelerometers and magnetometers: https://thecavepearlproject.org/2015/05 … r-arduino/